Animation.cpp 8.4 KB

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