Atlas.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace Spine
  5. {
  6. public class Atlas
  7. {
  8. public Atlas(string path, TextureLoader textureLoader)
  9. {
  10. using (StreamReader streamReader = new StreamReader(path))
  11. {
  12. try
  13. {
  14. this.Load(streamReader, Path.GetDirectoryName(path), textureLoader);
  15. }
  16. catch (Exception innerException)
  17. {
  18. throw new Exception("Error reading atlas file: " + path, innerException);
  19. }
  20. }
  21. }
  22. public Atlas(TextReader reader, string dir, TextureLoader textureLoader)
  23. {
  24. this.Load(reader, dir, textureLoader);
  25. }
  26. public Atlas(List<AtlasPage> pages, List<AtlasRegion> regions)
  27. {
  28. this.pages = pages;
  29. this.regions = regions;
  30. this.textureLoader = null;
  31. }
  32. private void Load(TextReader reader, string imagesDir, TextureLoader textureLoader)
  33. {
  34. if (textureLoader == null)
  35. {
  36. throw new ArgumentNullException("textureLoader cannot be null.");
  37. }
  38. this.textureLoader = textureLoader;
  39. string[] array = new string[4];
  40. AtlasPage atlasPage = null;
  41. for (;;)
  42. {
  43. string text = reader.ReadLine();
  44. if (text == null)
  45. {
  46. break;
  47. }
  48. if (text.Trim().Length == 0)
  49. {
  50. atlasPage = null;
  51. }
  52. else if (atlasPage == null)
  53. {
  54. atlasPage = new AtlasPage();
  55. atlasPage.name = text;
  56. if (Atlas.readTuple(reader, array) == 2)
  57. {
  58. atlasPage.width = int.Parse(array[0]);
  59. atlasPage.height = int.Parse(array[1]);
  60. Atlas.readTuple(reader, array);
  61. }
  62. atlasPage.format = (Format)Enum.Parse(typeof(Format), array[0], false);
  63. Atlas.readTuple(reader, array);
  64. atlasPage.minFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), array[0], false);
  65. atlasPage.magFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), array[1], false);
  66. string a = Atlas.readValue(reader);
  67. atlasPage.uWrap = TextureWrap.ClampToEdge;
  68. atlasPage.vWrap = TextureWrap.ClampToEdge;
  69. if (a == "x")
  70. {
  71. atlasPage.uWrap = TextureWrap.Repeat;
  72. }
  73. else if (a == "y")
  74. {
  75. atlasPage.vWrap = TextureWrap.Repeat;
  76. }
  77. else if (a == "xy")
  78. {
  79. atlasPage.uWrap = (atlasPage.vWrap = TextureWrap.Repeat);
  80. }
  81. textureLoader.Load(atlasPage, Path.Combine(imagesDir, text));
  82. this.pages.Add(atlasPage);
  83. }
  84. else
  85. {
  86. AtlasRegion atlasRegion = new AtlasRegion();
  87. atlasRegion.name = text;
  88. atlasRegion.page = atlasPage;
  89. atlasRegion.rotate = bool.Parse(Atlas.readValue(reader));
  90. Atlas.readTuple(reader, array);
  91. int num = int.Parse(array[0]);
  92. int num2 = int.Parse(array[1]);
  93. Atlas.readTuple(reader, array);
  94. int num3 = int.Parse(array[0]);
  95. int num4 = int.Parse(array[1]);
  96. atlasRegion.u = (float)num / (float)atlasPage.width;
  97. atlasRegion.v = (float)num2 / (float)atlasPage.height;
  98. if (atlasRegion.rotate)
  99. {
  100. atlasRegion.u2 = (float)(num + num4) / (float)atlasPage.width;
  101. atlasRegion.v2 = (float)(num2 + num3) / (float)atlasPage.height;
  102. }
  103. else
  104. {
  105. atlasRegion.u2 = (float)(num + num3) / (float)atlasPage.width;
  106. atlasRegion.v2 = (float)(num2 + num4) / (float)atlasPage.height;
  107. }
  108. atlasRegion.x = num;
  109. atlasRegion.y = num2;
  110. atlasRegion.width = Math.Abs(num3);
  111. atlasRegion.height = Math.Abs(num4);
  112. if (Atlas.readTuple(reader, array) == 4)
  113. {
  114. atlasRegion.splits = new int[]
  115. {
  116. int.Parse(array[0]),
  117. int.Parse(array[1]),
  118. int.Parse(array[2]),
  119. int.Parse(array[3])
  120. };
  121. if (Atlas.readTuple(reader, array) == 4)
  122. {
  123. atlasRegion.pads = new int[]
  124. {
  125. int.Parse(array[0]),
  126. int.Parse(array[1]),
  127. int.Parse(array[2]),
  128. int.Parse(array[3])
  129. };
  130. Atlas.readTuple(reader, array);
  131. }
  132. }
  133. atlasRegion.originalWidth = int.Parse(array[0]);
  134. atlasRegion.originalHeight = int.Parse(array[1]);
  135. Atlas.readTuple(reader, array);
  136. atlasRegion.offsetX = (float)int.Parse(array[0]);
  137. atlasRegion.offsetY = (float)int.Parse(array[1]);
  138. atlasRegion.index = int.Parse(Atlas.readValue(reader));
  139. this.regions.Add(atlasRegion);
  140. }
  141. }
  142. }
  143. private static string readValue(TextReader reader)
  144. {
  145. string text = reader.ReadLine();
  146. int num = text.IndexOf(':');
  147. if (num == -1)
  148. {
  149. throw new Exception("Invalid line: " + text);
  150. }
  151. return text.Substring(num + 1).Trim();
  152. }
  153. private static int readTuple(TextReader reader, string[] tuple)
  154. {
  155. string text = reader.ReadLine();
  156. int num = text.IndexOf(':');
  157. if (num == -1)
  158. {
  159. throw new Exception("Invalid line: " + text);
  160. }
  161. int i = 0;
  162. int num2 = num + 1;
  163. while (i < 3)
  164. {
  165. int num3 = text.IndexOf(',', num2);
  166. if (num3 == -1)
  167. {
  168. break;
  169. }
  170. tuple[i] = text.Substring(num2, num3 - num2).Trim();
  171. num2 = num3 + 1;
  172. i++;
  173. }
  174. tuple[i] = text.Substring(num2).Trim();
  175. return i + 1;
  176. }
  177. public void FlipV()
  178. {
  179. int i = 0;
  180. int count = this.regions.Count;
  181. while (i < count)
  182. {
  183. AtlasRegion atlasRegion = this.regions[i];
  184. atlasRegion.v = 1f - atlasRegion.v;
  185. atlasRegion.v2 = 1f - atlasRegion.v2;
  186. i++;
  187. }
  188. }
  189. public AtlasRegion FindRegion(string name)
  190. {
  191. int i = 0;
  192. int count = this.regions.Count;
  193. while (i < count)
  194. {
  195. if (this.regions[i].name == name)
  196. {
  197. return this.regions[i];
  198. }
  199. i++;
  200. }
  201. return null;
  202. }
  203. public void Dispose()
  204. {
  205. if (this.textureLoader == null)
  206. {
  207. return;
  208. }
  209. int i = 0;
  210. int count = this.pages.Count;
  211. while (i < count)
  212. {
  213. this.textureLoader.Unload(this.pages[i].rendererObject);
  214. i++;
  215. }
  216. }
  217. private List<AtlasPage> pages = new List<AtlasPage>();
  218. private List<AtlasRegion> regions = new List<AtlasRegion>();
  219. private TextureLoader textureLoader;
  220. }
  221. }