ObjectAnimation.cpp 3.6 KB

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