ObjectAnimation.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "AttributeAnimation.h"
  24. #include "Context.h"
  25. #include "ObjectAnimation.h"
  26. #include "XMLFile.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. ObjectAnimation::ObjectAnimation(Context* context) :
  31. Resource(context)
  32. {
  33. }
  34. ObjectAnimation::~ObjectAnimation()
  35. {
  36. }
  37. void ObjectAnimation::RegisterObject(Context* context)
  38. {
  39. context->RegisterFactory<ObjectAnimation>();
  40. }
  41. bool ObjectAnimation::Load(Deserializer& source)
  42. {
  43. XMLFile xmlFile(context_);
  44. if (xmlFile.Load(source))
  45. return false;
  46. XMLElement rootElem = xmlFile.GetRoot("ObjectAnimation");
  47. if (!rootElem)
  48. return false;
  49. return LoadXML(rootElem);
  50. }
  51. bool ObjectAnimation::Save(Serializer& dest) const
  52. {
  53. XMLFile xmlFile(context_);
  54. XMLElement rootElem = xmlFile.CreateRoot("ObjectAnimation");
  55. if (!SaveXML(rootElem))
  56. return false;
  57. return xmlFile.Save(dest);
  58. }
  59. bool ObjectAnimation::LoadXML(const XMLElement& source)
  60. {
  61. if (source.GetName() != "ObjectAnimation")
  62. return false;
  63. XMLElement animElem;
  64. animElem = source.GetChild("AttributeAnimation");
  65. while (animElem)
  66. {
  67. String name = animElem.GetAttribute("name");
  68. SharedPtr<AttributeAnimation> animation(new AttributeAnimation(context_));
  69. if (!animation->LoadXML(animElem))
  70. return false;
  71. AddAttributeAnimation(name, animation);
  72. animElem = animElem.GetNext("AttributeAnimation");
  73. }
  74. animElem = source.GetChild("ObjectAnimation");
  75. while (animElem)
  76. {
  77. String name = animElem.GetAttribute("name");
  78. SharedPtr<ObjectAnimation> animation(new ObjectAnimation(context_));
  79. if (!animation->LoadXML(animElem))
  80. return false;
  81. AddChildAnimation(name, animation);
  82. animElem = animElem.GetNext("ObjectAnimation");
  83. }
  84. return true;
  85. }
  86. bool ObjectAnimation::SaveXML(XMLElement& dest) const
  87. {
  88. if (dest.GetName() != "ObjectAnimation")
  89. return false;
  90. for (HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  91. {
  92. XMLElement animElem = dest.CreateChild("AttributeAnimation");
  93. animElem.SetAttribute("name", i->first_);
  94. if (!i->second_->SaveXML(animElem))
  95. return false;
  96. }
  97. for (HashMap<String, SharedPtr<ObjectAnimation> >::ConstIterator i = objectAnimations_.Begin(); i != objectAnimations_.End(); ++i)
  98. {
  99. XMLElement animElem = dest.CreateChild("ObjectAnimation");
  100. animElem.SetAttribute("name", i->first_);
  101. if (!i->second_->SaveXML(animElem))
  102. return false;
  103. }
  104. return true;
  105. }
  106. void ObjectAnimation::AddChildAnimation(const String& name, ObjectAnimation* objectAnimation)
  107. {
  108. if (!objectAnimation)
  109. return;
  110. objectAnimation->SetParentAnimation(this);
  111. objectAnimations_[name] = objectAnimation;
  112. }
  113. void ObjectAnimation::RemoveChildAnimation(const String& name)
  114. {
  115. RemoveChildAnimation(GetObjectAnimation(name));
  116. }
  117. void ObjectAnimation::RemoveChildAnimation(ObjectAnimation* objectAnimation)
  118. {
  119. if (!objectAnimation)
  120. return;
  121. for (HashMap<String, SharedPtr<ObjectAnimation> >::Iterator i = objectAnimations_.Begin(); i != objectAnimations_.End(); ++i)
  122. {
  123. if (i->second_ == objectAnimation)
  124. {
  125. objectAnimation->SetParentAnimation(0);
  126. objectAnimations_.Erase(i);
  127. return;
  128. }
  129. }
  130. }
  131. void ObjectAnimation::AddAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation)
  132. {
  133. if (!attributeAnimation)
  134. return;
  135. attributeAnimation->SetObjectAnimation(this);
  136. attributeAnimations_[name] = attributeAnimation;
  137. }
  138. void ObjectAnimation::RemoveAttributeAnimation(const String& name)
  139. {
  140. RemoveAttributeAnimation(GetAttributeAnimation(name));
  141. }
  142. void ObjectAnimation::RemoveAttributeAnimation(AttributeAnimation* attributeAnimation)
  143. {
  144. if (!attributeAnimation)
  145. return;
  146. for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  147. {
  148. if (i->second_ == attributeAnimation)
  149. {
  150. attributeAnimation->SetObjectAnimation(0);
  151. attributeAnimations_.Erase(i);
  152. return;
  153. }
  154. }
  155. }
  156. ObjectAnimation* ObjectAnimation::GetObjectAnimation(const String& name) const
  157. {
  158. HashMap<String, SharedPtr<ObjectAnimation> >::ConstIterator i = objectAnimations_.Find(name);
  159. if (i != objectAnimations_.End())
  160. return i->second_;
  161. return 0;
  162. }
  163. AttributeAnimation* ObjectAnimation::GetAttributeAnimation(const String& name) const
  164. {
  165. HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Find(name);
  166. if (i != objectAnimations_.End())
  167. return i->second_;
  168. return 0;
  169. }
  170. }