AnimationSet2D.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "Animation2D.h"
  24. #include "AnimationSet2D.h"
  25. #include "Context.h"
  26. #include "FileSystem.h"
  27. #include "Log.h"
  28. #include "ResourceCache.h"
  29. #include "Sprite2D.h"
  30. #include "SpriteSheet2D.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. AnimationSet2D::AnimationSet2D(Context* context) :
  36. Resource(context)
  37. {
  38. }
  39. AnimationSet2D::~AnimationSet2D()
  40. {
  41. }
  42. void AnimationSet2D::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<AnimationSet2D>();
  45. }
  46. bool AnimationSet2D::BeginLoad(Deserializer& source)
  47. {
  48. loadXMLFile_ = new XMLFile(context_);
  49. if (!loadXMLFile_->Load(source))
  50. {
  51. LOGERROR("Load XML failed " + source.GetName());
  52. loadXMLFile_.Reset();
  53. return false;
  54. }
  55. XMLElement rootElem = loadXMLFile_->GetRoot("spriter_data");
  56. if (!rootElem)
  57. {
  58. LOGERROR("Invalid spriter file " + source.GetName());
  59. loadXMLFile_.Reset();
  60. return false;
  61. }
  62. // When async loading, preprocess folders for spritesheet / sprite files and request them for background loading
  63. if (GetAsyncLoadState() == ASYNC_LOADING)
  64. {
  65. if (!LoadFolders(rootElem))
  66. {
  67. loadXMLFile_.Reset();
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. bool AnimationSet2D::EndLoad()
  74. {
  75. // Actually load the folders and animations now
  76. if (!loadXMLFile_)
  77. return false;
  78. XMLElement rootElem = loadXMLFile_->GetRoot("spriter_data");
  79. if (!LoadFolders(rootElem))
  80. {
  81. loadXMLFile_.Reset();
  82. return false;
  83. }
  84. XMLElement entityElem = rootElem.GetChild("entity");
  85. if (!entityElem)
  86. {
  87. LOGERROR("Could not find entity");
  88. loadXMLFile_.Reset();
  89. return false;
  90. }
  91. for (XMLElement animationElem = entityElem.GetChild("animation"); animationElem; animationElem = animationElem.GetNext("animation"))
  92. {
  93. if (!LoadAnimation(animationElem))
  94. {
  95. loadXMLFile_.Reset();
  96. return false;
  97. }
  98. }
  99. loadXMLFile_.Reset();
  100. return true;
  101. }
  102. unsigned AnimationSet2D::GetNumAnimations() const
  103. {
  104. return animations_.Size();
  105. }
  106. Animation2D* AnimationSet2D::GetAnimation(unsigned index) const
  107. {
  108. if (index < animations_.Size())
  109. return animations_[index];
  110. return 0;
  111. }
  112. Animation2D* AnimationSet2D::GetAnimation(const String& name) const
  113. {
  114. for (unsigned i = 0; i < animations_.Size(); ++i)
  115. {
  116. if (animations_[i]->GetName() == name)
  117. return animations_[i];
  118. }
  119. return 0;
  120. }
  121. bool AnimationSet2D::LoadFolders(const XMLElement& rootElem)
  122. {
  123. ResourceCache* cache = GetSubsystem<ResourceCache>();
  124. bool async = GetAsyncLoadState() == ASYNC_LOADING;
  125. String parentPath = GetParentPath(GetName());
  126. String spriteSheetFilePath = parentPath + GetFileName(GetName()) + ".xml";
  127. SpriteSheet2D* spriteSheet = 0;
  128. bool hasSpriteSheet = false;
  129. // When async loading, request the sprite sheet for background loading but do not actually get it
  130. if (!async)
  131. spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetFilePath, false);
  132. else
  133. {
  134. hasSpriteSheet = cache->Exists(spriteSheetFilePath);
  135. if (hasSpriteSheet)
  136. cache->BackgroundLoadResource<SpriteSheet2D>(spriteSheetFilePath, false, this);
  137. }
  138. for (XMLElement folderElem = rootElem.GetChild("folder"); folderElem; folderElem = folderElem.GetNext("folder"))
  139. {
  140. unsigned folderId = folderElem.GetUInt("id");
  141. for (XMLElement fileElem = folderElem.GetChild("file"); fileElem; fileElem = fileElem.GetNext("file"))
  142. {
  143. unsigned fileId = fileElem.GetUInt("id");
  144. String fileName = fileElem.GetAttribute("name");
  145. // When async loading, request the sprites for background loading but do not actually get them
  146. if (!async)
  147. {
  148. SharedPtr<Sprite2D> sprite;
  149. if (spriteSheet)
  150. sprite = spriteSheet->GetSprite(GetFileName(fileName));
  151. else
  152. sprite = (cache->GetResource<Sprite2D>(parentPath + fileName));
  153. if (!sprite)
  154. {
  155. LOGERROR("Could not load sprite " + fileName);
  156. return false;
  157. }
  158. Vector2 hotSpot(0.0f, 1.0f);
  159. if (fileElem.HasAttribute("pivot_x"))
  160. hotSpot.x_ = fileElem.GetFloat("pivot_x");
  161. if (fileElem.HasAttribute("pivot_y"))
  162. hotSpot.y_ = fileElem.GetFloat("pivot_y");
  163. // If sprite is trimmed, recalculate hot spot
  164. const IntVector2& offset = sprite->GetOffset();
  165. if (offset != IntVector2::ZERO)
  166. {
  167. int width = fileElem.GetInt("width");
  168. int height = fileElem.GetInt("height");
  169. float pivotX = width * hotSpot.x_;
  170. float pivotY = height * (1.0f - hotSpot.y_);
  171. const IntRect& rectangle = sprite->GetRectangle();
  172. hotSpot.x_ = (offset.x_ + pivotX) / rectangle.Width();
  173. hotSpot.y_ = 1.0f - (offset.y_ + pivotY) / rectangle.Height();
  174. }
  175. sprite->SetHotSpot(hotSpot);
  176. sprites_[(folderId << 16) + fileId] = sprite;
  177. }
  178. else if (!hasSpriteSheet)
  179. cache->BackgroundLoadResource<Sprite2D>(parentPath + fileName, true, this);
  180. }
  181. }
  182. return true;
  183. }
  184. Sprite2D* AnimationSet2D::GetSprite(unsigned folderId, unsigned fileId) const
  185. {
  186. unsigned key = (folderId << 16) + fileId;
  187. HashMap<unsigned, SharedPtr<Sprite2D> >::ConstIterator i = sprites_.Find(key);
  188. if (i != sprites_.End())
  189. return i->second_;
  190. return 0;
  191. }
  192. bool AnimationSet2D::LoadAnimation(const XMLElement& animationElem)
  193. {
  194. SharedPtr<Animation2D> animation(new Animation2D(this));
  195. String name = animationElem.GetAttribute("name");
  196. animation->SetName(name);
  197. float length = animationElem.GetFloat("length") * 0.001f;
  198. animation->SetLength(length);
  199. bool looped = true;
  200. if (animationElem.HasAttribute("looping"))
  201. looped = animationElem.GetBool("looping");
  202. animation->SetLooped(looped);
  203. // Load timelines
  204. for (XMLElement timelineElem = animationElem.GetChild("timeline"); timelineElem; timelineElem = timelineElem.GetNext("timeline"))
  205. {
  206. Timeline2D timeline;
  207. timeline.name_ = timelineElem.GetAttribute("name");
  208. if (timelineElem.GetAttribute("object_type") == "bone")
  209. timeline.type_ = OT_BONE;
  210. else
  211. timeline.type_ = OT_SPRITE;
  212. for (XMLElement keyElem = timelineElem.GetChild("key"); keyElem; keyElem = keyElem.GetNext("key"))
  213. {
  214. TimelineKey2D key;
  215. key.time_ = keyElem.GetFloat("time") * 0.001f;
  216. key.spin_ = 1;
  217. if (keyElem.HasAttribute("spin"))
  218. key.spin_ = keyElem.GetInt("spin");
  219. XMLElement childElem = keyElem.GetChild();
  220. Vector2 position;
  221. position.x_ = childElem.GetFloat("x");
  222. position.y_ = childElem.GetFloat("y");
  223. float angle = childElem.GetFloat("angle");
  224. Vector2 scale(Vector2::ONE);
  225. if (childElem.HasAttribute("scale_x"))
  226. scale.x_ = childElem.GetFloat("scale_x");
  227. if (childElem.HasAttribute("scale_y"))
  228. scale.y_ = childElem.GetFloat("scale_y");
  229. key.transform_ = Transform2D(position, angle, scale);
  230. if (timeline.type_ == OT_SPRITE)
  231. {
  232. int folder = childElem.GetUInt("folder");
  233. int file = childElem.GetUInt("file");
  234. key.sprite_ = GetSprite(folder, file);
  235. if (!key.sprite_)
  236. {
  237. LOGERROR("Could not find sprite");
  238. return false;
  239. }
  240. if (childElem.HasAttribute("pivot_x"))
  241. key.hotSpot_.x_ = childElem.GetFloat("pivot_x");
  242. else
  243. key.hotSpot_.x_ = key.sprite_->GetHotSpot().x_;
  244. if (childElem.HasAttribute("pivot_y"))
  245. key.hotSpot_.y_ = childElem.GetFloat("pivot_y");
  246. else
  247. key.hotSpot_.y_ = key.sprite_->GetHotSpot().y_;
  248. if (childElem.HasAttribute("a"))
  249. key.alpha_ = childElem.GetFloat("a");
  250. }
  251. timeline.timelineKeys_.Push(key);
  252. }
  253. // Add end key for looped animation
  254. if (looped && timeline.timelineKeys_.Back().time_ != length)
  255. {
  256. TimelineKey2D key = timeline.timelineKeys_.Front();
  257. key.time_ = length;
  258. timeline.timelineKeys_.Push(key);
  259. }
  260. animation->AddTimeline(timeline);
  261. }
  262. // Load main line
  263. XMLElement mainlineElem = animationElem.GetChild("mainline");
  264. for (XMLElement keyElem = mainlineElem.GetChild("key"); keyElem; keyElem = keyElem.GetNext("key"))
  265. {
  266. MainlineKey2D mainlineKey;
  267. int id = keyElem.GetInt("id");
  268. mainlineKey.time_ = keyElem.GetFloat("time") * 0.001f;
  269. for (XMLElement refElem = keyElem.GetChild(); refElem; refElem = refElem.GetNext())
  270. {
  271. Reference2D ref;
  272. int refId = refElem.GetInt("id");
  273. if (refElem.GetName() == "bone_ref")
  274. ref.type_ = OT_BONE;
  275. else
  276. ref.type_ = OT_SPRITE;
  277. ref.timeline_ = refElem.GetInt("timeline");
  278. if (refElem.HasAttribute("parent"))
  279. {
  280. int parent = refElem.GetInt("parent");
  281. int parentTimeline = mainlineKey.references_[parent].timeline_;
  282. animation->SetTimelineParent(ref.timeline_, parentTimeline);
  283. }
  284. if (refElem.GetName() == "object_ref")
  285. ref.zIndex_ = refElem.GetInt("z_index");
  286. mainlineKey.references_.Push(ref);
  287. }
  288. animation->AddMainlineKey(mainlineKey);
  289. }
  290. animations_.Push(animation);
  291. return true;
  292. }
  293. }