ValueAnimationInfo.h 3.4 KB

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