TextureData.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections.Generic;
  2. namespace DragonBones
  3. {
  4. public abstract class TextureAtlasData : BaseObject
  5. {
  6. /**
  7. * @language zh_CN
  8. * 是否开启共享搜索。
  9. * @default false
  10. * @version DragonBones 4.5
  11. */
  12. public bool autoSearch;
  13. /**
  14. * @language zh_CN
  15. * 贴图集缩放系数。
  16. * @version DragonBones 3.0
  17. */
  18. public float scale;
  19. /**
  20. * @private
  21. */
  22. public float width;
  23. /**
  24. * @private
  25. */
  26. public float height;
  27. /**
  28. * @language zh_CN
  29. * 贴图集名称。
  30. * @version DragonBones 3.0
  31. */
  32. public string name;
  33. /**
  34. * @language zh_CN
  35. * 贴图集图片路径。
  36. * @version DragonBones 3.0
  37. */
  38. public string imagePath;
  39. /**
  40. * @private
  41. */
  42. public readonly Dictionary<string, TextureData> textures = new Dictionary<string, TextureData>();
  43. /**
  44. * @private
  45. */
  46. public TextureAtlasData()
  47. {
  48. }
  49. /**
  50. * @private
  51. */
  52. protected override void _onClear()
  53. {
  54. foreach (var texture in textures.Values)
  55. {
  56. texture.ReturnToPool();
  57. }
  58. autoSearch = false;
  59. scale = 1.0f;
  60. width = 0.0f;
  61. height = 0.0f;
  62. name = null;
  63. imagePath = null;
  64. textures.Clear();
  65. }
  66. /**
  67. * @private
  68. */
  69. public abstract TextureData GenerateTextureData();
  70. /**
  71. * @private
  72. */
  73. public void AddTexture(TextureData value)
  74. {
  75. if (value != null && value.name != null && !textures.ContainsKey(value.name))
  76. {
  77. textures[value.name] = value;
  78. value.parent = this;
  79. }
  80. else
  81. {
  82. DragonBones.Assert(false, DragonBones.ARGUMENT_ERROR);
  83. }
  84. }
  85. /**
  86. * @private
  87. */
  88. public TextureData GetTexture(string name)
  89. {
  90. return textures.ContainsKey(name) ? textures[name] : null;
  91. }
  92. /**
  93. * @private
  94. */
  95. public void CopyFrom(TextureAtlasData value)
  96. {
  97. autoSearch = value.autoSearch;
  98. scale = value.scale;
  99. width = value.width;
  100. height = value.height;
  101. name = value.name;
  102. imagePath = value.imagePath;
  103. foreach (var texture in textures.Values)
  104. {
  105. texture.ReturnToPool();
  106. }
  107. textures.Clear();
  108. foreach (var pair in value.textures)
  109. {
  110. var texture = GenerateTextureData();
  111. texture.CopyFrom(pair.Value);
  112. textures[pair.Key] = texture;
  113. }
  114. }
  115. }
  116. /**
  117. * @private
  118. */
  119. public abstract class TextureData : BaseObject
  120. {
  121. public static Rectangle GenerateRectangle()
  122. {
  123. return new Rectangle();
  124. }
  125. public bool rotated;
  126. public string name;
  127. public readonly Rectangle region = new Rectangle();
  128. public Rectangle frame;
  129. public TextureAtlasData parent;
  130. public TextureData()
  131. {
  132. }
  133. protected override void _onClear()
  134. {
  135. rotated = false;
  136. name = null;
  137. region.Clear();
  138. frame = null;
  139. parent = null;
  140. }
  141. public void CopyFrom(TextureData value)
  142. {
  143. rotated = value.rotated;
  144. name = value.name;
  145. if (frame == null && value.frame == null)
  146. {
  147. frame = GenerateRectangle();
  148. }
  149. else if (frame != null && value.frame == null)
  150. {
  151. frame = null;
  152. }
  153. if (frame != null && value.frame != null)
  154. {
  155. frame.CopyFrom(value.frame);
  156. }
  157. parent = value.parent;
  158. region.CopyFrom(value.region);
  159. }
  160. }
  161. }