AttributeAnimationInstance.cpp 5.2 KB

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