Animatable.cpp 11 KB

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