ObjectAnimation.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Resource/XMLFile.h"
  25. #include "../Scene/ObjectAnimation.h"
  26. #include "../Scene/SceneEvents.h"
  27. #include "../Scene/ValueAnimation.h"
  28. #include "../Scene/ValueAnimationInfo.h"
  29. #include "../DebugNew.h"
  30. namespace Atomic
  31. {
  32. const char* wrapModeNames[] =
  33. {
  34. "Loop",
  35. "Once",
  36. "Clamp",
  37. 0
  38. };
  39. ObjectAnimation::ObjectAnimation(Context* context) :
  40. Resource(context)
  41. {
  42. }
  43. ObjectAnimation::~ObjectAnimation()
  44. {
  45. }
  46. void ObjectAnimation::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<ObjectAnimation>();
  49. }
  50. bool ObjectAnimation::BeginLoad(Deserializer& source)
  51. {
  52. XMLFile xmlFile(context_);
  53. if (!xmlFile.Load(source))
  54. return false;
  55. return LoadXML(xmlFile.GetRoot());
  56. }
  57. bool ObjectAnimation::Save(Serializer& dest) const
  58. {
  59. XMLFile xmlFile(context_);
  60. XMLElement rootElem = xmlFile.CreateRoot("objectanimation");
  61. if (!SaveXML(rootElem))
  62. return false;
  63. return xmlFile.Save(dest);
  64. }
  65. bool ObjectAnimation::LoadXML(const XMLElement& source)
  66. {
  67. attributeAnimationInfos_.Clear();
  68. XMLElement animElem;
  69. animElem = source.GetChild("attributeanimation");
  70. while (animElem)
  71. {
  72. String name = animElem.GetAttribute("name");
  73. SharedPtr<ValueAnimation> animation(new ValueAnimation(context_));
  74. if (!animation->LoadXML(animElem))
  75. return false;
  76. String wrapModeString = animElem.GetAttribute("wrapmode");
  77. WrapMode wrapMode = WM_LOOP;
  78. for (int i = 0; i <= WM_CLAMP; ++i)
  79. {
  80. if (wrapModeString == wrapModeNames[i])
  81. {
  82. wrapMode = (WrapMode)i;
  83. break;
  84. }
  85. }
  86. float speed = animElem.GetFloat("speed");
  87. AddAttributeAnimation(name, animation, wrapMode, speed);
  88. animElem = animElem.GetNext("attributeanimation");
  89. }
  90. return true;
  91. }
  92. bool ObjectAnimation::SaveXML(XMLElement& dest) const
  93. {
  94. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin();
  95. i != attributeAnimationInfos_.End(); ++i)
  96. {
  97. XMLElement animElem = dest.CreateChild("attributeanimation");
  98. animElem.SetAttribute("name", i->first_);
  99. const ValueAnimationInfo* info = i->second_;
  100. if (!info->GetAnimation()->SaveXML(animElem))
  101. return false;
  102. animElem.SetAttribute("wrapmode", wrapModeNames[info->GetWrapMode()]);
  103. animElem.SetFloat("speed", info->GetSpeed());
  104. }
  105. return true;
  106. }
  107. void ObjectAnimation::AddAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  108. {
  109. if (!attributeAnimation)
  110. return;
  111. attributeAnimation->SetOwner(this);
  112. attributeAnimationInfos_[name] = new ValueAnimationInfo(attributeAnimation, wrapMode, speed);
  113. SendAttributeAnimationAddedEvent(name);
  114. }
  115. void ObjectAnimation::RemoveAttributeAnimation(const String& name)
  116. {
  117. HashMap<String, SharedPtr<ValueAnimationInfo> >::Iterator i = attributeAnimationInfos_.Find(name);
  118. if (i != attributeAnimationInfos_.End())
  119. {
  120. SendAttributeAnimationRemovedEvent(name);
  121. i->second_->GetAnimation()->SetOwner(0);
  122. attributeAnimationInfos_.Erase(i);
  123. }
  124. }
  125. void ObjectAnimation::RemoveAttributeAnimation(ValueAnimation* attributeAnimation)
  126. {
  127. if (!attributeAnimation)
  128. return;
  129. for (HashMap<String, SharedPtr<ValueAnimationInfo> >::Iterator i = attributeAnimationInfos_.Begin();
  130. i != attributeAnimationInfos_.End(); ++i)
  131. {
  132. if (i->second_->GetAnimation() == attributeAnimation)
  133. {
  134. SendAttributeAnimationRemovedEvent(i->first_);
  135. attributeAnimation->SetOwner(0);
  136. attributeAnimationInfos_.Erase(i);
  137. return;
  138. }
  139. }
  140. }
  141. ValueAnimation* ObjectAnimation::GetAttributeAnimation(const String& name) const
  142. {
  143. ValueAnimationInfo* info = GetAttributeAnimationInfo(name);
  144. return info ? info->GetAnimation() : 0;
  145. }
  146. WrapMode ObjectAnimation::GetAttributeAnimationWrapMode(const String& name) const
  147. {
  148. ValueAnimationInfo* info = GetAttributeAnimationInfo(name);
  149. return info ? info->GetWrapMode() : WM_LOOP;
  150. }
  151. float ObjectAnimation::GetAttributeAnimationSpeed(const String& name) const
  152. {
  153. ValueAnimationInfo* info = GetAttributeAnimationInfo(name);
  154. return info ? info->GetSpeed() : 1.0f;
  155. }
  156. ValueAnimationInfo* ObjectAnimation::GetAttributeAnimationInfo(const String& name) const
  157. {
  158. HashMap<String, SharedPtr<ValueAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Find(name);
  159. if (i != attributeAnimationInfos_.End())
  160. return i->second_;
  161. return 0;
  162. }
  163. void ObjectAnimation::SendAttributeAnimationAddedEvent(const String& name)
  164. {
  165. using namespace AttributeAnimationAdded;
  166. VariantMap& eventData = GetEventDataMap();
  167. eventData[P_OBJECTANIMATION] = this;
  168. eventData[P_ATTRIBUTEANIMATIONNAME] = name;
  169. SendEvent(E_ATTRIBUTEANIMATIONADDED, eventData);
  170. }
  171. void ObjectAnimation::SendAttributeAnimationRemovedEvent(const String& name)
  172. {
  173. using namespace AttributeAnimationRemoved;
  174. VariantMap& eventData = GetEventDataMap();
  175. eventData[P_OBJECTANIMATION] = this;
  176. eventData[P_ATTRIBUTEANIMATIONNAME] = name;
  177. SendEvent(E_ATTRIBUTEANIMATIONREMOVED, eventData);
  178. }
  179. }