Animation.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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::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. XMLFile* file = cache->GetResource<XMLFile>(xmlName, false);
  109. if (file)
  110. {
  111. XMLElement rootElem = file->GetRoot();
  112. XMLElement triggerElem = rootElem.GetChild("trigger");
  113. while (triggerElem)
  114. {
  115. if (triggerElem.HasAttribute("normalizedtime"))
  116. AddTrigger(triggerElem.GetFloat("normalizedtime"), true, triggerElem.GetVariant());
  117. else if (triggerElem.HasAttribute("time"))
  118. AddTrigger(triggerElem.GetFloat("time"), false, triggerElem.GetVariant());
  119. triggerElem = triggerElem.GetNext("trigger");
  120. }
  121. memoryUse += triggers_.Size() * sizeof(AnimationTriggerPoint);
  122. }
  123. SetMemoryUse(memoryUse);
  124. return true;
  125. }
  126. bool Animation::Save(Serializer& dest) const
  127. {
  128. // Write ID, name and length
  129. dest.WriteFileID("UANI");
  130. dest.WriteString(animationName_);
  131. dest.WriteFloat(length_);
  132. // Write tracks
  133. dest.WriteUInt(tracks_.Size());
  134. for (unsigned i = 0; i < tracks_.Size(); ++i)
  135. {
  136. const AnimationTrack& track = tracks_[i];
  137. dest.WriteString(track.name_);
  138. dest.WriteUByte(track.channelMask_);
  139. dest.WriteUInt(track.keyFrames_.Size());
  140. // Write keyframes of the track
  141. for (unsigned j = 0; j < track.keyFrames_.Size(); ++j)
  142. {
  143. const AnimationKeyFrame& keyFrame = track.keyFrames_[j];
  144. dest.WriteFloat(keyFrame.time_);
  145. if (track.channelMask_ & CHANNEL_POSITION)
  146. dest.WriteVector3(keyFrame.position_);
  147. if (track.channelMask_ & CHANNEL_ROTATION)
  148. dest.WriteQuaternion(keyFrame.rotation_);
  149. if (track.channelMask_ & CHANNEL_SCALE)
  150. dest.WriteVector3(keyFrame.scale_);
  151. }
  152. }
  153. // If triggers have been defined, write an XML file for them
  154. if (triggers_.Size())
  155. {
  156. File* destFile = dynamic_cast<File*>(&dest);
  157. if (destFile)
  158. {
  159. String xmlName = ReplaceExtension(destFile->GetName(), ".xml");
  160. SharedPtr<XMLFile> xml(new XMLFile(context_));
  161. XMLElement rootElem = xml->CreateRoot("animation");
  162. for (unsigned i = 0; i < triggers_.Size(); ++i)
  163. {
  164. XMLElement triggerElem = rootElem.CreateChild("trigger");
  165. triggerElem.SetFloat("time", triggers_[i].time_);
  166. triggerElem.SetVariant(triggers_[i].data_);
  167. }
  168. File xmlFile(context_, xmlName, FILE_WRITE);
  169. xml->Save(xmlFile);
  170. }
  171. else
  172. LOGWARNING("Can not save animation trigger data when not saving into a file");
  173. }
  174. return true;
  175. }
  176. void Animation::SetAnimationName(const String& name)
  177. {
  178. animationName_ = name;
  179. animationNameHash_ = StringHash(name);
  180. }
  181. void Animation::SetLength(float length)
  182. {
  183. length_ = Max(length, 0.0f);
  184. }
  185. void Animation::SetTracks(const Vector<AnimationTrack>& tracks)
  186. {
  187. tracks_ = tracks;
  188. }
  189. void Animation::AddTrigger(float time, bool timeIsNormalized, const Variant& data)
  190. {
  191. AnimationTriggerPoint newTrigger;
  192. newTrigger.time_ = timeIsNormalized ? time * length_ : time;
  193. newTrigger.data_ = data;
  194. triggers_.Push(newTrigger);
  195. Sort(triggers_.Begin(), triggers_.End(), CompareTriggers);
  196. }
  197. void Animation::RemoveTrigger(unsigned index)
  198. {
  199. if (index < triggers_.Size())
  200. triggers_.Erase(index);
  201. }
  202. void Animation::RemoveAllTriggers()
  203. {
  204. triggers_.Clear();
  205. }
  206. void Animation::SetNumTriggers(unsigned num)
  207. {
  208. triggers_.Resize(num);
  209. }
  210. const AnimationTrack* Animation::GetTrack(unsigned index) const
  211. {
  212. return index < tracks_.Size() ? &tracks_[index] : 0;
  213. }
  214. const AnimationTrack* Animation::GetTrack(const String& name) const
  215. {
  216. for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  217. {
  218. if (i->name_ == name)
  219. return &(*i);
  220. }
  221. return 0;
  222. }
  223. const AnimationTrack* Animation::GetTrack(StringHash nameHash) const
  224. {
  225. for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  226. {
  227. if (i->nameHash_ == nameHash)
  228. return &(*i);
  229. }
  230. return 0;
  231. }
  232. }