AttributeAnimationInstance.cpp 5.9 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 "Animatable.h"
  24. #include "AttributeAnimation.h"
  25. #include "AttributeAnimationInstance.h"
  26. #include "Log.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. AttributeAnimationInstance::AttributeAnimationInstance(Animatable* animatable, const AttributeInfo& attributeInfo, AttributeAnimation* attributeAnimation, WrapMode wrapMode, float speed) :
  31. animatable_(animatable),
  32. attributeInfo_(attributeInfo),
  33. attributeAnimation_(attributeAnimation),
  34. wrapMode_(wrapMode),
  35. speed_(speed),
  36. currentTime_(0.0f),
  37. lastScaledTime_(0.0f)
  38. {
  39. speed_ = Max(0.0f, speed_);
  40. }
  41. AttributeAnimationInstance::AttributeAnimationInstance(const AttributeAnimationInstance& other) :
  42. animatable_(other.animatable_),
  43. attributeInfo_(other.attributeInfo_),
  44. attributeAnimation_(other.attributeAnimation_),
  45. wrapMode_(other.wrapMode_),
  46. speed_(other.speed_),
  47. currentTime_(0.0f),
  48. lastScaledTime_(0.0f)
  49. {
  50. }
  51. AttributeAnimationInstance::~AttributeAnimationInstance()
  52. {
  53. }
  54. bool AttributeAnimationInstance::Update(float timeStep)
  55. {
  56. if (!attributeAnimation_)
  57. return true;
  58. currentTime_ += timeStep * speed_;
  59. const Vector<AttributeKeyFrame>& keyFrames = attributeAnimation_->GetKeyFrames();
  60. if (keyFrames.Size() < 2)
  61. return true;
  62. bool finished = false;
  63. float scaledTime = CalculateScaledTime(currentTime_, finished);
  64. unsigned index = 1;
  65. for (; index < keyFrames.Size(); ++index)
  66. {
  67. const AttributeKeyFrame& currKeyFrame = keyFrames[index];
  68. if (scaledTime < currKeyFrame.time_)
  69. break;
  70. }
  71. const AttributeKeyFrame& prevKeyFrame = keyFrames[index - 1];
  72. if (index >= keyFrames.Size() || !attributeAnimation_->IsInterpolatable())
  73. animatable_->OnSetAttribute(attributeInfo_, prevKeyFrame.value_);
  74. else
  75. {
  76. const AttributeKeyFrame& currKeyFrame = keyFrames[index];
  77. animatable_->OnSetAttribute(attributeInfo_, Interpolation(prevKeyFrame, currKeyFrame, scaledTime));
  78. }
  79. if (attributeAnimation_->HasEventFrames())
  80. {
  81. Vector<const AttributeEventFrame*> eventFrames;
  82. switch (wrapMode_)
  83. {
  84. case WM_LOOP:
  85. if (lastScaledTime_ < scaledTime)
  86. attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
  87. else
  88. {
  89. attributeAnimation_->GetEventFrames(lastScaledTime_, attributeAnimation_->GetEndTime(), eventFrames);
  90. attributeAnimation_->GetEventFrames(attributeAnimation_->GetBeginTime(), scaledTime, eventFrames);
  91. }
  92. break;
  93. case WM_ONCE:
  94. case WM_CLAMP:
  95. attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
  96. break;
  97. }
  98. for (unsigned i = 0; i < eventFrames.Size(); ++i)
  99. animatable_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
  100. }
  101. lastScaledTime_ = scaledTime;
  102. return finished;
  103. }
  104. Animatable* AttributeAnimationInstance::GetAnimatable() const
  105. {
  106. return animatable_;
  107. }
  108. AttributeAnimation* AttributeAnimationInstance::GetAttributeAnimation() const
  109. {
  110. return attributeAnimation_;
  111. }
  112. float AttributeAnimationInstance::CalculateScaledTime(float currentTime, bool& finished) const
  113. {
  114. float beginTime = attributeAnimation_->GetBeginTime();
  115. float endTime = attributeAnimation_->GetEndTime();
  116. switch (wrapMode_)
  117. {
  118. case WM_LOOP:
  119. {
  120. float span = endTime - beginTime;
  121. return beginTime + fmodf(currentTime - beginTime, span);
  122. }
  123. case WM_ONCE:
  124. finished = (currentTime >= endTime);
  125. case WM_CLAMP:
  126. return Clamp(currentTime, beginTime, endTime);
  127. }
  128. return beginTime;
  129. }
  130. Variant AttributeAnimationInstance::Interpolation(const AttributeKeyFrame& prevKeyFrame, const AttributeKeyFrame& currKeyFrame, float scaledTime) const
  131. {
  132. float factor = (scaledTime - prevKeyFrame.time_) / (currKeyFrame.time_ - prevKeyFrame.time_);
  133. switch (attributeAnimation_->GetValueType())
  134. {
  135. case VAR_FLOAT:
  136. return Lerp(prevKeyFrame.value_.GetFloat(), currKeyFrame.value_.GetFloat(), factor);
  137. case VAR_VECTOR2:
  138. return prevKeyFrame.value_.GetVector2().Lerp(currKeyFrame.value_.GetVector2(), factor);
  139. case VAR_VECTOR3:
  140. return prevKeyFrame.value_.GetVector3().Lerp(currKeyFrame.value_.GetVector3(), factor);
  141. case VAR_VECTOR4:
  142. return prevKeyFrame.value_.GetVector4().Lerp(currKeyFrame.value_.GetVector4(), factor);
  143. case VAR_QUATERNION:
  144. return prevKeyFrame.value_.GetQuaternion().Slerp(currKeyFrame.value_.GetQuaternion(), factor);
  145. case VAR_COLOR:
  146. return prevKeyFrame.value_.GetColor().Lerp(currKeyFrame.value_.GetColor(), factor);
  147. }
  148. LOGERROR("value type");
  149. return Variant::EMPTY;
  150. }
  151. }