up-to-date submodule(rive-cpp)
[platform/core/uifw/rive-tizen.git] / submodule / include / animation / blend_state_instance.hpp
1 #ifndef _RIVE_BLEND_STATE_INSTANCE_HPP_
2 #define _RIVE_BLEND_STATE_INSTANCE_HPP_
3
4 #include <string>
5 #include <vector>
6 #include "animation/state_instance.hpp"
7 #include "animation/blend_state.hpp"
8 #include "animation/linear_animation_instance.hpp"
9
10 namespace rive
11 {
12         class AnimationState;
13
14         template <class K, class T> class BlendStateInstance;
15         template <class T> class BlendStateAnimationInstance
16         {
17                 template <class A, class B> friend class BlendStateInstance;
18
19         private:
20                 const T* m_BlendAnimation;
21                 LinearAnimationInstance m_AnimationInstance;
22                 float m_Mix = 0.0f;
23
24         public:
25                 const T* blendAnimation() const { return m_BlendAnimation; }
26                 const LinearAnimationInstance* animationInstance() const
27                 {
28                         return &m_AnimationInstance;
29                 }
30
31                 BlendStateAnimationInstance(const T* blendAnimation) :
32                     m_BlendAnimation(blendAnimation),
33                     m_AnimationInstance(blendAnimation->animation())
34                 {
35                 }
36
37                 void mix(float value) { m_Mix = value; }
38         };
39
40         template <class K, class T> class BlendStateInstance : public StateInstance
41         {
42         protected:
43                 std::vector<BlendStateAnimationInstance<T>> m_AnimationInstances;
44                 bool m_KeepGoing = true;
45
46         public:
47                 BlendStateInstance(const K* blendState) : StateInstance(blendState)
48                 {
49                         for (auto blendAnimation : blendState->animations())
50                         {
51                                 m_AnimationInstances.emplace_back(
52                                     BlendStateAnimationInstance<T>(
53                                         static_cast<T*>(blendAnimation)));
54                         }
55                 }
56
57                 bool keepGoing() const override { return m_KeepGoing; }
58
59                 void advance(float seconds, SMIInput** inputs) override
60                 {
61                         m_KeepGoing = false;
62                         for (auto& animation : m_AnimationInstances)
63                         {
64                                 if (animation.m_AnimationInstance.advance(seconds))
65                                 {
66                                         m_KeepGoing = true;
67                                 }
68                         }
69                 }
70
71                 void apply(Artboard* artboard, float mix) override
72                 {
73                         for (auto& animation : m_AnimationInstances)
74                         {
75                                 float m = mix * animation.m_Mix;
76                                 animation.m_AnimationInstance.apply(artboard, m);
77                         }
78                 }
79
80                 // Find the animationInstance that corresponds to the blendAnimation.
81                 const LinearAnimationInstance*
82                 animationInstance(const BlendAnimation* blendAnimation) const
83                 {
84                         for (auto& animation : m_AnimationInstances)
85                         {
86                                 if (animation.m_BlendAnimation == blendAnimation)
87                                 {
88                                         return animation.animationInstance();
89                                 }
90                         }
91                         return nullptr;
92                 }
93         };
94 } // namespace rive
95 #endif