ValueAnimationInfo.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // Copyright (c) 2008-2020 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 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() = default;
  58. bool ValueAnimationInfo::Update(float timeStep)
  59. {
  60. if (!animation_ || !target_)
  61. return true;
  62. return SetTime(currentTime_ + timeStep * speed_);
  63. }
  64. bool ValueAnimationInfo::SetTime(float time)
  65. {
  66. if (!animation_ || !target_)
  67. return true;
  68. currentTime_ = time;
  69. if (!animation_->IsValid())
  70. return true;
  71. bool finished = false;
  72. // Calculate scale time by wrap mode
  73. float scaledTime = CalculateScaledTime(currentTime_, finished);
  74. // Apply to the target object
  75. ApplyValue(animation_->GetAnimationValue(scaledTime));
  76. // Send keyframe event if necessary
  77. if (animation_->HasEventFrames())
  78. {
  79. PODVector<const VAnimEventFrame*> eventFrames;
  80. GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
  81. if (eventFrames.Size())
  82. {
  83. // Make a copy of the target weakptr, since if it expires, the AnimationInfo is deleted as well, in which case the
  84. // member variable cannot be accessed
  85. WeakPtr<Object> targetWeak(target_);
  86. for (unsigned i = 0; i < eventFrames.Size(); ++i)
  87. target_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
  88. // Break immediately if target expired due to event
  89. if (targetWeak.Expired())
  90. return true;
  91. }
  92. }
  93. lastScaledTime_ = scaledTime;
  94. return finished;
  95. }
  96. Object* ValueAnimationInfo::GetTarget() const
  97. {
  98. return target_;
  99. }
  100. void ValueAnimationInfo::ApplyValue(const Variant& newValue)
  101. {
  102. }
  103. float ValueAnimationInfo::CalculateScaledTime(float currentTime, bool& finished) const
  104. {
  105. float beginTime = animation_->GetBeginTime();
  106. float endTime = animation_->GetEndTime();
  107. switch (wrapMode_)
  108. {
  109. case WM_LOOP:
  110. {
  111. float span = endTime - beginTime;
  112. float time = fmodf(currentTime - beginTime, span);
  113. if (time < 0.0f)
  114. time += span;
  115. return beginTime + time;
  116. }
  117. case WM_ONCE:
  118. finished = (currentTime >= endTime);
  119. // Fallthrough
  120. case WM_CLAMP:
  121. return Clamp(currentTime, beginTime, endTime);
  122. default:
  123. URHO3D_LOGERROR("Unsupported attribute animation wrap mode");
  124. return beginTime;
  125. }
  126. }
  127. void ValueAnimationInfo::GetEventFrames(float beginTime, float endTime, PODVector<const VAnimEventFrame*>& eventFrames)
  128. {
  129. switch (wrapMode_)
  130. {
  131. case WM_LOOP:
  132. /// \todo This can miss an event if the deltatime is exactly the animation's length
  133. if (beginTime <= endTime)
  134. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  135. else
  136. {
  137. animation_->GetEventFrames(beginTime, animation_->GetEndTime(), eventFrames);
  138. animation_->GetEventFrames(animation_->GetBeginTime(), endTime, eventFrames);
  139. }
  140. break;
  141. case WM_ONCE:
  142. case WM_CLAMP:
  143. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  144. break;
  145. }
  146. }
  147. }