Animation2D.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "../Container/Ptr.h"
  24. #include "../Container/RefCounted.h"
  25. #include "../Math/Vector2.h"
  26. namespace Atomic
  27. {
  28. class AnimationSet2D;
  29. class Sprite2D;
  30. /// 2D Transform class for spriter animation.
  31. struct Transform2D
  32. {
  33. /// Construct.
  34. Transform2D();
  35. /// Construct from position, angle, scale.
  36. Transform2D(const Vector2& position, float angle, const Vector2& scale);
  37. /// Copy-construct from another transform.
  38. Transform2D(const Transform2D& other);
  39. /// Assign from another transform.
  40. Transform2D& operator =(const Transform2D& other);
  41. /// Multiply a transform.
  42. Transform2D operator *(const Transform2D& other) const;
  43. /// Linear interpolation with another transform.
  44. Transform2D Lerp(const Transform2D& other, float t, int spin) const;
  45. /// Position.
  46. Vector2 position_;
  47. /// Angle.
  48. float angle_;
  49. /// Scale.
  50. Vector2 scale_;
  51. };
  52. /// 2D animation key frame.
  53. struct AnimationKeyFrame2D
  54. {
  55. /// Construct.
  56. AnimationKeyFrame2D();
  57. /// Time.
  58. float time_;
  59. /// Enabled (2D animation may disable node on fly).
  60. bool enabled_;
  61. /// Parent (2D animation may change parent on fly).
  62. int parent_;
  63. /// Transform.
  64. Transform2D transform_;
  65. /// Spin direction.
  66. int spin_;
  67. /// Draw order.
  68. int zIndex_;
  69. /// Sprite.
  70. SharedPtr<Sprite2D> sprite_;
  71. /// Alpha.
  72. float alpha_;
  73. /// Use hot spot.
  74. bool useHotSpot_;
  75. /// Hot spot.
  76. Vector2 hotSpot_;
  77. };
  78. /// 2D animation track.
  79. struct AnimationTrack2D
  80. {
  81. /// Name.
  82. String name_;
  83. /// Is sprite track.
  84. bool hasSprite_;
  85. /// Animation key frames.
  86. Vector<AnimationKeyFrame2D> keyFrames_;
  87. };
  88. /// 2D Animation.
  89. class ATOMIC_API Animation2D : public RefCounted
  90. {
  91. REFCOUNTED(Animation2D)
  92. public:
  93. /// Construct.
  94. Animation2D(AnimationSet2D* animationSet);
  95. /// Destruct
  96. virtual ~Animation2D();
  97. /// Set name.
  98. void SetName(const String& name);
  99. /// Set length.
  100. void SetLength(float length);
  101. /// Set looped.
  102. void SetLooped(bool looped);
  103. /// Return animation set.
  104. AnimationSet2D* GetAnimationSet() const;
  105. /// Return name.
  106. const String& GetName() const { return name_; }
  107. /// Return length.
  108. float GetLength() const { return length_; }
  109. /// Return looped.
  110. bool IsLooped() const { return looped_; }
  111. /// Return number of animation tracks.
  112. unsigned GetNumTracks() const { return tracks_.Size(); }
  113. /// Return animation track.
  114. const AnimationTrack2D& GetTrack(unsigned index) const;
  115. /// Return all animation tracks (internal use only).
  116. Vector<AnimationTrack2D>& GetAllTracks() { return tracks_; }
  117. private:
  118. /// Animation set.
  119. WeakPtr<AnimationSet2D> animationSet_;
  120. /// Name.
  121. String name_;
  122. /// Length.
  123. float length_;
  124. /// Looped.
  125. bool looped_;
  126. /// Animation tracks.
  127. Vector<AnimationTrack2D> tracks_;
  128. };
  129. }