From 7609f7f27d756dea7c19fd51d1171f44f1829582 Mon Sep 17 00:00:00 2001 From: Kyle Carlstrom Date: Mon, 16 Aug 2021 16:46:47 -0700 Subject: [PATCH] Create Vec2ExpressionAnimator Change-Id: I056eb5ca61c14dd58c2edeadcd122f383094fcb9 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/439976 Commit-Queue: Kyle Carlstrom Commit-Queue: Avinash Parchuri Reviewed-by: Avinash Parchuri Reviewed-by: Florin Malita --- .../src/animator/Vec2KeyframeAnimator.cpp | 29 +++++++++++++++++-- modules/skottie/tests/Expression.cpp | 23 +++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/modules/skottie/src/animator/Vec2KeyframeAnimator.cpp b/modules/skottie/src/animator/Vec2KeyframeAnimator.cpp index 0652ce6e52..891e24d5d2 100644 --- a/modules/skottie/src/animator/Vec2KeyframeAnimator.cpp +++ b/modules/skottie/src/animator/Vec2KeyframeAnimator.cpp @@ -101,6 +101,29 @@ private: using INHERITED = KeyframeAnimator; }; +class Vec2ExpressionAnimator final : public Animator { +public: + Vec2ExpressionAnimator(sk_sp>> expression_evaluator, + Vec2Value* target_value) + : fExpressionEvaluator(std::move(expression_evaluator)) + , fTarget(target_value) {} + +private: + + StateChanged onSeek(float t) override { + auto old_value = *fTarget; + + std::vector result = fExpressionEvaluator->evaluate(t); + fTarget->x = result.size() > 0 ? result[0] : 0; + fTarget->y = result.size() > 1 ? result[1] : 0; + + return *fTarget != old_value; + } + + sk_sp>> fExpressionEvaluator; + Vec2Value* fTarget; +}; + class Vec2AnimatorBuilder final : public AnimatorBuilder { public: Vec2AnimatorBuilder(Vec2Value* vec_target, float* rot_target) @@ -125,8 +148,10 @@ class Vec2AnimatorBuilder final : public AnimatorBuilder { fRotTarget)); } - sk_sp makeFromExpression(ExpressionManager&, const char*) override { - return nullptr; + sk_sp makeFromExpression(ExpressionManager& em, const char* expr) override { + sk_sp>> expression_evaluator = + em.createArrayExpressionEvaluator(expr); + return sk_make_sp(expression_evaluator, fVecTarget); } bool parseValue(const AnimationBuilder&, const skjson::Value& jv) const override { diff --git a/modules/skottie/tests/Expression.cpp b/modules/skottie/tests/Expression.cpp index 29d2177b1e..fba3f6b34d 100644 --- a/modules/skottie/tests/Expression.cpp +++ b/modules/skottie/tests/Expression.cpp @@ -22,6 +22,13 @@ public: } }; +class FakeVectorExpressionEvaluator : public ExpressionEvaluator> { +public: + std::vector evaluate(float t) override { + return {1.0f, 2.0f}; + } +}; + class FakeExpressionManager : public ExpressionManager { public: sk_sp> createNumberExpressionEvaluator( @@ -36,18 +43,24 @@ public: sk_sp>> createArrayExpressionEvaluator( const char expression[]) override { - return nullptr; + return sk_make_sp(); } }; class FakePropertyObserver : public PropertyObserver { public: void onOpacityProperty(const char node_name[], - const LazyHandle& opacity_handle) override { + const LazyHandle& opacity_handle) override { opacity_handle_.reset(opacity_handle().release()); } + void onTransformProperty(const char node_name[], + const LazyHandle& transform_handle) override { + transform_handle_.reset(transform_handle().release()); + } + std::unique_ptr opacity_handle_; + std::unique_ptr transform_handle_; }; } // namespace @@ -97,7 +110,8 @@ DEF_TEST(Skottie_ScalarExpression, r) { 0 ], "ix": 1, - "l": 2 + "l": 2, + "x": "fake; return value is specified by the FakeArrayExpressionEvaluator." }, "s": { "a": 0, @@ -135,4 +149,7 @@ DEF_TEST(Skottie_ScalarExpression, r) { anim->seekFrameTime(0); REPORTER_ASSERT(r, SkScalarNearlyEqual(observer->opacity_handle_->get(), 7.0f)); + SkPoint anchor_point = observer->transform_handle_->get().fAnchorPoint; + REPORTER_ASSERT(r, SkScalarNearlyEqual(anchor_point.fX, 1.0f)); + REPORTER_ASSERT(r, SkScalarNearlyEqual(anchor_point.fY, 2.0f)); }