AnimationSet2D.cpp 9.9 KB

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