AnimationState.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // Copyright (c) 2008-2013 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 "HashMap.h"
  24. #include "Ptr.h"
  25. namespace Urho3D
  26. {
  27. class Animation;
  28. class AnimatedModel;
  29. class Deserializer;
  30. class Serializer;
  31. class Skeleton;
  32. struct AnimationTrack;
  33. struct Bone;
  34. /// %Animation instance.
  35. class AnimationState : public RefCounted
  36. {
  37. public:
  38. /// Construct with animated model and animation pointers.
  39. AnimationState(AnimatedModel* model, Animation* animation);
  40. /// Construct with root scene node and animation pointers.
  41. AnimationState(Node* node, Animation* animation);
  42. /// Destruct.
  43. ~AnimationState();
  44. /// Set start bone. Not supported in node animation mode.
  45. void SetStartBone(Bone* bone);
  46. /// Set looping enabled/disabled.
  47. void SetLooped(bool looped);
  48. /// Set blending weight.
  49. void SetWeight(float weight);
  50. /// Set time position. Does not fire animation triggers.
  51. void SetTime(float time);
  52. /// Modify blending weight.
  53. void AddWeight(float delta);
  54. /// Modify time position. %Animation triggers will be fired.
  55. void AddTime(float delta);
  56. /// Set blending layer.
  57. void SetLayer(unsigned char layer);
  58. /// Return animation.
  59. Animation* GetAnimation() const { return animation_; }
  60. /// Return start bone.
  61. Bone* GetStartBone() const;
  62. /// Return whether weight is nonzero.
  63. bool IsEnabled() const { return weight_ > 0.0f; }
  64. /// Return whether looped.
  65. bool IsLooped() const { return looped_; }
  66. /// Return blending weight.
  67. float GetWeight() const { return weight_; }
  68. /// Return time position.
  69. float GetTime() const { return time_; }
  70. /// Return animation length.
  71. float GetLength() const;
  72. /// Return blending layer.
  73. unsigned char GetLayer() const { return layer_; }
  74. /// Apply the animation at the current time position.
  75. void Apply();
  76. private:
  77. /// Apply animation to a skeleton.
  78. void ApplyToModel();
  79. /// Apply animation to a scene node hierarchy.
  80. void ApplyToNodes();
  81. /// Apply animation track to a scene node, full weight.
  82. void ApplyTrackToNodeFullWeight(unsigned index, Node* node);
  83. /// Apply animation track to a scene node, blended with current node transform.
  84. void ApplyTrackToNodeBlended(unsigned index, Node* node);
  85. /// Animated model (model mode.)
  86. WeakPtr<AnimatedModel> model_;
  87. /// Root scene node (node hierarchy mode.)
  88. WeakPtr<Node> node_;
  89. /// Animation.
  90. SharedPtr<Animation> animation_;
  91. /// Start bone.
  92. Bone* startBone_;
  93. /// Mapping of animation track indices to bones.
  94. HashMap<unsigned, Bone*> trackToBoneMap_;
  95. /// Mapping of animation track indices to scene nodes.
  96. HashMap<unsigned, WeakPtr<Node> > trackToNodeMap_;
  97. /// Last keyframe on each animation track for optimized keyframe search.
  98. PODVector<unsigned> lastKeyFrame_;
  99. /// Looped flag.
  100. bool looped_;
  101. /// Blending weight.
  102. float weight_;
  103. /// Time position.
  104. float time_;
  105. /// Blending layer.
  106. unsigned char layer_;
  107. };
  108. }