Animatable.cpp 13 KB

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