ValueAnimationInfo.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2008-2016 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. return SetTime(currentTime_ + timeStep * speed_);
  65. }
  66. bool ValueAnimationInfo::SetTime(float time)
  67. {
  68. if (!animation_ || !target_)
  69. return true;
  70. currentTime_ = time;
  71. if (!animation_->IsValid())
  72. return true;
  73. bool finished = false;
  74. // Calculate scale time by wrap mode
  75. float scaledTime = CalculateScaledTime(currentTime_, finished);
  76. // Apply to the target object
  77. ApplyValue(animation_->GetAnimationValue(scaledTime));
  78. // Send keyframe event if necessary
  79. if (animation_->HasEventFrames())
  80. {
  81. PODVector<const VAnimEventFrame*> eventFrames;
  82. GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
  83. for (unsigned i = 0; i < eventFrames.Size(); ++i)
  84. target_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
  85. }
  86. lastScaledTime_ = scaledTime;
  87. return finished;
  88. }
  89. Object* ValueAnimationInfo::GetTarget() const
  90. {
  91. return target_;
  92. }
  93. void ValueAnimationInfo::ApplyValue(const Variant& newValue)
  94. {
  95. }
  96. float ValueAnimationInfo::CalculateScaledTime(float currentTime, bool& finished) const
  97. {
  98. float beginTime = animation_->GetBeginTime();
  99. float endTime = animation_->GetEndTime();
  100. switch (wrapMode_)
  101. {
  102. case WM_LOOP:
  103. {
  104. float span = endTime - beginTime;
  105. float time = fmodf(currentTime - beginTime, span);
  106. if (time < 0.0f)
  107. time += span;
  108. return beginTime + time;
  109. }
  110. case WM_ONCE:
  111. finished = (currentTime >= endTime);
  112. // Fallthrough
  113. case WM_CLAMP:
  114. return Clamp(currentTime, beginTime, endTime);
  115. default:
  116. ATOMIC_LOGERROR("Unsupported attribute animation wrap mode");
  117. return beginTime;
  118. }
  119. }
  120. void ValueAnimationInfo::GetEventFrames(float beginTime, float endTime, PODVector<const VAnimEventFrame*>& eventFrames)
  121. {
  122. switch (wrapMode_)
  123. {
  124. case WM_LOOP:
  125. if (beginTime < endTime)
  126. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  127. else
  128. {
  129. animation_->GetEventFrames(beginTime, animation_->GetEndTime(), eventFrames);
  130. animation_->GetEventFrames(animation_->GetBeginTime(), endTime, eventFrames);
  131. }
  132. break;
  133. case WM_ONCE:
  134. case WM_CLAMP:
  135. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  136. break;
  137. }
  138. }
  139. }