AnimationController.h 7.0 KB

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