Animation.cpp 8.5 KB

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