Animation.cpp 8.4 KB

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