SpriteSheet2D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // Copyright (c) 2008-2017 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 "../Core/Context.h"
  24. #include "../Graphics/Texture2D.h"
  25. #include "../IO/Deserializer.h"
  26. #include "../IO/FileSystem.h"
  27. #include "../IO/Log.h"
  28. #include "../Resource/PListFile.h"
  29. #include "../Resource/ResourceCache.h"
  30. #include "../Resource/XMLFile.h"
  31. #include "../Resource/JSONFile.h"
  32. #include "../Atomic2D/Sprite2D.h"
  33. #include "../Atomic2D/SpriteSheet2D.h"
  34. #include "../DebugNew.h"
  35. namespace Atomic
  36. {
  37. SpriteSheet2D::SpriteSheet2D(Context* context) :
  38. Resource(context)
  39. {
  40. }
  41. SpriteSheet2D::~SpriteSheet2D()
  42. {
  43. }
  44. void SpriteSheet2D::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<SpriteSheet2D>();
  47. }
  48. bool SpriteSheet2D::BeginLoad(Deserializer& source)
  49. {
  50. if (GetName().Empty())
  51. SetName(source.GetName());
  52. loadTextureName_.Clear();
  53. spriteMapping_.Clear();
  54. String extension = GetExtension(source.GetName());
  55. if (extension == ".plist")
  56. return BeginLoadFromPListFile(source);
  57. if (extension == ".xml")
  58. return BeginLoadFromXMLFile(source);
  59. if (extension == ".json")
  60. return BeginLoadFromJSONFile(source);
  61. ATOMIC_LOGERROR("Unsupported file type");
  62. return false;
  63. }
  64. bool SpriteSheet2D::EndLoad()
  65. {
  66. if (loadPListFile_)
  67. return EndLoadFromPListFile();
  68. if (loadXMLFile_)
  69. return EndLoadFromXMLFile();
  70. if (loadJSONFile_)
  71. return EndLoadFromJSONFile();
  72. return false;
  73. }
  74. void SpriteSheet2D::SetTexture(Texture2D* texture)
  75. {
  76. loadTextureName_.Clear();
  77. texture_ = texture;
  78. }
  79. void SpriteSheet2D::DefineSprite(const String& name, const IntRect& rectangle, const Vector2& hotSpot, const IntVector2& offset)
  80. {
  81. if (!texture_)
  82. return;
  83. if (GetSprite(name))
  84. return;
  85. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  86. sprite->SetName(name);
  87. sprite->SetTexture(texture_);
  88. sprite->SetRectangle(rectangle);
  89. sprite->SetHotSpot(hotSpot);
  90. sprite->SetOffset(offset);
  91. sprite->SetSpriteSheet(this);
  92. spriteMapping_[name] = sprite;
  93. }
  94. Sprite2D* SpriteSheet2D::GetSprite(const String& name) const
  95. {
  96. HashMap<String, SharedPtr<Sprite2D> >::ConstIterator i = spriteMapping_.Find(name);
  97. if (i == spriteMapping_.End())
  98. return 0;
  99. return i->second_;
  100. }
  101. bool SpriteSheet2D::BeginLoadFromPListFile(Deserializer& source)
  102. {
  103. loadPListFile_ = new PListFile(context_);
  104. if (!loadPListFile_->Load(source))
  105. {
  106. ATOMIC_LOGERROR("Could not load sprite sheet");
  107. loadPListFile_.Reset();
  108. return false;
  109. }
  110. SetMemoryUse(source.GetSize());
  111. const PListValueMap& root = loadPListFile_->GetRoot();
  112. const PListValueMap& metadata = root["metadata"]->GetValueMap();
  113. const String& textureFileName = metadata["realTextureFileName"]->GetString();
  114. // If we're async loading, request the texture now. Finish during EndLoad().
  115. loadTextureName_ = GetParentPath(GetName()) + textureFileName;
  116. if (GetAsyncLoadState() == ASYNC_LOADING)
  117. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(loadTextureName_, true, this);
  118. return true;
  119. }
  120. bool SpriteSheet2D::EndLoadFromPListFile()
  121. {
  122. ResourceCache* cache = GetSubsystem<ResourceCache>();
  123. texture_ = cache->GetResource<Texture2D>(loadTextureName_);
  124. if (!texture_)
  125. {
  126. ATOMIC_LOGERROR("Could not load texture " + loadTextureName_);
  127. loadPListFile_.Reset();
  128. loadTextureName_.Clear();
  129. return false;
  130. }
  131. const PListValueMap& root = loadPListFile_->GetRoot();
  132. const PListValueMap& frames = root["frames"]->GetValueMap();
  133. for (PListValueMap::ConstIterator i = frames.Begin(); i != frames.End(); ++i)
  134. {
  135. String name = i->first_.Split('.')[0];
  136. const PListValueMap& frameInfo = i->second_.GetValueMap();
  137. if (frameInfo["rotated"]->GetBool())
  138. {
  139. ATOMIC_LOGWARNING("Rotated sprite is not support now");
  140. continue;
  141. }
  142. IntRect rectangle = frameInfo["frame"]->GetIntRect();
  143. Vector2 hotSpot(0.5f, 0.5f);
  144. IntVector2 offset(0, 0);
  145. IntRect sourceColorRect = frameInfo["sourceColorRect"]->GetIntRect();
  146. if (sourceColorRect.left_ != 0 && sourceColorRect.top_ != 0)
  147. {
  148. offset.x_ = -sourceColorRect.left_;
  149. offset.y_ = -sourceColorRect.top_;
  150. IntVector2 sourceSize = frameInfo["sourceSize"]->GetIntVector2();
  151. hotSpot.x_ = ((float)offset.x_ + sourceSize.x_ / 2) / rectangle.Width();
  152. hotSpot.y_ = 1.0f - ((float)offset.y_ + sourceSize.y_ / 2) / rectangle.Height();
  153. }
  154. DefineSprite(name, rectangle, hotSpot, offset);
  155. }
  156. loadPListFile_.Reset();
  157. loadTextureName_.Clear();
  158. return true;
  159. }
  160. bool SpriteSheet2D::BeginLoadFromXMLFile(Deserializer& source)
  161. {
  162. loadXMLFile_ = new XMLFile(context_);
  163. if (!loadXMLFile_->Load(source))
  164. {
  165. ATOMIC_LOGERROR("Could not load sprite sheet");
  166. loadXMLFile_.Reset();
  167. return false;
  168. }
  169. SetMemoryUse(source.GetSize());
  170. XMLElement rootElem = loadXMLFile_->GetRoot("TextureAtlas");
  171. if (!rootElem)
  172. {
  173. ATOMIC_LOGERROR("Invalid sprite sheet");
  174. loadXMLFile_.Reset();
  175. return false;
  176. }
  177. // If we're async loading, request the texture now. Finish during EndLoad().
  178. loadTextureName_ = GetParentPath(GetName()) + rootElem.GetAttribute("imagePath");
  179. if (GetAsyncLoadState() == ASYNC_LOADING)
  180. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(loadTextureName_, true, this);
  181. return true;
  182. }
  183. bool SpriteSheet2D::EndLoadFromXMLFile()
  184. {
  185. ResourceCache* cache = GetSubsystem<ResourceCache>();
  186. texture_ = cache->GetResource<Texture2D>(loadTextureName_);
  187. if (!texture_)
  188. {
  189. ATOMIC_LOGERROR("Could not load texture " + loadTextureName_);
  190. loadXMLFile_.Reset();
  191. loadTextureName_.Clear();
  192. return false;
  193. }
  194. XMLElement rootElem = loadXMLFile_->GetRoot("TextureAtlas");
  195. XMLElement subTextureElem = rootElem.GetChild("SubTexture");
  196. while (subTextureElem)
  197. {
  198. String name = subTextureElem.GetAttribute("name");
  199. int x = subTextureElem.GetInt("x");
  200. int y = subTextureElem.GetInt("y");
  201. int width = subTextureElem.GetInt("width");
  202. int height = subTextureElem.GetInt("height");
  203. IntRect rectangle(x, y, x + width, y + height);
  204. Vector2 hotSpot(0.5f, 0.5f);
  205. IntVector2 offset(0, 0);
  206. if (subTextureElem.HasAttribute("frameWidth") && subTextureElem.HasAttribute("frameHeight"))
  207. {
  208. offset.x_ = subTextureElem.GetInt("frameX");
  209. offset.y_ = subTextureElem.GetInt("frameY");
  210. int frameWidth = subTextureElem.GetInt("frameWidth");
  211. int frameHeight = subTextureElem.GetInt("frameHeight");
  212. hotSpot.x_ = ((float)offset.x_ + frameWidth / 2) / width;
  213. hotSpot.y_ = 1.0f - ((float)offset.y_ + frameHeight / 2) / height;
  214. }
  215. DefineSprite(name, rectangle, hotSpot, offset);
  216. subTextureElem = subTextureElem.GetNext("SubTexture");
  217. }
  218. loadXMLFile_.Reset();
  219. loadTextureName_.Clear();
  220. return true;
  221. }
  222. bool SpriteSheet2D::BeginLoadFromJSONFile(Deserializer& source)
  223. {
  224. loadJSONFile_ = new JSONFile(context_);
  225. if (!loadJSONFile_->Load(source))
  226. {
  227. ATOMIC_LOGERROR("Could not load sprite sheet");
  228. loadJSONFile_.Reset();
  229. return false;
  230. }
  231. SetMemoryUse(source.GetSize());
  232. JSONValue rootElem = loadJSONFile_->GetRoot();
  233. if (rootElem.IsNull())
  234. {
  235. ATOMIC_LOGERROR("Invalid sprite sheet");
  236. loadJSONFile_.Reset();
  237. return false;
  238. }
  239. // If we're async loading, request the texture now. Finish during EndLoad().
  240. loadTextureName_ = GetParentPath(GetName()) + rootElem.Get("imagePath").GetString();
  241. if (GetAsyncLoadState() == ASYNC_LOADING)
  242. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(loadTextureName_, true, this);
  243. return true;
  244. }
  245. bool SpriteSheet2D::EndLoadFromJSONFile()
  246. {
  247. ResourceCache* cache = GetSubsystem<ResourceCache>();
  248. texture_ = cache->GetResource<Texture2D>(loadTextureName_);
  249. if (!texture_)
  250. {
  251. ATOMIC_LOGERROR("Could not load texture " + loadTextureName_);
  252. loadJSONFile_.Reset();
  253. loadTextureName_.Clear();
  254. return false;
  255. }
  256. JSONValue rootVal = loadJSONFile_->GetRoot();
  257. JSONArray subTextureArray = rootVal.Get("subtextures").GetArray();
  258. for (unsigned i = 0; i < subTextureArray.Size(); i++)
  259. {
  260. const JSONValue& subTextureVal = subTextureArray.At(i);
  261. String name = subTextureVal.Get("name").GetString();
  262. int x = subTextureVal.Get("x").GetInt();
  263. int y = subTextureVal.Get("y").GetInt();
  264. int width = subTextureVal.Get("width").GetInt();
  265. int height = subTextureVal.Get("height").GetInt();
  266. IntRect rectangle(x, y, x + width, y + height);
  267. Vector2 hotSpot(0.5f, 0.5f);
  268. IntVector2 offset(0, 0);
  269. JSONValue frameWidthVal = subTextureVal.Get("frameWidth");
  270. JSONValue frameHeightVal = subTextureVal.Get("frameHeight");
  271. if (!frameWidthVal.IsNull() && !frameHeightVal.IsNull())
  272. {
  273. offset.x_ = subTextureVal.Get("frameX").GetInt();
  274. offset.y_ = subTextureVal.Get("frameY").GetInt();
  275. int frameWidth = frameWidthVal.GetInt();
  276. int frameHeight = frameHeightVal.GetInt();
  277. hotSpot.x_ = ((float)offset.x_ + frameWidth / 2) / width;
  278. hotSpot.y_ = 1.0f - ((float)offset.y_ + frameHeight / 2) / height;
  279. }
  280. DefineSprite(name, rectangle, hotSpot, offset);
  281. }
  282. loadJSONFile_.Reset();
  283. loadTextureName_.Clear();
  284. return true;
  285. }
  286. }