ValueAnimation.h 5.4 KB

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