AttributeAnimationInstance.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. for (unsigned i = 1; i < keyFrames.Size(); ++i)
  65. {
  66. const AttributeKeyFrame& currKeyFrame = keyFrames[i];
  67. if (scaledTime <= currKeyFrame.time_)
  68. {
  69. const AttributeKeyFrame& prevKeyFrame = keyFrames[i - 1];
  70. if (!attributeAnimation_->IsInterpolatable())
  71. animatable_->OnSetAttribute(attributeInfo_, prevKeyFrame.value_);
  72. else
  73. animatable_->OnSetAttribute(attributeInfo_, Interpolation(prevKeyFrame, currKeyFrame, scaledTime));
  74. break;
  75. }
  76. }
  77. if (attributeAnimation_->HasEventFrames())
  78. {
  79. Vector<const AttributeEventFrame*> eventFrames;
  80. switch (wrapMode_)
  81. {
  82. case WM_LOOP:
  83. if (lastScaledTime_ < scaledTime)
  84. attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
  85. else
  86. {
  87. attributeAnimation_->GetEventFrames(lastScaledTime_, attributeAnimation_->GetEndTime(), eventFrames);
  88. attributeAnimation_->GetEventFrames(attributeAnimation_->GetBeginTime(), scaledTime, eventFrames);
  89. }
  90. break;
  91. case WM_ONCE:
  92. case WM_CLAMP:
  93. attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
  94. break;
  95. }
  96. for (unsigned i = 0; i < eventFrames.Size(); ++i)
  97. animatable_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
  98. }
  99. lastScaledTime_ = scaledTime;
  100. return finished;
  101. }
  102. Animatable* AttributeAnimationInstance::GetAnimatable() const
  103. {
  104. return animatable_;
  105. }
  106. AttributeAnimation* AttributeAnimationInstance::GetAttributeAnimation() const
  107. {
  108. return attributeAnimation_;
  109. }
  110. float AttributeAnimationInstance::CalculateScaledTime(float currentTime, bool& finished) const
  111. {
  112. float beginTime = attributeAnimation_->GetBeginTime();
  113. float endTime = attributeAnimation_->GetEndTime();
  114. switch (wrapMode_)
  115. {
  116. case WM_LOOP:
  117. {
  118. float span = endTime - beginTime;
  119. return beginTime + fmodf(currentTime - beginTime, span);
  120. }
  121. case WM_ONCE:
  122. finished = (currentTime >= endTime);
  123. case WM_CLAMP:
  124. return Clamp(currentTime, beginTime, endTime);
  125. }
  126. return beginTime;
  127. }
  128. Variant AttributeAnimationInstance::Interpolation(const AttributeKeyFrame& prevKeyFrame, const AttributeKeyFrame& currKeyFrame, float scaledTime) const
  129. {
  130. float factor = (scaledTime - prevKeyFrame.time_) / (currKeyFrame.time_ - prevKeyFrame.time_);
  131. switch (attributeAnimation_->GetValueType())
  132. {
  133. case VAR_FLOAT:
  134. return Lerp(prevKeyFrame.value_.GetFloat(), currKeyFrame.value_.GetFloat(), factor);
  135. case VAR_VECTOR2:
  136. return prevKeyFrame.value_.GetVector2().Lerp(currKeyFrame.value_.GetVector2(), factor);
  137. case VAR_VECTOR3:
  138. return prevKeyFrame.value_.GetVector3().Lerp(currKeyFrame.value_.GetVector3(), factor);
  139. case VAR_VECTOR4:
  140. return prevKeyFrame.value_.GetVector4().Lerp(currKeyFrame.value_.GetVector4(), factor);
  141. case VAR_QUATERNION:
  142. return prevKeyFrame.value_.GetQuaternion().Slerp(currKeyFrame.value_.GetQuaternion(), factor);
  143. case VAR_COLOR:
  144. return prevKeyFrame.value_.GetColor().Lerp(currKeyFrame.value_.GetColor(), factor);
  145. }
  146. LOGERROR("value type");
  147. return Variant::EMPTY;
  148. }
  149. }