Animatable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "AttributeAnimationInstance.h"
  25. #include "Context.h"
  26. #include "Log.h"
  27. #include "ObjectAnimation.h"
  28. #include "ResourceCache.h"
  29. #include "ValueAnimation.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<ValueAnimation> attributeAnimation(new ValueAnimation(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. ValueAnimation* attributeAnimation = i->second_->GetAnimation();
  98. if (attributeAnimation->GetOwner())
  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::SetObjectAnimation(ObjectAnimation* objectAnimation)
  111. {
  112. if (objectAnimation == objectAnimation_)
  113. return;
  114. if (objectAnimation_)
  115. OnObjectAnimationRemoved(objectAnimation_);
  116. objectAnimation_ = objectAnimation;
  117. if (objectAnimation_)
  118. OnObjectAnimationAdded(objectAnimation_);
  119. }
  120. void Animatable::SetAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  121. {
  122. AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
  123. if (attributeAnimation)
  124. {
  125. if (currentInstance && attributeAnimation == currentInstance->GetAnimation())
  126. {
  127. currentInstance->SetWrapMode(wrapMode);
  128. currentInstance->SetSpeed(speed);
  129. return;
  130. }
  131. // Get attribute info
  132. const AttributeInfo* attributeInfo = 0;
  133. if (currentInstance)
  134. attributeInfo = &currentInstance->GetAttributeInfo();
  135. else
  136. {
  137. const Vector<AttributeInfo>* attributes = GetAttributes();
  138. if (!attributes)
  139. {
  140. LOGERROR(GetTypeName() + " has no attributes");
  141. return;
  142. }
  143. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  144. {
  145. if (name == (*i).name_)
  146. {
  147. attributeInfo = &(*i);
  148. break;
  149. }
  150. }
  151. }
  152. if (!attributeInfo)
  153. {
  154. LOGERROR("Invalid name: " + name);
  155. return;
  156. }
  157. // Check value type is same with attribute type
  158. if (attributeAnimation->GetValueType() != attributeInfo->type_)
  159. {
  160. LOGERROR("Invalid value type");
  161. return;
  162. }
  163. // Add network attribute to set
  164. if (attributeInfo->mode_ & AM_NET)
  165. animatedNetworkAttributes_.Insert(attributeInfo);
  166. attributeAnimationInstances_[name] = new AttributeAnimationInstance(this, *attributeInfo, attributeAnimation, wrapMode, speed);
  167. if (!currentInstance)
  168. OnAttributeAnimationAdded();
  169. }
  170. else
  171. {
  172. if (!currentInstance)
  173. return;
  174. // Remove network attribute from set
  175. if (currentInstance->GetAttributeInfo().mode_ & AM_NET)
  176. animatedNetworkAttributes_.Erase(&currentInstance->GetAttributeInfo());
  177. attributeAnimationInstances_.Erase(name);
  178. OnAttributeAnimationRemoved();
  179. }
  180. }
  181. void Animatable::SetAttributeAnimationWrapMode(const String& name, WrapMode wrapMode)
  182. {
  183. AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
  184. if (currentInstance)
  185. currentInstance->SetWrapMode(wrapMode);
  186. }
  187. void Animatable::SetAttributeAnimationSpeed(const String& name, float speed)
  188. {
  189. AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
  190. if (currentInstance)
  191. currentInstance->SetSpeed(speed);
  192. }
  193. ObjectAnimation* Animatable::GetObjectAnimation() const
  194. {
  195. return objectAnimation_;
  196. }
  197. ValueAnimation* Animatable::GetAttributeAnimation(const String& name) const
  198. {
  199. const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
  200. return instance ? instance->GetAnimation() : 0;
  201. }
  202. WrapMode Animatable::GetAttributeAnimationWrapMode(const String& name) const
  203. {
  204. const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
  205. return instance ? instance->GetWrapMode() : WM_LOOP;
  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. const HashMap<String, SharedPtr<ValueAnimationInfo> >& attributeAnimationInfos = objectAnimation->GetAttributeAnimationInfos();
  230. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = attributeAnimationInfos.Begin(); i != attributeAnimationInfos.End(); ++i)
  231. {
  232. const String& name = i->first_;
  233. ValueAnimationInfo* info = i->second_;
  234. SetAttributeAnimation(name, info->GetAnimation(), info->GetWrapMode(), info->GetSpeed());
  235. }
  236. }
  237. void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
  238. {
  239. if (!objectAnimation)
  240. return;
  241. // Just remove all attribute animations from the object animation
  242. Vector<String> names;
  243. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::Iterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  244. {
  245. if (i->second_->GetAnimation()->GetOwner() == objectAnimation)
  246. names.Push(i->first_);
  247. }
  248. for (unsigned int i = 0; i < names.Size(); ++i)
  249. SetAttributeAnimation(names[i], 0);
  250. }
  251. void Animatable::UpdateAttributeAnimations(float timeStep)
  252. {
  253. if (!animationEnabled_)
  254. return;
  255. Vector<String> finishedNames;
  256. for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
  257. {
  258. if (i->second_->Update(timeStep))
  259. finishedNames.Push(i->second_->GetAttributeInfo().name_);
  260. }
  261. for (unsigned i = 0; i < finishedNames.Size(); ++i)
  262. SetAttributeAnimation(finishedNames[i], 0);
  263. }
  264. bool Animatable::IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const
  265. {
  266. return animatedNetworkAttributes_.Find(&attrInfo) != animatedNetworkAttributes_.End();
  267. }
  268. AttributeAnimationInstance* Animatable::GetAttributeAnimationInstance(const String& name) const
  269. {
  270. HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Find(name);
  271. if (i != attributeAnimationInstances_.End())
  272. return i->second_;
  273. return 0;
  274. }
  275. }