AnimatedSprite2D.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // Copyright (c) 2008-2015 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 "../Atomic2D/Animation2D.h"
  24. #include "../Atomic2D/StaticSprite2D.h"
  25. /// Loop mode.
  26. enum LoopMode2D
  27. {
  28. /// Default, use animation's value.
  29. LM_DEFAULT = 0,
  30. /// Force looped.
  31. LM_FORCE_LOOPED,
  32. /// Force clamped.
  33. LM_FORCE_CLAMPED
  34. };
  35. namespace Atomic
  36. {
  37. class AnimationSet2D;
  38. /// Animated sprite component, it uses to play animation created by Spriter (http://www.brashmonkey.com/).
  39. class ATOMIC_API AnimatedSprite2D : public StaticSprite2D
  40. {
  41. OBJECT(AnimatedSprite2D);
  42. public:
  43. /// Construct.
  44. AnimatedSprite2D(Context* context);
  45. /// Destruct.
  46. virtual ~AnimatedSprite2D();
  47. /// Register object factory.
  48. static void RegisterObject(Context* context);
  49. /// Handle enabled/disabled state change.
  50. virtual void OnSetEnabled();
  51. /// Set speed.
  52. void SetSpeed(float speed);
  53. /// Set animation by animation set, name and loop mode.
  54. void SetAnimation(AnimationSet2D* animationSet, const String& name, LoopMode2D loopMode = LM_DEFAULT);
  55. /// Set animation by name and loop mode.
  56. void SetAnimation(const String& name, LoopMode2D loopMode = LM_DEFAULT);
  57. /// Set animation set.
  58. void SetAnimationSet(AnimationSet2D* animationSet);
  59. /// Set loop mode.
  60. void SetLoopMode(LoopMode2D loopMode);
  61. /// Return speed.
  62. float GetSpeed() const { return speed_; }
  63. /// Return animation name.
  64. const String& GetAnimation() const { return animationName_; }
  65. /// Return animation.
  66. AnimationSet2D* GetAnimationSet() const;
  67. /// Return loop mode.
  68. LoopMode2D GetLoopMode() const { return loopMode_; }
  69. /// Return root node.
  70. Node* GetRootNode() const;
  71. /// Set animation set attribute.
  72. void SetAnimationSetAttr(const ResourceRef& value);
  73. /// Return animation set attribute.
  74. ResourceRef GetAnimationSetAttr() const;
  75. /// Set animation by name.
  76. void SetAnimationAttr(const String& name);
  77. protected:
  78. /// Handle scene being assigned.
  79. virtual void OnSceneSet(Scene* scene);
  80. /// Recalculate the world-space bounding box.
  81. virtual void OnWorldBoundingBoxUpdate();
  82. /// Handle draw order changed.
  83. virtual void OnDrawOrderChanged();
  84. /// Handle update vertices.
  85. virtual void UpdateSourceBatches();
  86. /// Handle flip changed.
  87. virtual void OnFlipChanged();
  88. /// Set animation.
  89. void SetAnimation(Animation2D* animation, LoopMode2D loopMode);
  90. /// Update animation.
  91. void UpdateAnimation(float timeStep);
  92. /// Calculate time line world world transform.
  93. void CalculateTimelineWorldTransform(int index);
  94. /// Handle scene post update.
  95. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  96. /// Speed.
  97. float speed_;
  98. /// Animation set.
  99. SharedPtr<AnimationSet2D> animationSet_;
  100. /// Animation name.
  101. String animationName_;
  102. /// Animation.
  103. SharedPtr<Animation2D> animation_;
  104. /// Loop mode.
  105. LoopMode2D loopMode_;
  106. /// Looped.
  107. bool looped_;
  108. /// Current time.
  109. float currentTime_;
  110. /// Root node.
  111. SharedPtr<Node> rootNode_;
  112. /// Number of tracks.
  113. unsigned numTracks_;
  114. /// Track nodes.
  115. Vector<SharedPtr<Node> > trackNodes_;
  116. /// Track node info.
  117. struct TrackNodeInfo
  118. {
  119. /// Has sprite.
  120. bool hasSprite;
  121. /// World space.
  122. bool worldSpace;
  123. /// Current value.
  124. AnimationKeyFrame2D value;
  125. };
  126. /// Track node infos.
  127. Vector<TrackNodeInfo> trackNodeInfos_;
  128. };
  129. }