Animation2D.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "../Precompiled.h"
  23. #include "../IO/Log.h"
  24. #include "../Atomic2D/Animation2D.h"
  25. #include "../Atomic2D/AnimationSet2D.h"
  26. #include "../Atomic2D/Sprite2D.h"
  27. #include "../DebugNew.h"
  28. namespace Atomic
  29. {
  30. Transform2D::Transform2D() :
  31. position_(Vector2::ZERO),
  32. angle_(0.0f),
  33. scale_(1.0f, 1.0f)
  34. {
  35. }
  36. Transform2D::Transform2D(const Vector2& position, float angle, const Vector2& scale) :
  37. position_(position),
  38. angle_(angle),
  39. scale_(scale)
  40. {
  41. }
  42. Transform2D::Transform2D(const Transform2D& other) :
  43. position_(other.position_),
  44. angle_(other.angle_),
  45. scale_(other.scale_)
  46. {
  47. }
  48. Transform2D& Transform2D::operator =(const Transform2D& other)
  49. {
  50. position_ = other.position_;
  51. angle_ = other.angle_;
  52. scale_ = other.scale_;
  53. return *this;
  54. }
  55. Transform2D Transform2D::operator *(const Transform2D& other) const
  56. {
  57. float x = scale_.x_ * other.position_.x_;
  58. float y = scale_.y_ * other.position_.y_;
  59. float s = Sin(angle_);
  60. float c = Cos(angle_);
  61. Vector2 position;
  62. position.x_ = (x * c) - (y * s);
  63. position.y_ = (x * s) + (y * c);
  64. position = position_ + position;
  65. float angle = angle_ + other.angle_;
  66. Vector2 scale = scale_ * other.scale_;
  67. return Transform2D(position, angle, scale);
  68. }
  69. Transform2D Transform2D::Lerp(const Transform2D& other, float t, int spin) const
  70. {
  71. Transform2D ret;
  72. ret.position_ = position_.Lerp(other.position_, t);
  73. if (spin > 0 && angle_ > other.angle_)
  74. ret.angle_ = Atomic::Lerp(angle_, other.angle_ + 360.0f, t);
  75. else if (spin < 0 && angle_ < other.angle_)
  76. ret.angle_ = Atomic::Lerp(angle_, other.angle_ - 360.0f, t);
  77. else
  78. ret.angle_ = Atomic::Lerp(angle_, other.angle_, t);
  79. ret.scale_ = scale_.Lerp(other.scale_, t);
  80. return ret;
  81. }
  82. AnimationKeyFrame2D::AnimationKeyFrame2D() :
  83. time_(0.0f),
  84. enabled_(false),
  85. parent_(-1),
  86. spin_(1),
  87. zIndex_(0),
  88. alpha_(1.0f),
  89. useHotSpot_(false)
  90. {
  91. }
  92. Animation2D::Animation2D(AnimationSet2D* animationSet) :
  93. animationSet_(animationSet),
  94. length_(0.0f),
  95. looped_(true)
  96. {
  97. }
  98. Animation2D::~Animation2D()
  99. {
  100. }
  101. void Animation2D::SetName(const String& name)
  102. {
  103. name_ = name;
  104. }
  105. void Animation2D::SetLength(float length)
  106. {
  107. length_ = Max(0.0f, length);
  108. }
  109. void Animation2D::SetLooped(bool looped)
  110. {
  111. looped_ = looped;
  112. }
  113. AnimationSet2D* Animation2D::GetAnimationSet() const
  114. {
  115. return animationSet_;
  116. }
  117. const AnimationTrack2D& Animation2D::GetTrack(unsigned index) const
  118. {
  119. if (index >= tracks_.Size())
  120. {
  121. LOGWARNING("Index out of range");
  122. index = 0;
  123. }
  124. return tracks_[index];
  125. }
  126. }