ObjectAnimation.cpp 5.5 KB

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