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 | namespace skjson { |
16 | |
17 | class ObjectValue; |
18 | |
19 | } // namespace skjson |
20 | |
21 | namespace skottie { |
22 | namespace internal { |
23 | |
24 | class AnimationBuilder; |
25 | class KeyframeAnimatorBuilder; |
26 | |
27 | class Animator : public SkRefCnt { |
28 | public: |
29 | virtual ~Animator() = default; |
30 | |
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 | bool isStatic() const { return fAnimators.empty(); } |
58 | |
59 | protected: |
60 | virtual void onSync() = 0; |
61 | |
62 | void shrink_to_fit(); |
63 | |
64 | void attachDiscardableAdapter(sk_sp<AnimatablePropertyContainer>); |
65 | |
66 | private: |
67 | StateChanged onSeek(float) final; |
68 | |
69 | bool bindImpl(const AnimationBuilder&, |
70 | const skjson::ObjectValue*, |
71 | KeyframeAnimatorBuilder&, |
72 | void*); |
73 | |
74 | std::vector<sk_sp<Animator>> fAnimators; |
75 | bool fHasSynced = false; |
76 | }; |
77 | |
78 | } // namespace internal |
79 | } // namespace skottie |
80 | |
81 | #endif // SkottieAnimator_DEFINED |
82 | |