Animatable.cpp 19 KB

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