Animatable.cpp 12 KB

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