ValueAnimationInfo.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Container/Ptr.h"
  24. #include "../Container/RefCounted.h"
  25. #include "../Container/Vector.h"
  26. #include "../Scene/AnimationDefs.h"
  27. namespace Atomic
  28. {
  29. class Object;
  30. class ValueAnimation;
  31. class Variant;
  32. struct VAnimEventFrame;
  33. /// Base class for a value animation instance, which includes animation runtime information and updates the target object's value automatically.
  34. class ATOMIC_API ValueAnimationInfo : public RefCounted
  35. {
  36. ATOMIC_REFCOUNTED(ValueAnimationInfo)
  37. public:
  38. /// Construct without target object.
  39. ValueAnimationInfo(ValueAnimation* animation, WrapMode wrapMode, float speed);
  40. /// Construct with target object.
  41. ValueAnimationInfo(Object* target, ValueAnimation* animation, WrapMode wrapMode, float speed);
  42. /// Copy construct.
  43. ValueAnimationInfo(const ValueAnimationInfo& other);
  44. /// Destruct.
  45. ~ValueAnimationInfo();
  46. /// Advance time position and apply. Return true when the animation is finished. No-op when the target object is not defined.
  47. bool Update(float timeStep);
  48. /// Set time position and apply. Return true when the animation is finished. No-op when the target object is not defined.
  49. bool SetTime(float time);
  50. /// Set wrap mode.
  51. void SetWrapMode(WrapMode wrapMode) { wrapMode_ = wrapMode; }
  52. /// Set speed.
  53. void SetSpeed(float speed) { speed_ = speed; }
  54. /// Return target object.
  55. Object* GetTarget() const;
  56. /// Return animation.
  57. ValueAnimation* GetAnimation() const { return animation_; }
  58. /// Return wrap mode.
  59. WrapMode GetWrapMode() const { return wrapMode_; }
  60. /// Return time position.
  61. float GetTime() const { return currentTime_; }
  62. /// Return speed.
  63. float GetSpeed() const { return speed_; }
  64. protected:
  65. /// Apply new animation value to the target object. Called by Update().
  66. virtual void ApplyValue(const Variant& newValue);
  67. /// Calculate scaled time.
  68. float CalculateScaledTime(float currentTime, bool& finished) const;
  69. /// Return event frames.
  70. void GetEventFrames(float beginTime, float endTime, PODVector<const VAnimEventFrame*>& eventFrames);
  71. /// Target object.
  72. WeakPtr<Object> target_;
  73. /// Attribute animation.
  74. SharedPtr<ValueAnimation> animation_;
  75. /// Wrap mode.
  76. WrapMode wrapMode_;
  77. /// Animation speed.
  78. float speed_;
  79. /// Current time.
  80. float currentTime_;
  81. /// Last scaled time.
  82. float lastScaledTime_;
  83. };
  84. }