Animatable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. float speed = elem.GetFloat("speed");
  68. SetAttributeAnimation(name, attributeAnimation, speed);
  69. elem = elem.GetNext("attributeAnimation");
  70. }
  71. return true;
  72. }
  73. bool Animatable::SaveXML(XMLElement& dest) const
  74. {
  75. if (!Serializable::SaveXML(dest))
  76. return false;
  77. // Object animation without name
  78. if (objectAnimation_ && objectAnimation_->GetName().Empty())
  79. {
  80. XMLElement elem = dest.CreateChild("objectAnimation");
  81. if (!objectAnimation_->SaveXML(elem))
  82. return false;
  83. }
  84. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  85. {
  86. AttributeAnimation* attributeAnimation = i->second_->GetAttributeAnimation();
  87. if (attributeAnimation->GetObjectAnimation())
  88. continue;
  89. const AttributeInfo& attr = i->second_->GetAttributeInfo();
  90. XMLElement elem = dest.CreateChild("attributeAnimation");
  91. elem.SetAttribute("name", attr.name_);
  92. if (!attributeAnimation->SaveXML(elem))
  93. return false;
  94. elem.SetFloat("speed", i->second_->GetSpeed());
  95. }
  96. return true;
  97. }
  98. void Animatable::SetAnimationEnabled(bool animationEnabled)
  99. {
  100. animationEnabled_ = animationEnabled;
  101. }
  102. void Animatable::SetObjectAnimation(ObjectAnimation* objectAnimation)
  103. {
  104. if (objectAnimation == objectAnimation_)
  105. return;
  106. if (objectAnimation_)
  107. OnObjectAnimationRemoved(objectAnimation_);
  108. objectAnimation_ = objectAnimation;
  109. if (objectAnimation_)
  110. OnObjectAnimationAdded(objectAnimation_);
  111. }
  112. void Animatable::SetAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation, float speed)
  113. {
  114. AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
  115. if (attributeAnimation)
  116. {
  117. if (currentInstance && attributeAnimation == currentInstance->GetAttributeAnimation())
  118. {
  119. currentInstance->SetSpeed(speed);
  120. return;
  121. }
  122. // Get attribute info
  123. const AttributeInfo* attributeInfo = 0;
  124. if (currentInstance)
  125. attributeInfo = &currentInstance->GetAttributeInfo();
  126. else
  127. {
  128. const Vector<AttributeInfo>* attributes = GetAttributes();
  129. if (!attributes)
  130. {
  131. LOGERROR(GetTypeName() + " has no attributes");
  132. return;
  133. }
  134. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  135. {
  136. if (name == (*i).name_)
  137. {
  138. attributeInfo = &(*i);
  139. break;
  140. }
  141. }
  142. }
  143. if (!attributeInfo)
  144. {
  145. LOGERROR("Invalid name: " + name);
  146. return;
  147. }
  148. // Check value type is same with attribute type
  149. if (attributeAnimation->GetValueType() != attributeInfo->type_)
  150. {
  151. LOGERROR("Invalid value type");
  152. return;
  153. }
  154. // Add network attribute to set
  155. if (attributeInfo->mode_ & AM_NET)
  156. {
  157. const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
  158. for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
  159. {
  160. if (name == (*i).name_)
  161. {
  162. animatedNetworkAttributes_.Insert(&(*i));
  163. break;
  164. }
  165. }
  166. }
  167. attributeAnimationInstances_[name] = new AttributeAnimationInstance(this, *attributeInfo, attributeAnimation, speed);
  168. if (!currentInstance)
  169. OnAttributeAnimationAdded();
  170. }
  171. else
  172. {
  173. if (!currentInstance)
  174. return;
  175. // Remove network attribute from set
  176. if (currentInstance->GetAttributeInfo().mode_ & AM_NET)
  177. {
  178. const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
  179. for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
  180. {
  181. if (name == (*i).name_)
  182. {
  183. animatedNetworkAttributes_.Erase(&(*i));
  184. break;
  185. }
  186. }
  187. }
  188. attributeAnimationInstances_.Erase(name);
  189. OnAttributeAnimationRemoved();
  190. }
  191. }
  192. void Animatable::SetAttributeAnimationSpeed(const String& name, float speed)
  193. {
  194. AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
  195. if (currentInstance)
  196. currentInstance->SetSpeed(speed);
  197. }
  198. ObjectAnimation* Animatable::GetObjectAnimation() const
  199. {
  200. return objectAnimation_;
  201. }
  202. AttributeAnimation* Animatable::GetAttributeAnimation(const String& name) const
  203. {
  204. const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
  205. return instance ? instance->GetAttributeAnimation() : 0;
  206. }
  207. float Animatable::GetAttributeAnimationSpeed(const String& name) const
  208. {
  209. const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
  210. return instance ? instance->GetSpeed() : 1.0f;
  211. }
  212. void Animatable::SetObjectAnimationAttr(ResourceRef value)
  213. {
  214. if (!value.name_.Empty())
  215. {
  216. ResourceCache* cache = GetSubsystem<ResourceCache>();
  217. SetObjectAnimation(cache->GetResource<ObjectAnimation>(value.name_));
  218. }
  219. }
  220. ResourceRef Animatable::GetObjectAnimationAttr() const
  221. {
  222. return GetResourceRef(objectAnimation_, ObjectAnimation::GetTypeStatic());
  223. }
  224. void Animatable::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
  225. {
  226. if (!objectAnimation)
  227. return;
  228. // Set all attribute animations from the object animation
  229. HashMap<String, SharedPtr<AttributeAnimation> > attributeAnimations = objectAnimation->GetAttributeAnimations();
  230. for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations.Begin(); i != attributeAnimations.End(); ++i)
  231. SetAttributeAnimation(i->first_, i->second_, objectAnimation->GetAttributeAnimationSpeed(i->first_));
  232. }
  233. void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
  234. {
  235. if (!objectAnimation)
  236. return;
  237. // Just remove all attribute animations from the object animation
  238. Vector<String> names;
  239. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::Iterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  240. {
  241. if (i->second_->GetAttributeAnimation()->GetObjectAnimation() == objectAnimation)
  242. names.Push(i->first_);
  243. }
  244. for (unsigned int i = 0; i < names.Size(); ++i)
  245. SetAttributeAnimation(names[i], 0);
  246. }
  247. void Animatable::UpdateAttributeAnimations(float timeStep)
  248. {
  249. if (!animationEnabled_)
  250. return;
  251. Vector<String> finishedNames;
  252. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  253. {
  254. if (i->second_->Update(timeStep))
  255. finishedNames.Push(i->second_->GetAttributeInfo().name_);
  256. }
  257. for (unsigned i = 0; i < finishedNames.Size(); ++i)
  258. SetAttributeAnimation(finishedNames[i], 0);
  259. }
  260. bool Animatable::IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const
  261. {
  262. return animatedNetworkAttributes_.Find(&attrInfo) != animatedNetworkAttributes_.End();
  263. }
  264. AttributeAnimationInstance* Animatable::GetAttributeAnimationInstance(const String& name) const
  265. {
  266. HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Find(name);
  267. if (i != attributeAnimationInstances_.End())
  268. return i->second_;
  269. return 0;
  270. }
  271. }