ValueAnimation.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Core/Variant.h"
  6. #include "../Resource/Resource.h"
  7. namespace Urho3D
  8. {
  9. class XMLElement;
  10. class JSONValue;
  11. /// Interpolation method.
  12. enum InterpMethod
  13. {
  14. /// No interpolation.
  15. IM_NONE = 0,
  16. /// Linear interpolation (default).
  17. IM_LINEAR,
  18. /// Cardinal spline interpolation, default tension value is 0.5f. For more information please refer to http://cubic.org/docs/hermite.htm.
  19. IM_SPLINE,
  20. };
  21. /// Value animation key frame.
  22. struct VAnimKeyFrame
  23. {
  24. /// Time.
  25. float time_;
  26. /// Value.
  27. Variant value_;
  28. };
  29. /// Value animation event frame.
  30. struct VAnimEventFrame
  31. {
  32. /// Time.
  33. float time_;
  34. /// Event type.
  35. StringHash eventType_;
  36. /// Event data.
  37. VariantMap eventData_;
  38. };
  39. /// Value animation class.
  40. class URHO3D_API ValueAnimation : public Resource
  41. {
  42. URHO3D_OBJECT(ValueAnimation, Resource);
  43. public:
  44. /// Construct.
  45. explicit ValueAnimation(Context* context);
  46. /// Destruct.
  47. ~ValueAnimation() override;
  48. /// Register object factory.
  49. /// @nobind
  50. static void RegisterObject(Context* context);
  51. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  52. bool BeginLoad(Deserializer& source) override;
  53. /// Save resource. Return true if successful.
  54. bool Save(Serializer& dest) const override;
  55. /// Load from XML data. Return true if successful.
  56. bool LoadXML(const XMLElement& source);
  57. /// Save as XML data. Return true if successful.
  58. bool SaveXML(XMLElement& dest) const;
  59. /// Load from JSON data. Return true if successful.
  60. bool LoadJSON(const JSONValue& source);
  61. /// Save as XML data. Return true if successful.
  62. bool SaveJSON(JSONValue& dest) const;
  63. /// Set owner.
  64. void SetOwner(void* owner);
  65. /// Set interpolation method.
  66. /// @property
  67. void SetInterpolationMethod(InterpMethod method);
  68. /// Set spline tension, should be between 0.0f and 1.0f, but this is not a must.
  69. /// @property
  70. void SetSplineTension(float tension);
  71. /// Set value type.
  72. /// @property
  73. void SetValueType(VariantType valueType);
  74. /// Set key frame.
  75. bool SetKeyFrame(float time, const Variant& value);
  76. /// Set event frame.
  77. void SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData = VariantMap());
  78. /// Return animation is valid.
  79. bool IsValid() const;
  80. /// Return owner.
  81. void* GetOwner() const { return owner_; }
  82. /// Return interpolation method.
  83. /// @property
  84. InterpMethod GetInterpolationMethod() const { return interpolationMethod_; }
  85. /// Return spline tension.
  86. /// @property
  87. float GetSplineTension() const { return splineTension_; }
  88. /// Return value type.
  89. /// @property
  90. VariantType GetValueType() const { return valueType_; }
  91. /// Return begin time.
  92. float GetBeginTime() const { return beginTime_; }
  93. /// Return end time.
  94. float GetEndTime() const { return endTime_; }
  95. /// Return animation value.
  96. Variant GetAnimationValue(float scaledTime) const;
  97. /// Return all key frames.
  98. const Vector<VAnimKeyFrame>& GetKeyFrames() const { return keyFrames_; }
  99. /// Has event frames.
  100. bool HasEventFrames() const { return !eventFrames_.Empty(); }
  101. /// Return all event frames between time.
  102. void GetEventFrames(float beginTime, float endTime, Vector<const VAnimEventFrame*>& eventFrames) const;
  103. protected:
  104. /// Linear interpolation.
  105. Variant LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const;
  106. /// Spline interpolation.
  107. Variant SplineInterpolation(unsigned index1, unsigned index2, float scaledTime) const;
  108. /// Update spline tangents.
  109. void UpdateSplineTangents() const;
  110. /// Return (value1 - value2) * t.
  111. Variant SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const;
  112. /// Owner.
  113. void* owner_;
  114. /// Interpolation method.
  115. InterpMethod interpolationMethod_;
  116. /// Spline tension.
  117. float splineTension_;
  118. /// Value type.
  119. VariantType valueType_;
  120. /// Interpolatable flag.
  121. bool interpolatable_;
  122. /// Begin time.
  123. float beginTime_;
  124. /// End time.
  125. float endTime_;
  126. /// Key frames.
  127. Vector<VAnimKeyFrame> keyFrames_;
  128. /// Spline tangents.
  129. mutable VariantVector splineTangents_;
  130. /// Spline tangents dirty.
  131. mutable bool splineTangentsDirty_;
  132. /// Event frames.
  133. Vector<VAnimEventFrame> eventFrames_;
  134. };
  135. }