CPJResource.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonLang;
  5. using CommonUI.Cell.Game;
  6. namespace CommonUI.Cell
  7. {
  8. public class CPJResource : IDisposable
  9. {
  10. protected CPJLoader loader;
  11. protected HashMap<string, CPJAtlas> images = new HashMap<string, CPJAtlas>();
  12. protected HashMap<string, CSpriteMeta> sprites = new HashMap<string, CSpriteMeta>();
  13. // -------------------------------------------------------------------------------------
  14. public CPJResource(CPJLoader loader)
  15. {
  16. this.loader = loader;
  17. foreach (ImagesSet img in loader.ImgTable.Values)
  18. {
  19. CPJAtlas atlas = null;
  20. if (img.IsTiles)
  21. {
  22. atlas = new CPJAtlasTiles(img, this);
  23. }
  24. else if (img.SplitSize != 0)
  25. {
  26. atlas = new CPJAtlasSplitGroup(img, this);
  27. }
  28. else
  29. {
  30. atlas = new CPJAtlasGroup(img, this);
  31. }
  32. images.Add(img.Name, atlas);
  33. }
  34. }
  35. public CPJLoader Loader
  36. {
  37. get { return loader; }
  38. }
  39. public void Dispose()
  40. {
  41. if (images != null)
  42. {
  43. foreach (CPJAtlas img in images.Values)
  44. {
  45. img.Dispose();
  46. }
  47. images = null;
  48. }
  49. if (sprites != null)
  50. {
  51. sprites.Clear();
  52. sprites = null;
  53. }
  54. if (loader != null)
  55. {
  56. loader.Dispose();
  57. loader = null;
  58. }
  59. }
  60. public CPJAtlas GetAtlas(String key)
  61. {
  62. return images.Get(key);
  63. }
  64. public ImagesSet GetSetImages(String key)
  65. {
  66. return loader.ImgTable.Get(key);
  67. }
  68. public CSpriteMeta GetSpriteMeta(String key)
  69. {
  70. if (sprites.ContainsKey(key))
  71. {
  72. return sprites.Get(key);
  73. }
  74. else
  75. {
  76. SpriteSet ss = loader.SprTable.Get(key);
  77. if (ss != null)
  78. {
  79. CPJAtlas img = images.Get(ss.ImagesName);
  80. if (img != null)
  81. {
  82. CSpriteMeta ret = new CSpriteMeta(ss, img);
  83. sprites.Put(key, ret);
  84. return ret;
  85. }
  86. }
  87. return null;
  88. }
  89. }
  90. public void ReleaseTexture()
  91. {
  92. if (images != null)
  93. {
  94. foreach (CPJAtlas img in images.Values)
  95. {
  96. img.ReleaseTexture();
  97. }
  98. }
  99. }
  100. //-------------------------------------------------------------------------------------
  101. public static CPJResource CreateResource(string file_name)
  102. {
  103. string prefix = file_name.Substring(0, file_name.LastIndexOf('.'));
  104. if (CommonLang.IO.Resource.ExistData(prefix + ".bin"))
  105. {
  106. CPJLoaderBIN loader = new CPJLoaderBIN(prefix + ".bin");
  107. return new CPJResource(loader);
  108. }
  109. else
  110. {
  111. CPJLoaderXML loader = new CPJLoaderXML(file_name);
  112. return new CPJResource(loader);
  113. }
  114. }
  115. //-------------------------------------------------------------------------------------
  116. }
  117. }