Animation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "../Precompiled.h"
  23. #include "../Container/Sort.h"
  24. #include "../Core/Context.h"
  25. #include "../Core/Profiler.h"
  26. #include "../Graphics/Animation.h"
  27. #include "../IO/Deserializer.h"
  28. #include "../IO/FileSystem.h"
  29. #include "../IO/Log.h"
  30. #include "../IO/Serializer.h"
  31. #include "../Resource/ResourceCache.h"
  32. #include "../Resource/XMLFile.h"
  33. #include "../DebugNew.h"
  34. namespace Urho3D
  35. {
  36. inline bool CompareTriggers(AnimationTriggerPoint& lhs, AnimationTriggerPoint& rhs)
  37. {
  38. return lhs.time_ < rhs.time_;
  39. }
  40. inline bool CompareKeyFrames(AnimationKeyFrame& lhs, AnimationKeyFrame& rhs)
  41. {
  42. return lhs.time_ < rhs.time_;
  43. }
  44. void AnimationTrack::SetKeyFrame(unsigned index, const AnimationKeyFrame& keyFrame)
  45. {
  46. if (index < keyFrames_.Size())
  47. {
  48. keyFrames_[index] = keyFrame;
  49. Urho3D::Sort(keyFrames_.Begin(), keyFrames_.End(), CompareKeyFrames);
  50. }
  51. else if (index == keyFrames_.Size())
  52. AddKeyFrame(keyFrame);
  53. }
  54. void AnimationTrack::AddKeyFrame(const AnimationKeyFrame& keyFrame)
  55. {
  56. bool needSort = keyFrames_.Size() ? keyFrames_.Back().time_ > keyFrame.time_ : false;
  57. keyFrames_.Push(keyFrame);
  58. if (needSort)
  59. Urho3D::Sort(keyFrames_.Begin(), keyFrames_.End(), CompareKeyFrames);
  60. }
  61. void AnimationTrack::InsertKeyFrame(unsigned index, const AnimationKeyFrame& keyFrame)
  62. {
  63. keyFrames_.Insert(index, keyFrame);
  64. Urho3D::Sort(keyFrames_.Begin(), keyFrames_.End(), CompareKeyFrames);
  65. }
  66. void AnimationTrack::RemoveKeyFrame(unsigned index)
  67. {
  68. keyFrames_.Erase(index);
  69. }
  70. void AnimationTrack::RemoveAllKeyFrames()
  71. {
  72. keyFrames_.Clear();
  73. }
  74. AnimationKeyFrame* AnimationTrack::GetKeyFrame(unsigned index)
  75. {
  76. return index < keyFrames_.Size() ? &keyFrames_[index] : (AnimationKeyFrame*)0;
  77. }
  78. void AnimationTrack::GetKeyFrameIndex(float time, unsigned& index) const
  79. {
  80. if (time < 0.0f)
  81. time = 0.0f;
  82. if (index >= keyFrames_.Size())
  83. index = keyFrames_.Size() - 1;
  84. // Check for being too far ahead
  85. while (index && time < keyFrames_[index].time_)
  86. --index;
  87. // Check for being too far behind
  88. while (index < keyFrames_.Size() - 1 && time >= keyFrames_[index + 1].time_)
  89. ++index;
  90. }
  91. Animation::Animation(Context* context) :
  92. Resource(context),
  93. length_(0.f)
  94. {
  95. }
  96. Animation::~Animation()
  97. {
  98. }
  99. void Animation::RegisterObject(Context* context)
  100. {
  101. context->RegisterFactory<Animation>();
  102. }
  103. bool Animation::BeginLoad(Deserializer& source)
  104. {
  105. unsigned memoryUse = sizeof(Animation);
  106. // Check ID
  107. if (source.ReadFileID() != "UANI")
  108. {
  109. URHO3D_LOGERROR(source.GetName() + " is not a valid animation file");
  110. return false;
  111. }
  112. // Read name and length
  113. animationName_ = source.ReadString();
  114. animationNameHash_ = animationName_;
  115. length_ = source.ReadFloat();
  116. tracks_.Clear();
  117. unsigned tracks = source.ReadUInt();
  118. memoryUse += tracks * sizeof(AnimationTrack);
  119. // Read tracks
  120. for (unsigned i = 0; i < tracks; ++i)
  121. {
  122. AnimationTrack* newTrack = CreateTrack(source.ReadString());
  123. newTrack->channelMask_ = source.ReadUByte();
  124. unsigned keyFrames = source.ReadUInt();
  125. newTrack->keyFrames_.Resize(keyFrames);
  126. memoryUse += keyFrames * sizeof(AnimationKeyFrame);
  127. // Read keyframes of the track
  128. for (unsigned j = 0; j < keyFrames; ++j)
  129. {
  130. AnimationKeyFrame& newKeyFrame = newTrack->keyFrames_[j];
  131. newKeyFrame.time_ = source.ReadFloat();
  132. if (newTrack->channelMask_ & CHANNEL_POSITION)
  133. newKeyFrame.position_ = source.ReadVector3();
  134. if (newTrack->channelMask_ & CHANNEL_ROTATION)
  135. newKeyFrame.rotation_ = source.ReadQuaternion();
  136. if (newTrack->channelMask_ & CHANNEL_SCALE)
  137. newKeyFrame.scale_ = source.ReadVector3();
  138. }
  139. }
  140. // Optionally read triggers from an XML file
  141. ResourceCache* cache = GetSubsystem<ResourceCache>();
  142. String xmlName = ReplaceExtension(GetName(), ".xml");
  143. SharedPtr<XMLFile> file(cache->GetTempResource<XMLFile>(xmlName, false));
  144. if (file)
  145. {
  146. XMLElement rootElem = file->GetRoot();
  147. XMLElement triggerElem = rootElem.GetChild("trigger");
  148. while (triggerElem)
  149. {
  150. if (triggerElem.HasAttribute("normalizedtime"))
  151. AddTrigger(triggerElem.GetFloat("normalizedtime"), true, triggerElem.GetVariant());
  152. else if (triggerElem.HasAttribute("time"))
  153. AddTrigger(triggerElem.GetFloat("time"), false, triggerElem.GetVariant());
  154. triggerElem = triggerElem.GetNext("trigger");
  155. }
  156. memoryUse += triggers_.Size() * sizeof(AnimationTriggerPoint);
  157. }
  158. SetMemoryUse(memoryUse);
  159. return true;
  160. }
  161. bool Animation::Save(Serializer& dest) const
  162. {
  163. // Write ID, name and length
  164. dest.WriteFileID("UANI");
  165. dest.WriteString(animationName_);
  166. dest.WriteFloat(length_);
  167. // Write tracks
  168. dest.WriteUInt(tracks_.Size());
  169. for (HashMap<StringHash, AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  170. {
  171. const AnimationTrack& track = i->second_;
  172. dest.WriteString(track.name_);
  173. dest.WriteUByte(track.channelMask_);
  174. dest.WriteUInt(track.keyFrames_.Size());
  175. // Write keyframes of the track
  176. for (unsigned j = 0; j < track.keyFrames_.Size(); ++j)
  177. {
  178. const AnimationKeyFrame& keyFrame = track.keyFrames_[j];
  179. dest.WriteFloat(keyFrame.time_);
  180. if (track.channelMask_ & CHANNEL_POSITION)
  181. dest.WriteVector3(keyFrame.position_);
  182. if (track.channelMask_ & CHANNEL_ROTATION)
  183. dest.WriteQuaternion(keyFrame.rotation_);
  184. if (track.channelMask_ & CHANNEL_SCALE)
  185. dest.WriteVector3(keyFrame.scale_);
  186. }
  187. }
  188. // If triggers have been defined, write an XML file for them
  189. if (triggers_.Size())
  190. {
  191. File* destFile = dynamic_cast<File*>(&dest);
  192. if (destFile)
  193. {
  194. String xmlName = ReplaceExtension(destFile->GetName(), ".xml");
  195. SharedPtr<XMLFile> xml(new XMLFile(context_));
  196. XMLElement rootElem = xml->CreateRoot("animation");
  197. for (unsigned i = 0; i < triggers_.Size(); ++i)
  198. {
  199. XMLElement triggerElem = rootElem.CreateChild("trigger");
  200. triggerElem.SetFloat("time", triggers_[i].time_);
  201. triggerElem.SetVariant(triggers_[i].data_);
  202. }
  203. File xmlFile(context_, xmlName, FILE_WRITE);
  204. xml->Save(xmlFile);
  205. }
  206. else
  207. URHO3D_LOGWARNING("Can not save animation trigger data when not saving into a file");
  208. }
  209. return true;
  210. }
  211. void Animation::SetAnimationName(const String& name)
  212. {
  213. animationName_ = name;
  214. animationNameHash_ = StringHash(name);
  215. }
  216. void Animation::SetLength(float length)
  217. {
  218. length_ = Max(length, 0.0f);
  219. }
  220. AnimationTrack* Animation::CreateTrack(const String& name)
  221. {
  222. /// \todo When tracks / keyframes are created dynamically, memory use is not updated
  223. StringHash nameHash(name);
  224. AnimationTrack* oldTrack = GetTrack(nameHash);
  225. if (oldTrack)
  226. return oldTrack;
  227. AnimationTrack& newTrack = tracks_[nameHash];
  228. newTrack.name_ = name;
  229. newTrack.nameHash_ = nameHash;
  230. return &newTrack;
  231. }
  232. bool Animation::RemoveTrack(const String& name)
  233. {
  234. HashMap<StringHash, AnimationTrack>::Iterator i = tracks_.Find(StringHash(name));
  235. if (i != tracks_.End())
  236. {
  237. tracks_.Erase(i);
  238. return true;
  239. }
  240. else
  241. return false;
  242. }
  243. void Animation::RemoveAllTracks()
  244. {
  245. tracks_.Clear();
  246. }
  247. void Animation::SetTrigger(unsigned index, const AnimationTriggerPoint& trigger)
  248. {
  249. if (index == triggers_.Size())
  250. AddTrigger(trigger);
  251. else if (index < triggers_.Size())
  252. {
  253. triggers_[index] = trigger;
  254. Sort(triggers_.Begin(), triggers_.End(), CompareTriggers);
  255. }
  256. }
  257. void Animation::AddTrigger(const AnimationTriggerPoint& trigger)
  258. {
  259. triggers_.Push(trigger);
  260. Sort(triggers_.Begin(), triggers_.End(), CompareTriggers);
  261. }
  262. void Animation::AddTrigger(float time, bool timeIsNormalized, const Variant& data)
  263. {
  264. AnimationTriggerPoint newTrigger;
  265. newTrigger.time_ = timeIsNormalized ? time * length_ : time;
  266. newTrigger.data_ = data;
  267. triggers_.Push(newTrigger);
  268. Sort(triggers_.Begin(), triggers_.End(), CompareTriggers);
  269. }
  270. void Animation::RemoveTrigger(unsigned index)
  271. {
  272. if (index < triggers_.Size())
  273. triggers_.Erase(index);
  274. }
  275. void Animation::RemoveAllTriggers()
  276. {
  277. triggers_.Clear();
  278. }
  279. void Animation::SetNumTriggers(unsigned num)
  280. {
  281. triggers_.Resize(num);
  282. }
  283. AnimationTrack* Animation::GetTrack(const String& name)
  284. {
  285. HashMap<StringHash, AnimationTrack>::Iterator i = tracks_.Find(StringHash(name));
  286. return i != tracks_.End() ? &i->second_ : (AnimationTrack*)0;
  287. }
  288. AnimationTrack* Animation::GetTrack(StringHash nameHash)
  289. {
  290. HashMap<StringHash, AnimationTrack>::Iterator i = tracks_.Find(nameHash);
  291. return i != tracks_.End() ? &i->second_ : (AnimationTrack*)0;
  292. }
  293. AnimationTriggerPoint* Animation::GetTrigger(unsigned index)
  294. {
  295. return index < triggers_.Size() ? &triggers_[index] : (AnimationTriggerPoint*)0;
  296. }
  297. }