AttributeAnimationInstance.cpp 5.6 KB

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