AnimationSet2D.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "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. Sprite2D* AnimationSet2D::GetSprite(unsigned folderId, unsigned fileId) const
  95. {
  96. unsigned key = (folderId << 16) + fileId;
  97. HashMap<unsigned, SharedPtr<Sprite2D> >::ConstIterator i = sprites_.Find(key);
  98. if (i != sprites_.End())
  99. return i->second_;
  100. return 0;
  101. }
  102. bool AnimationSet2D::LoadFolders(const XMLElement& rootElem)
  103. {
  104. ResourceCache* cache = GetSubsystem<ResourceCache>();
  105. String parentPath = GetParentPath(GetName());
  106. for (XMLElement folderElem = rootElem.GetChild("folder"); folderElem; folderElem = folderElem.GetNext("folder"))
  107. {
  108. unsigned folderId = folderElem.GetUInt("id");
  109. for (XMLElement fileElem = folderElem.GetChild("file"); fileElem; fileElem = fileElem.GetNext("file"))
  110. {
  111. unsigned fileId = fileElem.GetUInt("id");
  112. String fileName = fileElem.GetAttribute("name");
  113. SharedPtr<Sprite2D> sprite(cache->GetResource<Sprite2D>(parentPath + fileName));
  114. if (!sprite)
  115. {
  116. LOGERROR("Could not load sprite " + parentPath + fileName);
  117. return false;
  118. }
  119. Vector2 hotSpot(0.0f, 1.0f);
  120. if (fileElem.HasAttribute("pivot_x"))
  121. hotSpot.x_ = fileElem.GetFloat("pivot_x");
  122. if (fileElem.HasAttribute("pivot_y"))
  123. hotSpot.y_ = fileElem.GetFloat("pivot_y");
  124. sprite->SetHotSpot(hotSpot);
  125. sprites_[(folderId << 16) + fileId] = sprite;
  126. }
  127. }
  128. return true;
  129. }
  130. bool AnimationSet2D::LoadAnimation(const XMLElement& animationElem)
  131. {
  132. SharedPtr<Animation2D> animation(new Animation2D(this));
  133. String name = animationElem.GetAttribute("name");
  134. animation->SetName(name);
  135. float length = animationElem.GetFloat("length") * 0.001f;
  136. animation->SetLength(length);
  137. bool looped = true;
  138. if (animationElem.HasAttribute("looping"))
  139. looped = animationElem.GetBool("looping");
  140. animation->SetLooped(looped);
  141. // Load main line
  142. XMLElement mainlineElem = animationElem.GetChild("mainline");
  143. for (XMLElement keyElem = mainlineElem.GetChild("key"); keyElem; keyElem = keyElem.GetNext("key"))
  144. {
  145. MainlineKey mainlineKey;
  146. mainlineKey.time_ = keyElem.GetFloat("time") * 0.001f;
  147. for (XMLElement refElem = keyElem.GetChild(); refElem; refElem = refElem.GetNext())
  148. {
  149. ObjectRef ref;
  150. if (refElem.GetName() == "bone_ref")
  151. ref.isBone_ = true;
  152. else
  153. ref.isBone_ = false;
  154. if (refElem.HasAttribute("parent"))
  155. ref.parent_ = refElem.GetInt("parent");
  156. ref.timeline_ = refElem.GetInt("timeline");
  157. ref.key_ = refElem.GetInt("key");
  158. if (refElem.GetName() == "object_ref")
  159. ref.zIndex_ = refElem.GetInt("z_index");
  160. mainlineKey.objectRefs_.Push(ref);
  161. }
  162. animation->AddMainlineKey(mainlineKey);
  163. }
  164. // Load time lines
  165. for (XMLElement timelineElem = animationElem.GetChild("timeline"); timelineElem; timelineElem = timelineElem.GetNext("timeline"))
  166. {
  167. Timeline timeline;
  168. timeline.name_ = timelineElem.GetAttribute("name");
  169. if (timelineElem.GetAttribute("object_type") == "bone")
  170. timeline.isBone_ = true;
  171. else
  172. timeline.isBone_ = false;
  173. for (XMLElement keyElem = timelineElem.GetChild("key"); keyElem; keyElem = keyElem.GetNext("key"))
  174. {
  175. ObjectKey objectKey;
  176. objectKey.time_ = keyElem.GetFloat("time") * 0.001f;
  177. objectKey.spin_ = 1;
  178. if (keyElem.HasAttribute("spin"))
  179. objectKey.spin_ = keyElem.GetInt("spin");
  180. XMLElement childElem = keyElem.GetChild();
  181. objectKey.position_.x_ = childElem.GetFloat("x") * PIXEL_SIZE;
  182. objectKey.position_.y_ = childElem.GetFloat("y") * PIXEL_SIZE;
  183. objectKey.angle_= childElem.GetFloat("angle");
  184. if (childElem.HasAttribute("scale_x"))
  185. objectKey.scale_.x_ = childElem.GetFloat("scale_x");
  186. if (childElem.HasAttribute("scale_y"))
  187. objectKey.scale_.y_ = childElem.GetFloat("scale_y");
  188. if (!timeline.isBone_)
  189. {
  190. int folder = childElem.GetUInt("folder");
  191. int file = childElem.GetUInt("file");
  192. objectKey.sprite_ = GetSprite(folder, file);
  193. if (!objectKey.sprite_)
  194. {
  195. LOGERROR("Could not find sprite");
  196. return false;
  197. }
  198. if (childElem.HasAttribute("pivot_x"))
  199. objectKey.hotSpot_.x_ = childElem.GetFloat("pivot_x");
  200. else
  201. objectKey.hotSpot_.x_ = objectKey.sprite_->GetHotSpot().x_;
  202. if (childElem.HasAttribute("pivot_y"))
  203. objectKey.hotSpot_.y_ = childElem.GetFloat("pivot_y");
  204. else
  205. objectKey.hotSpot_.y_ = objectKey.sprite_->GetHotSpot().y_;
  206. if (childElem.HasAttribute("a"))
  207. objectKey.alpha_ = childElem.GetFloat("a");
  208. }
  209. timeline.objectKeys_.Push(objectKey);
  210. }
  211. // Add end key for looped animation
  212. if (looped && timeline.objectKeys_.Back().time_ != length)
  213. {
  214. ObjectKey objectKey = timeline.objectKeys_.Front();
  215. objectKey.time_ = length;
  216. timeline.objectKeys_.Push(objectKey);
  217. }
  218. animation->AddTimeline(timeline);
  219. }
  220. animations_.Push(animation);
  221. return true;
  222. }
  223. }