CPJAtlas.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonUI.Display;
  5. using CommonLang;
  6. using CommonUI.Gemo;
  7. namespace CommonUI.Cell
  8. {
  9. public abstract class CPJAtlas : IDisposable
  10. {
  11. protected CPJResource set;
  12. protected ImagesSet img;
  13. private HashMap<String, int> imageKeys;
  14. public abstract void ReleaseTexture();
  15. public CPJAtlas(ImagesSet img, CPJResource res)
  16. {
  17. this.set = res;
  18. this.img = img;
  19. this.imageKeys = new HashMap<String, int>();
  20. for(int i = 0; i < img.Count; i++)
  21. {
  22. if(img.ClipsKey[i] != null && !string.IsNullOrEmpty(img.ClipsKey[i]))
  23. {
  24. imageKeys.Put(img.ClipsKey[i], i);
  25. }
  26. }
  27. }
  28. public ImagesSet ImagesSet { get { return img; } }
  29. public int Count { get { return img.Count; } }
  30. public bool IsNullTile(int index)
  31. {
  32. return img.getClipW(index) == 0;
  33. }
  34. abstract public Image GetTile(int index);
  35. /// <summary>
  36. /// 获取渲染图片区域
  37. /// </summary>
  38. /// <param name="index"></param>
  39. /// <returns></returns>
  40. abstract public Rectangle2D GetAtlasRegion(int index);
  41. public Rectangle2D GetClipRect(int index)
  42. {
  43. return new Rectangle2D(
  44. img.getClipX(index),
  45. img.getClipY(index),
  46. img.getClipW(index),
  47. img.getClipH(index));
  48. }
  49. public int getWidth(int Index)
  50. {
  51. return img.getClipW(Index);
  52. }
  53. public int getHeight(int Index)
  54. {
  55. return img.getClipH(Index);
  56. }
  57. public void render(Graphics g, string key, float x, float y, Trans trans)
  58. {
  59. if(imageKeys.ContainsKey(key))
  60. {
  61. int index = imageKeys[key];
  62. render(g, index, x, y, trans);
  63. }
  64. }
  65. public int GetIndexByKey(string key)
  66. {
  67. int ret = -1;
  68. if(imageKeys.TryGetValue(key, out ret))
  69. {
  70. return ret;
  71. }
  72. return -1;
  73. }
  74. abstract public void render(Graphics g, int index, float x, float y, Trans trans);
  75. abstract public void begin(Graphics g);
  76. abstract public void addVertex(VertexBuffer v, int index, float x, float y, Trans trans, uint color_rgba);
  77. public virtual void Dispose()
  78. {
  79. img = null;
  80. set = null;
  81. if(imageKeys != null)
  82. {
  83. imageKeys.Clear();
  84. imageKeys = null;
  85. }
  86. }
  87. }
  88. //------------------------------------------
  89. public class CPJAtlasGroup : CPJAtlas
  90. {
  91. private Image atlas;
  92. public CPJAtlasGroup(ImagesSet img, CPJResource res)
  93. : base(img, res)
  94. {
  95. atlas = res.Loader.LoadImage(img.Name + "." + img.Extention);
  96. }
  97. override public Image GetTile(int index)
  98. {
  99. return atlas;
  100. }
  101. override public Rectangle2D GetAtlasRegion(int index)
  102. {
  103. return new Rectangle2D(
  104. img.ClipsX[index],
  105. img.ClipsY[index],
  106. img.ClipsW[index],
  107. img.ClipsH[index]);
  108. }
  109. override public void render(Graphics g, int index, float x, float y, Trans trans)
  110. {
  111. if(img.ClipsW[index] > 0)
  112. {
  113. g.drawRegion(atlas,
  114. img.ClipsX[index],
  115. img.ClipsY[index],
  116. img.ClipsW[index],
  117. img.ClipsH[index],
  118. trans, x, y
  119. );
  120. }
  121. }
  122. override public void begin(Graphics g)
  123. {
  124. g.beginImage(atlas);
  125. }
  126. override public void addVertex(VertexBuffer v, int index, float x, float y, Trans trans, uint color_rgba)
  127. {
  128. if (img.ClipsW[index] > 0)
  129. {
  130. VertexUtils.AddImageQuard(v, atlas, color_rgba,
  131. img.ClipsX[index],
  132. img.ClipsY[index],
  133. img.ClipsW[index],
  134. img.ClipsH[index],
  135. trans, x, y);
  136. }
  137. }
  138. override public void Dispose()
  139. {
  140. if(atlas != null)
  141. {
  142. atlas.Dispose();
  143. atlas = null;
  144. }
  145. base.Dispose();
  146. }
  147. public override void ReleaseTexture()
  148. {
  149. if (atlas != null)
  150. {
  151. atlas.ReleaseTexture();
  152. }
  153. }
  154. }
  155. public class CPJAtlasSplitGroup : CPJAtlas
  156. {
  157. private Image[,] mPartMatrix;
  158. private int mSplitSize;
  159. private int mPartCountX;
  160. private int mPartCountY;
  161. public CPJAtlasSplitGroup(ImagesSet img, CPJResource res)
  162. : base(img, res)
  163. {
  164. mSplitSize = img.SplitSize;
  165. mPartCountX = (int)CMath.ccNextPOT(img.TotalW) / mSplitSize;
  166. mPartCountY = (int)CMath.ccNextPOT(img.TotalH) / mSplitSize;
  167. mPartMatrix = new Image[mPartCountX, mPartCountY];
  168. for(int x = 0; x < mPartCountX; x++)
  169. {
  170. for(int y = 0; y < mPartCountY; y++)
  171. {
  172. string img_path = string.Format("{0}_{1}_{2}.{3}", img.Name, x, y, img.Extention);
  173. mPartMatrix[x, y] = res.Loader.LoadImage(img_path);
  174. }
  175. }
  176. }
  177. override public Image GetTile(int index)
  178. {
  179. int bx = ((int)img.ClipsX[index]) / mSplitSize;
  180. int by = ((int)img.ClipsY[index]) / mSplitSize;
  181. Image pTexture = mPartMatrix[bx, by];
  182. return pTexture;
  183. }
  184. override public Rectangle2D GetAtlasRegion(int index)
  185. {
  186. return new Rectangle2D(
  187. img.ClipsX[index] % mSplitSize,
  188. img.ClipsY[index] % mSplitSize,
  189. img.ClipsW[index],
  190. img.ClipsH[index]);
  191. }
  192. override public void render(Graphics g, int index, float x, float y, Trans trans)
  193. {
  194. if(img.ClipsW[index] > 0)
  195. {
  196. int bx = ((int)img.ClipsX[index]) / mSplitSize;
  197. int by = ((int)img.ClipsY[index]) / mSplitSize;
  198. Image pTexture = mPartMatrix[bx, by];
  199. if(pTexture != null)
  200. {
  201. g.drawRegion(pTexture,
  202. img.ClipsX[index] % mSplitSize,
  203. img.ClipsY[index] % mSplitSize,
  204. img.ClipsW[index],
  205. img.ClipsH[index],
  206. trans, x, y);
  207. }
  208. }
  209. }
  210. override public void begin(Graphics g)
  211. {
  212. throw new NotImplementedException();
  213. }
  214. override public void addVertex(VertexBuffer v, int index, float x, float y, Trans trans, uint color_rgba)
  215. {
  216. throw new NotImplementedException();
  217. }
  218. override public void Dispose()
  219. {
  220. foreach(Image img in mPartMatrix)
  221. {
  222. img.Dispose();
  223. }
  224. base.Dispose();
  225. }
  226. public override void ReleaseTexture()
  227. {
  228. for (int x = 0; x < mPartCountX; x++)
  229. {
  230. for (int y = 0; y < mPartCountY; y++)
  231. {
  232. mPartMatrix[x, y].ReleaseTexture();
  233. }
  234. }
  235. }
  236. }
  237. public class CPJAtlasTiles : CPJAtlas
  238. {
  239. private Image[] tiles;
  240. public CPJAtlasTiles(ImagesSet img, CPJResource res)
  241. : base(img, res)
  242. {
  243. tiles = new Image[img.Count];
  244. for(int i = 0; i < img.Count; i++)
  245. {
  246. if(img.ClipsW[i] > 0 && img.ClipsH[i] > 0)
  247. {
  248. tiles[i] = res.Loader.LoadImage(img.Name + "/" + i + "." + img.Extention);
  249. }
  250. }
  251. }
  252. override public Image GetTile(int index)
  253. {
  254. return tiles[index];
  255. }
  256. override public Rectangle2D GetAtlasRegion(int index)
  257. {
  258. return new Rectangle2D(0, 0, img.ClipsW[index], img.ClipsH[index]);
  259. }
  260. override public void render(Graphics g, int index, float x, float y, Trans trans)
  261. {
  262. Image tile = tiles[index];
  263. if(tile != null)
  264. {
  265. g.drawImageTrans(tile, x, y, trans);
  266. }
  267. }
  268. override public void begin(Graphics g)
  269. {
  270. throw new NotImplementedException();
  271. }
  272. override public void addVertex(VertexBuffer v, int index, float x, float y, Trans trans, uint color_rgba)
  273. {
  274. throw new NotImplementedException();
  275. }
  276. override public void Dispose()
  277. {
  278. for(int i = 0; i < img.Count; i++)
  279. {
  280. if(tiles[i] != null)
  281. {
  282. tiles[i].Dispose();
  283. }
  284. }
  285. tiles = null;
  286. base.Dispose();
  287. }
  288. public override void ReleaseTexture()
  289. {
  290. for (int i = 0; i < img.Count; i++)
  291. {
  292. if (img.ClipsW[i] > 0 && img.ClipsH[i] > 0)
  293. {
  294. tiles[i].ReleaseTexture();
  295. }
  296. }
  297. }
  298. }
  299. }