AnimationSet2D.cpp 10.0 KB

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