AttributeAnimation.cpp 6.9 KB

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