AttributeAnimation.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Animatable;
  28. class ObjectAnimation;
  29. class XMLElement;
  30. struct AttributeInfo;
  31. /// Interpolation method.
  32. enum InterpolationMethod
  33. {
  34. /// Linear interpolation (default).
  35. IM_LINEAR = 0,
  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. /// Attribute key frame
  40. struct AttributeKeyFrame
  41. {
  42. /// Time.
  43. float time_;
  44. /// Value.
  45. Variant value_;
  46. };
  47. /// Attribute event frame.
  48. struct AttributeEventFrame
  49. {
  50. /// Time.
  51. float time_;
  52. /// Event type.
  53. StringHash eventType_;
  54. /// Event data.
  55. VariantMap eventData_;
  56. };
  57. /// Base class for attribute animation.
  58. class URHO3D_API AttributeAnimation : public Resource
  59. {
  60. OBJECT(AttributeAnimation);
  61. public:
  62. /// Construct.
  63. AttributeAnimation(Context* context);
  64. /// Destruct.
  65. virtual ~AttributeAnimation();
  66. /// Register object factory.
  67. static void RegisterObject(Context* context);
  68. /// Load resource. Return true if successful.
  69. virtual bool Load(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. /// Set object animation.
  77. void SetObjectAnimation(ObjectAnimation* objectAnimation);
  78. /// Set interpolation method.
  79. void SetInterpolationMethod(InterpolationMethod method);
  80. /// Set spline tension.
  81. void SetSplineTension(float tension);
  82. /// Set value type.
  83. void SetValueType(VariantType valueType);
  84. /// Set key frame.
  85. bool SetKeyFrame(float time, const Variant& value);
  86. /// Set event frame.
  87. void SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData = VariantMap());
  88. /// Return animation is valid.
  89. bool IsValid() const;
  90. /// Return object animation.
  91. ObjectAnimation* GetObjectAnimation() const;
  92. /// Return interpolation method.
  93. InterpolationMethod GetInterpolationMethod() const { return interpolationMethod_; }
  94. /// Return spline tension.
  95. float GetSplineTension() const { return splineTension_; }
  96. /// Return value type.
  97. VariantType GetValueType() const { return valueType_; }
  98. /// Return begin time.
  99. float GetBeginTime() const { return beginTime_; }
  100. /// Return end time.
  101. float GetEndTime() const { return endTime_; }
  102. /// Update object's attribute value.
  103. void UpdateAttributeValue(Animatable* animatable, const AttributeInfo& attributeInfo, float scaledTime);
  104. /// Has event frames.
  105. bool HasEventFrames() const { return eventFrames_.Size() != 0; }
  106. /// Return all event frames between time.
  107. void GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const;
  108. protected:
  109. /// Linear interpolation.
  110. Variant LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const;
  111. /// Linear interpolation.
  112. Variant LinearInterpolation(const Variant& value1, const Variant& value2, float t) const;
  113. /// Spline interpolation.
  114. Variant SplineInterpolation(unsigned index1, unsigned index2, float scaledTime);
  115. /// Update spline tangents.
  116. void UpdateSplineTangents();
  117. /// Return (value1 - value2) * t.
  118. Variant SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const;
  119. /// Object animation.
  120. WeakPtr<ObjectAnimation> objectAnimation_;
  121. /// Interpolation method.
  122. InterpolationMethod interpolationMethod_;
  123. /// Spline tension.
  124. float splineTension_;
  125. /// Value type.
  126. VariantType valueType_;
  127. /// Is interpolatable.
  128. bool isInterpolatable_;
  129. /// Begin time.
  130. float beginTime_;
  131. /// End time.
  132. float endTime_;
  133. /// Key frames.
  134. Vector<AttributeKeyFrame> keyFrames_;
  135. /// Spline tangents.
  136. Vector<Variant> splineTangents_;
  137. /// Spline tangents dirty.
  138. bool splineTangentsDirty_;
  139. /// Event frames.
  140. Vector<AttributeEventFrame> eventFrames_;
  141. };
  142. }