AttributeAnimation.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 "Animatable.h"
  24. #include "AttributeAnimation.h"
  25. #include "Context.h"
  26. #include "Deserializer.h"
  27. #include "Log.h"
  28. #include "ObjectAnimation.h"
  29. #include "Serializer.h"
  30. #include "XMLFile.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. AttributeAnimation::AttributeAnimation(Context* context) :
  35. Resource(context),
  36. valueType_(VAR_NONE),
  37. isInterpolatable_(false),
  38. beginTime_(M_INFINITY),
  39. endTime_(-M_INFINITY)
  40. {
  41. }
  42. AttributeAnimation::~AttributeAnimation()
  43. {
  44. }
  45. void AttributeAnimation::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<AttributeAnimation>();
  48. }
  49. bool AttributeAnimation::Load(Deserializer& source)
  50. {
  51. XMLFile xmlFile(context_);
  52. if (!xmlFile.Load(source))
  53. return false;
  54. return LoadXML(xmlFile.GetRoot());
  55. }
  56. bool AttributeAnimation::Save(Serializer& dest) const
  57. {
  58. XMLFile xmlFile(context_);
  59. XMLElement rootElem = xmlFile.CreateRoot("attributeanimation");
  60. if (!SaveXML(rootElem))
  61. return false;
  62. return xmlFile.Save(dest);
  63. }
  64. bool AttributeAnimation::LoadXML(const XMLElement& source)
  65. {
  66. valueType_ = VAR_NONE;
  67. eventFrames_.Clear();
  68. XMLElement keyFrameEleme = source.GetChild("keyframe");
  69. while (keyFrameEleme)
  70. {
  71. float time = keyFrameEleme.GetFloat("time");
  72. Variant value = keyFrameEleme.GetVariant();
  73. SetKeyFrame(time, value);
  74. keyFrameEleme = keyFrameEleme.GetNext("keyframe");
  75. }
  76. XMLElement eventFrameElem = source.GetChild("eventframe");
  77. while (eventFrameElem)
  78. {
  79. float time = eventFrameElem.GetFloat("time");
  80. unsigned eventType = eventFrameElem.GetUInt("eventtype");
  81. VariantMap eventData = eventFrameElem.GetChild("eventdata").GetVariantMap();
  82. SetEventFrame(time, StringHash(eventType), eventData);
  83. eventFrameElem = eventFrameElem.GetNext("eventframe");
  84. }
  85. return true;
  86. }
  87. bool AttributeAnimation::SaveXML(XMLElement& dest) const
  88. {
  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.SetVariant(keyFrame.value_);
  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) || (valueType_ == VAR_VECTOR4) ||
  116. (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR) || (valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2);
  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::UpdateAttributeValue(Animatable* animatable, const AttributeInfo& attributeInfo, float scaledTime) const
  172. {
  173. unsigned index = 1;
  174. for (; index < keyFrames_.Size(); ++index)
  175. {
  176. if (scaledTime < keyFrames_[index].time_)
  177. break;
  178. }
  179. if (index >= keyFrames_.Size() || !isInterpolatable_)
  180. animatable->OnSetAttribute(attributeInfo, keyFrames_[index - 1].value_);
  181. else
  182. animatable->OnSetAttribute(attributeInfo, LinearInterpolation(index - 1, index, scaledTime));
  183. }
  184. void AttributeAnimation::GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const
  185. {
  186. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  187. {
  188. const AttributeEventFrame& eventFrame = eventFrames_[i];
  189. if (eventFrame.time_ >= endTime)
  190. break;
  191. if (eventFrame.time_ >= beginTime)
  192. eventFrames.Push(&eventFrame);
  193. }
  194. }
  195. Variant AttributeAnimation::LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const
  196. {
  197. const AttributeKeyFrame& keyFrame1 = keyFrames_[index1];
  198. const AttributeKeyFrame& keyFrame2 = keyFrames_[index2];
  199. float t = (scaledTime - keyFrame1.time_) / (keyFrame2.time_ - keyFrame1.time_);
  200. switch (valueType_)
  201. {
  202. case VAR_FLOAT:
  203. return Lerp(keyFrame1.value_.GetFloat(), keyFrame2.value_.GetFloat(), t);
  204. case VAR_VECTOR2:
  205. return keyFrame1.value_.GetVector2().Lerp(keyFrame2.value_.GetVector2(), t);
  206. case VAR_VECTOR3:
  207. return keyFrame1.value_.GetVector3().Lerp(keyFrame2.value_.GetVector3(), t);
  208. case VAR_VECTOR4:
  209. return keyFrame1.value_.GetVector4().Lerp(keyFrame2.value_.GetVector4(), t);
  210. case VAR_QUATERNION:
  211. return keyFrame1.value_.GetQuaternion().Slerp(keyFrame2.value_.GetQuaternion(), t);
  212. case VAR_COLOR:
  213. return keyFrame1.value_.GetColor().Lerp(keyFrame2.value_.GetColor(), t);
  214. case VAR_INTRECT:
  215. {
  216. float s = 1.0f - t;
  217. const IntRect& r1 = keyFrame1.value_.GetIntRect();
  218. const IntRect& r2 = keyFrame2.value_.GetIntRect();
  219. return IntRect((int)(r1.left_ * s + r2.left_ * t), (int)(r1.top_ * s + r2.top_ * t), (int)(r1.right_ * s + r2.right_ * t), (int)(r1.bottom_ * s + r2.bottom_ * t));
  220. }
  221. case VAR_INTVECTOR2:
  222. {
  223. float s = 1.0f - t;
  224. const IntVector2& v1 = keyFrame1.value_.GetIntVector2();
  225. const IntVector2& v2 = keyFrame2.value_.GetIntVector2();
  226. return IntVector2((int)(v1.x_ * s + v2.x_ * t), (int)(v1.y_ * s + v2.y_ * t));
  227. }
  228. }
  229. LOGERROR("Invalid value type for linear interpolation");
  230. return Variant::EMPTY;
  231. }
  232. }