Animation.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "Animation.h"
  24. #include "Context.h"
  25. #include "Deserializer.h"
  26. #include "FileSystem.h"
  27. #include "Log.h"
  28. #include "Profiler.h"
  29. #include "ResourceCache.h"
  30. #include "Serializer.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. inline bool CompareTriggers(AnimationTriggerPoint& lhs, AnimationTriggerPoint& rhs)
  36. {
  37. return lhs.time_ < rhs.time_;
  38. }
  39. void AnimationTrack::GetKeyFrameIndex(float time, unsigned& index) const
  40. {
  41. if (time < 0.0f)
  42. time = 0.0f;
  43. if (index >= keyFrames_.Size())
  44. index = keyFrames_.Size() - 1;
  45. // Check for being too far ahead
  46. while (index && time < keyFrames_[index].time_)
  47. --index;
  48. // Check for being too far behind
  49. while (index < keyFrames_.Size() - 1 && time >= keyFrames_[index + 1].time_)
  50. ++index;
  51. }
  52. Animation::Animation(Context* context) :
  53. Resource(context),
  54. length_(0.f)
  55. {
  56. }
  57. Animation::~Animation()
  58. {
  59. }
  60. void Animation::RegisterObject(Context* context)
  61. {
  62. context->RegisterFactory<Animation>();
  63. }
  64. bool Animation::BeginLoad(Deserializer& source)
  65. {
  66. unsigned memoryUse = sizeof(Animation);
  67. // Check ID
  68. if (source.ReadFileID() != "UANI")
  69. {
  70. LOGERROR(source.GetName() + " is not a valid animation file");
  71. return false;
  72. }
  73. // Read name and length
  74. animationName_ = source.ReadString();
  75. animationNameHash_ = animationName_;
  76. length_ = source.ReadFloat();
  77. tracks_.Clear();
  78. unsigned tracks = source.ReadUInt();
  79. tracks_.Resize(tracks);
  80. memoryUse += tracks * sizeof(AnimationTrack);
  81. // Read tracks
  82. for (unsigned i = 0; i < tracks; ++i)
  83. {
  84. AnimationTrack& newTrack = tracks_[i];
  85. newTrack.name_ = source.ReadString();
  86. newTrack.nameHash_ = newTrack.name_;
  87. newTrack.channelMask_ = source.ReadUByte();
  88. unsigned keyFrames = source.ReadUInt();
  89. newTrack.keyFrames_.Resize(keyFrames);
  90. memoryUse += keyFrames * sizeof(AnimationKeyFrame);
  91. // Read keyframes of the track
  92. for (unsigned j = 0; j < keyFrames; ++j)
  93. {
  94. AnimationKeyFrame& newKeyFrame = newTrack.keyFrames_[j];
  95. newKeyFrame.time_ = source.ReadFloat();
  96. if (newTrack.channelMask_ & CHANNEL_POSITION)
  97. newKeyFrame.position_ = source.ReadVector3();
  98. if (newTrack.channelMask_ & CHANNEL_ROTATION)
  99. newKeyFrame.rotation_ = source.ReadQuaternion();
  100. if (newTrack.channelMask_ & CHANNEL_SCALE)
  101. newKeyFrame.scale_ = source.ReadVector3();
  102. }
  103. }
  104. // Optionally read triggers from an XML file
  105. ResourceCache* cache = GetSubsystem<ResourceCache>();
  106. String xmlName = ReplaceExtension(GetName(), ".xml");
  107. SharedPtr<XMLFile> file(cache->GetTempResource<XMLFile>(xmlName, false));
  108. if (file)
  109. {
  110. XMLElement rootElem = file->GetRoot();
  111. XMLElement triggerElem = rootElem.GetChild("trigger");
  112. while (triggerElem)
  113. {
  114. if (triggerElem.HasAttribute("normalizedtime"))
  115. AddTrigger(triggerElem.GetFloat("normalizedtime"), true, triggerElem.GetVariant());
  116. else if (triggerElem.HasAttribute("time"))
  117. AddTrigger(triggerElem.GetFloat("time"), false, triggerElem.GetVariant());
  118. triggerElem = triggerElem.GetNext("trigger");
  119. }
  120. memoryUse += triggers_.Size() * sizeof(AnimationTriggerPoint);
  121. }
  122. SetMemoryUse(memoryUse);
  123. return true;
  124. }
  125. bool Animation::Save(Serializer& dest) const
  126. {
  127. // Write ID, name and length
  128. dest.WriteFileID("UANI");
  129. dest.WriteString(animationName_);
  130. dest.WriteFloat(length_);
  131. // Write tracks
  132. dest.WriteUInt(tracks_.Size());
  133. for (unsigned i = 0; i < tracks_.Size(); ++i)
  134. {
  135. const AnimationTrack& track = tracks_[i];
  136. dest.WriteString(track.name_);
  137. dest.WriteUByte(track.channelMask_);
  138. dest.WriteUInt(track.keyFrames_.Size());
  139. // Write keyframes of the track
  140. for (unsigned j = 0; j < track.keyFrames_.Size(); ++j)
  141. {
  142. const AnimationKeyFrame& keyFrame = track.keyFrames_[j];
  143. dest.WriteFloat(keyFrame.time_);
  144. if (track.channelMask_ & CHANNEL_POSITION)
  145. dest.WriteVector3(keyFrame.position_);
  146. if (track.channelMask_ & CHANNEL_ROTATION)
  147. dest.WriteQuaternion(keyFrame.rotation_);
  148. if (track.channelMask_ & CHANNEL_SCALE)
  149. dest.WriteVector3(keyFrame.scale_);
  150. }
  151. }
  152. // If triggers have been defined, write an XML file for them
  153. if (triggers_.Size())
  154. {
  155. File* destFile = dynamic_cast<File*>(&dest);
  156. if (destFile)
  157. {
  158. String xmlName = ReplaceExtension(destFile->GetName(), ".xml");
  159. SharedPtr<XMLFile> xml(new XMLFile(context_));
  160. XMLElement rootElem = xml->CreateRoot("animation");
  161. for (unsigned i = 0; i < triggers_.Size(); ++i)
  162. {
  163. XMLElement triggerElem = rootElem.CreateChild("trigger");
  164. triggerElem.SetFloat("time", triggers_[i].time_);
  165. triggerElem.SetVariant(triggers_[i].data_);
  166. }
  167. File xmlFile(context_, xmlName, FILE_WRITE);
  168. xml->Save(xmlFile);
  169. }
  170. else
  171. LOGWARNING("Can not save animation trigger data when not saving into a file");
  172. }
  173. return true;
  174. }
  175. void Animation::SetAnimationName(const String& name)
  176. {
  177. animationName_ = name;
  178. animationNameHash_ = StringHash(name);
  179. }
  180. void Animation::SetLength(float length)
  181. {
  182. length_ = Max(length, 0.0f);
  183. }
  184. void Animation::SetTracks(const Vector<AnimationTrack>& tracks)
  185. {
  186. tracks_ = tracks;
  187. }
  188. void Animation::AddTrigger(float time, bool timeIsNormalized, const Variant& data)
  189. {
  190. AnimationTriggerPoint newTrigger;
  191. newTrigger.time_ = timeIsNormalized ? time * length_ : time;
  192. newTrigger.data_ = data;
  193. triggers_.Push(newTrigger);
  194. Sort(triggers_.Begin(), triggers_.End(), CompareTriggers);
  195. }
  196. void Animation::RemoveTrigger(unsigned index)
  197. {
  198. if (index < triggers_.Size())
  199. triggers_.Erase(index);
  200. }
  201. void Animation::RemoveAllTriggers()
  202. {
  203. triggers_.Clear();
  204. }
  205. void Animation::SetNumTriggers(unsigned num)
  206. {
  207. triggers_.Resize(num);
  208. }
  209. const AnimationTrack* Animation::GetTrack(unsigned index) const
  210. {
  211. return index < tracks_.Size() ? &tracks_[index] : 0;
  212. }
  213. const AnimationTrack* Animation::GetTrack(const String& name) const
  214. {
  215. for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  216. {
  217. if (i->name_ == name)
  218. return &(*i);
  219. }
  220. return 0;
  221. }
  222. const AnimationTrack* Animation::GetTrack(StringHash nameHash) const
  223. {
  224. for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  225. {
  226. if (i->nameHash_ == nameHash)
  227. return &(*i);
  228. }
  229. return 0;
  230. }
  231. }