AttributeAnimation.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "Deserializer.h"
  27. #include "Serializer.h"
  28. #include "XMLFile.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. AttributeAnimation::AttributeAnimation(Context* context) :
  33. Resource(context),
  34. valueType_(VAR_NONE),
  35. isInterpolatable_(false),
  36. beginTime_(M_INFINITY),
  37. endTime_(-M_INFINITY)
  38. {
  39. }
  40. AttributeAnimation::~AttributeAnimation()
  41. {
  42. }
  43. void AttributeAnimation::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<AttributeAnimation>();
  46. }
  47. bool AttributeAnimation::Load(Deserializer& source)
  48. {
  49. XMLFile xmlFile(context_);
  50. if (!xmlFile.Load(source))
  51. return false;
  52. return LoadXML(xmlFile.GetRoot());
  53. }
  54. bool AttributeAnimation::Save(Serializer& dest) const
  55. {
  56. XMLFile xmlFile(context_);
  57. XMLElement rootElem = xmlFile.CreateRoot("attributeAnimation");
  58. if (!SaveXML(rootElem))
  59. return false;
  60. return xmlFile.Save(dest);
  61. }
  62. bool AttributeAnimation::LoadXML(const XMLElement& source)
  63. {
  64. valueType_ = VAR_NONE;
  65. eventFrames_.Clear();
  66. SetValueType(Variant::GetTypeFromName(source.GetAttribute("valueType")));
  67. XMLElement keyFrameEleme = source.GetChild("keyFrame");
  68. while (keyFrameEleme)
  69. {
  70. float time = keyFrameEleme.GetFloat("time");
  71. Variant value(valueType_, keyFrameEleme.GetAttribute("value"));
  72. SetKeyFrame(time, value);
  73. keyFrameEleme = keyFrameEleme.GetNext("keyFrame");
  74. }
  75. XMLElement eventFrameElem = source.GetChild("eventFrame");
  76. while (eventFrameElem)
  77. {
  78. float time = eventFrameElem.GetFloat("time");
  79. unsigned eventType = eventFrameElem.GetUInt("eventType");
  80. VariantMap eventData = eventFrameElem.GetChild("eventData").GetVariantMap();
  81. SetEventFrame(time, StringHash(eventType), eventData);
  82. eventFrameElem = eventFrameElem.GetNext("eventFrame");
  83. }
  84. return true;
  85. }
  86. bool AttributeAnimation::SaveXML(XMLElement& dest) const
  87. {
  88. dest.SetAttribute("valueType", Variant::GetTypeName(valueType_));
  89. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  90. {
  91. const AttributeKeyFrame& keyFrame = keyFrames_[i];
  92. XMLElement keyFrameEleme = dest.CreateChild("keyFrame");
  93. keyFrameEleme.SetFloat("time", keyFrame.time_);
  94. keyFrameEleme.SetAttribute("value", keyFrame.value_.ToString());
  95. }
  96. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  97. {
  98. const AttributeEventFrame& eventFrame = eventFrames_[i];
  99. XMLElement eventFrameElem = dest.CreateChild("eventFrame");
  100. eventFrameElem.SetFloat("time", eventFrame.time_);
  101. eventFrameElem.SetUInt("eventType", eventFrame.eventType_.Value());
  102. eventFrameElem.CreateChild("eventData").SetVariantMap(eventFrame.eventData_);
  103. }
  104. return true;
  105. }
  106. void AttributeAnimation::SetObjectAnimation(ObjectAnimation* objectAnimation)
  107. {
  108. objectAnimation_ = objectAnimation;
  109. }
  110. void AttributeAnimation::SetValueType(VariantType valueType)
  111. {
  112. if (valueType == valueType_)
  113. return;
  114. valueType_ = valueType;
  115. isInterpolatable_ = (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) ||
  116. (valueType_ == VAR_VECTOR4) || (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
  117. keyFrames_.Clear();
  118. beginTime_ = M_INFINITY;
  119. endTime_ = -M_INFINITY;
  120. }
  121. bool AttributeAnimation::SetKeyFrame(float time, const Variant& value)
  122. {
  123. if (valueType_ == VAR_NONE)
  124. SetValueType(value.GetType());
  125. else if (value.GetType() != valueType_)
  126. return false;
  127. beginTime_ = Min(time, beginTime_);
  128. endTime_ = Max(time, endTime_);
  129. AttributeKeyFrame keyFrame;
  130. keyFrame.time_ = time;
  131. keyFrame.value_ = value;
  132. if (keyFrames_.Empty() || time >= keyFrames_.Back().time_)
  133. keyFrames_.Push(keyFrame);
  134. else
  135. {
  136. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  137. {
  138. if (time < keyFrames_[i].time_)
  139. {
  140. keyFrames_.Insert(i, keyFrame);
  141. break;
  142. }
  143. }
  144. }
  145. return true;
  146. }
  147. void AttributeAnimation::SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData)
  148. {
  149. AttributeEventFrame eventFrame;
  150. eventFrame.time_ = time;
  151. eventFrame.eventType_ = eventType;
  152. eventFrame.eventData_ = eventData;
  153. if (eventFrames_.Empty() || time >= eventFrames_.Back().time_)
  154. eventFrames_.Push(eventFrame);
  155. else
  156. {
  157. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  158. {
  159. if (time < eventFrames_[i].time_)
  160. {
  161. eventFrames_.Insert(i, eventFrame);
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. ObjectAnimation* AttributeAnimation::GetObjectAnimation() const
  168. {
  169. return objectAnimation_;
  170. }
  171. void AttributeAnimation::GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const
  172. {
  173. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  174. {
  175. const AttributeEventFrame& eventFrame = eventFrames_[i];
  176. if (eventFrame.time_ >= endTime)
  177. break;
  178. if (eventFrame.time_ >= beginTime)
  179. eventFrames.Push(&eventFrame);
  180. }
  181. }
  182. }