AttributeAnimation.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. const char* wrapModeNames[] =
  33. {
  34. "Loop",
  35. "Once",
  36. "Clamp",
  37. 0
  38. };
  39. AttributeAnimation::AttributeAnimation(Context* context) :
  40. Resource(context),
  41. wrapMode_(WM_LOOP),
  42. valueType_(VAR_NONE),
  43. isInterpolatable_(false),
  44. beginTime_(M_INFINITY),
  45. endTime_(-M_INFINITY)
  46. {
  47. }
  48. AttributeAnimation::~AttributeAnimation()
  49. {
  50. }
  51. void AttributeAnimation::RegisterObject(Context* context)
  52. {
  53. context->RegisterFactory<AttributeAnimation>();
  54. }
  55. bool AttributeAnimation::Load(Deserializer& source)
  56. {
  57. XMLFile xmlFile(context_);
  58. if (!xmlFile.Load(source))
  59. return false;
  60. return LoadXML(xmlFile.GetRoot());
  61. }
  62. bool AttributeAnimation::Save(Serializer& dest) const
  63. {
  64. XMLFile xmlFile(context_);
  65. XMLElement rootElem = xmlFile.CreateRoot("attributeAnimation");
  66. if (!SaveXML(rootElem))
  67. return false;
  68. return xmlFile.Save(dest);
  69. }
  70. bool AttributeAnimation::LoadXML(const XMLElement& source)
  71. {
  72. valueType_ = VAR_NONE;
  73. eventFrames_.Clear();
  74. String wrapModeString = source.GetAttribute("wrapMode");
  75. 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. SetValueType(Variant::GetTypeFromName(source.GetAttribute("valueType")));
  85. XMLElement keyFrameEleme = source.GetChild("keyFrame");
  86. while (keyFrameEleme)
  87. {
  88. float time = keyFrameEleme.GetFloat("time");
  89. Variant value(valueType_, keyFrameEleme.GetAttribute("value"));
  90. SetKeyFrame(time, value);
  91. keyFrameEleme = keyFrameEleme.GetNext("keyFrame");
  92. }
  93. XMLElement eventFrameElem = source.GetChild("eventFrame");
  94. while (eventFrameElem)
  95. {
  96. float time = eventFrameElem.GetFloat("time");
  97. unsigned eventType = eventFrameElem.GetUInt("eventType");
  98. VariantMap eventData = eventFrameElem.GetChild("eventData").GetVariantMap();
  99. SetEventFrame(time, StringHash(eventType), eventData);
  100. eventFrameElem = eventFrameElem.GetNext("eventFrame");
  101. }
  102. return true;
  103. }
  104. bool AttributeAnimation::SaveXML(XMLElement& dest) const
  105. {
  106. dest.SetAttribute("wrapMode", wrapModeNames[wrapMode_]);
  107. dest.SetAttribute("valueType", Variant::GetTypeName(valueType_));
  108. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  109. {
  110. const AttributeKeyFrame& keyFrame = keyFrames_[i];
  111. XMLElement keyFrameEleme = dest.CreateChild("keyFrame");
  112. keyFrameEleme.SetFloat("time", keyFrame.time_);
  113. keyFrameEleme.SetAttribute("value", keyFrame.value_.ToString());
  114. }
  115. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  116. {
  117. const AttributeEventFrame& eventFrame = eventFrames_[i];
  118. XMLElement eventFrameElem = dest.CreateChild("eventFrame");
  119. eventFrameElem.SetFloat("time", eventFrame.time_);
  120. eventFrameElem.SetUInt("eventType", eventFrame.eventType_.Value());
  121. eventFrameElem.CreateChild("eventData").SetVariantMap(eventFrame.eventData_);
  122. }
  123. return true;
  124. }
  125. void AttributeAnimation::SetObjectAnimation(ObjectAnimation* objectAnimation)
  126. {
  127. objectAnimation_ = objectAnimation;
  128. }
  129. void AttributeAnimation::SetWrapMode(WrapMode wrapMode)
  130. {
  131. wrapMode_ = wrapMode;
  132. }
  133. void AttributeAnimation::SetValueType(VariantType valueType)
  134. {
  135. if (valueType == valueType_)
  136. return;
  137. valueType_ = valueType;
  138. isInterpolatable_ = (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) ||
  139. (valueType_ == VAR_VECTOR4) || (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
  140. keyFrames_.Clear();
  141. beginTime_ = M_INFINITY;
  142. endTime_ = -M_INFINITY;
  143. }
  144. bool AttributeAnimation::SetKeyFrame(float time, const Variant& value)
  145. {
  146. if (valueType_ == VAR_NONE)
  147. SetValueType(value.GetType());
  148. else if (value.GetType() != valueType_)
  149. return false;
  150. beginTime_ = Min(time, beginTime_);
  151. endTime_ = Max(time, endTime_);
  152. AttributeKeyFrame keyFrame;
  153. keyFrame.time_ = time;
  154. keyFrame.value_ = value;
  155. if (keyFrames_.Empty() || time >= keyFrames_.Back().time_)
  156. keyFrames_.Push(keyFrame);
  157. else
  158. {
  159. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  160. {
  161. if (time < keyFrames_[i].time_)
  162. {
  163. keyFrames_.Insert(i, keyFrame);
  164. break;
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. void AttributeAnimation::SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData)
  171. {
  172. AttributeEventFrame eventFrame;
  173. eventFrame.time_ = time;
  174. eventFrame.eventType_ = eventType;
  175. eventFrame.eventData_ = eventData;
  176. if (eventFrames_.Empty() || time >= eventFrames_.Back().time_)
  177. eventFrames_.Push(eventFrame);
  178. else
  179. {
  180. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  181. {
  182. if (time < eventFrames_[i].time_)
  183. {
  184. eventFrames_.Insert(i, eventFrame);
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. ObjectAnimation* AttributeAnimation::GetObjectAnimation() const
  191. {
  192. return objectAnimation_;
  193. }
  194. float AttributeAnimation::CalculateScaledTime(float currentTime, bool& finished) const
  195. {
  196. switch (wrapMode_)
  197. {
  198. case WM_LOOP:
  199. {
  200. float span = endTime_ - beginTime_;
  201. return beginTime_ + fmodf(currentTime - beginTime_, span);
  202. }
  203. case WM_ONCE:
  204. finished = (currentTime >= endTime_);
  205. case WM_CLAMP:
  206. return Clamp(currentTime, beginTime_, endTime_);
  207. }
  208. return beginTime_;
  209. }
  210. void AttributeAnimation::GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const
  211. {
  212. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  213. {
  214. const AttributeEventFrame& eventFrame = eventFrames_[i];
  215. if (eventFrame.time_ >= endTime)
  216. break;
  217. if (eventFrame.time_ >= beginTime)
  218. eventFrames.Push(&eventFrame);
  219. }
  220. }
  221. }