ValueAnimationInfo.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "ValueAnimation.h"
  24. #include "ValueAnimationInfo.h"
  25. #include "Log.h"
  26. #include "DebugNew.h"
  27. namespace Urho3D
  28. {
  29. ValueAnimationInfo::ValueAnimationInfo(ValueAnimation* animation, WrapMode wrapMode, float speed) :
  30. animation_(animation),
  31. wrapMode_(wrapMode),
  32. speed_(speed),
  33. currentTime_(0.0f),
  34. lastScaledTime_(0.0f)
  35. {
  36. speed_ = Max(0.0f, speed_);
  37. }
  38. ValueAnimationInfo::ValueAnimationInfo(Object* target, ValueAnimation* animation, WrapMode wrapMode, float speed) :
  39. target_(target),
  40. animation_(animation),
  41. wrapMode_(wrapMode),
  42. speed_(speed),
  43. currentTime_(0.0f),
  44. lastScaledTime_(0.0f)
  45. {
  46. speed_ = Max(0.0f, speed_);
  47. }
  48. ValueAnimationInfo::ValueAnimationInfo(const ValueAnimationInfo& other) :
  49. target_(other.target_),
  50. animation_(other.animation_),
  51. wrapMode_(other.wrapMode_),
  52. speed_(other.speed_),
  53. currentTime_(0.0f),
  54. lastScaledTime_(0.0f)
  55. {
  56. }
  57. ValueAnimationInfo::~ValueAnimationInfo()
  58. {
  59. }
  60. bool ValueAnimationInfo::Update(float timeStep)
  61. {
  62. if (!animation_ || !target_)
  63. return true;
  64. currentTime_ += timeStep * speed_;
  65. if (!animation_->IsValid())
  66. return true;
  67. bool finished = false;
  68. // Calculate scale time by wrap mode
  69. float scaledTime = CalculateScaledTime(currentTime_, finished);
  70. // Apply to the target object
  71. ApplyValue(animation_->GetAnimationValue(scaledTime));
  72. // Send keyframe event if necessary
  73. if (animation_->HasEventFrames())
  74. {
  75. PODVector<const VAnimEventFrame*> eventFrames;
  76. GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
  77. for (unsigned i = 0; i < eventFrames.Size(); ++i)
  78. target_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
  79. }
  80. lastScaledTime_ = scaledTime;
  81. return finished;
  82. }
  83. Object* ValueAnimationInfo::GetTarget() const
  84. {
  85. return target_;
  86. }
  87. float ValueAnimationInfo::CalculateScaledTime(float currentTime, bool& finished) const
  88. {
  89. float beginTime = animation_->GetBeginTime();
  90. float endTime = animation_->GetEndTime();
  91. switch (wrapMode_)
  92. {
  93. case WM_LOOP:
  94. {
  95. float span = endTime - beginTime;
  96. float time = fmodf(currentTime - beginTime, span);
  97. if (time < 0.0f)
  98. time += span;
  99. return beginTime + time;
  100. }
  101. case WM_ONCE:
  102. finished = (currentTime >= endTime);
  103. // Fallthrough
  104. case WM_CLAMP:
  105. return Clamp(currentTime, beginTime, endTime);
  106. default:
  107. LOGERROR("Unsupported attribute animation wrap mode");
  108. return beginTime;
  109. }
  110. }
  111. void ValueAnimationInfo::GetEventFrames(float beginTime, float endTime, PODVector<const VAnimEventFrame*>& eventFrames)
  112. {
  113. switch (wrapMode_)
  114. {
  115. case WM_LOOP:
  116. if (beginTime < endTime)
  117. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  118. else
  119. {
  120. animation_->GetEventFrames(beginTime, animation_->GetEndTime(), eventFrames);
  121. animation_->GetEventFrames(animation_->GetBeginTime(), endTime, eventFrames);
  122. }
  123. break;
  124. case WM_ONCE:
  125. case WM_CLAMP:
  126. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  127. break;
  128. }
  129. }
  130. }