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