diff options
author | Florin Malita <fmalita@chromium.org> | 2018-02-01 11:33:08 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-02-01 17:48:08 +0000 |
commit | f8393c861975350c4ca9d4f6c95b186c081dbbcf (patch) | |
tree | 1860044cf07c2685794112187bc5045ef864fbd6 | |
parent | 3cda39f270b8ebb49a0e428dc1b9af7c2735617e (diff) |
[skottie] Split-position support
TBR=
Change-Id: I82433bc9a73f89956c2b25146e1521412d125945
Reviewed-on: https://skia-review.googlesource.com/102622
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
-rw-r--r-- | experimental/skottie/SkottieAnimator.cpp | 74 |
1 files changed, 72 insertions, 2 deletions
diff --git a/experimental/skottie/SkottieAnimator.cpp b/experimental/skottie/SkottieAnimator.cpp index 838bf2a9ec..c1e83f7e90 100644 --- a/experimental/skottie/SkottieAnimator.cpp +++ b/experimental/skottie/SkottieAnimator.cpp @@ -279,7 +279,7 @@ template <typename T> static inline bool BindPropertyImpl(const Json::Value& jprop, sksg::AnimatorList* animators, std::function<void(const T&)>&& apply, - const T* noop) { + const T* noop = nullptr) { if (!jprop.isObject()) return false; @@ -316,6 +316,74 @@ static inline bool BindPropertyImpl(const Json::Value& jprop, return true; } +class SplitPointAnimator final : public sksg::Animator { +public: + static std::unique_ptr<SplitPointAnimator> Make(const Json::Value& jprop, + std::function<void(const VectorValue&)>&& apply, + const VectorValue*) { + if (!jprop.isObject()) + return nullptr; + + std::unique_ptr<SplitPointAnimator> split_animator( + new SplitPointAnimator(std::move(apply))); + + // This raw pointer is captured in lambdas below. But the lambdas are owned by + // the object itself, so the scope is bound to the life time of the object. + auto* split_animator_ptr = split_animator.get(); + + if (!BindPropertyImpl<ScalarValue>(jprop["x"], &split_animator->fAnimators, + [split_animator_ptr](const ScalarValue& x) { split_animator_ptr->setX(x); }) || + !BindPropertyImpl<ScalarValue>(jprop["y"], &split_animator->fAnimators, + [split_animator_ptr](const ScalarValue& y) { split_animator_ptr->setY(y); })) { + LogFail(jprop, "Could not parse split property"); + return nullptr; + } + + if (split_animator->fAnimators.empty()) { + // Static split property, no need to hold on to the split animator. + return nullptr; + } + + return split_animator; + } + + void onTick(float t) override { + for (const auto& animator : fAnimators) { + animator->tick(t); + } + + const VectorValue vec = { fX, fY }; + fApplyFunc(vec); + } + + void setX(const ScalarValue& x) { fX = x; } + void setY(const ScalarValue& y) { fY = y; } + +private: + explicit SplitPointAnimator(std::function<void(const VectorValue&)>&& apply) + : fApplyFunc(std::move(apply)) {} + + const std::function<void(const VectorValue&)> fApplyFunc; + sksg::AnimatorList fAnimators; + + ScalarValue fX = 0, + fY = 0; + + using INHERITED = sksg::Animator; +}; + +bool BindSplitPositionProperty(const Json::Value& jprop, + sksg::AnimatorList* animators, + std::function<void(const VectorValue&)>&& apply, + const VectorValue* noop) { + if (auto split_animator = SplitPointAnimator::Make(jprop, std::move(apply), noop)) { + animators->push_back(std::unique_ptr<sksg::Animator>(split_animator.release())); + return true; + } + + return false; +} + } // namespace template <> @@ -331,7 +399,9 @@ bool BindProperty(const Json::Value& jprop, sksg::AnimatorList* animators, std::function<void(const VectorValue&)>&& apply, const VectorValue* noop) { - return BindPropertyImpl(jprop, animators, std::move(apply), noop); + return ParseDefault(jprop["s"], false) + ? BindSplitPositionProperty(jprop, animators, std::move(apply), noop) + : BindPropertyImpl(jprop, animators, std::move(apply), noop); } template <> |