Animatable.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  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();
  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. virtual void ApplyValue(const Variant& newValue);
  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 ATOMIC_API Animatable : public Serializable
  55. {
  56. ATOMIC_OBJECT(Animatable, Serializable);
  57. public:
  58. /// Construct.
  59. Animatable(Context* context);
  60. /// Destruct.
  61. virtual ~Animatable();
  62. /// Register object factory.
  63. static void RegisterObject(Context* context);
  64. /// Load from XML data. When setInstanceDefault is set to true, after setting the attribute value, store the value as instance's default value. Return true if successful.
  65. virtual bool LoadXML(const XMLElement& source, bool setInstanceDefault = false);
  66. /// Save as XML data. Return true if successful.
  67. virtual bool SaveXML(XMLElement& dest) const;
  68. /// Load from JSON data. When setInstanceDefault is set to true, after setting the attribute value, store the value as instance's default value. Return true if successful.
  69. virtual bool LoadJSON(const JSONValue& source, bool setInstanceDefault = false);
  70. /// Save as JSON data. Return true if successful.
  71. virtual bool SaveJSON(JSONValue& dest) const;
  72. /// Set automatic update of animation, default true.
  73. void SetAnimationEnabled(bool enable);
  74. /// Set time position of all attribute animations or an object animation manually. Automatic update should be disabled in this case.
  75. void SetAnimationTime(float time);
  76. /// Set object animation.
  77. void SetObjectAnimation(ObjectAnimation* objectAnimation);
  78. /// Set attribute animation.
  79. void SetAttributeAnimation
  80. (const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode = WM_LOOP, float speed = 1.0f);
  81. /// Set attribute animation wrap mode.
  82. void SetAttributeAnimationWrapMode(const String& name, WrapMode wrapMode);
  83. /// Set attribute animation speed.
  84. void SetAttributeAnimationSpeed(const String& name, float speed);
  85. /// Set attribute animation time position manually. Automatic update should be disabled in this case.
  86. void SetAttributeAnimationTime(const String& name, float time);
  87. /// Remove object animation. Same as calling SetObjectAnimation with a null pointer.
  88. void RemoveObjectAnimation();
  89. /// Remove attribute animation. Same as calling SetAttributeAnimation with a null pointer.
  90. void RemoveAttributeAnimation(const String& name);
  91. /// Return animation enabled.
  92. bool GetAnimationEnabled() const { return animationEnabled_; }
  93. /// Return object animation.
  94. ObjectAnimation* GetObjectAnimation() const;
  95. /// Return attribute animation.
  96. ValueAnimation* GetAttributeAnimation(const String& name) const;
  97. /// Return attribute animation wrap mode.
  98. WrapMode GetAttributeAnimationWrapMode(const String& name) const;
  99. /// Return attribute animation speed.
  100. float GetAttributeAnimationSpeed(const String& name) const;
  101. /// Return attribute animation time position.
  102. float GetAttributeAnimationTime(const String& name) const;
  103. /// Set object animation attribute.
  104. void SetObjectAnimationAttr(const ResourceRef& value);
  105. /// Return object animation attribute.
  106. ResourceRef GetObjectAnimationAttr() const;
  107. protected:
  108. /// Handle attribute animation added.
  109. virtual void OnAttributeAnimationAdded() = 0;
  110. /// Handle attribute animation removed.
  111. virtual void OnAttributeAnimationRemoved() = 0;
  112. /// Find target of an attribute animation from object hierarchy by name.
  113. virtual Animatable* FindAttributeAnimationTarget(const String& name, String& outName);
  114. /// Set object attribute animation internal.
  115. void SetObjectAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed);
  116. /// Handle object animation added.
  117. void OnObjectAnimationAdded(ObjectAnimation* objectAnimation);
  118. /// Handle object animation removed.
  119. void OnObjectAnimationRemoved(ObjectAnimation* objectAnimation);
  120. /// Update attribute animations.
  121. void UpdateAttributeAnimations(float timeStep);
  122. /// Is animated network attribute.
  123. bool IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const;
  124. /// Return attribute animation info.
  125. AttributeAnimationInfo* GetAttributeAnimationInfo(const String& name) const;
  126. /// Handle attribute animation added.
  127. void HandleAttributeAnimationAdded(StringHash eventType, VariantMap& eventData);
  128. /// Handle attribute animation removed.
  129. void HandleAttributeAnimationRemoved(StringHash eventType, VariantMap& eventData);
  130. /// Animation enabled.
  131. bool animationEnabled_;
  132. /// Animation.
  133. SharedPtr<ObjectAnimation> objectAnimation_;
  134. /// Animated network attribute set.
  135. HashSet<const AttributeInfo*> animatedNetworkAttributes_;
  136. /// Attribute animation infos.
  137. HashMap<String, SharedPtr<AttributeAnimationInfo> > attributeAnimationInfos_;
  138. };
  139. }