Animatable.cpp 12 KB

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