Animatable.h 5.6 KB

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