AttributeAnimation.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /// Attribute key frame
  32. struct AttributeKeyFrame
  33. {
  34. /// Time.
  35. float time_;
  36. /// Value.
  37. Variant value_;
  38. };
  39. /// Attribute event frame.
  40. struct AttributeEventFrame
  41. {
  42. /// Time.
  43. float time_;
  44. /// Event type.
  45. StringHash eventType_;
  46. /// Event data.
  47. VariantMap eventData_;
  48. };
  49. /// Base class for attribute animation.
  50. class URHO3D_API AttributeAnimation : public Resource
  51. {
  52. OBJECT(AttributeAnimation);
  53. public:
  54. /// Construct.
  55. AttributeAnimation(Context* context);
  56. /// Destruct.
  57. virtual ~AttributeAnimation();
  58. /// Register object factory.
  59. static void RegisterObject(Context* context);
  60. /// Load resource. Return true if successful.
  61. virtual bool Load(Deserializer& source);
  62. /// Save resource. Return true if successful.
  63. virtual bool Save(Serializer& dest) const;
  64. /// Load from XML data. Return true if successful.
  65. bool LoadXML(const XMLElement& source);
  66. /// Save as XML data. Return true if successful.
  67. bool SaveXML(XMLElement& dest) const;
  68. /// Set object animation.
  69. void SetObjectAnimation(ObjectAnimation* objectAnimation);
  70. /// Set value type.
  71. void SetValueType(VariantType valueType);
  72. /// Set key frame.
  73. bool SetKeyFrame(float time, const Variant& value);
  74. /// Set event frame.
  75. void SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData = VariantMap());
  76. /// Return animation is valid.
  77. bool IsValid() const { return keyFrames_.Size() > 1; }
  78. /// Return object animation.
  79. ObjectAnimation* GetObjectAnimation() const;
  80. /// Return value type.
  81. VariantType GetValueType() const { return valueType_; }
  82. /// Return begin time.
  83. float GetBeginTime() const { return beginTime_; }
  84. /// Return end time.
  85. float GetEndTime() const { return endTime_; }
  86. /// Update object's attribute value.
  87. void UpdateAttributeValue(Animatable* animatable, const AttributeInfo& attributeInfo, float scaledTime) const;
  88. /// Has event frames.
  89. bool HasEventFrames() const { return eventFrames_.Size() != 0; }
  90. /// Return all event frames between time.
  91. void GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const;
  92. protected:
  93. /// Linear interpolation.
  94. Variant LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const;
  95. /// Object animation.
  96. WeakPtr<ObjectAnimation> objectAnimation_;
  97. /// Value type.
  98. VariantType valueType_;
  99. /// Is interpolatable.
  100. bool isInterpolatable_;
  101. /// Begin time.
  102. float beginTime_;
  103. /// End time.
  104. float endTime_;
  105. /// Key frames.
  106. Vector<AttributeKeyFrame> keyFrames_;
  107. /// Event frames.
  108. Vector<AttributeEventFrame> eventFrames_;
  109. };
  110. }