Atlas.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #if (UNITY_5 || UNITY_5_3_OR_NEWER || UNITY_WSA || UNITY_WP8 || UNITY_WP8_1)
  30. #define IS_UNITY
  31. #endif
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Globalization;
  35. using System.IO;
  36. using System.Reflection;
  37. #if WINDOWS_STOREAPP
  38. using System.Threading.Tasks;
  39. using Windows.Storage;
  40. #endif
  41. namespace Spine {
  42. public class Atlas : IEnumerable<AtlasRegion> {
  43. readonly List<AtlasPage> pages = new List<AtlasPage>();
  44. List<AtlasRegion> regions = new List<AtlasRegion>();
  45. TextureLoader textureLoader;
  46. #region IEnumerable implementation
  47. public IEnumerator<AtlasRegion> GetEnumerator () {
  48. return regions.GetEnumerator();
  49. }
  50. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () {
  51. return regions.GetEnumerator();
  52. }
  53. #endregion
  54. #if !(IS_UNITY)
  55. #if WINDOWS_STOREAPP
  56. private async Task ReadFile(string path, TextureLoader textureLoader) {
  57. var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
  58. var file = await folder.GetFileAsync(path).AsTask().ConfigureAwait(false);
  59. using (var reader = new StreamReader(await file.OpenStreamForReadAsync().ConfigureAwait(false))) {
  60. try {
  61. Load(reader, Path.GetDirectoryName(path), textureLoader);
  62. } catch (Exception ex) {
  63. throw new Exception("Error reading atlas file: " + path, ex);
  64. }
  65. }
  66. }
  67. public Atlas(string path, TextureLoader textureLoader) {
  68. this.ReadFile(path, textureLoader).Wait();
  69. }
  70. #else
  71. public Atlas (string path, TextureLoader textureLoader) {
  72. #if WINDOWS_PHONE
  73. Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(path);
  74. using (StreamReader reader = new StreamReader(stream)) {
  75. #else
  76. using (StreamReader reader = new StreamReader(path)) {
  77. #endif // WINDOWS_PHONE
  78. try {
  79. Load(reader, Path.GetDirectoryName(path), textureLoader);
  80. } catch (Exception ex) {
  81. throw new Exception("Error reading atlas file: " + path, ex);
  82. }
  83. }
  84. }
  85. #endif // WINDOWS_STOREAPP
  86. #endif
  87. public Atlas (TextReader reader, string dir, TextureLoader textureLoader) {
  88. Load(reader, dir, textureLoader);
  89. }
  90. public Atlas (List<AtlasPage> pages, List<AtlasRegion> regions) {
  91. this.pages = pages;
  92. this.regions = regions;
  93. this.textureLoader = null;
  94. }
  95. private void Load (TextReader reader, string imagesDir, TextureLoader textureLoader) {
  96. if (textureLoader == null) throw new ArgumentNullException("textureLoader", "textureLoader cannot be null.");
  97. this.textureLoader = textureLoader;
  98. string[] tuple = new string[4];
  99. AtlasPage page = null;
  100. while (true) {
  101. string line = reader.ReadLine();
  102. if (line == null) break;
  103. if (line.Trim().Length == 0)
  104. page = null;
  105. else if (page == null) {
  106. page = new AtlasPage();
  107. page.name = line;
  108. if (ReadTuple(reader, tuple) == 2) { // size is only optional for an atlas packed with an old TexturePacker.
  109. page.width = int.Parse(tuple[0], CultureInfo.InvariantCulture);
  110. page.height = int.Parse(tuple[1], CultureInfo.InvariantCulture);
  111. ReadTuple(reader, tuple);
  112. }
  113. page.format = (Format)Enum.Parse(typeof(Format), tuple[0], false);
  114. ReadTuple(reader, tuple);
  115. page.minFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0], false);
  116. page.magFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1], false);
  117. string direction = ReadValue(reader);
  118. page.uWrap = TextureWrap.ClampToEdge;
  119. page.vWrap = TextureWrap.ClampToEdge;
  120. if (direction == "x")
  121. page.uWrap = TextureWrap.Repeat;
  122. else if (direction == "y")
  123. page.vWrap = TextureWrap.Repeat;
  124. else if (direction == "xy")
  125. page.uWrap = page.vWrap = TextureWrap.Repeat;
  126. textureLoader.Load(page, Path.Combine(imagesDir, line));
  127. pages.Add(page);
  128. } else {
  129. AtlasRegion region = new AtlasRegion();
  130. region.name = line;
  131. region.page = page;
  132. string rotateValue = ReadValue(reader);
  133. if (rotateValue == "true")
  134. region.degrees = 90;
  135. else if (rotateValue == "false")
  136. region.degrees = 0;
  137. else
  138. region.degrees = int.Parse(rotateValue);
  139. region.rotate = region.degrees == 90;
  140. ReadTuple(reader, tuple);
  141. int x = int.Parse(tuple[0], CultureInfo.InvariantCulture);
  142. int y = int.Parse(tuple[1], CultureInfo.InvariantCulture);
  143. ReadTuple(reader, tuple);
  144. int width = int.Parse(tuple[0], CultureInfo.InvariantCulture);
  145. int height = int.Parse(tuple[1], CultureInfo.InvariantCulture);
  146. region.u = x / (float)page.width;
  147. region.v = y / (float)page.height;
  148. if (region.rotate) {
  149. region.u2 = (x + height) / (float)page.width;
  150. region.v2 = (y + width) / (float)page.height;
  151. } else {
  152. region.u2 = (x + width) / (float)page.width;
  153. region.v2 = (y + height) / (float)page.height;
  154. }
  155. region.x = x;
  156. region.y = y;
  157. region.width = Math.Abs(width);
  158. region.height = Math.Abs(height);
  159. if (ReadTuple(reader, tuple) == 4) { // split is optional
  160. region.splits = new [] {int.Parse(tuple[0], CultureInfo.InvariantCulture),
  161. int.Parse(tuple[1], CultureInfo.InvariantCulture),
  162. int.Parse(tuple[2], CultureInfo.InvariantCulture),
  163. int.Parse(tuple[3], CultureInfo.InvariantCulture)};
  164. if (ReadTuple(reader, tuple) == 4) { // pad is optional, but only present with splits
  165. region.pads = new [] {int.Parse(tuple[0], CultureInfo.InvariantCulture),
  166. int.Parse(tuple[1], CultureInfo.InvariantCulture),
  167. int.Parse(tuple[2], CultureInfo.InvariantCulture),
  168. int.Parse(tuple[3], CultureInfo.InvariantCulture)};
  169. ReadTuple(reader, tuple);
  170. }
  171. }
  172. region.originalWidth = int.Parse(tuple[0], CultureInfo.InvariantCulture);
  173. region.originalHeight = int.Parse(tuple[1], CultureInfo.InvariantCulture);
  174. ReadTuple(reader, tuple);
  175. region.offsetX = int.Parse(tuple[0], CultureInfo.InvariantCulture);
  176. region.offsetY = int.Parse(tuple[1], CultureInfo.InvariantCulture);
  177. region.index = int.Parse(ReadValue(reader), CultureInfo.InvariantCulture);
  178. regions.Add(region);
  179. }
  180. }
  181. }
  182. static string ReadValue (TextReader reader) {
  183. string line = reader.ReadLine();
  184. int colon = line.IndexOf(':');
  185. if (colon == -1) throw new Exception("Invalid line: " + line);
  186. return line.Substring(colon + 1).Trim();
  187. }
  188. /// <summary>Returns the number of tuple values read (1, 2 or 4).</summary>
  189. static int ReadTuple (TextReader reader, string[] tuple) {
  190. string line = reader.ReadLine();
  191. int colon = line.IndexOf(':');
  192. if (colon == -1) throw new Exception("Invalid line: " + line);
  193. int i = 0, lastMatch = colon + 1;
  194. for (; i < 3; i++) {
  195. int comma = line.IndexOf(',', lastMatch);
  196. if (comma == -1) break;
  197. tuple[i] = line.Substring(lastMatch, comma - lastMatch).Trim();
  198. lastMatch = comma + 1;
  199. }
  200. tuple[i] = line.Substring(lastMatch).Trim();
  201. return i + 1;
  202. }
  203. public void FlipV () {
  204. for (int i = 0, n = regions.Count; i < n; i++) {
  205. AtlasRegion region = regions[i];
  206. region.v = 1 - region.v;
  207. region.v2 = 1 - region.v2;
  208. }
  209. }
  210. /// <summary>Returns the first region found with the specified name. This method uses string comparison to find the region, so the result
  211. /// should be cached rather than calling this method multiple times.</summary>
  212. /// <returns>The region, or null.</returns>
  213. public AtlasRegion FindRegion (string name) {
  214. for (int i = 0, n = regions.Count; i < n; i++)
  215. if (regions[i].name == name) return regions[i];
  216. return null;
  217. }
  218. public void Dispose () {
  219. if (textureLoader == null) return;
  220. for (int i = 0, n = pages.Count; i < n; i++)
  221. textureLoader.Unload(pages[i].rendererObject);
  222. }
  223. }
  224. public enum Format {
  225. Alpha,
  226. Intensity,
  227. LuminanceAlpha,
  228. RGB565,
  229. RGBA4444,
  230. RGB888,
  231. RGBA8888
  232. }
  233. public enum TextureFilter {
  234. Nearest,
  235. Linear,
  236. MipMap,
  237. MipMapNearestNearest,
  238. MipMapLinearNearest,
  239. MipMapNearestLinear,
  240. MipMapLinearLinear
  241. }
  242. public enum TextureWrap {
  243. MirroredRepeat,
  244. ClampToEdge,
  245. Repeat
  246. }
  247. public class AtlasPage {
  248. public string name;
  249. public Format format;
  250. public TextureFilter minFilter;
  251. public TextureFilter magFilter;
  252. public TextureWrap uWrap;
  253. public TextureWrap vWrap;
  254. public object rendererObject;
  255. public int width, height;
  256. public AtlasPage Clone () {
  257. return MemberwiseClone() as AtlasPage;
  258. }
  259. }
  260. public class AtlasRegion {
  261. public AtlasPage page;
  262. public string name;
  263. public int x, y, width, height;
  264. public float u, v, u2, v2;
  265. public float offsetX, offsetY;
  266. public int originalWidth, originalHeight;
  267. public int index;
  268. public bool rotate;
  269. public int degrees;
  270. public int[] splits;
  271. public int[] pads;
  272. public AtlasRegion Clone () {
  273. return MemberwiseClone() as AtlasRegion;
  274. }
  275. }
  276. public interface TextureLoader {
  277. void Load (AtlasPage page, string path);
  278. void Unload (Object texture);
  279. }
  280. }