XAnimationSet2D.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "Context.h"
  24. #include "Drawable2D.h"
  25. #include "FileSystem.h"
  26. #include "Log.h"
  27. #include "ResourceCache.h"
  28. #include "Sprite2D.h"
  29. #include "XAnimation2D.h"
  30. #include "XAnimationSet2D.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. XAnimationSet2D::XAnimationSet2D(Context* context) :
  36. Resource(context)
  37. {
  38. }
  39. XAnimationSet2D::~XAnimationSet2D()
  40. {
  41. }
  42. void XAnimationSet2D::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<XAnimationSet2D>();
  45. }
  46. bool XAnimationSet2D::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 XAnimationSet2D::GetNumAnimations() const
  76. {
  77. return animations_.Size();
  78. }
  79. XAnimation2D* XAnimationSet2D::GetAnimation(unsigned index) const
  80. {
  81. if (index < animations_.Size())
  82. return animations_[index];
  83. return 0;
  84. }
  85. XAnimation2D* XAnimationSet2D::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* XAnimationSet2D::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 XAnimationSet2D::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. sprites_[(folderId << 16) + fileId] = sprite;
  120. }
  121. }
  122. return true;
  123. }
  124. bool XAnimationSet2D::LoadAnimation(const XMLElement& animationElem)
  125. {
  126. SharedPtr<XAnimation2D> animation(new XAnimation2D(this));
  127. String name = animationElem.GetAttribute("name");
  128. animation->SetName(name);
  129. float length = animationElem.GetFloat("length") * 0.001f;
  130. animation->SetLength(length);
  131. bool looped = true;
  132. if (animationElem.HasAttribute("looping"))
  133. looped = animationElem.GetBool("looping");
  134. animation->SetLooped(looped);
  135. // Load main line
  136. XMLElement mainlineElem = animationElem.GetChild("mainline");
  137. for (XMLElement keyElem = mainlineElem.GetChild("key"); keyElem; keyElem = keyElem.GetNext("key"))
  138. {
  139. MainlineKey mainlineKey;
  140. mainlineKey.time_ = keyElem.GetFloat("time") * 0.001f;
  141. // Just support object ref now
  142. for (XMLElement objectRefElem = keyElem.GetChild("object_ref"); objectRefElem; objectRefElem = objectRefElem.GetNext("object_ref"))
  143. {
  144. ObjectRef objectRef;
  145. objectRef.timeline_ = objectRefElem.GetInt("timeline");
  146. objectRef.key_ = objectRefElem.GetInt("key");
  147. objectRef.zIndex_ = objectRefElem.GetInt("z_index");
  148. mainlineKey.objectRefs_.Push(objectRef);
  149. }
  150. animation->AddMainlineKey(mainlineKey);
  151. }
  152. // Load time lines
  153. for (XMLElement timelineElem = animationElem.GetChild("timeline"); timelineElem; timelineElem = timelineElem.GetNext("timeline"))
  154. {
  155. Timeline timeline;
  156. timeline.name_ = timelineElem.GetAttribute("name");
  157. for (XMLElement keyElem = timelineElem.GetChild("key"); keyElem; keyElem = keyElem.GetNext("key"))
  158. {
  159. ObjectKey objectKey;
  160. objectKey.time_ = keyElem.GetFloat("time") * 0.001f;
  161. objectKey.spin_ = 1;
  162. if (keyElem.HasAttribute("spin"))
  163. objectKey.spin_ = keyElem.GetInt("spin");
  164. XMLElement objectElem = keyElem.GetChild("object");
  165. int folder = objectElem.GetUInt("folder");
  166. int file = objectElem.GetUInt("file");
  167. objectKey.sprite_ = GetSprite(folder, file);
  168. if (!objectKey.sprite_)
  169. {
  170. LOGERROR("Could not find sprite");
  171. return false;
  172. }
  173. objectKey.position_.x_ = objectElem.GetFloat("x") * PIXEL_SIZE;
  174. objectKey.position_.y_ = objectElem.GetFloat("y") * PIXEL_SIZE;
  175. if (objectElem.HasAttribute("pivot_x"))
  176. objectKey.pivot_.x_ = objectElem.GetFloat("pivot_x");
  177. if (objectElem.HasAttribute("pivot_y"))
  178. objectKey.pivot_.y_ = objectElem.GetFloat("pivot_y");
  179. if (objectElem.HasAttribute("scale_x"))
  180. objectKey.scale_.x_ = objectElem.GetFloat("scale_x");
  181. if (objectElem.HasAttribute("scale_y"))
  182. objectKey.scale_.y_ = objectElem.GetFloat("scale_y");
  183. objectKey.angle_= objectElem.GetFloat("angle");
  184. if (objectElem.HasAttribute("a"))
  185. objectKey.alpha_ = objectElem.GetFloat("a");
  186. timeline.objectKeys_.Push(objectKey);
  187. }
  188. // Add end key for looped animation
  189. if (looped && timeline.objectKeys_.Back().time_ != length)
  190. {
  191. ObjectKey objectKey = timeline.objectKeys_.Front();
  192. objectKey.time_ = length;
  193. timeline.objectKeys_.Push(objectKey);
  194. }
  195. animation->AddTimeline(timeline);
  196. }
  197. animations_.Push(animation);
  198. return true;
  199. }
  200. }