AttributeAnimation.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. XMLElement keyFrameEleme = source.GetChild("keyframe");
  67. while (keyFrameEleme)
  68. {
  69. float time = keyFrameEleme.GetFloat("time");
  70. Variant value = keyFrameEleme.GetVariant();
  71. SetKeyFrame(time, value);
  72. keyFrameEleme = keyFrameEleme.GetNext("keyframe");
  73. }
  74. XMLElement eventFrameElem = source.GetChild("eventframe");
  75. while (eventFrameElem)
  76. {
  77. float time = eventFrameElem.GetFloat("time");
  78. unsigned eventType = eventFrameElem.GetUInt("eventtype");
  79. VariantMap eventData = eventFrameElem.GetChild("eventdata").GetVariantMap();
  80. SetEventFrame(time, StringHash(eventType), eventData);
  81. eventFrameElem = eventFrameElem.GetNext("eventframe");
  82. }
  83. return true;
  84. }
  85. bool AttributeAnimation::SaveXML(XMLElement& dest) const
  86. {
  87. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  88. {
  89. const AttributeKeyFrame& keyFrame = keyFrames_[i];
  90. XMLElement keyFrameEleme = dest.CreateChild("keyframe");
  91. keyFrameEleme.SetFloat("time", keyFrame.time_);
  92. keyFrameEleme.SetVariant(keyFrame.value_);
  93. }
  94. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  95. {
  96. const AttributeEventFrame& eventFrame = eventFrames_[i];
  97. XMLElement eventFrameElem = dest.CreateChild("eventframe");
  98. eventFrameElem.SetFloat("time", eventFrame.time_);
  99. eventFrameElem.SetUInt("eventtype", eventFrame.eventType_.Value());
  100. eventFrameElem.CreateChild("eventdata").SetVariantMap(eventFrame.eventData_);
  101. }
  102. return true;
  103. }
  104. void AttributeAnimation::SetObjectAnimation(ObjectAnimation* objectAnimation)
  105. {
  106. objectAnimation_ = objectAnimation;
  107. }
  108. void AttributeAnimation::SetValueType(VariantType valueType)
  109. {
  110. if (valueType == valueType_)
  111. return;
  112. valueType_ = valueType;
  113. isInterpolatable_ = (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) ||
  114. (valueType_ == VAR_VECTOR4) || (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
  115. keyFrames_.Clear();
  116. beginTime_ = M_INFINITY;
  117. endTime_ = -M_INFINITY;
  118. }
  119. bool AttributeAnimation::SetKeyFrame(float time, const Variant& value)
  120. {
  121. if (valueType_ == VAR_NONE)
  122. SetValueType(value.GetType());
  123. else if (value.GetType() != valueType_)
  124. return false;
  125. beginTime_ = Min(time, beginTime_);
  126. endTime_ = Max(time, endTime_);
  127. AttributeKeyFrame keyFrame;
  128. keyFrame.time_ = time;
  129. keyFrame.value_ = value;
  130. if (keyFrames_.Empty() || time >= keyFrames_.Back().time_)
  131. keyFrames_.Push(keyFrame);
  132. else
  133. {
  134. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  135. {
  136. if (time < keyFrames_[i].time_)
  137. {
  138. keyFrames_.Insert(i, keyFrame);
  139. break;
  140. }
  141. }
  142. }
  143. return true;
  144. }
  145. void AttributeAnimation::SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData)
  146. {
  147. AttributeEventFrame eventFrame;
  148. eventFrame.time_ = time;
  149. eventFrame.eventType_ = eventType;
  150. eventFrame.eventData_ = eventData;
  151. if (eventFrames_.Empty() || time >= eventFrames_.Back().time_)
  152. eventFrames_.Push(eventFrame);
  153. else
  154. {
  155. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  156. {
  157. if (time < eventFrames_[i].time_)
  158. {
  159. eventFrames_.Insert(i, eventFrame);
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. ObjectAnimation* AttributeAnimation::GetObjectAnimation() const
  166. {
  167. return objectAnimation_;
  168. }
  169. void AttributeAnimation::GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const
  170. {
  171. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  172. {
  173. const AttributeEventFrame& eventFrame = eventFrames_[i];
  174. if (eventFrame.time_ >= endTime)
  175. break;
  176. if (eventFrame.time_ >= beginTime)
  177. eventFrames.Push(&eventFrame);
  178. }
  179. }
  180. }