AnimationSet2D.cpp 12 KB

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