AttributeAnimation.cpp 7.0 KB

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