Create Vec2ExpressionAnimator

Change-Id: I056eb5ca61c14dd58c2edeadcd122f383094fcb9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/439976
Commit-Queue: Kyle Carlstrom <kylecarlstrom@google.com>
Commit-Queue: Avinash Parchuri <aparchur@google.com>
Reviewed-by: Avinash Parchuri <aparchur@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Kyle Carlstrom 2021-08-16 16:46:47 -07:00 committed by SkCQ
parent 03f17cd210
commit 7609f7f27d
2 changed files with 47 additions and 5 deletions

View File

@ -101,6 +101,29 @@ private:
using INHERITED = KeyframeAnimator;
};
class Vec2ExpressionAnimator final : public Animator {
public:
Vec2ExpressionAnimator(sk_sp<ExpressionEvaluator<std::vector<float>>> 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<float> 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<ExpressionEvaluator<std::vector<float>>> 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<Animator> makeFromExpression(ExpressionManager&, const char*) override {
return nullptr;
sk_sp<Animator> makeFromExpression(ExpressionManager& em, const char* expr) override {
sk_sp<ExpressionEvaluator<std::vector<SkScalar>>> expression_evaluator =
em.createArrayExpressionEvaluator(expr);
return sk_make_sp<Vec2ExpressionAnimator>(expression_evaluator, fVecTarget);
}
bool parseValue(const AnimationBuilder&, const skjson::Value& jv) const override {

View File

@ -22,6 +22,13 @@ public:
}
};
class FakeVectorExpressionEvaluator : public ExpressionEvaluator<std::vector<float>> {
public:
std::vector<float> evaluate(float t) override {
return {1.0f, 2.0f};
}
};
class FakeExpressionManager : public ExpressionManager {
public:
sk_sp<ExpressionEvaluator<float>> createNumberExpressionEvaluator(
@ -36,18 +43,24 @@ public:
sk_sp<ExpressionEvaluator<std::vector<float>>> createArrayExpressionEvaluator(
const char expression[]) override {
return nullptr;
return sk_make_sp<FakeVectorExpressionEvaluator>();
}
};
class FakePropertyObserver : public PropertyObserver {
public:
void onOpacityProperty(const char node_name[],
const LazyHandle<OpacityPropertyHandle>& opacity_handle) override {
const LazyHandle<OpacityPropertyHandle>& opacity_handle) override {
opacity_handle_.reset(opacity_handle().release());
}
void onTransformProperty(const char node_name[],
const LazyHandle<TransformPropertyHandle>& transform_handle) override {
transform_handle_.reset(transform_handle().release());
}
std::unique_ptr<OpacityPropertyHandle> opacity_handle_;
std::unique_ptr<TransformPropertyHandle> 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));
}