AttributeAnimation.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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* cycleModeNames[] =
  33. {
  34. "Loop",
  35. "Clamp",
  36. "Pingpong",
  37. 0
  38. };
  39. AttributeAnimation::AttributeAnimation(Context* context) :
  40. Resource(context),
  41. cycleMode_(CM_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. if (!SaveXML(xmlFile.CreateRoot("attributeAnimation")))
  66. return false;
  67. return xmlFile.Save(dest);
  68. }
  69. bool AttributeAnimation::LoadXML(const XMLElement& source)
  70. {
  71. valueType_ = VAR_NONE;
  72. eventFrames_.Clear();
  73. String cycleModeString = source.GetAttribute("cycleMode");
  74. cycleMode_ = CM_LOOP;
  75. for (int i = 0; i <= CM_PINGPONG; ++i)
  76. {
  77. if (cycleModeString == cycleModeNames[i])
  78. {
  79. cycleMode_ = (CycleMode)i;
  80. break;
  81. }
  82. }
  83. SetValueType(Variant::GetTypeFromName(source.GetAttribute("valueType")));
  84. XMLElement keyFrameEleme = source.GetChild("keyFrame");
  85. while (keyFrameEleme)
  86. {
  87. float time = keyFrameEleme.GetFloat("time");
  88. Variant value(valueType_, keyFrameEleme.GetAttribute("value"));
  89. SetKeyFrame(time, value);
  90. keyFrameEleme = keyFrameEleme.GetNext("keyFrame");
  91. }
  92. XMLElement eventFrameElem = source.GetChild("eventFrame");
  93. while (eventFrameElem)
  94. {
  95. float time = eventFrameElem.GetFloat("time");
  96. unsigned eventType = eventFrameElem.GetUInt("eventType");
  97. VariantMap eventData = eventFrameElem.GetChild("eventData").GetVariantMap();
  98. SetEventFrame(time, StringHash(eventType), eventData);
  99. eventFrameElem = eventFrameElem.GetNext("eventFrame");
  100. }
  101. return true;
  102. }
  103. bool AttributeAnimation::SaveXML(XMLElement& dest) const
  104. {
  105. dest.SetAttribute("cycleMode", cycleModeNames[cycleMode_]);
  106. dest.SetAttribute("valueType", Variant::GetTypeName(valueType_));
  107. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  108. {
  109. const AttributeKeyFrame& keyFrame = keyFrames_[i];
  110. XMLElement keyFrameEleme = dest.CreateChild("keyFrame");
  111. keyFrameEleme.SetFloat("time", keyFrame.time_);
  112. keyFrameEleme.SetAttribute("value", keyFrame.value_.ToString());
  113. }
  114. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  115. {
  116. const AttributeEventFrame& eventFrame = eventFrames_[i];
  117. XMLElement eventFrameElem = dest.CreateChild("eventFrame");
  118. eventFrameElem.SetFloat("time", eventFrame.time_);
  119. eventFrameElem.SetUInt("eventType", eventFrame.eventType_.Value());
  120. eventFrameElem.CreateChild("eventData").SetVariantMap(eventFrame.eventData_);
  121. }
  122. return true;
  123. }
  124. void AttributeAnimation::SetObjectAnimation(ObjectAnimation* objectAnimation)
  125. {
  126. objectAnimation_ = objectAnimation;
  127. }
  128. void AttributeAnimation::SetCycleMode(CycleMode cycleMode)
  129. {
  130. cycleMode_ = cycleMode;
  131. }
  132. void AttributeAnimation::SetValueType(VariantType valueType)
  133. {
  134. if (valueType == valueType_)
  135. return;
  136. valueType_ = valueType;
  137. isInterpolatable_ = (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) ||
  138. (valueType_ == VAR_VECTOR4) || (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
  139. keyFrames_.Clear();
  140. beginTime_ = M_INFINITY;
  141. endTime_ = -M_INFINITY;
  142. }
  143. bool AttributeAnimation::SetKeyFrame(float time, const Variant& value)
  144. {
  145. if (valueType_ == VAR_NONE)
  146. SetValueType(value.GetType());
  147. else if (value.GetType() != valueType_)
  148. return false;
  149. beginTime_ = Min(time, beginTime_);
  150. endTime_ = Max(time, endTime_);
  151. AttributeKeyFrame keyFrame;
  152. keyFrame.time_ = time;
  153. keyFrame.value_ = value;
  154. if (keyFrames_.Empty() || time >= keyFrames_.Back().time_)
  155. keyFrames_.Push(keyFrame);
  156. else
  157. {
  158. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  159. {
  160. if (time < keyFrames_[i].time_)
  161. {
  162. keyFrames_.Insert(i, keyFrame);
  163. break;
  164. }
  165. }
  166. }
  167. return true;
  168. }
  169. void AttributeAnimation::SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData)
  170. {
  171. AttributeEventFrame eventFrame;
  172. eventFrame.time_ = time;
  173. eventFrame.eventType_ = eventType;
  174. eventFrame.eventData_ = eventData;
  175. if (eventFrames_.Empty() || time >= eventFrames_.Back().time_)
  176. eventFrames_.Push(eventFrame);
  177. else
  178. {
  179. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  180. {
  181. if (time < eventFrames_[i].time_)
  182. {
  183. eventFrames_.Insert(i, eventFrame);
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. ObjectAnimation* AttributeAnimation::GetObjectAnimation() const
  190. {
  191. return objectAnimation_;
  192. }
  193. float AttributeAnimation::CalculateScaledTime(float currentTime) const
  194. {
  195. switch (cycleMode_)
  196. {
  197. case CM_LOOP:
  198. {
  199. float span = endTime_ - beginTime_;
  200. return beginTime_ + fmodf(currentTime - beginTime_, span);
  201. }
  202. case CM_CLAMP:
  203. return Clamp(currentTime, beginTime_, endTime_);
  204. case CM_PINGPONG:
  205. {
  206. float span = endTime_ - beginTime_;
  207. float doubleSpan = span * 2.0f;
  208. float fract = fmodf(currentTime - beginTime_, doubleSpan);
  209. return (fract < span) ? beginTime_ + fract : beginTime_ + doubleSpan - fract;
  210. }
  211. break;
  212. }
  213. return beginTime_;
  214. }
  215. void AttributeAnimation::GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const
  216. {
  217. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  218. {
  219. const AttributeEventFrame& eventFrame = eventFrames_[i];
  220. if (eventFrame.time_ >= endTime)
  221. break;
  222. if (eventFrame.time_ >= beginTime)
  223. eventFrames.Push(&eventFrame);
  224. }
  225. }
  226. }