ObjectAnimation.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. return LoadXML(xmlFile.GetRoot());
  47. }
  48. bool ObjectAnimation::Save(Serializer& dest) const
  49. {
  50. XMLFile xmlFile(context_);
  51. if (!SaveXML(xmlFile.CreateRoot("objectAnimation")))
  52. return false;
  53. return xmlFile.Save(dest);
  54. }
  55. bool ObjectAnimation::LoadXML(const XMLElement& source)
  56. {
  57. attributeAnimations_.Clear();
  58. XMLElement animElem;
  59. animElem = source.GetChild("attributeAnimation");
  60. while (animElem)
  61. {
  62. String name = animElem.GetAttribute("name");
  63. SharedPtr<AttributeAnimation> animation(new AttributeAnimation(context_));
  64. if (!animation->LoadXML(animElem))
  65. return false;
  66. AddAttributeAnimation(name, animation);
  67. animElem = animElem.GetNext("attributeAnimation");
  68. }
  69. return true;
  70. }
  71. bool ObjectAnimation::SaveXML(XMLElement& dest) const
  72. {
  73. for (HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  74. {
  75. XMLElement animElem = dest.CreateChild("attributeAnimation");
  76. animElem.SetAttribute("name", i->first_);
  77. if (!i->second_->SaveXML(animElem))
  78. return false;
  79. }
  80. return true;
  81. }
  82. void ObjectAnimation::AddAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation)
  83. {
  84. if (!attributeAnimation)
  85. return;
  86. attributeAnimation->SetObjectAnimation(this);
  87. attributeAnimations_[name] = attributeAnimation;
  88. }
  89. void ObjectAnimation::RemoveAttributeAnimation(const String& name)
  90. {
  91. RemoveAttributeAnimation(GetAttributeAnimation(name));
  92. }
  93. void ObjectAnimation::RemoveAttributeAnimation(AttributeAnimation* attributeAnimation)
  94. {
  95. if (!attributeAnimation)
  96. return;
  97. for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  98. {
  99. if (i->second_ == attributeAnimation)
  100. {
  101. attributeAnimation->SetObjectAnimation(0);
  102. attributeAnimations_.Erase(i);
  103. return;
  104. }
  105. }
  106. }
  107. AttributeAnimation* ObjectAnimation::GetAttributeAnimation(const String& name) const
  108. {
  109. HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Find(name);
  110. if (i != attributeAnimations_.End())
  111. return i->second_;
  112. return 0;
  113. }
  114. }