Animatable.cpp 13 KB

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