Animatable.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //
  2. // Copyright (c) 2008-2017 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/JSONValue.h"
  27. #include "../Resource/XMLElement.h"
  28. #include "../Scene/Animatable.h"
  29. #include "../Scene/ObjectAnimation.h"
  30. #include "../Scene/SceneEvents.h"
  31. #include "../Scene/ValueAnimation.h"
  32. #include "../DebugNew.h"
  33. namespace Atomic
  34. {
  35. extern const char* wrapModeNames[];
  36. AttributeAnimationInfo::AttributeAnimationInfo(Animatable* target, const AttributeInfo& attributeInfo,
  37. ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed) :
  38. ValueAnimationInfo(target, attributeAnimation, wrapMode, speed),
  39. attributeInfo_(attributeInfo)
  40. {
  41. }
  42. AttributeAnimationInfo::AttributeAnimationInfo(const AttributeAnimationInfo& other) :
  43. ValueAnimationInfo(other),
  44. attributeInfo_(other.attributeInfo_)
  45. {
  46. }
  47. AttributeAnimationInfo::~AttributeAnimationInfo()
  48. {
  49. }
  50. void AttributeAnimationInfo::ApplyValue(const Variant& newValue)
  51. {
  52. Animatable* animatable = static_cast<Animatable*>(target_.Get());
  53. if (animatable)
  54. {
  55. animatable->OnSetAttribute(attributeInfo_, newValue);
  56. animatable->ApplyAttributes();
  57. }
  58. }
  59. Animatable::Animatable(Context* context) :
  60. Serializable(context),
  61. animationEnabled_(true)
  62. {
  63. }
  64. Animatable::~Animatable()
  65. {
  66. }
  67. void Animatable::RegisterObject(Context* context)
  68. {
  69. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Object Animation", GetObjectAnimationAttr, SetObjectAnimationAttr, ResourceRef,
  70. ResourceRef(ObjectAnimation::GetTypeStatic()), AM_DEFAULT);
  71. }
  72. bool Animatable::LoadXML(const XMLElement& source, bool setInstanceDefault)
  73. {
  74. if (!Serializable::LoadXML(source, setInstanceDefault))
  75. return false;
  76. SetObjectAnimation(0);
  77. attributeAnimationInfos_.Clear();
  78. XMLElement elem = source.GetChild("objectanimation");
  79. if (elem)
  80. {
  81. SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
  82. if (!objectAnimation->LoadXML(elem))
  83. return false;
  84. SetObjectAnimation(objectAnimation);
  85. }
  86. elem = source.GetChild("attributeanimation");
  87. while (elem)
  88. {
  89. String name = elem.GetAttribute("name");
  90. SharedPtr<ValueAnimation> attributeAnimation(new ValueAnimation(context_));
  91. if (!attributeAnimation->LoadXML(elem))
  92. return false;
  93. String wrapModeString = source.GetAttribute("wrapmode");
  94. WrapMode wrapMode = WM_LOOP;
  95. for (int i = 0; i <= WM_CLAMP; ++i)
  96. {
  97. if (wrapModeString == wrapModeNames[i])
  98. {
  99. wrapMode = (WrapMode)i;
  100. break;
  101. }
  102. }
  103. float speed = elem.GetFloat("speed");
  104. SetAttributeAnimation(name, attributeAnimation, wrapMode, speed);
  105. elem = elem.GetNext("attributeanimation");
  106. }
  107. return true;
  108. }
  109. bool Animatable::LoadJSON(const JSONValue& source, bool setInstanceDefault)
  110. {
  111. if (!Serializable::LoadJSON(source, setInstanceDefault))
  112. return false;
  113. SetObjectAnimation(0);
  114. attributeAnimationInfos_.Clear();
  115. JSONValue value = source.Get("objectanimation");
  116. if (!value.IsNull())
  117. {
  118. SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
  119. if (!objectAnimation->LoadJSON(value))
  120. return false;
  121. SetObjectAnimation(objectAnimation);
  122. }
  123. JSONValue attributeAnimationValue = source.Get("attributeanimation");
  124. if (attributeAnimationValue.IsNull())
  125. return true;
  126. if (!attributeAnimationValue.IsObject())
  127. {
  128. ATOMIC_LOGWARNING("'attributeanimation' value is present in JSON data, but is not a JSON object; skipping it");
  129. return true;
  130. }
  131. const JSONObject& attributeAnimationObject = attributeAnimationValue.GetObject();
  132. for (JSONObject::ConstIterator it = attributeAnimationObject.Begin(); it != attributeAnimationObject.End(); it++)
  133. {
  134. String name = it->first_;
  135. JSONValue value = it->second_;
  136. SharedPtr<ValueAnimation> attributeAnimation(new ValueAnimation(context_));
  137. if (!attributeAnimation->LoadJSON(it->second_))
  138. return false;
  139. String wrapModeString = source.Get("wrapmode").GetString();
  140. WrapMode wrapMode = WM_LOOP;
  141. for (int i = 0; i <= WM_CLAMP; ++i)
  142. {
  143. if (wrapModeString == wrapModeNames[i])
  144. {
  145. wrapMode = (WrapMode)i;
  146. break;
  147. }
  148. }
  149. float speed = value.Get("speed").GetFloat();
  150. SetAttributeAnimation(name, attributeAnimation, wrapMode, speed);
  151. it++;
  152. }
  153. return true;
  154. }
  155. bool Animatable::SaveXML(XMLElement& dest) const
  156. {
  157. if (!Serializable::SaveXML(dest))
  158. return false;
  159. // Object animation without name
  160. if (objectAnimation_ && objectAnimation_->GetName().Empty())
  161. {
  162. XMLElement elem = dest.CreateChild("objectanimation");
  163. if (!objectAnimation_->SaveXML(elem))
  164. return false;
  165. }
  166. for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  167. i != attributeAnimationInfos_.End(); ++i)
  168. {
  169. ValueAnimation* attributeAnimation = i->second_->GetAnimation();
  170. if (attributeAnimation->GetOwner())
  171. continue;
  172. const AttributeInfo& attr = i->second_->GetAttributeInfo();
  173. XMLElement elem = dest.CreateChild("attributeanimation");
  174. elem.SetAttribute("name", attr.name_);
  175. if (!attributeAnimation->SaveXML(elem))
  176. return false;
  177. elem.SetAttribute("wrapmode", wrapModeNames[i->second_->GetWrapMode()]);
  178. elem.SetFloat("speed", i->second_->GetSpeed());
  179. }
  180. return true;
  181. }
  182. bool Animatable::SaveJSON(JSONValue& dest) const
  183. {
  184. if (!Serializable::SaveJSON(dest))
  185. return false;
  186. // Object animation without name
  187. if (objectAnimation_ && objectAnimation_->GetName().Empty())
  188. {
  189. JSONValue objectAnimationValue;
  190. if (!objectAnimation_->SaveJSON(objectAnimationValue))
  191. return false;
  192. dest.Set("objectanimation", objectAnimationValue);
  193. }
  194. JSONValue attributeAnimationValue;
  195. for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  196. i != attributeAnimationInfos_.End(); ++i)
  197. {
  198. ValueAnimation* attributeAnimation = i->second_->GetAnimation();
  199. if (attributeAnimation->GetOwner())
  200. continue;
  201. const AttributeInfo& attr = i->second_->GetAttributeInfo();
  202. JSONValue attributeValue;
  203. attributeValue.Set("name", attr.name_);
  204. if (!attributeAnimation->SaveJSON(attributeValue))
  205. return false;
  206. attributeValue.Set("wrapmode", wrapModeNames[i->second_->GetWrapMode()]);
  207. attributeValue.Set("speed", (float) i->second_->GetSpeed());
  208. attributeAnimationValue.Set(attr.name_, attributeValue);
  209. }
  210. return true;
  211. }
  212. void Animatable::SetAnimationEnabled(bool enable)
  213. {
  214. if (objectAnimation_)
  215. {
  216. // In object animation there may be targets in hierarchy. Set same enable/disable state in all
  217. HashSet<Animatable*> targets;
  218. const HashMap<String, SharedPtr<ValueAnimationInfo> >& infos = objectAnimation_->GetAttributeAnimationInfos();
  219. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = infos.Begin(); i != infos.End(); ++i)
  220. {
  221. String outName;
  222. Animatable* target = FindAttributeAnimationTarget(i->first_, outName);
  223. if (target && target != this)
  224. targets.Insert(target);
  225. }
  226. for (HashSet<Animatable*>::Iterator i = targets.Begin(); i != targets.End(); ++i)
  227. (*i)->animationEnabled_ = enable;
  228. }
  229. animationEnabled_ = enable;
  230. }
  231. void Animatable::SetAnimationTime(float time)
  232. {
  233. if (objectAnimation_)
  234. {
  235. // In object animation there may be targets in hierarchy. Set same time in all
  236. const HashMap<String, SharedPtr<ValueAnimationInfo> >& infos = objectAnimation_->GetAttributeAnimationInfos();
  237. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = infos.Begin(); i != infos.End(); ++i)
  238. {
  239. String outName;
  240. Animatable* target = FindAttributeAnimationTarget(i->first_, outName);
  241. if (target)
  242. target->SetAttributeAnimationTime(outName, time);
  243. }
  244. }
  245. else
  246. {
  247. for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  248. i != attributeAnimationInfos_.End(); ++i)
  249. i->second_->SetTime(time);
  250. }
  251. }
  252. void Animatable::SetObjectAnimation(ObjectAnimation* objectAnimation)
  253. {
  254. if (objectAnimation == objectAnimation_)
  255. return;
  256. if (objectAnimation_)
  257. {
  258. OnObjectAnimationRemoved(objectAnimation_);
  259. UnsubscribeFromEvent(objectAnimation_, E_ATTRIBUTEANIMATIONADDED);
  260. UnsubscribeFromEvent(objectAnimation_, E_ATTRIBUTEANIMATIONREMOVED);
  261. }
  262. objectAnimation_ = objectAnimation;
  263. if (objectAnimation_)
  264. {
  265. OnObjectAnimationAdded(objectAnimation_);
  266. SubscribeToEvent(objectAnimation_, E_ATTRIBUTEANIMATIONADDED, ATOMIC_HANDLER(Animatable, HandleAttributeAnimationAdded));
  267. SubscribeToEvent(objectAnimation_, E_ATTRIBUTEANIMATIONREMOVED, ATOMIC_HANDLER(Animatable, HandleAttributeAnimationRemoved));
  268. }
  269. }
  270. void Animatable::SetAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  271. {
  272. AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  273. if (attributeAnimation)
  274. {
  275. if (info && attributeAnimation == info->GetAnimation())
  276. {
  277. info->SetWrapMode(wrapMode);
  278. info->SetSpeed(speed);
  279. return;
  280. }
  281. // Get attribute info
  282. const AttributeInfo* attributeInfo = 0;
  283. if (info)
  284. attributeInfo = &info->GetAttributeInfo();
  285. else
  286. {
  287. const Vector<AttributeInfo>* attributes = GetAttributes();
  288. if (!attributes)
  289. {
  290. ATOMIC_LOGERROR(GetTypeName() + " has no attributes");
  291. return;
  292. }
  293. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  294. {
  295. if (name == (*i).name_)
  296. {
  297. attributeInfo = &(*i);
  298. break;
  299. }
  300. }
  301. }
  302. if (!attributeInfo)
  303. {
  304. ATOMIC_LOGERROR("Invalid name: " + name);
  305. return;
  306. }
  307. // Check value type is same with attribute type
  308. if (attributeAnimation->GetValueType() != attributeInfo->type_)
  309. {
  310. ATOMIC_LOGERROR("Invalid value type");
  311. return;
  312. }
  313. // Add network attribute to set
  314. if (attributeInfo->mode_ & AM_NET)
  315. animatedNetworkAttributes_.Insert(attributeInfo);
  316. attributeAnimationInfos_[name] = new AttributeAnimationInfo(this, *attributeInfo, attributeAnimation, wrapMode, speed);
  317. if (!info)
  318. OnAttributeAnimationAdded();
  319. }
  320. else
  321. {
  322. if (!info)
  323. return;
  324. // Remove network attribute from set
  325. if (info->GetAttributeInfo().mode_ & AM_NET)
  326. animatedNetworkAttributes_.Erase(&info->GetAttributeInfo());
  327. attributeAnimationInfos_.Erase(name);
  328. OnAttributeAnimationRemoved();
  329. }
  330. }
  331. void Animatable::SetAttributeAnimationWrapMode(const String& name, WrapMode wrapMode)
  332. {
  333. AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  334. if (info)
  335. info->SetWrapMode(wrapMode);
  336. }
  337. void Animatable::SetAttributeAnimationSpeed(const String& name, float speed)
  338. {
  339. AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  340. if (info)
  341. info->SetSpeed(speed);
  342. }
  343. void Animatable::SetAttributeAnimationTime(const String& name, float time)
  344. {
  345. AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  346. if (info)
  347. info->SetTime(time);
  348. }
  349. void Animatable::RemoveObjectAnimation()
  350. {
  351. SetObjectAnimation(0);
  352. }
  353. void Animatable::RemoveAttributeAnimation(const String& name)
  354. {
  355. SetAttributeAnimation(name, 0);
  356. }
  357. ObjectAnimation* Animatable::GetObjectAnimation() const
  358. {
  359. return objectAnimation_;
  360. }
  361. ValueAnimation* Animatable::GetAttributeAnimation(const String& name) const
  362. {
  363. const AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  364. return info ? info->GetAnimation() : 0;
  365. }
  366. WrapMode Animatable::GetAttributeAnimationWrapMode(const String& name) const
  367. {
  368. const AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  369. return info ? info->GetWrapMode() : WM_LOOP;
  370. }
  371. float Animatable::GetAttributeAnimationSpeed(const String& name) const
  372. {
  373. const AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  374. return info ? info->GetSpeed() : 1.0f;
  375. }
  376. float Animatable::GetAttributeAnimationTime(const String& name) const
  377. {
  378. const AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  379. return info ? info->GetTime() : 0.0f;
  380. }
  381. void Animatable::SetObjectAnimationAttr(const ResourceRef& value)
  382. {
  383. if (!value.name_.Empty())
  384. {
  385. ResourceCache* cache = GetSubsystem<ResourceCache>();
  386. SetObjectAnimation(cache->GetResource<ObjectAnimation>(value.name_));
  387. }
  388. }
  389. ResourceRef Animatable::GetObjectAnimationAttr() const
  390. {
  391. return GetResourceRef(objectAnimation_, ObjectAnimation::GetTypeStatic());
  392. }
  393. Animatable* Animatable::FindAttributeAnimationTarget(const String& name, String& outName)
  394. {
  395. // Base implementation only handles self
  396. outName = name;
  397. return this;
  398. }
  399. void Animatable::SetObjectAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  400. {
  401. String outName;
  402. Animatable* target = FindAttributeAnimationTarget(name, outName);
  403. if (target)
  404. target->SetAttributeAnimation(outName, attributeAnimation, wrapMode, speed);
  405. }
  406. void Animatable::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
  407. {
  408. if (!objectAnimation)
  409. return;
  410. // Set all attribute animations from the object animation
  411. const HashMap<String, SharedPtr<ValueAnimationInfo> >& attributeAnimationInfos = objectAnimation->GetAttributeAnimationInfos();
  412. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = attributeAnimationInfos.Begin();
  413. i != attributeAnimationInfos.End(); ++i)
  414. {
  415. const String& name = i->first_;
  416. ValueAnimationInfo* info = i->second_;
  417. SetObjectAttributeAnimation(name, info->GetAnimation(), info->GetWrapMode(), info->GetSpeed());
  418. }
  419. }
  420. void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
  421. {
  422. if (!objectAnimation)
  423. return;
  424. // Just remove all attribute animations listed by the object animation
  425. const HashMap<String, SharedPtr<ValueAnimationInfo> >& infos = objectAnimation->GetAttributeAnimationInfos();
  426. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = infos.Begin(); i != infos.End(); ++i)
  427. SetObjectAttributeAnimation(i->first_, 0, WM_LOOP, 1.0f);
  428. }
  429. void Animatable::UpdateAttributeAnimations(float timeStep)
  430. {
  431. if (!animationEnabled_)
  432. return;
  433. // Keep weak pointer to self to check for destruction caused by event handling
  434. WeakPtr<Animatable> self(this);
  435. Vector<String> finishedNames;
  436. for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  437. i != attributeAnimationInfos_.End(); ++i)
  438. {
  439. bool finished = i->second_->Update(timeStep);
  440. // If self deleted as a result of an event sent during animation playback, nothing more to do
  441. if (self.Expired())
  442. return;
  443. if (finished)
  444. finishedNames.Push(i->second_->GetAttributeInfo().name_);
  445. }
  446. for (unsigned i = 0; i < finishedNames.Size(); ++i)
  447. SetAttributeAnimation(finishedNames[i], 0);
  448. }
  449. bool Animatable::IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const
  450. {
  451. return animatedNetworkAttributes_.Find(&attrInfo) != animatedNetworkAttributes_.End();
  452. }
  453. AttributeAnimationInfo* Animatable::GetAttributeAnimationInfo(const String& name) const
  454. {
  455. HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Find(name);
  456. if (i != attributeAnimationInfos_.End())
  457. return i->second_;
  458. return 0;
  459. }
  460. void Animatable::HandleAttributeAnimationAdded(StringHash eventType, VariantMap& eventData)
  461. {
  462. if (!objectAnimation_)
  463. return;
  464. using namespace AttributeAnimationAdded;
  465. const String& name = eventData[P_ATTRIBUTEANIMATIONNAME].GetString();
  466. ValueAnimationInfo* info = objectAnimation_->GetAttributeAnimationInfo(name);
  467. if (!info)
  468. return;
  469. SetObjectAttributeAnimation(name, info->GetAnimation(), info->GetWrapMode(), info->GetSpeed());
  470. }
  471. void Animatable::HandleAttributeAnimationRemoved(StringHash eventType, VariantMap& eventData)
  472. {
  473. if (!objectAnimation_)
  474. return;
  475. using namespace AttributeAnimationRemoved;
  476. const String& name = eventData[P_ATTRIBUTEANIMATIONNAME].GetString();
  477. SetObjectAttributeAnimation(name, 0, WM_LOOP, 1.0f);
  478. }
  479. }