AttributeAnimation.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 ObjectAnimation;
  28. class XMLElement;
  29. /// Cycle mode.
  30. enum CycleMode
  31. {
  32. /// Loop mode.
  33. CM_LOOP = 0,
  34. /// Clamp mode.
  35. CM_CLAMP,
  36. /// Pingpong Mode.
  37. CM_PINGPONG,
  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 cycle mode.
  79. void SetCycleMode(CycleMode cycleMode);
  80. /// Set value type.
  81. void SetValueType(VariantType valueType);
  82. /// Set key frame.
  83. bool SetKeyFrame(float time, const Variant& value);
  84. /// Set event frame.
  85. void SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData = VariantMap());
  86. /// Return object animation.
  87. ObjectAnimation* GetObjectAnimation() const;
  88. /// Return cycle mode.
  89. CycleMode GetCycleMode() const { return cycleMode_; }
  90. /// Return value type.
  91. VariantType GetValueType() const { return valueType_; }
  92. /// Is interpolatable.
  93. bool IsInterpolatable() const { return isInterpolatable_; }
  94. /// Return begin time.
  95. float GetBeginTime() const { return beginTime_; }
  96. /// Return end time.
  97. float GetEndTime() const { return endTime_; }
  98. /// Calculate scaled time.
  99. float CalculateScaledTime(float currentTime) const;
  100. /// Return all key frames.
  101. const Vector<AttributeKeyFrame>& GetKeyFrames() const { return keyframes_; }
  102. /// Return all event frames between time.
  103. void GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const;
  104. protected:
  105. /// Object animation.
  106. WeakPtr<ObjectAnimation> objectAnimation_;
  107. /// Cycle mode.
  108. CycleMode cycleMode_;
  109. /// Value type.
  110. VariantType valueType_;
  111. /// Is interpolatable.
  112. bool isInterpolatable_;
  113. /// Begin time.
  114. float beginTime_;
  115. /// End time.
  116. float endTime_;
  117. /// Key frames.
  118. Vector<AttributeKeyFrame> keyframes_;
  119. /// Event frames.
  120. Vector<AttributeEventFrame> eventFrames_;
  121. };
  122. }