AnimationController.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../IO/VectorBuffer.h"
  5. #include "../Scene/Component.h"
  6. #include "../Graphics/AnimationState.h"
  7. namespace Urho3D
  8. {
  9. class AnimatedModel;
  10. class Animation;
  11. struct Bone;
  12. /// Control data for an animation.
  13. /// @nocount
  14. struct URHO3D_API AnimationControl
  15. {
  16. /// Construct with defaults.
  17. AnimationControl() :
  18. speed_(1.0f),
  19. targetWeight_(0.0f),
  20. fadeTime_(0.0f),
  21. autoFadeTime_(0.0f),
  22. setTimeTtl_(0.0f),
  23. setWeightTtl_(0.0f),
  24. setTime_(0),
  25. setWeight_(0),
  26. setTimeRev_(0),
  27. setWeightRev_(0),
  28. removeOnCompletion_(true)
  29. {
  30. }
  31. /// Animation resource name.
  32. String name_;
  33. /// Animation resource name hash.
  34. StringHash hash_;
  35. /// Animation speed.
  36. float speed_;
  37. /// Animation target weight.
  38. float targetWeight_;
  39. /// Animation weight fade time, 0 if no fade.
  40. float fadeTime_;
  41. /// Animation autofade on stop -time, 0 if disabled.
  42. float autoFadeTime_;
  43. /// Set time command time-to-live.
  44. float setTimeTtl_;
  45. /// Set weight command time-to-live.
  46. float setWeightTtl_;
  47. /// Set time command.
  48. unsigned short setTime_;
  49. /// Set weight command.
  50. unsigned char setWeight_;
  51. /// Set time command revision.
  52. unsigned char setTimeRev_;
  53. /// Set weight command revision.
  54. unsigned char setWeightRev_;
  55. /// Sets whether this should automatically be removed when it finishes playing.
  56. bool removeOnCompletion_;
  57. };
  58. /// %Component that drives an AnimatedModel's animations.
  59. class URHO3D_API AnimationController : public Component
  60. {
  61. URHO3D_OBJECT(AnimationController, Component);
  62. public:
  63. /// Construct.
  64. explicit AnimationController(Context* context);
  65. /// Destruct.
  66. ~AnimationController() override;
  67. /// Register object factory.
  68. /// @nobind
  69. static void RegisterObject(Context* context);
  70. /// Handle enabled/disabled state change.
  71. void OnSetEnabled() override;
  72. /// Update the animations. Is called from HandleScenePostUpdate().
  73. virtual void Update(float timeStep);
  74. /// Play an animation and set full target weight. Name must be the full resource name. Return true on success.
  75. bool Play(const String& name, unsigned char layer, bool looped, float fadeInTime = 0.0f);
  76. /// Play an animation, set full target weight and fade out all other animations on the same layer. Name must be the full resource name. Return true on success.
  77. bool PlayExclusive(const String& name, unsigned char layer, bool looped, float fadeTime = 0.0f);
  78. /// Stop an animation. Zero fadetime is instant. Return true on success.
  79. bool Stop(const String& name, float fadeOutTime = 0.0f);
  80. /// Stop all animations on a specific layer. Zero fadetime is instant.
  81. void StopLayer(unsigned char layer, float fadeOutTime = 0.0f);
  82. /// Stop all animations. Zero fadetime is instant.
  83. void StopAll(float fadeOutTime = 0.0f);
  84. /// Fade animation to target weight. Return true on success.
  85. bool Fade(const String& name, float targetWeight, float fadeTime);
  86. /// Fade other animations on the same layer to target weight. Return true on success.
  87. bool FadeOthers(const String& name, float targetWeight, float fadeTime);
  88. /// Set animation blending layer priority. Return true on success.
  89. bool SetLayer(const String& name, unsigned char layer);
  90. /// Set animation start bone. Return true on success.
  91. bool SetStartBone(const String& name, const String& startBoneName);
  92. /// Set animation time position. Return true on success.
  93. bool SetTime(const String& name, float time);
  94. /// Set animation weight. Return true on success.
  95. bool SetWeight(const String& name, float weight);
  96. /// Set animation looping. Return true on success.
  97. bool SetLooped(const String& name, bool enable);
  98. /// Set animation speed. Return true on success.
  99. bool SetSpeed(const String& name, float speed);
  100. /// Set animation autofade at end (non-looped animations only). Zero time disables. Return true on success.
  101. bool SetAutoFade(const String& name, float fadeOutTime);
  102. /// Set whether an animation auto-removes on completion.
  103. bool SetRemoveOnCompletion(const String& name, bool removeOnCompletion);
  104. /// Set animation blending mode. Return true on success.
  105. bool SetBlendMode(const String& name, AnimationBlendMode mode);
  106. /// Return whether an animation is active. Note that non-looping animations that are being clamped at the end also return true.
  107. bool IsPlaying(const String& name) const;
  108. /// Return whether any animation is active on a specific layer.
  109. bool IsPlaying(unsigned char layer) const;
  110. /// Return whether an animation is fading in.
  111. bool IsFadingIn(const String& name) const;
  112. /// Return whether an animation is fading out.
  113. bool IsFadingOut(const String& name) const;
  114. /// Return whether an animation is at its end. Will return false if the animation is not active at all.
  115. bool IsAtEnd(const String& name) const;
  116. /// Return animation blending layer.
  117. unsigned char GetLayer(const String& name) const;
  118. /// Return animation start bone, or null if no such animation.
  119. Bone* GetStartBone(const String& name) const;
  120. /// Return animation start bone name, or empty string if no such animation.
  121. const String& GetStartBoneName(const String& name) const;
  122. /// Return animation time position.
  123. float GetTime(const String& name) const;
  124. /// Return animation weight.
  125. float GetWeight(const String& name) const;
  126. /// Return animation looping.
  127. bool IsLooped(const String& name) const;
  128. /// Return animation blending mode.
  129. AnimationBlendMode GetBlendMode(const String& name) const;
  130. /// Return animation length.
  131. float GetLength(const String& name) const;
  132. /// Return animation speed.
  133. float GetSpeed(const String& name) const;
  134. /// Return animation fade target weight.
  135. float GetFadeTarget(const String& name) const;
  136. /// Return animation fade time.
  137. float GetFadeTime(const String& name) const;
  138. /// Return animation autofade time.
  139. float GetAutoFade(const String& name) const;
  140. /// Return whether animation auto-removes on completion, or false if no such animation.
  141. bool GetRemoveOnCompletion(const String& name) const;
  142. /// Find an animation state by animation name.
  143. AnimationState* GetAnimationState(const String& name) const;
  144. /// Find an animation state by animation name hash.
  145. AnimationState* GetAnimationState(StringHash nameHash) const;
  146. /// Return the animation control structures for inspection.
  147. const Vector<AnimationControl>& GetAnimations() const { return animations_; }
  148. /// Set animation control structures attribute.
  149. void SetAnimationsAttr(const VariantVector& value);
  150. /// Set animations attribute for network replication.
  151. void SetNetAnimationsAttr(const PODVector<unsigned char>& value);
  152. /// Set node animation states attribute.
  153. void SetNodeAnimationStatesAttr(const VariantVector& value);
  154. /// Return animation control structures attribute.
  155. VariantVector GetAnimationsAttr() const;
  156. /// Return animations attribute for network replication.
  157. const PODVector<unsigned char>& GetNetAnimationsAttr() const;
  158. /// Return node animation states attribute.
  159. VariantVector GetNodeAnimationStatesAttr() const;
  160. protected:
  161. /// Handle scene being assigned.
  162. void OnSceneSet(Scene* scene) override;
  163. private:
  164. /// Add an animation state either to AnimatedModel or as a node animation.
  165. AnimationState* AddAnimationState(Animation* animation);
  166. /// Remove an animation state.
  167. void RemoveAnimationState(AnimationState* state);
  168. /// Find the internal index and animation state of an animation.
  169. void FindAnimation(const String& name, unsigned& index, AnimationState*& state) const;
  170. /// Handle scene post-update event.
  171. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  172. /// Animation control structures.
  173. Vector<AnimationControl> animations_;
  174. /// Node hierarchy mode animation states.
  175. Vector<SharedPtr<AnimationState>> nodeAnimationStates_;
  176. /// Attribute buffer for network replication.
  177. mutable VectorBuffer attrBuffer_;
  178. };
  179. }