-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
173 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <CALayer.h> | ||
|
||
namespace NXKit { | ||
|
||
class CABlurLayer: public CALayer { | ||
public: | ||
CABlurLayer(); | ||
CABlurLayer(CABlurLayer* layer); | ||
virtual ~CABlurLayer() = default; | ||
|
||
void draw(SkCanvas* context) override; | ||
std::shared_ptr<CALayer> copy() override; | ||
|
||
void setBlurValue(NXFloat blurValue); | ||
[[nodiscard]] NXFloat blurValue() const { return _blurValue; } | ||
|
||
std::optional<AnimatableProperty> value(std::string forKeyPath) override; | ||
protected: | ||
void update(std::shared_ptr<CALayer> presentation, std::shared_ptr<CABasicAnimation> animation, float progress) override; | ||
|
||
private: | ||
NXFloat _blurValue = 20; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include <UIView.h> | ||
#include <CABlurLayer.h> | ||
|
||
namespace NXKit { | ||
|
||
class UIBlurView: public UIView { | ||
public: | ||
UIBlurView(); | ||
|
||
void setBlurValue(NXFloat blurValue) { _blurLayer()->setBlurValue(blurValue); } | ||
[[nodiscard]] NXFloat blurValue() const { return _blurLayer()->blurValue(); } | ||
|
||
private: | ||
std::shared_ptr<CABlurLayer> _blurLayer() const; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <CABlurLayer.h> | ||
#include <tools/Tools.hpp> | ||
|
||
#include "include/effects/SkImageFilters.h" | ||
|
||
using namespace NXKit; | ||
|
||
CABlurLayer::CABlurLayer(): CALayer() { | ||
} | ||
|
||
CABlurLayer::CABlurLayer(CABlurLayer* layer): CALayer(layer) { | ||
_blurValue = layer->_blurValue; | ||
} | ||
|
||
std::shared_ptr<CALayer> CABlurLayer::copy() { | ||
return new_shared<CABlurLayer>(this); | ||
} | ||
|
||
void CABlurLayer::draw(SkCanvas* context) { | ||
SkPaint blurPaint; | ||
blurPaint.setAntiAlias(true); | ||
|
||
blurPaint.setStyle(SkPaint::kFill_Style); | ||
// blurPaint.setStrokeWidth(10); | ||
|
||
sk_sp<SkImageFilter> newBlurFilter = SkImageFilters::Blur(_blurValue, _blurValue, SkTileMode::kClamp, nullptr); | ||
blurPaint.setImageFilter(std::move(newBlurFilter)); | ||
// | ||
context->save(); | ||
|
||
SkRect rect = SkRect::MakeXYWH(0, 0, bounds().width(), bounds().height()); | ||
context->clipRect(rect, true); | ||
// // Make a separate layer using the blur filter, clipped to the middle rectangle's bounds | ||
SkCanvas::SaveLayerRec slr(&rect, &blurPaint, SkCanvas::kInitWithPrevious_SaveLayerFlag); | ||
context->saveLayer(slr); | ||
context->restore(); | ||
context->restore(); | ||
} | ||
|
||
void CABlurLayer::setBlurValue(NXFloat blurValue) { | ||
if (_blurValue == blurValue) return; | ||
onWillSet("blurValue"); | ||
_blurValue = blurValue; | ||
} | ||
|
||
std::optional<AnimatableProperty> CABlurLayer::value(std::string forKeyPath) { | ||
if (forKeyPath == "blurValue") return _blurValue; | ||
return CALayer::value(forKeyPath); | ||
} | ||
|
||
void CABlurLayer::update(std::shared_ptr<CALayer> presentation, std::shared_ptr<CABasicAnimation> animation, float progress) { | ||
if (!animation->keyPath.has_value() || !animation->fromValue.has_value()) return; | ||
|
||
auto keyPath = animation->keyPath.value(); | ||
auto fromValue = animation->fromValue.value(); | ||
|
||
if (keyPath == "blurValue") { | ||
auto start = any_optional_cast<NXFloat>(fromValue); | ||
if (!start.has_value()) { return; } | ||
|
||
auto end = any_optional_cast<NXFloat>(animation->toValue); | ||
if (!end.has_value()) end = this->_blurValue; | ||
|
||
std::static_pointer_cast<CABlurLayer>(presentation)->setBlurValue(start.value() + (end.value() - start.value()) * progress); | ||
} | ||
|
||
CALayer::update(presentation, animation, progress); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <UIBlurView.h> | ||
|
||
using namespace NXKit; | ||
|
||
UIBlurView::UIBlurView(): UIView(NXRect(), new_shared<CABlurLayer>()) {} | ||
|
||
std::shared_ptr<CABlurLayer> UIBlurView::_blurLayer() const { | ||
return std::static_pointer_cast<CABlurLayer>(layer()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters