ObjectAnimation.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. const char* wrapModeNames[] =
  31. {
  32. "Loop",
  33. "Once",
  34. "Clamp",
  35. 0
  36. };
  37. ObjectAnimation::ObjectAnimation(Context* context) :
  38. Resource(context)
  39. {
  40. }
  41. ObjectAnimation::~ObjectAnimation()
  42. {
  43. }
  44. void ObjectAnimation::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<ObjectAnimation>();
  47. }
  48. bool ObjectAnimation::Load(Deserializer& source)
  49. {
  50. XMLFile xmlFile(context_);
  51. if (!xmlFile.Load(source))
  52. return false;
  53. return LoadXML(xmlFile.GetRoot());
  54. }
  55. bool ObjectAnimation::Save(Serializer& dest) const
  56. {
  57. XMLFile xmlFile(context_);
  58. XMLElement rootElem = xmlFile.CreateRoot("objectAnimation");
  59. if (!SaveXML(rootElem))
  60. return false;
  61. return xmlFile.Save(dest);
  62. }
  63. bool ObjectAnimation::LoadXML(const XMLElement& source)
  64. {
  65. attributeAnimations_.Clear();
  66. XMLElement animElem;
  67. animElem = source.GetChild("attributeAnimation");
  68. while (animElem)
  69. {
  70. String name = animElem.GetAttribute("name");
  71. SharedPtr<AttributeAnimation> animation(new AttributeAnimation(context_));
  72. if (!animation->LoadXML(animElem))
  73. return false;
  74. String wrapModeString = source.GetAttribute("wrapMode");
  75. WrapMode wrapMode = WM_LOOP;
  76. for (int i = 0; i <= WM_CLAMP; ++i)
  77. {
  78. if (wrapModeString == wrapModeNames[i])
  79. {
  80. wrapMode = (WrapMode)i;
  81. break;
  82. }
  83. }
  84. float speed = animElem.GetFloat("speed");
  85. AddAttributeAnimation(name, animation, wrapMode, speed);
  86. animElem = animElem.GetNext("attributeAnimation");
  87. }
  88. return true;
  89. }
  90. bool ObjectAnimation::SaveXML(XMLElement& dest) const
  91. {
  92. for (HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  93. {
  94. XMLElement animElem = dest.CreateChild("attributeAnimation");
  95. animElem.SetAttribute("name", i->first_);
  96. if (!i->second_->SaveXML(animElem))
  97. return false;
  98. animElem.SetAttribute("wrapMode", wrapModeNames[GetAttributeAnimationWrapMode(i->first_)]);
  99. animElem.SetFloat("speed", GetAttributeAnimationSpeed(i->first_));
  100. }
  101. return true;
  102. }
  103. void ObjectAnimation::AddAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  104. {
  105. if (!attributeAnimation)
  106. return;
  107. attributeAnimation->SetObjectAnimation(this);
  108. attributeAnimations_[name] = attributeAnimation;
  109. attributeAnimationWrapModes_[name] = wrapMode;
  110. attributeAnimationSpeeds_[name] = speed;
  111. }
  112. void ObjectAnimation::RemoveAttributeAnimation(const String& name)
  113. {
  114. RemoveAttributeAnimation(GetAttributeAnimation(name));
  115. }
  116. void ObjectAnimation::RemoveAttributeAnimation(AttributeAnimation* attributeAnimation)
  117. {
  118. if (!attributeAnimation)
  119. return;
  120. String name;
  121. for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  122. {
  123. if (i->second_ == attributeAnimation)
  124. {
  125. name = i->first_;
  126. attributeAnimation->SetObjectAnimation(0);
  127. attributeAnimations_.Erase(i);
  128. break;
  129. }
  130. }
  131. if (!name.Empty())
  132. {
  133. attributeAnimationWrapModes_.Erase(name);
  134. attributeAnimationSpeeds_.Erase(name);
  135. }
  136. }
  137. AttributeAnimation* ObjectAnimation::GetAttributeAnimation(const String& name) const
  138. {
  139. HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Find(name);
  140. if (i != attributeAnimations_.End())
  141. return i->second_;
  142. return 0;
  143. }
  144. WrapMode ObjectAnimation::GetAttributeAnimationWrapMode(const String& name) const
  145. {
  146. HashMap<String, WrapMode>::ConstIterator i = attributeAnimationWrapModes_.Find(name);
  147. if (i != attributeAnimationWrapModes_.End())
  148. return i->second_;
  149. return WM_LOOP;
  150. }
  151. float ObjectAnimation::GetAttributeAnimationSpeed(const String& name) const
  152. {
  153. HashMap<String, float>::ConstIterator i = attributeAnimationSpeeds_.Find(name);
  154. if (i != attributeAnimationSpeeds_.End())
  155. return i->second_;
  156. return 1.0f;
  157. }
  158. }