Animatable.cpp 13 KB

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