Animatable.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Copyright (c) 2008-2020 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 "../Container/HashSet.h"
  24. #include "../Scene/Serializable.h"
  25. #include "../Scene/ValueAnimationInfo.h"
  26. namespace Urho3D
  27. {
  28. class Animatable;
  29. class ValueAnimation;
  30. class AttributeAnimationInfo;
  31. class ObjectAnimation;
  32. /// Attribute animation instance.
  33. class AttributeAnimationInfo : public ValueAnimationInfo
  34. {
  35. public:
  36. /// Construct.
  37. AttributeAnimationInfo
  38. (Animatable* animatable, const AttributeInfo& attributeInfo, ValueAnimation* attributeAnimation, WrapMode wrapMode,
  39. float speed);
  40. /// Copy construct.
  41. AttributeAnimationInfo(const AttributeAnimationInfo& other);
  42. /// Destruct.
  43. ~AttributeAnimationInfo() override;
  44. /// Return attribute information.
  45. const AttributeInfo& GetAttributeInfo() const { return attributeInfo_; }
  46. protected:
  47. /// Apply new animation value to the target object. Called by Update().
  48. void ApplyValue(const Variant& newValue) override;
  49. private:
  50. /// Attribute information.
  51. const AttributeInfo& attributeInfo_;
  52. };
  53. /// Base class for animatable object, an animatable object can be set animation on it's attributes, or can be set an object animation to it.
  54. class URHO3D_API Animatable : public Serializable
  55. {
  56. URHO3D_OBJECT(Animatable, Serializable);
  57. public:
  58. /// Construct.
  59. explicit Animatable(Context* context);
  60. /// Destruct.
  61. ~Animatable() override;
  62. /// Register object factory.
  63. static void RegisterObject(Context* context);
  64. /// Load from XML data. Return true if successful.
  65. bool LoadXML(const XMLElement& source) override;
  66. /// Save as XML data. Return true if successful.
  67. bool SaveXML(XMLElement& dest) const override;
  68. /// Load from JSON data. Return true if successful.
  69. bool LoadJSON(const JSONValue& source) override;
  70. /// Save as JSON data. Return true if successful.
  71. bool SaveJSON(JSONValue& dest) const override;
  72. /// Set automatic update of animation, default true.
  73. /// @property
  74. void SetAnimationEnabled(bool enable);
  75. /// Set time position of all attribute animations or an object animation manually. Automatic update should be disabled in this case.
  76. void SetAnimationTime(float time);
  77. /// Set object animation.
  78. /// @property
  79. void SetObjectAnimation(ObjectAnimation* objectAnimation);
  80. /// Set attribute animation.
  81. void SetAttributeAnimation
  82. (const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode = WM_LOOP, float speed = 1.0f);
  83. /// Set attribute animation wrap mode.
  84. void SetAttributeAnimationWrapMode(const String& name, WrapMode wrapMode);
  85. /// Set attribute animation speed.
  86. void SetAttributeAnimationSpeed(const String& name, float speed);
  87. /// Set attribute animation time position manually. Automatic update should be disabled in this case.
  88. void SetAttributeAnimationTime(const String& name, float time);
  89. /// Remove object animation. Same as calling SetObjectAnimation with a null pointer.
  90. void RemoveObjectAnimation();
  91. /// Remove attribute animation. Same as calling SetAttributeAnimation with a null pointer.
  92. void RemoveAttributeAnimation(const String& name);
  93. /// Return animation enabled.
  94. /// @property
  95. bool GetAnimationEnabled() const { return animationEnabled_; }
  96. /// Return object animation.
  97. /// @property
  98. ObjectAnimation* GetObjectAnimation() const;
  99. /// Return attribute animation.
  100. ValueAnimation* GetAttributeAnimation(const String& name) const;
  101. /// Return attribute animation wrap mode.
  102. WrapMode GetAttributeAnimationWrapMode(const String& name) const;
  103. /// Return attribute animation speed.
  104. float GetAttributeAnimationSpeed(const String& name) const;
  105. /// Return attribute animation time position.
  106. float GetAttributeAnimationTime(const String& name) const;
  107. /// Set object animation attribute.
  108. void SetObjectAnimationAttr(const ResourceRef& value);
  109. /// Return object animation attribute.
  110. ResourceRef GetObjectAnimationAttr() const;
  111. protected:
  112. /// Handle attribute animation added.
  113. virtual void OnAttributeAnimationAdded() = 0;
  114. /// Handle attribute animation removed.
  115. virtual void OnAttributeAnimationRemoved() = 0;
  116. /// Find target of an attribute animation from object hierarchy by name.
  117. virtual Animatable* FindAttributeAnimationTarget(const String& name, String& outName);
  118. /// Set object attribute animation internal.
  119. void SetObjectAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed);
  120. /// Handle object animation added.
  121. void OnObjectAnimationAdded(ObjectAnimation* objectAnimation);
  122. /// Handle object animation removed.
  123. void OnObjectAnimationRemoved(ObjectAnimation* objectAnimation);
  124. /// Update attribute animations.
  125. void UpdateAttributeAnimations(float timeStep);
  126. /// Is animated network attribute.
  127. bool IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const;
  128. /// Return attribute animation info.
  129. AttributeAnimationInfo* GetAttributeAnimationInfo(const String& name) const;
  130. /// Handle attribute animation added.
  131. void HandleAttributeAnimationAdded(StringHash eventType, VariantMap& eventData);
  132. /// Handle attribute animation removed.
  133. void HandleAttributeAnimationRemoved(StringHash eventType, VariantMap& eventData);
  134. /// Animation enabled.
  135. bool animationEnabled_;
  136. /// Animation.
  137. SharedPtr<ObjectAnimation> objectAnimation_;
  138. /// Animated network attribute set.
  139. HashSet<const AttributeInfo*> animatedNetworkAttributes_;
  140. /// Attribute animation infos.
  141. HashMap<String, SharedPtr<AttributeAnimationInfo> > attributeAnimationInfos_;
  142. };
  143. }