From f8393c861975350c4ca9d4f6c95b186c081dbbcf Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Thu, 1 Feb 2018 11:33:08 -0500 Subject: [skottie] Split-position support TBR= Change-Id: I82433bc9a73f89956c2b25146e1521412d125945 Reviewed-on: https://skia-review.googlesource.com/102622 Reviewed-by: Florin Malita Commit-Queue: Florin Malita --- experimental/skottie/SkottieAnimator.cpp | 74 +++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'experimental') 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 static inline bool BindPropertyImpl(const Json::Value& jprop, sksg::AnimatorList* animators, std::function&& 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 Make(const Json::Value& jprop, + std::function&& apply, + const VectorValue*) { + if (!jprop.isObject()) + return nullptr; + + std::unique_ptr 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(jprop["x"], &split_animator->fAnimators, + [split_animator_ptr](const ScalarValue& x) { split_animator_ptr->setX(x); }) || + !BindPropertyImpl(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&& apply) + : fApplyFunc(std::move(apply)) {} + + const std::function fApplyFunc; + sksg::AnimatorList fAnimators; + + ScalarValue fX = 0, + fY = 0; + + using INHERITED = sksg::Animator; +}; + +bool BindSplitPositionProperty(const Json::Value& jprop, + sksg::AnimatorList* animators, + std::function&& apply, + const VectorValue* noop) { + if (auto split_animator = SplitPointAnimator::Make(jprop, std::move(apply), noop)) { + animators->push_back(std::unique_ptr(split_animator.release())); + return true; + } + + return false; +} + } // namespace template <> @@ -331,7 +399,9 @@ bool BindProperty(const Json::Value& jprop, sksg::AnimatorList* animators, std::function&& 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 <> -- cgit v1.2.3