AnimationController.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. {
  39. }
  40. /// Animation resource name hash
  41. StringHash hash_;
  42. /// Animation speed
  43. float speed_;
  44. /// Animation target weight
  45. float targetWeight_;
  46. /// Animation weight fade time, 0 if no fade
  47. float fadeTime_;
  48. /// Animation autofade on stop -time, 0 if disabled
  49. float autoFadeTime_;
  50. };
  51. /// Component that drives an AnimatedModel's animations
  52. class AnimationController : public Component
  53. {
  54. OBJECT(AnimationController);
  55. public:
  56. /// Construct
  57. AnimationController(Context* context);
  58. /// Destruct
  59. virtual ~AnimationController();
  60. /// Register object factory
  61. static void RegisterObject(Context* context);
  62. /// Handle attribute write access
  63. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& value);
  64. /// Handle attribute read access
  65. virtual Variant OnGetAttribute(const AttributeInfo& attr);
  66. /// Update the animations. Is called from HandleScenePostUpdate()
  67. void Update(float timeStep);
  68. /// Play an animation and set full target weight. Name must be the full resource name. Return true on success
  69. bool Play(const String& name, int layer, bool looped, float fadeInTime = 0.0f);
  70. /// 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
  71. bool PlayExclusive(const String& name, int layer, bool looped, float fadeTime = 0.0f);
  72. /// Stop an animation. Zero fadetime is instant. Return true on success
  73. bool Stop(const String& name, float fadeOutTime = 0.0f);
  74. /// Stop all animations on a specific layer. Zero fadetime is instant
  75. void StopLayer(int layer, float fadeOutTime = 0.0f);
  76. /// Stop all animations. Zero fadetime is instant
  77. void StopAll(float fadeTime = 0.0f);
  78. /// Fade animation to target weight. Return true on success
  79. bool Fade(const String& name, float targetWeight, float fadeTime);
  80. /// Fade other animations on the same layer to target weight. Return true on success
  81. bool FadeOthers(const String& name, float targetWeight, float fadeTime);
  82. /// Set animation blending layer priority. Return true on success
  83. bool SetLayer(const String& name, int layer);
  84. /// Set animation start bone. Return true on success
  85. bool SetStartBone(const String& name, const String& startBoneName);
  86. /// Set animation time position. Return true on success
  87. bool SetTime(const String& name, float time);
  88. /// Set animation weight. Return true on success
  89. bool SetWeight(const String& name, float weight);
  90. /// Set animation looping. Return true on success
  91. bool SetLooped(const String& name, bool enable);
  92. /// Set animation speed. Return true on success
  93. bool SetSpeed(const String& name, float speed);
  94. /// Set animation autofade on stop (non-looped animations only.) Zero time disables. Return true on success
  95. bool SetAutoFade(const String& name, float fadeOutTime);
  96. /// Return whether an animation is active
  97. bool IsPlaying(const String& name) const;
  98. /// Return whether an animation is fading in
  99. bool IsFadingIn(const String& name) const;
  100. /// Return whether an animation is fading out
  101. bool IsFadingOut(const String& name) const;
  102. /// Return animation blending layer
  103. int GetLayer(const String& name) const;
  104. /// Return animation start bone, or null if no such animation
  105. Bone* GetStartBone(const String& name) const;
  106. /// Return animation start bone name, or null if no such animation
  107. const String& GetStartBoneName(const String& name) const;
  108. /// Return animation time position
  109. float GetTime(const String& name) const;
  110. /// Return animation weight
  111. float GetWeight(const String& name) const;
  112. /// Return animation looping
  113. bool IsLooped(const String& name) const;
  114. /// Return animation length
  115. float GetLength(const String& name) const;
  116. /// Return animation speed
  117. float GetSpeed(const String& name) const;
  118. /// Return animation fade target weight
  119. float GetFadeTarget(const String& name) const;
  120. /// Return animation fade time
  121. float GetFadeTime(const String& name) const;
  122. /// Return animation autofade time
  123. float GetAutoFade(const String& name) const;
  124. protected:
  125. /// Handle node being assigned
  126. virtual void OnNodeSet(Node* node);
  127. private:
  128. /// Find the internal index and animation state of an animation
  129. void FindAnimation(const String& name, unsigned& index, AnimationState*& state) const;
  130. /// Find the animation state only
  131. AnimationState* FindAnimationState(const String& name) const;
  132. /// Handle scene post-update event
  133. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  134. /// Controlled animations
  135. Vector<AnimationControl> animations_;
  136. };