ObjectAnimation.cpp 8.3 KB

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