ValueAnimation.h 5.7 KB

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