Animation2D.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "Ptr.h"
  24. #include "RefCounted.h"
  25. #include "Vector2.h"
  26. namespace Urho3D
  27. {
  28. class AnimationSet2D;
  29. class Sprite2D;
  30. /// Object type.
  31. enum ObjectType2D
  32. {
  33. /// Bone.
  34. OT_BONE = 0,
  35. /// Sprite.
  36. OT_SPRITE,
  37. };
  38. /// Reference.
  39. struct Reference2D
  40. {
  41. /// Construct.
  42. Reference2D();
  43. /// Object type.
  44. ObjectType2D type_;
  45. /// Timeline.
  46. int timeline_;
  47. /// Z index (draw order).
  48. int zIndex_;
  49. };
  50. /// Mainline Key.
  51. struct MainlineKey2D
  52. {
  53. public:
  54. /// Construct.
  55. MainlineKey2D();
  56. /// Return reference by timeline.
  57. const Reference2D* GetReference(int timeline) const;
  58. /// Time.
  59. float time_;
  60. /// References.
  61. Vector<Reference2D> references_;
  62. };
  63. /// 2D Transform class for spriter animation.
  64. struct Transform2D
  65. {
  66. /// Construct.
  67. Transform2D();
  68. /// Construct from position, angle, scale.
  69. Transform2D(const Vector2& position, float angle, const Vector2& scale);
  70. /// Copy-construct from another transform.
  71. Transform2D(const Transform2D& other);
  72. /// Assign from another transform.
  73. Transform2D& operator = (const Transform2D& other);
  74. /// Multiply a transform.
  75. Transform2D operator * (const Transform2D& other) const;
  76. /// Linear interpolation with another transform.
  77. Transform2D Lerp(const Transform2D& other, float t, int spin) const;
  78. /// Position.
  79. Vector2 position_;
  80. /// Angle.
  81. float angle_;
  82. /// Scale.
  83. Vector2 scale_;
  84. };
  85. /// Timeline key.
  86. struct TimelineKey2D
  87. {
  88. /// Construct.
  89. TimelineKey2D();
  90. /// Time.
  91. float time_;
  92. /// Spin direction.
  93. int spin_;
  94. /// Transform.
  95. Transform2D transform_;
  96. /// Sprite.
  97. SharedPtr<Sprite2D> sprite_;
  98. /// Hot spot (pivot).
  99. Vector2 hotSpot_;
  100. /// Alpha.
  101. float alpha_;
  102. };
  103. /// Timeline.
  104. struct Timeline2D
  105. {
  106. /// Construct.
  107. Timeline2D();
  108. /// Name.
  109. String name_;
  110. /// Parent.
  111. int parent_;
  112. /// Object type.
  113. ObjectType2D type_;
  114. /// Object keys.
  115. Vector<TimelineKey2D> timelineKeys_;
  116. };
  117. /// Spriter animation. for more information please refer to http://www.brashmonkey.com/spriter.htm.
  118. class URHO3D_API Animation2D : public RefCounted
  119. {
  120. public:
  121. /// Construct.
  122. Animation2D(AnimationSet2D* animationSet);
  123. /// Destruct
  124. virtual ~Animation2D();
  125. /// Set name.
  126. void SetName(const String& name);
  127. /// Set length.
  128. void SetLength(float length);
  129. /// Set looped.
  130. void SetLooped(bool looped);
  131. /// Add mainline key.
  132. void AddMainlineKey(const MainlineKey2D& mainlineKey);
  133. /// Add timeline.
  134. void AddTimeline(const Timeline2D& timeline);
  135. /// Set timeline parent.
  136. void SetTimelineParent(int timeline, int timelineParent);
  137. /// Return animation set.
  138. AnimationSet2D* GetAnimationSet() const;
  139. /// Return name.
  140. const String& GetName() const { return name_; }
  141. /// Return length.
  142. float GetLength() const { return length_; }
  143. /// Return looped.
  144. bool IsLooped() const { return looped_; }
  145. /// Return all mainline keys.
  146. const Vector<MainlineKey2D>& GetMainlineKeys() const { return mainlineKeys_; }
  147. /// Return number of timelines.
  148. unsigned GetNumTimelines() const { return timelines_.Size();}
  149. /// Return timeline by index.
  150. const Timeline2D& GetTimeline(unsigned index) const { return timelines_[index]; }
  151. private:
  152. /// Animation set.
  153. WeakPtr<AnimationSet2D> animationSet_;
  154. /// Name.
  155. String name_;
  156. /// Length.
  157. float length_;
  158. /// Looped.
  159. bool looped_;
  160. /// All mainline Keys.
  161. Vector<MainlineKey2D> mainlineKeys_;
  162. /// All timelines.
  163. Vector<Timeline2D> timelines_;
  164. };
  165. }