Animatable.cpp 12 KB

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