Animatable.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "XMLElement.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. Animatable::Animatable(Context* context) :
  35. Serializable(context),
  36. animationEnabled_(true)
  37. {
  38. }
  39. Animatable::~Animatable()
  40. {
  41. }
  42. void Animatable::RegisterObject(Context* context)
  43. {
  44. ACCESSOR_ATTRIBUTE(Animatable, VAR_RESOURCEREF, "Object Animation", GetObjectAnimationAttr, SetObjectAnimationAttr, ResourceRef, ResourceRef(ObjectAnimation::GetTypeStatic()), AM_DEFAULT);
  45. }
  46. bool Animatable::LoadXML(const XMLElement& source, bool setInstanceDefault)
  47. {
  48. if (!Serializable::LoadXML(source, setInstanceDefault))
  49. return false;
  50. SetObjectAnimation(0);
  51. attributeAnimationInstances_.Clear();
  52. XMLElement elem = source.GetChild("objectAnimation");
  53. if (elem)
  54. {
  55. SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
  56. if (!objectAnimation->LoadXML(elem))
  57. return false;
  58. SetObjectAnimation(objectAnimation);
  59. }
  60. elem = source.GetChild("attributeAnimation");
  61. while (elem)
  62. {
  63. String name = elem.GetAttribute("name");
  64. SharedPtr<AttributeAnimation> attributeAnimation(new AttributeAnimation(context_));
  65. if (!attributeAnimation->LoadXML(elem))
  66. return false;
  67. SetAttributeAnimation(name, attributeAnimation);
  68. elem = elem.GetNext("attributeAnimation");
  69. }
  70. return true;
  71. }
  72. bool Animatable::SaveXML(XMLElement& dest) const
  73. {
  74. if (!Serializable::SaveXML(dest))
  75. return false;
  76. // Object animation without name
  77. if (objectAnimation_ && objectAnimation_->GetName().Empty())
  78. {
  79. XMLElement elem = dest.CreateChild("objectAnimation");
  80. if (!objectAnimation_->SaveXML(elem))
  81. return false;
  82. }
  83. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  84. {
  85. AttributeAnimation* attributeAnimation = i->second_->GetAttributeAnimation();
  86. if (attributeAnimation->GetObjectAnimation())
  87. continue;
  88. const AttributeInfo& attr = i->second_->GetAttributeInfo();
  89. XMLElement elem = dest.CreateChild("attributeAnimation");
  90. elem.SetAttribute("name", attr.name_);
  91. if (!attributeAnimation->SaveXML(elem))
  92. return false;
  93. }
  94. return true;
  95. }
  96. void Animatable::SetAnimationEnabled(bool animationEnabled)
  97. {
  98. animationEnabled_ = animationEnabled;
  99. }
  100. void Animatable::SetObjectAnimation(ObjectAnimation* objectAnimation)
  101. {
  102. if (objectAnimation == objectAnimation_)
  103. return;
  104. if (objectAnimation_)
  105. OnObjectAnimationRemoved(objectAnimation_);
  106. objectAnimation_ = objectAnimation;
  107. if (objectAnimation_)
  108. OnObjectAnimationAdded(objectAnimation_);
  109. }
  110. void Animatable::SetAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation)
  111. {
  112. const AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
  113. if (attributeAnimation)
  114. {
  115. if (currentInstance && attributeAnimation == currentInstance->GetAttributeAnimation())
  116. return;
  117. // Get attribute info
  118. const AttributeInfo* attributeInfo = 0;
  119. if (currentInstance)
  120. attributeInfo = &currentInstance->GetAttributeInfo();
  121. else
  122. {
  123. const Vector<AttributeInfo>* attributes = GetAttributes();
  124. if (!attributes)
  125. {
  126. LOGERROR(GetTypeName() + " has no attributes");
  127. return;
  128. }
  129. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  130. {
  131. if (name == (*i).name_)
  132. {
  133. attributeInfo = &(*i);
  134. break;
  135. }
  136. }
  137. }
  138. if (!attributeInfo)
  139. {
  140. LOGERROR("Invalid name: " + name);
  141. return;
  142. }
  143. // Check value type is same with attribute type
  144. if (attributeAnimation->GetValueType() != attributeInfo->type_)
  145. {
  146. LOGERROR("Invalid value type");
  147. return;
  148. }
  149. // Add network attribute to set
  150. if (attributeInfo->mode_ & AM_NET)
  151. {
  152. const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
  153. for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
  154. {
  155. if (name == (*i).name_)
  156. {
  157. animatedNetworkAttributes_.Insert(&(*i));
  158. break;
  159. }
  160. }
  161. }
  162. attributeAnimationInstances_[name] = new AttributeAnimationInstance(this, *attributeInfo, attributeAnimation);
  163. if (!currentInstance)
  164. OnAttributeAnimationAdded();
  165. }
  166. else
  167. {
  168. if (!currentInstance)
  169. return;
  170. // Remove network attribute from set
  171. if (currentInstance->GetAttributeInfo().mode_ & AM_NET)
  172. {
  173. const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
  174. for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
  175. {
  176. if (name == (*i).name_)
  177. {
  178. animatedNetworkAttributes_.Erase(&(*i));
  179. break;
  180. }
  181. }
  182. }
  183. attributeAnimationInstances_.Erase(name);
  184. OnAttributeAnimationRemoved();
  185. }
  186. }
  187. const ObjectAnimation* Animatable::GetObjectAnimation() const
  188. {
  189. return objectAnimation_;
  190. }
  191. const AttributeAnimation* Animatable::GetAttributeAnimation(const String& name) const
  192. {
  193. const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
  194. return instance ? instance->GetAttributeAnimation() : 0;
  195. }
  196. void Animatable::SetObjectAnimationAttr(ResourceRef value)
  197. {
  198. if (!value.name_.Empty())
  199. {
  200. ResourceCache* cache = GetSubsystem<ResourceCache>();
  201. SetObjectAnimation(cache->GetResource<ObjectAnimation>(value.name_));
  202. }
  203. }
  204. ResourceRef Animatable::GetObjectAnimationAttr() const
  205. {
  206. return GetResourceRef(objectAnimation_, ObjectAnimation::GetTypeStatic());
  207. }
  208. void Animatable::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
  209. {
  210. if (!objectAnimation)
  211. return;
  212. // Set all attribute animations from the object animation
  213. HashMap<String, SharedPtr<AttributeAnimation> > attributeAnimations = objectAnimation->GetAttributeAnimations();
  214. for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations.Begin(); i != attributeAnimations.End(); ++i)
  215. SetAttributeAnimation(i->first_, i->second_);
  216. }
  217. void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
  218. {
  219. if (!objectAnimation)
  220. return;
  221. // Just remove all attribute animations from the object animation
  222. Vector<String> names;
  223. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::Iterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  224. {
  225. if (i->second_->GetAttributeAnimation()->GetObjectAnimation() == objectAnimation)
  226. names.Push(i->first_);
  227. }
  228. for (unsigned int i = 0; i < names.Size(); ++i)
  229. SetAttributeAnimation(names[i], 0);
  230. }
  231. void Animatable::UpdateAttributeAnimations(float timeStep)
  232. {
  233. if (!animationEnabled_)
  234. return;
  235. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  236. i->second_->Update(timeStep);
  237. }
  238. bool Animatable::IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const
  239. {
  240. return animatedNetworkAttributes_.Find(&attrInfo) != animatedNetworkAttributes_.End();
  241. }
  242. const AttributeAnimationInstance* Animatable::GetAttributeAnimationInstance(const String& name) const
  243. {
  244. HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Find(name);
  245. if (i != attributeAnimationInstances_.End())
  246. return i->second_;
  247. return 0;
  248. }
  249. }