| 1 | /* |
| 2 | * Copyright 2020 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "modules/skottie/src/SkottieJson.h" |
| 9 | #include "modules/skottie/src/SkottieValue.h" |
| 10 | #include "modules/skottie/src/animator/Animator.h" |
| 11 | #include "modules/skottie/src/animator/KeyframeAnimator.h" |
| 12 | |
| 13 | namespace skottie::internal { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // Scalar specialization: stores scalar values (floats) inline in keyframes. |
| 18 | class ScalarKeyframeAnimator final : public KeyframeAnimator { |
| 19 | public: |
| 20 | class Builder final : public KeyframeAnimatorBuilder { |
| 21 | public: |
| 22 | sk_sp<KeyframeAnimator> make(const AnimationBuilder& abuilder, |
| 23 | const skjson::ArrayValue& jkfs, |
| 24 | void* target_value) override { |
| 25 | SkASSERT(jkfs.size() > 0); |
| 26 | if (!this->parseKeyframes(abuilder, jkfs)) { |
| 27 | return nullptr; |
| 28 | } |
| 29 | |
| 30 | return sk_sp<ScalarKeyframeAnimator>( |
| 31 | new ScalarKeyframeAnimator(std::move(fKFs), |
| 32 | std::move(fCMs), |
| 33 | static_cast<ScalarValue*>(target_value))); |
| 34 | } |
| 35 | |
| 36 | bool parseValue(const AnimationBuilder&, const skjson::Value& jv, void* v) const override { |
| 37 | return Parse(jv, static_cast<float*>(v)); |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | bool parseKFValue(const AnimationBuilder&, |
| 42 | const skjson::ObjectValue&, |
| 43 | const skjson::Value& jv, |
| 44 | Keyframe::Value* v) override { |
| 45 | return Parse(jv, &v->flt); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | private: |
| 50 | ScalarKeyframeAnimator(std::vector<Keyframe> kfs, |
| 51 | std::vector<SkCubicMap> cms, |
| 52 | ScalarValue* target_value) |
| 53 | : INHERITED(std::move(kfs), std::move(cms)) |
| 54 | , fTarget(target_value) {} |
| 55 | |
| 56 | StateChanged onSeek(float t) override { |
| 57 | const auto& lerp_info = this->getLERPInfo(t); |
| 58 | const auto old_value = *fTarget; |
| 59 | |
| 60 | *fTarget = Lerp(lerp_info.vrec0.flt, lerp_info.vrec1.flt, lerp_info.weight); |
| 61 | |
| 62 | return *fTarget != old_value; |
| 63 | } |
| 64 | |
| 65 | ScalarValue* fTarget; |
| 66 | |
| 67 | using INHERITED = KeyframeAnimator; |
| 68 | }; |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | template <> |
| 73 | bool AnimatablePropertyContainer::bind<ScalarValue>(const AnimationBuilder& abuilder, |
| 74 | const skjson::ObjectValue* jprop, |
| 75 | ScalarValue* v) { |
| 76 | ScalarKeyframeAnimator::Builder builder; |
| 77 | |
| 78 | return this->bindImpl(abuilder, jprop, builder, v); |
| 79 | } |
| 80 | |
| 81 | } // namespace skottie::internal |
| 82 | |