ValueAnimation.h 5.0 KB

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