ValueAnimationInfo.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 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. 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. if (eventFrames.Size())
  84. {
  85. // Make a copy of the target weakptr, since if it expires, the AnimationInfo is deleted as well, in which case the
  86. // member variable cannot be accessed
  87. WeakPtr<Object> targetWeak(target_);
  88. for (unsigned i = 0; i < eventFrames.Size(); ++i)
  89. target_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
  90. // Break immediately if target expired due to event
  91. if (targetWeak.Expired())
  92. return true;
  93. }
  94. }
  95. lastScaledTime_ = scaledTime;
  96. return finished;
  97. }
  98. Object* ValueAnimationInfo::GetTarget() const
  99. {
  100. return target_;
  101. }
  102. void ValueAnimationInfo::ApplyValue(const Variant& newValue)
  103. {
  104. }
  105. float ValueAnimationInfo::CalculateScaledTime(float currentTime, bool& finished) const
  106. {
  107. float beginTime = animation_->GetBeginTime();
  108. float endTime = animation_->GetEndTime();
  109. switch (wrapMode_)
  110. {
  111. case WM_LOOP:
  112. {
  113. float span = endTime - beginTime;
  114. float time = fmodf(currentTime - beginTime, span);
  115. if (time < 0.0f)
  116. time += span;
  117. return beginTime + time;
  118. }
  119. case WM_ONCE:
  120. finished = (currentTime >= endTime);
  121. // Fallthrough
  122. case WM_CLAMP:
  123. return Clamp(currentTime, beginTime, endTime);
  124. default:
  125. URHO3D_LOGERROR("Unsupported attribute animation wrap mode");
  126. return beginTime;
  127. }
  128. }
  129. void ValueAnimationInfo::GetEventFrames(float beginTime, float endTime, PODVector<const VAnimEventFrame*>& eventFrames)
  130. {
  131. switch (wrapMode_)
  132. {
  133. case WM_LOOP:
  134. /// \todo This can miss an event if the deltatime is exactly the animation's length
  135. if (beginTime <= endTime)
  136. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  137. else
  138. {
  139. animation_->GetEventFrames(beginTime, animation_->GetEndTime(), eventFrames);
  140. animation_->GetEventFrames(animation_->GetBeginTime(), endTime, eventFrames);
  141. }
  142. break;
  143. case WM_ONCE:
  144. case WM_CLAMP:
  145. animation_->GetEventFrames(beginTime, endTime, eventFrames);
  146. break;
  147. }
  148. }
  149. }