AnimatedSprite2D.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Copyright (c) 2008-2014 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 "Animation2D.h"
  24. #include "Drawable.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 Urho3D
  36. {
  37. class AnimationSet2D;
  38. /// Spriter animation component.
  39. class URHO3D_API AnimatedSprite2D : public Drawable
  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 layer.
  52. void SetLayer(int layer);
  53. /// Set order in layer.
  54. void SetOrderInLayer(int orderInLayer);
  55. /// Set blend mode.
  56. void SetBlendMode(BlendMode mode);
  57. /// Set flip.
  58. void SetFlip(bool flipX, bool flipY);
  59. /// Set flip X.
  60. void SetFlipX(bool flipX);
  61. /// Set flip Y.
  62. void SetFlipY(bool flipY);
  63. /// Set color.
  64. void SetColor(const Color& color);
  65. /// Set speed.
  66. void SetSpeed(float speed);
  67. /// Set animation by animation set, name and loop mode.
  68. void SetAnimation(AnimationSet2D* animationSet, const String& name, LoopMode2D loopMode = LM_DEFAULT);
  69. /// Set animation by name and loop mode.
  70. void SetAnimation(const String& name, LoopMode2D loopMode = LM_DEFAULT);
  71. /// Set animation set.
  72. void SetAnimationSet(AnimationSet2D* animationSet);
  73. /// Set loop mode.
  74. void SetLoopMode(LoopMode2D loopMode);
  75. /// Return layer.
  76. int GetLayer() const { return layer_; }
  77. /// Return order in layer.
  78. int GetOrderInLayer() const { return orderInLayer_; }
  79. /// Return blend mode.
  80. BlendMode GetBlendMode() const { return blendMode_; }
  81. /// Return flip X.
  82. bool GetFlipX() const { return flipX_; }
  83. /// Return flip Y.
  84. bool GetFlipY() const { return flipY_; }
  85. /// Return color.
  86. const Color& GetColor() const { return color_; }
  87. /// Return speed.
  88. float GetSpeed() const { return speed_; }
  89. /// Return animation name.
  90. const String& GetAnimation() const { return animationName_; }
  91. /// Return animation.
  92. AnimationSet2D* GetAnimationSet() const;
  93. /// Return loop mode.
  94. LoopMode2D GetLoopMode() const { return loopMode_; }
  95. /// Return root node.
  96. Node* GetRootNode() const;
  97. /// Set animation set attribute.
  98. void SetAnimationSetAttr(const ResourceRef& value);
  99. /// Return animation set attribute.
  100. ResourceRef GetAnimationSetAttr() const;
  101. /// Set anmiation by name.
  102. void SetAnimationAttr(const String& name);
  103. protected:
  104. /// Handle node being assigned.
  105. virtual void OnNodeSet(Node* node);
  106. /// Recalculate the world-space bounding box.
  107. virtual void OnWorldBoundingBoxUpdate();
  108. /// Set animation.
  109. void SetAnimation(Animation2D* animation, LoopMode2D loopMode);
  110. /// Update animation.
  111. void UpdateAnimation(float timeStep);
  112. /// Calculate timeline world world transform.
  113. void CalculateTimelineWorldTransform(unsigned index);
  114. /// Handle scene post update.
  115. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  116. /// Layer.
  117. int layer_;
  118. /// Order in layer.
  119. int orderInLayer_;
  120. /// Blend mode.
  121. BlendMode blendMode_;
  122. /// Flip X.
  123. bool flipX_;
  124. /// Flip Y.
  125. bool flipY_;
  126. /// Color.
  127. Color color_;
  128. /// Speed.
  129. float speed_;
  130. /// Animation set.
  131. SharedPtr<AnimationSet2D> animationSet_;
  132. /// Animation name.
  133. String animationName_;
  134. /// Animation.
  135. SharedPtr<Animation2D> animation_;
  136. /// Loop mode.
  137. LoopMode2D loopMode_;
  138. /// Looped.
  139. bool looped_;
  140. /// Current time.
  141. float currentTime_;
  142. /// Root node.
  143. SharedPtr<Node> rootNode_;
  144. /// Timeline nodes.
  145. Vector<SharedPtr<Node> > timelineNodes_;
  146. /// Transform info.
  147. struct TransformInfo
  148. {
  149. /// Construct.
  150. TransformInfo() :
  151. parent_(-1),
  152. worldSpace_(false)
  153. {
  154. }
  155. /// Parent.
  156. int parent_;
  157. /// World space.
  158. bool worldSpace_;
  159. /// Transform.
  160. Transform2D transform_;
  161. };
  162. /// Timeline transform infos.
  163. Vector<TransformInfo> timelineTransformInfos_;
  164. };
  165. }