Animatable.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 Urho3D
  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. URHO3D_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::SetAnimationEnabled(bool enable)
  136. {
  137. if (objectAnimation_)
  138. {
  139. // In object animation there may be targets in hierarchy. Set same enable/disable state in all
  140. HashSet<Animatable*> targets;
  141. const HashMap<String, SharedPtr<ValueAnimationInfo> >& infos = objectAnimation_->GetAttributeAnimationInfos();
  142. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = infos.Begin(); i != infos.End(); ++i)
  143. {
  144. String outName;
  145. Animatable* target = FindAttributeAnimationTarget(i->first_, outName);
  146. if (target && target != this)
  147. targets.Insert(target);
  148. }
  149. for (HashSet<Animatable*>::Iterator i = targets.Begin(); i != targets.End(); ++i)
  150. (*i)->animationEnabled_ = enable;
  151. }
  152. animationEnabled_ = enable;
  153. }
  154. void Animatable::SetAnimationTime(float time)
  155. {
  156. if (objectAnimation_)
  157. {
  158. // In object animation there may be targets in hierarchy. Set same time in all
  159. const HashMap<String, SharedPtr<ValueAnimationInfo> >& infos = objectAnimation_->GetAttributeAnimationInfos();
  160. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = infos.Begin(); i != infos.End(); ++i)
  161. {
  162. String outName;
  163. Animatable* target = FindAttributeAnimationTarget(i->first_, outName);
  164. if (target)
  165. target->SetAttributeAnimationTime(outName, time);
  166. }
  167. }
  168. else
  169. {
  170. for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  171. i != attributeAnimationInfos_.End(); ++i)
  172. i->second_->SetTime(time);
  173. }
  174. }
  175. void Animatable::SetObjectAnimation(ObjectAnimation* objectAnimation)
  176. {
  177. if (objectAnimation == objectAnimation_)
  178. return;
  179. if (objectAnimation_)
  180. {
  181. OnObjectAnimationRemoved(objectAnimation_);
  182. UnsubscribeFromEvent(objectAnimation_, E_ATTRIBUTEANIMATIONADDED);
  183. UnsubscribeFromEvent(objectAnimation_, E_ATTRIBUTEANIMATIONREMOVED);
  184. }
  185. objectAnimation_ = objectAnimation;
  186. if (objectAnimation_)
  187. {
  188. OnObjectAnimationAdded(objectAnimation_);
  189. SubscribeToEvent(objectAnimation_, E_ATTRIBUTEANIMATIONADDED, URHO3D_HANDLER(Animatable, HandleAttributeAnimationAdded));
  190. SubscribeToEvent(objectAnimation_, E_ATTRIBUTEANIMATIONREMOVED, URHO3D_HANDLER(Animatable, HandleAttributeAnimationRemoved));
  191. }
  192. }
  193. void Animatable::SetAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  194. {
  195. AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  196. if (attributeAnimation)
  197. {
  198. if (info && attributeAnimation == info->GetAnimation())
  199. {
  200. info->SetWrapMode(wrapMode);
  201. info->SetSpeed(speed);
  202. return;
  203. }
  204. // Get attribute info
  205. const AttributeInfo* attributeInfo = 0;
  206. if (info)
  207. attributeInfo = &info->GetAttributeInfo();
  208. else
  209. {
  210. const Vector<AttributeInfo>* attributes = GetAttributes();
  211. if (!attributes)
  212. {
  213. URHO3D_LOGERROR(GetTypeName() + " has no attributes");
  214. return;
  215. }
  216. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  217. {
  218. if (name == (*i).name_)
  219. {
  220. attributeInfo = &(*i);
  221. break;
  222. }
  223. }
  224. }
  225. if (!attributeInfo)
  226. {
  227. URHO3D_LOGERROR("Invalid name: " + name);
  228. return;
  229. }
  230. // Check value type is same with attribute type
  231. if (attributeAnimation->GetValueType() != attributeInfo->type_)
  232. {
  233. URHO3D_LOGERROR("Invalid value type");
  234. return;
  235. }
  236. // Add network attribute to set
  237. if (attributeInfo->mode_ & AM_NET)
  238. animatedNetworkAttributes_.Insert(attributeInfo);
  239. attributeAnimationInfos_[name] = new AttributeAnimationInfo(this, *attributeInfo, attributeAnimation, wrapMode, speed);
  240. if (!info)
  241. OnAttributeAnimationAdded();
  242. }
  243. else
  244. {
  245. if (!info)
  246. return;
  247. // Remove network attribute from set
  248. if (info->GetAttributeInfo().mode_ & AM_NET)
  249. animatedNetworkAttributes_.Erase(&info->GetAttributeInfo());
  250. attributeAnimationInfos_.Erase(name);
  251. OnAttributeAnimationRemoved();
  252. }
  253. }
  254. void Animatable::SetAttributeAnimationWrapMode(const String& name, WrapMode wrapMode)
  255. {
  256. AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  257. if (info)
  258. info->SetWrapMode(wrapMode);
  259. }
  260. void Animatable::SetAttributeAnimationSpeed(const String& name, float speed)
  261. {
  262. AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  263. if (info)
  264. info->SetSpeed(speed);
  265. }
  266. void Animatable::SetAttributeAnimationTime(const String& name, float time)
  267. {
  268. AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  269. if (info)
  270. info->SetTime(time);
  271. }
  272. void Animatable::RemoveObjectAnimation()
  273. {
  274. SetObjectAnimation(0);
  275. }
  276. void Animatable::RemoveAttributeAnimation(const String& name)
  277. {
  278. SetAttributeAnimation(name, 0);
  279. }
  280. ObjectAnimation* Animatable::GetObjectAnimation() const
  281. {
  282. return objectAnimation_;
  283. }
  284. ValueAnimation* Animatable::GetAttributeAnimation(const String& name) const
  285. {
  286. const AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  287. return info ? info->GetAnimation() : 0;
  288. }
  289. WrapMode Animatable::GetAttributeAnimationWrapMode(const String& name) const
  290. {
  291. const AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  292. return info ? info->GetWrapMode() : WM_LOOP;
  293. }
  294. float Animatable::GetAttributeAnimationSpeed(const String& name) const
  295. {
  296. const AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  297. return info ? info->GetSpeed() : 1.0f;
  298. }
  299. float Animatable::GetAttributeAnimationTime(const String& name) const
  300. {
  301. const AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
  302. return info ? info->GetTime() : 0.0f;
  303. }
  304. void Animatable::SetObjectAnimationAttr(const ResourceRef& value)
  305. {
  306. if (!value.name_.Empty())
  307. {
  308. ResourceCache* cache = GetSubsystem<ResourceCache>();
  309. SetObjectAnimation(cache->GetResource<ObjectAnimation>(value.name_));
  310. }
  311. }
  312. ResourceRef Animatable::GetObjectAnimationAttr() const
  313. {
  314. return GetResourceRef(objectAnimation_, ObjectAnimation::GetTypeStatic());
  315. }
  316. Animatable* Animatable::FindAttributeAnimationTarget(const String& name, String& outName)
  317. {
  318. // Base implementation only handles self
  319. outName = name;
  320. return this;
  321. }
  322. void Animatable::SetObjectAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  323. {
  324. String outName;
  325. Animatable* target = FindAttributeAnimationTarget(name, outName);
  326. if (target)
  327. target->SetAttributeAnimation(outName, attributeAnimation, wrapMode, speed);
  328. }
  329. void Animatable::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
  330. {
  331. if (!objectAnimation)
  332. return;
  333. // Set all attribute animations from the object animation
  334. const HashMap<String, SharedPtr<ValueAnimationInfo> >& attributeAnimationInfos = objectAnimation->GetAttributeAnimationInfos();
  335. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = attributeAnimationInfos.Begin();
  336. i != attributeAnimationInfos.End(); ++i)
  337. {
  338. const String& name = i->first_;
  339. ValueAnimationInfo* info = i->second_;
  340. SetObjectAttributeAnimation(name, info->GetAnimation(), info->GetWrapMode(), info->GetSpeed());
  341. }
  342. }
  343. void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
  344. {
  345. if (!objectAnimation)
  346. return;
  347. // Just remove all attribute animations listed by the object animation
  348. const HashMap<String, SharedPtr<ValueAnimationInfo> >& infos = objectAnimation->GetAttributeAnimationInfos();
  349. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = infos.Begin(); i != infos.End(); ++i)
  350. SetObjectAttributeAnimation(i->first_, 0, WM_LOOP, 1.0f);
  351. }
  352. void Animatable::UpdateAttributeAnimations(float timeStep)
  353. {
  354. if (!animationEnabled_)
  355. return;
  356. Vector<String> finishedNames;
  357. for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  358. i != attributeAnimationInfos_.End(); ++i)
  359. {
  360. if (i->second_->Update(timeStep))
  361. finishedNames.Push(i->second_->GetAttributeInfo().name_);
  362. }
  363. for (unsigned i = 0; i < finishedNames.Size(); ++i)
  364. SetAttributeAnimation(finishedNames[i], 0);
  365. }
  366. bool Animatable::IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const
  367. {
  368. return animatedNetworkAttributes_.Find(&attrInfo) != animatedNetworkAttributes_.End();
  369. }
  370. AttributeAnimationInfo* Animatable::GetAttributeAnimationInfo(const String& name) const
  371. {
  372. HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Find(name);
  373. if (i != attributeAnimationInfos_.End())
  374. return i->second_;
  375. return 0;
  376. }
  377. void Animatable::HandleAttributeAnimationAdded(StringHash eventType, VariantMap& eventData)
  378. {
  379. if (!objectAnimation_)
  380. return;
  381. using namespace AttributeAnimationAdded;
  382. const String& name = eventData[P_ATTRIBUTEANIMATIONNAME].GetString();
  383. ValueAnimationInfo* info = objectAnimation_->GetAttributeAnimationInfo(name);
  384. if (!info)
  385. return;
  386. SetObjectAttributeAnimation(name, info->GetAnimation(), info->GetWrapMode(), info->GetSpeed());
  387. }
  388. void Animatable::HandleAttributeAnimationRemoved(StringHash eventType, VariantMap& eventData)
  389. {
  390. if (!objectAnimation_)
  391. return;
  392. using namespace AttributeAnimationRemoved;
  393. const String& name = eventData[P_ATTRIBUTEANIMATIONNAME].GetString();
  394. SetObjectAttributeAnimation(name, 0, WM_LOOP, 1.0f);
  395. }
  396. }