AttributeAnimation.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Copyright (c) 2008-2014 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 "AttributeAnimation.h"
  24. #include "DebugNew.h"
  25. namespace Urho3D
  26. {
  27. AttributeAnimation::AttributeAnimation(Context* context) :
  28. Serializable(context),
  29. animationMode_(AM_LOOP)
  30. {
  31. }
  32. AttributeAnimation::~AttributeAnimation()
  33. {
  34. }
  35. void AttributeAnimation::SetAnimationMode(AnimationMode animationMode)
  36. {
  37. animationMode_ = animationMode;
  38. }
  39. void AttributeAnimation::AddKeyFrame(float time, const Variant& value)
  40. {
  41. KeyFrame keyFrame;
  42. keyFrame.time_ = time;
  43. keyFrame.value_ = value;
  44. keyframes_.Push(keyFrame);
  45. }
  46. VariantType AttributeAnimation::GetValueType() const
  47. {
  48. return keyframes_.Empty() ? VAR_NONE : keyframes_[0].value_.GetType();
  49. }
  50. void AttributeAnimation::GetValue(float animationTime, Variant& animationValue) const
  51. {
  52. if (keyframes_.Empty())
  53. return;
  54. float keyTime = CalculateTime(animationTime, keyframes_.Back().time_);
  55. for (unsigned i = 1; i < keyframes_.Size(); ++i)
  56. {
  57. if (keyframes_[i].time_ > keyTime)
  58. {
  59. // Hack here
  60. if (GetValueType() == VAR_COLOR)
  61. {
  62. float t = (keyTime - keyframes_[i - 1].time_) / (keyframes_[i].time_ - keyframes_[i - 1].time_);
  63. animationValue = keyframes_[i - 1].value_.GetColor().Lerp(keyframes_[i].value_.GetColor(), t);
  64. }
  65. else
  66. animationValue = keyframes_[i - 1].value_;
  67. break;
  68. }
  69. }
  70. }
  71. float AttributeAnimation::CalculateTime(float animationTime, float totalTime) const
  72. {
  73. switch (animationMode_)
  74. {
  75. case AM_LOOP:
  76. return fmodf(animationTime, totalTime);
  77. case AM_CLAMP:
  78. return Clamp(animationTime, 0.0f, totalTime);
  79. case AM_PINGPONG:
  80. {
  81. float doubleTotalTime = totalTime * 2.0f;
  82. float fract = fmodf(animationTime, doubleTotalTime);
  83. return (fract < totalTime) ? fract : doubleTotalTime - fract;
  84. }
  85. break;
  86. }
  87. return animationTime;
  88. }
  89. }