Animation.cpp 8.4 KB

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