Animatable.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include "Precompiled.h"
  23. #include "Animatable.h"
  24. #include "AttributeAnimation.h"
  25. #include "AttributeAnimationInstance.h"
  26. #include "Context.h"
  27. #include "Log.h"
  28. #include "ObjectAnimation.h"
  29. #include "ResourceCache.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. Animatable::Animatable(Context* context) :
  34. Serializable(context),
  35. animationEnabled_(true)
  36. {
  37. }
  38. Animatable::~Animatable()
  39. {
  40. }
  41. void Animatable::RegisterObject(Context* context)
  42. {
  43. ACCESSOR_ATTRIBUTE(Animatable, VAR_RESOURCEREF, "Object Animation", GetObjectAnimationAttr, SetObjectAnimationAttr, ResourceRef, ResourceRef(ObjectAnimation::GetTypeStatic()), AM_DEFAULT);
  44. }
  45. void Animatable::SetAnimationEnabled(bool animationEnabled)
  46. {
  47. animationEnabled_ = animationEnabled;
  48. }
  49. void Animatable::SetObjectAnimation(ObjectAnimation* objectAnimation)
  50. {
  51. if (objectAnimation == objectAnimation_)
  52. return;
  53. if (objectAnimation_)
  54. OnObjectAnimationRemoved(objectAnimation_);
  55. objectAnimation_ = objectAnimation;
  56. if (objectAnimation_)
  57. OnObjectAnimationAdded(objectAnimation_);
  58. }
  59. void Animatable::SetAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation)
  60. {
  61. const AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
  62. if (attributeAnimation)
  63. {
  64. if (currentInstance && attributeAnimation == currentInstance->GetAttributeAnimation())
  65. return;
  66. // Get attribute info
  67. const AttributeInfo* attributeInfo = 0;
  68. if (currentInstance)
  69. attributeInfo = &currentInstance->GetAttributeInfo();
  70. else
  71. {
  72. const Vector<AttributeInfo>* attributes = GetAttributes();
  73. if (!attributes)
  74. {
  75. LOGERROR(GetTypeName() + " has no attributes");
  76. return;
  77. }
  78. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  79. {
  80. if (name == (*i).name_)
  81. {
  82. attributeInfo = &(*i);
  83. break;
  84. }
  85. }
  86. }
  87. if (!attributeInfo)
  88. {
  89. LOGERROR("Invalid name: " + name);
  90. return;
  91. }
  92. // Check value type is same with attribute type
  93. if (attributeAnimation->GetValueType() != attributeInfo->type_)
  94. {
  95. LOGERROR("Invalid value type");
  96. return;
  97. }
  98. // Add network attribute to set
  99. if (attributeInfo->mode_ & AM_NET)
  100. {
  101. const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
  102. for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
  103. {
  104. if (name == (*i).name_)
  105. {
  106. animatedNetworkAttributes_.Insert(&(*i));
  107. break;
  108. }
  109. }
  110. }
  111. attributeAnimationInstances_[name] = new AttributeAnimationInstance(this, *attributeInfo, attributeAnimation);
  112. if (!currentInstance)
  113. OnAttributeAnimationAdded();
  114. }
  115. else
  116. {
  117. if (!currentInstance)
  118. return;
  119. // Remove network attribute from set
  120. if (currentInstance->GetAttributeInfo().mode_ & AM_NET)
  121. {
  122. const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
  123. for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
  124. {
  125. if (name == (*i).name_)
  126. {
  127. animatedNetworkAttributes_.Erase(&(*i));
  128. break;
  129. }
  130. }
  131. }
  132. attributeAnimationInstances_.Erase(name);
  133. OnAttributeAnimationRemoved();
  134. }
  135. }
  136. const ObjectAnimation* Animatable::GetObjectAnimation() const
  137. {
  138. return objectAnimation_;
  139. }
  140. const AttributeAnimation* Animatable::GetAttributeAnimation(const String& name) const
  141. {
  142. const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
  143. return instance ? instance->GetAttributeAnimation() : 0;
  144. }
  145. void Animatable::SetObjectAnimationAttr(ResourceRef value)
  146. {
  147. if (!value.name_.Empty())
  148. {
  149. ResourceCache* cache = GetSubsystem<ResourceCache>();
  150. SetObjectAnimation(cache->GetResource<ObjectAnimation>(value.name_));
  151. }
  152. }
  153. ResourceRef Animatable::GetObjectAnimationAttr() const
  154. {
  155. if (objectAnimation_ && !objectAnimation_->GetParentAnimation())
  156. return GetResourceRef(objectAnimation_, ObjectAnimation::GetTypeStatic());
  157. return ResourceRef();
  158. }
  159. void Animatable::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
  160. {
  161. if (!objectAnimation)
  162. return;
  163. // Set all attribute animations from the object animation
  164. HashMap<String, SharedPtr<AttributeAnimation> > attributeAnimations = objectAnimation->GetAttributeAnimations();
  165. for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations.Begin(); i != attributeAnimations.End(); ++i)
  166. SetAttributeAnimation(i->first_, i->second_);
  167. }
  168. void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
  169. {
  170. if (!objectAnimation)
  171. return;
  172. // Just remove all attribute animations from the object animation
  173. Vector<String> names;
  174. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::Iterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  175. {
  176. if (i->second_->GetAttributeAnimation()->GetObjectAnimation() == objectAnimation)
  177. names.Push(i->first_);
  178. }
  179. for (unsigned int i = 0; i < names.Size(); ++i)
  180. SetAttributeAnimation(names[i], 0);
  181. }
  182. void Animatable::UpdateAttributeAnimations(float timeStep)
  183. {
  184. if (!animationEnabled_)
  185. return;
  186. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  187. i->second_->Update(timeStep);
  188. }
  189. bool Animatable::IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const
  190. {
  191. return animatedNetworkAttributes_.Find(&attrInfo) != animatedNetworkAttributes_.End();
  192. }
  193. const AttributeAnimationInstance* Animatable::GetAttributeAnimationInstance(const String& name) const
  194. {
  195. HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Find(name);
  196. if (i != attributeAnimationInstances_.End())
  197. return i->second_;
  198. return 0;
  199. }
  200. }