1 #ifndef _RIVE_BLEND_STATE_INSTANCE_HPP_
2 #define _RIVE_BLEND_STATE_INSTANCE_HPP_
6 #include "animation/state_instance.hpp"
7 #include "animation/blend_state.hpp"
8 #include "animation/linear_animation_instance.hpp"
14 template <class K, class T> class BlendStateInstance;
15 template <class T> class BlendStateAnimationInstance
17 template <class A, class B> friend class BlendStateInstance;
20 const T* m_BlendAnimation;
21 LinearAnimationInstance m_AnimationInstance;
25 const T* blendAnimation() const { return m_BlendAnimation; }
26 const LinearAnimationInstance* animationInstance() const
28 return &m_AnimationInstance;
31 BlendStateAnimationInstance(const T* blendAnimation) :
32 m_BlendAnimation(blendAnimation),
33 m_AnimationInstance(blendAnimation->animation())
37 void mix(float value) { m_Mix = value; }
40 template <class K, class T> class BlendStateInstance : public StateInstance
43 std::vector<BlendStateAnimationInstance<T>> m_AnimationInstances;
44 bool m_KeepGoing = true;
47 BlendStateInstance(const K* blendState) : StateInstance(blendState)
49 for (auto blendAnimation : blendState->animations())
51 m_AnimationInstances.emplace_back(
52 BlendStateAnimationInstance<T>(
53 static_cast<T*>(blendAnimation)));
57 bool keepGoing() const override { return m_KeepGoing; }
59 void advance(float seconds, SMIInput** inputs) override
62 for (auto& animation : m_AnimationInstances)
64 if (animation.m_AnimationInstance.advance(seconds))
71 void apply(Artboard* artboard, float mix) override
73 for (auto& animation : m_AnimationInstances)
75 float m = mix * animation.m_Mix;
76 animation.m_AnimationInstance.apply(artboard, m);
80 // Find the animationInstance that corresponds to the blendAnimation.
81 const LinearAnimationInstance*
82 animationInstance(const BlendAnimation* blendAnimation) const
84 for (auto& animation : m_AnimationInstances)
86 if (animation.m_BlendAnimation == blendAnimation)
88 return animation.animationInstance();