AnimationController.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Component.h"
  24. #include "VectorBuffer.h"
  25. namespace Urho3D
  26. {
  27. class AnimatedModel;
  28. class Animation;
  29. class AnimationState;
  30. struct Bone;
  31. /// Control data for an animation.
  32. struct AnimationControl
  33. {
  34. /// Construct with defaults.
  35. AnimationControl() :
  36. speed_(1.0f),
  37. targetWeight_(0.0f),
  38. fadeTime_(0.0f),
  39. autoFadeTime_(0.0f),
  40. setTimeTtl_(0.0f),
  41. setWeightTtl_(0.0f),
  42. setTime_(0),
  43. setWeight_(0),
  44. setTimeRev_(0),
  45. setWeightRev_(0)
  46. {
  47. }
  48. /// Animation resource name.
  49. String name_;
  50. /// Animation resource name hash.
  51. StringHash hash_;
  52. /// Animation speed.
  53. float speed_;
  54. /// Animation target weight.
  55. float targetWeight_;
  56. /// Animation weight fade time, 0 if no fade.
  57. float fadeTime_;
  58. /// Animation autofade on stop -time, 0 if disabled.
  59. float autoFadeTime_;
  60. /// Set time command time-to-live.
  61. float setTimeTtl_;
  62. /// Set weight command time-to-live.
  63. float setWeightTtl_;
  64. /// Set time command.
  65. unsigned short setTime_;
  66. /// Set weight command.
  67. unsigned char setWeight_;
  68. /// Set time command revision.
  69. unsigned char setTimeRev_;
  70. /// Set weight command revision.
  71. unsigned char setWeightRev_;
  72. };
  73. /// %Component that drives an AnimatedModel's animations.
  74. class URHO3D_API AnimationController : public Component
  75. {
  76. OBJECT(AnimationController);
  77. public:
  78. /// Construct.
  79. AnimationController(Context* context);
  80. /// Destruct.
  81. virtual ~AnimationController();
  82. /// Register object factory.
  83. static void RegisterObject(Context* context);
  84. /// Handle enabled/disabled state change.
  85. virtual void OnSetEnabled();
  86. /// Update the animations. Is called from HandleScenePostUpdate().
  87. void Update(float timeStep);
  88. /// Play an animation and set full target weight. Name must be the full resource name. Return true on success.
  89. bool Play(const String& name, unsigned char layer, bool looped, float fadeInTime = 0.0f);
  90. /// 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.
  91. bool PlayExclusive(const String& name, unsigned char layer, bool looped, float fadeTime = 0.0f);
  92. /// Stop an animation. Zero fadetime is instant. Return true on success.
  93. bool Stop(const String& name, float fadeOutTime = 0.0f);
  94. /// Stop all animations on a specific layer. Zero fadetime is instant.
  95. void StopLayer(unsigned char layer, float fadeOutTime = 0.0f);
  96. /// Stop all animations. Zero fadetime is instant.
  97. void StopAll(float fadeTime = 0.0f);
  98. /// Fade animation to target weight. Return true on success.
  99. bool Fade(const String& name, float targetWeight, float fadeTime);
  100. /// Fade other animations on the same layer to target weight. Return true on success.
  101. bool FadeOthers(const String& name, float targetWeight, float fadeTime);
  102. /// Set animation blending layer priority. Return true on success.
  103. bool SetLayer(const String& name, unsigned char layer);
  104. /// Set animation start bone. Return true on success.
  105. bool SetStartBone(const String& name, const String& startBoneName);
  106. /// Set animation time position. Return true on success.
  107. bool SetTime(const String& name, float time);
  108. /// Set animation weight. Return true on success.
  109. bool SetWeight(const String& name, float weight);
  110. /// Set animation looping. Return true on success.
  111. bool SetLooped(const String& name, bool enable);
  112. /// Set animation speed. Return true on success.
  113. bool SetSpeed(const String& name, float speed);
  114. /// Set animation autofade on stop (non-looped animations only.) Zero time disables. Return true on success.
  115. bool SetAutoFade(const String& name, float fadeOutTime);
  116. /// Return whether an animation is active.
  117. bool IsPlaying(const String& name) const;
  118. /// Return whether an animation is fading in.
  119. bool IsFadingIn(const String& name) const;
  120. /// Return whether an animation is fading out.
  121. bool IsFadingOut(const String& name) const;
  122. /// Return animation blending layer.
  123. unsigned char GetLayer(const String& name) const;
  124. /// Return animation start bone, or null if no such animation.
  125. Bone* GetStartBone(const String& name) const;
  126. /// Return animation start bone name, or empty string if no such animation.
  127. const String& GetStartBoneName(const String& name) const;
  128. /// Return animation time position.
  129. float GetTime(const String& name) const;
  130. /// Return animation weight.
  131. float GetWeight(const String& name) const;
  132. /// Return animation looping.
  133. bool IsLooped(const String& name) const;
  134. /// Return animation length.
  135. float GetLength(const String& name) const;
  136. /// Return animation speed.
  137. float GetSpeed(const String& name) const;
  138. /// Return animation fade target weight.
  139. float GetFadeTarget(const String& name) const;
  140. /// Return animation fade time.
  141. float GetFadeTime(const String& name) const;
  142. /// Return animation autofade time.
  143. float GetAutoFade(const String& name) const;
  144. /// Find an animation state by animation name.
  145. AnimationState* GetAnimationState(const String& name) const;
  146. /// Find an animation state by animation name hash
  147. AnimationState* GetAnimationState(StringHash nameHash) const;
  148. /// Set animation control structures attribute.
  149. void SetAnimationsAttr(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(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 node being assigned.
  162. virtual void OnNodeSet(Node* node);
  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. }