AnimationController.h 7.2 KB

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