Animation2D.cpp 4.4 KB

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