ValueAnimationInfo.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Copyright (c) 2008-2015 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 "../IO/Log.h"
  24. #include "../Scene/ValueAnimation.h"
  25. #include "../Scene/ValueAnimationInfo.h"
  26. #include "../DebugNew.h"
  27. namespace Atomic
  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. void ValueAnimationInfo::ApplyValue(const Variant& newValue)
  88. {
  89. }
  90. float ValueAnimationInfo::CalculateScaledTime(float currentTime, bool& finished) const
  91. {
  92. float beginTime = animation_->GetBeginTime();
  93. float endTime = animation_->GetEndTime();
  94. switch (wrapMode_)
  95. {
  96. case WM_LOOP:
  97. {
  98. float span = endTime - beginTime;
  99. float time = fmodf(currentTime - beginTime, span);
  100. if (time < 0.0f)
  101. time += span;
  102. return beginTime + time;
  103. }
  104. case WM_ONCE:
  105. finished = (currentTime >= endTime);
  106. // Fallthrough
  107. case WM_CLAMP:
  108. return Clamp(currentTime, beginTime, endTime);
  109. default:
  110. LOGERROR("Unsupported attribute animation wrap mode");
  111. return beginTime;
  112. }
  113. }
  114. void ValueAnimationInfo::GetEventFrames(float beginTime, float endTime, PODVector<const VAnimEventFrame*>& eventFrames)
  115. {
  116. switch (wrapMode_)
  117. {
  118. case WM_LOOP:
  119. if (beginTime < endTime)
  120. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  121. else
  122. {
  123. animation_->GetEventFrames(beginTime, animation_->GetEndTime(), eventFrames);
  124. animation_->GetEventFrames(animation_->GetBeginTime(), endTime, eventFrames);
  125. }
  126. break;
  127. case WM_ONCE:
  128. case WM_CLAMP:
  129. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  130. break;
  131. }
  132. }
  133. }