ValueAnimationInfo.cpp 4.4 KB

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