Animation2D.cpp 4.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. #include "Precompiled.h"
  23. #include "Animation2D.h"
  24. #include "AnimationSet2D.h"
  25. #include "Sprite2D.h"
  26. #include "DebugNew.h"
  27. namespace Urho3D
  28. {
  29. Reference2D::Reference2D() :
  30. type_(OT_BONE),
  31. timeline_(0),
  32. zIndex_(0)
  33. {
  34. }
  35. MainlineKey2D::MainlineKey2D() :
  36. time_(0)
  37. {
  38. }
  39. Transform2D::Transform2D() :
  40. position_(Vector2::ZERO),
  41. angle_(0.0f),
  42. scale_(Vector2::ONE)
  43. {
  44. }
  45. Transform2D::Transform2D(const Vector2& position, float angle, const Vector2& scale) :
  46. position_(position),
  47. angle_(angle),
  48. scale_(scale)
  49. {
  50. }
  51. Transform2D::Transform2D(const Transform2D& other) :
  52. position_(other.position_),
  53. angle_(other.angle_),
  54. scale_(other.scale_)
  55. {
  56. }
  57. Transform2D& Transform2D::operator = (const Transform2D& other)
  58. {
  59. position_ = other.position_;
  60. angle_ = other.angle_;
  61. scale_ = other.scale_;
  62. return *this;
  63. }
  64. Transform2D Transform2D::operator * (const Transform2D& other) const
  65. {
  66. float x = scale_.x_ * other.position_.x_;
  67. float y = scale_.y_ * other.position_.y_;
  68. float s = Sin(angle_);
  69. float c = Cos(angle_);
  70. Vector2 position;
  71. position.x_ = (x * c) - (y * s);
  72. position.y_ = (x * s) + (y * c);
  73. position = position_ + position;
  74. float angle = angle_ + other.angle_;
  75. Vector2 scale = scale_ * other.scale_;
  76. return Transform2D(position, angle, scale);
  77. }
  78. Transform2D Transform2D::Lerp(const Transform2D& other, float t, int spin) const
  79. {
  80. Transform2D ret;
  81. ret.position_ = position_.Lerp(other.position_, t);
  82. if (spin > 0 && angle_ > other.angle_)
  83. ret.angle_ = Urho3D::Lerp(angle_, other.angle_ + 360.0f, t);
  84. else if (spin < 0 && angle_ < other.angle_)
  85. ret.angle_= Urho3D::Lerp(angle_, other.angle_ - 360.0f, t);
  86. else
  87. ret.angle_= Urho3D::Lerp(angle_, other.angle_, t);
  88. ret.scale_ = scale_.Lerp(other.scale_, t);
  89. return ret;
  90. }
  91. const Reference2D* MainlineKey2D::GetReference(int timeline) const
  92. {
  93. for (unsigned i = 0; i < references_.Size(); ++i)
  94. {
  95. if (references_[i].timeline_ == timeline)
  96. return &references_[i];
  97. }
  98. return 0;
  99. }
  100. TimelineKey2D::TimelineKey2D() :
  101. time_(0.0f),
  102. spin_(1),
  103. hotSpot_(0.0f, 1.0f),
  104. alpha_(1.0f)
  105. {
  106. }
  107. Timeline2D::Timeline2D() :
  108. parent_(-1),
  109. type_(OT_BONE)
  110. {
  111. }
  112. Animation2D::Animation2D(AnimationSet2D* animationSet) :
  113. animationSet_(animationSet),
  114. length_(0.0f),
  115. looped_(true)
  116. {
  117. }
  118. Animation2D::~Animation2D()
  119. {
  120. }
  121. void Animation2D::SetName(const String& name)
  122. {
  123. name_ = name;
  124. }
  125. void Animation2D::SetLength(float length)
  126. {
  127. length_ = Max(0.0f, length);
  128. }
  129. void Animation2D::SetLooped(bool looped)
  130. {
  131. looped_ = looped;
  132. }
  133. void Animation2D::AddMainlineKey(const MainlineKey2D& mainlineKey)
  134. {
  135. mainlineKeys_.Push(mainlineKey);
  136. }
  137. void Animation2D::AddTimeline(const Timeline2D& timeline)
  138. {
  139. timelines_.Push(timeline);
  140. }
  141. void Animation2D::SetTimelineParent(int timeline, int timelineParent)
  142. {
  143. if (timeline == timelineParent)
  144. return;
  145. timelines_[timeline].parent_ = timelineParent;
  146. }
  147. AnimationSet2D* Animation2D::GetAnimationSet() const
  148. {
  149. return animationSet_;
  150. }
  151. }