AttributeAnimation.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 InterpMethod
  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(InterpMethod method);
  80. /// Set spline tension, should be between 0.0f and 1.0f, but this is not a must.
  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 { return objectAnimation_; }
  92. /// Return interpolation method.
  93. InterpMethod 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_.Empty(); }
  106. /// Return all event frames between time.
  107. void GetEventFrames(float beginTime, float endTime, PODVector<const AttributeEventFrame*>& eventFrames) const;
  108. protected:
  109. /// Linear interpolation.
  110. Variant LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const;
  111. /// Spline interpolation.
  112. Variant SplineInterpolation(unsigned index1, unsigned index2, float scaledTime);
  113. /// Update spline tangents.
  114. void UpdateSplineTangents();
  115. /// Return (value1 - value2) * t.
  116. Variant SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const;
  117. /// Object animation.
  118. WeakPtr<ObjectAnimation> objectAnimation_;
  119. /// Interpolation method.
  120. InterpMethod interpolationMethod_;
  121. /// Spline tension.
  122. float splineTension_;
  123. /// Value type.
  124. VariantType valueType_;
  125. /// Interpolatable flag.
  126. bool interpolatable_;
  127. /// Begin time.
  128. float beginTime_;
  129. /// End time.
  130. float endTime_;
  131. /// Key frames.
  132. PODVector<AttributeKeyFrame> keyFrames_;
  133. /// Spline tangents.
  134. VariantVector splineTangents_;
  135. /// Spline tangents dirty.
  136. bool splineTangentsDirty_;
  137. /// Event frames.
  138. PODVector<AttributeEventFrame> eventFrames_;
  139. };
  140. }