Animatable.cpp 11 KB

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