AttributeAnimation.cpp 6.7 KB

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