| 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 | #ifndef SkottieAnimator_DEFINED |
| 9 | #define SkottieAnimator_DEFINED |
| 10 | |
| 11 | #include "include/core/SkRefCnt.h" |
| 12 | |
| 13 | #include <vector> |
| 14 | |
| 15 | struct SkV2; |
| 16 | |
| 17 | namespace skjson { |
| 18 | |
| 19 | class ObjectValue; |
| 20 | |
| 21 | } // namespace skjson |
| 22 | |
| 23 | namespace skottie { |
| 24 | namespace internal { |
| 25 | |
| 26 | class AnimationBuilder; |
| 27 | class KeyframeAnimatorBuilder; |
| 28 | |
| 29 | class Animator : public SkRefCnt { |
| 30 | public: |
| 31 | using StateChanged = bool; |
| 32 | StateChanged seek(float t) { return this->onSeek(t); } |
| 33 | |
| 34 | protected: |
| 35 | Animator() = default; |
| 36 | |
| 37 | virtual StateChanged onSeek(float t) = 0; |
| 38 | |
| 39 | private: |
| 40 | Animator(const Animator&) = delete; |
| 41 | Animator& operator=(const Animator&) = delete; |
| 42 | }; |
| 43 | |
| 44 | class AnimatablePropertyContainer : public Animator { |
| 45 | public: |
| 46 | // This is the workhorse for property binding: depending on whether the property is animated, |
| 47 | // it will either apply immediately or instantiate and attach a keyframe animator, scoped to |
| 48 | // this container. |
| 49 | template <typename T> |
| 50 | bool bind(const AnimationBuilder&, const skjson::ObjectValue*, T*); |
| 51 | |
| 52 | template <typename T> |
| 53 | bool bind(const AnimationBuilder& abuilder, const skjson::ObjectValue* jobject, T& v) { |
| 54 | return this->bind<T>(abuilder, jobject, &v); |
| 55 | } |
| 56 | |
| 57 | // A flavor of bind<Vec2Value> which drives an additional/optional orientation target |
| 58 | // (rotation in degrees), when bound to a motion path property. |
| 59 | bool bindAutoOrientable(const AnimationBuilder& abuilder, |
| 60 | const skjson::ObjectValue* jobject, |
| 61 | SkV2* v, float* orientation); |
| 62 | |
| 63 | bool isStatic() const { return fAnimators.empty(); } |
| 64 | |
| 65 | protected: |
| 66 | virtual void onSync() = 0; |
| 67 | |
| 68 | void shrink_to_fit(); |
| 69 | |
| 70 | void attachDiscardableAdapter(sk_sp<AnimatablePropertyContainer>); |
| 71 | |
| 72 | private: |
| 73 | StateChanged onSeek(float) final; |
| 74 | |
| 75 | bool bindImpl(const AnimationBuilder&, const skjson::ObjectValue*, KeyframeAnimatorBuilder&); |
| 76 | |
| 77 | std::vector<sk_sp<Animator>> fAnimators; |
| 78 | bool fHasSynced = false; |
| 79 | }; |
| 80 | |
| 81 | } // namespace internal |
| 82 | } // namespace skottie |
| 83 | |
| 84 | #endif // SkottieAnimator_DEFINED |
| 85 | |