ObjectAnimation.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // Copyright (c) 2008-2017 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 "../Resource/XMLFile.h"
  25. #include "../Resource/JSONFile.h"
  26. #include "../Scene/ObjectAnimation.h"
  27. #include "../Scene/SceneEvents.h"
  28. #include "../Scene/ValueAnimation.h"
  29. #include "../Scene/ValueAnimationInfo.h"
  30. #include "../DebugNew.h"
  31. namespace Atomic
  32. {
  33. const char* wrapModeNames[] =
  34. {
  35. "Loop",
  36. "Once",
  37. "Clamp",
  38. 0
  39. };
  40. ObjectAnimation::ObjectAnimation(Context* context) :
  41. Resource(context)
  42. {
  43. }
  44. ObjectAnimation::~ObjectAnimation()
  45. {
  46. }
  47. void ObjectAnimation::RegisterObject(Context* context)
  48. {
  49. context->RegisterFactory<ObjectAnimation>();
  50. }
  51. bool ObjectAnimation::BeginLoad(Deserializer& source)
  52. {
  53. XMLFile xmlFile(context_);
  54. if (!xmlFile.Load(source))
  55. return false;
  56. return LoadXML(xmlFile.GetRoot());
  57. }
  58. bool ObjectAnimation::Save(Serializer& dest) const
  59. {
  60. XMLFile xmlFile(context_);
  61. XMLElement rootElem = xmlFile.CreateRoot("objectanimation");
  62. if (!SaveXML(rootElem))
  63. return false;
  64. return xmlFile.Save(dest);
  65. }
  66. bool ObjectAnimation::LoadXML(const XMLElement& source)
  67. {
  68. attributeAnimationInfos_.Clear();
  69. XMLElement animElem;
  70. animElem = source.GetChild("attributeanimation");
  71. while (animElem)
  72. {
  73. String name = animElem.GetAttribute("name");
  74. SharedPtr<ValueAnimation> animation(new ValueAnimation(context_));
  75. if (!animation->LoadXML(animElem))
  76. return false;
  77. String wrapModeString = animElem.GetAttribute("wrapmode");
  78. WrapMode wrapMode = WM_LOOP;
  79. for (int i = 0; i <= WM_CLAMP; ++i)
  80. {
  81. if (wrapModeString == wrapModeNames[i])
  82. {
  83. wrapMode = (WrapMode)i;
  84. break;
  85. }
  86. }
  87. float speed = animElem.GetFloat("speed");
  88. AddAttributeAnimation(name, animation, wrapMode, speed);
  89. animElem = animElem.GetNext("attributeanimation");
  90. }
  91. return true;
  92. }
  93. bool ObjectAnimation::SaveXML(XMLElement& dest) const
  94. {
  95. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  96. i != attributeAnimationInfos_.End(); ++i)
  97. {
  98. XMLElement animElem = dest.CreateChild("attributeanimation");
  99. animElem.SetAttribute("name", i->first_);
  100. const ValueAnimationInfo* info = i->second_;
  101. if (!info->GetAnimation()->SaveXML(animElem))
  102. return false;
  103. animElem.SetAttribute("wrapmode", wrapModeNames[info->GetWrapMode()]);
  104. animElem.SetFloat("speed", info->GetSpeed());
  105. }
  106. return true;
  107. }
  108. bool ObjectAnimation::LoadJSON(const JSONValue& source)
  109. {
  110. attributeAnimationInfos_.Clear();
  111. JSONValue attributeAnimationsValue = source.Get("attributeanimations");
  112. if (attributeAnimationsValue.IsNull())
  113. return true;
  114. if (!attributeAnimationsValue.IsObject())
  115. return true;
  116. const JSONObject& attributeAnimationsObject = attributeAnimationsValue.GetObject();
  117. for (JSONObject::ConstIterator it = attributeAnimationsObject.Begin(); it != attributeAnimationsObject.End(); it++)
  118. {
  119. String name = it->first_;
  120. JSONValue value = it->second_;
  121. SharedPtr<ValueAnimation> animation(new ValueAnimation(context_));
  122. if (!animation->LoadJSON(value))
  123. return false;
  124. String wrapModeString = value.Get("wrapmode").GetString();
  125. WrapMode wrapMode = WM_LOOP;
  126. for (int i = 0; i <= WM_CLAMP; ++i)
  127. {
  128. if (wrapModeString == wrapModeNames[i])
  129. {
  130. wrapMode = (WrapMode)i;
  131. break;
  132. }
  133. }
  134. float speed = value.Get("speed").GetFloat();
  135. AddAttributeAnimation(name, animation, wrapMode, speed);
  136. }
  137. return true;
  138. }
  139. bool ObjectAnimation::SaveJSON(JSONValue& dest) const
  140. {
  141. JSONValue attributeAnimationsValue;
  142. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  143. i != attributeAnimationInfos_.End(); ++i)
  144. {
  145. JSONValue animValue;
  146. animValue.Set("name", i->first_);
  147. const ValueAnimationInfo* info = i->second_;
  148. if (!info->GetAnimation()->SaveJSON(animValue))
  149. return false;
  150. animValue.Set("wrapmode", wrapModeNames[info->GetWrapMode()]);
  151. animValue.Set("speed", (float) info->GetSpeed());
  152. attributeAnimationsValue.Set(i->first_, animValue);
  153. }
  154. dest.Set("attributeanimations", attributeAnimationsValue);
  155. return true;
  156. }
  157. void ObjectAnimation::AddAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  158. {
  159. if (!attributeAnimation)
  160. return;
  161. attributeAnimation->SetOwner(this);
  162. attributeAnimationInfos_[name] = new ValueAnimationInfo(attributeAnimation, wrapMode, speed);
  163. SendAttributeAnimationAddedEvent(name);
  164. }
  165. void ObjectAnimation::RemoveAttributeAnimation(const String& name)
  166. {
  167. HashMap<String, SharedPtr<ValueAnimationInfo> >::Iterator i = attributeAnimationInfos_.Find(name);
  168. if (i != attributeAnimationInfos_.End())
  169. {
  170. SendAttributeAnimationRemovedEvent(name);
  171. i->second_->GetAnimation()->SetOwner(0);
  172. attributeAnimationInfos_.Erase(i);
  173. }
  174. }
  175. void ObjectAnimation::RemoveAttributeAnimation(ValueAnimation* attributeAnimation)
  176. {
  177. if (!attributeAnimation)
  178. return;
  179. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::Iterator i = attributeAnimationInfos_.Begin();
  180. i != attributeAnimationInfos_.End(); ++i)
  181. {
  182. if (i->second_->GetAnimation() == attributeAnimation)
  183. {
  184. SendAttributeAnimationRemovedEvent(i->first_);
  185. attributeAnimation->SetOwner(0);
  186. attributeAnimationInfos_.Erase(i);
  187. return;
  188. }
  189. }
  190. }
  191. ValueAnimation* ObjectAnimation::GetAttributeAnimation(const String& name) const
  192. {
  193. ValueAnimationInfo* info = GetAttributeAnimationInfo(name);
  194. return info ? info->GetAnimation() : 0;
  195. }
  196. WrapMode ObjectAnimation::GetAttributeAnimationWrapMode(const String& name) const
  197. {
  198. ValueAnimationInfo* info = GetAttributeAnimationInfo(name);
  199. return info ? info->GetWrapMode() : WM_LOOP;
  200. }
  201. float ObjectAnimation::GetAttributeAnimationSpeed(const String& name) const
  202. {
  203. ValueAnimationInfo* info = GetAttributeAnimationInfo(name);
  204. return info ? info->GetSpeed() : 1.0f;
  205. }
  206. ValueAnimationInfo* ObjectAnimation::GetAttributeAnimationInfo(const String& name) const
  207. {
  208. HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Find(name);
  209. if (i != attributeAnimationInfos_.End())
  210. return i->second_;
  211. return 0;
  212. }
  213. void ObjectAnimation::SendAttributeAnimationAddedEvent(const String& name)
  214. {
  215. using namespace AttributeAnimationAdded;
  216. VariantMap& eventData = GetEventDataMap();
  217. eventData[P_OBJECTANIMATION] = this;
  218. eventData[P_ATTRIBUTEANIMATIONNAME] = name;
  219. SendEvent(E_ATTRIBUTEANIMATIONADDED, eventData);
  220. }
  221. void ObjectAnimation::SendAttributeAnimationRemovedEvent(const String& name)
  222. {
  223. using namespace AttributeAnimationRemoved;
  224. VariantMap& eventData = GetEventDataMap();
  225. eventData[P_OBJECTANIMATION] = this;
  226. eventData[P_ATTRIBUTEANIMATIONNAME] = name;
  227. SendEvent(E_ATTRIBUTEANIMATIONREMOVED, eventData);
  228. }
  229. }