TextureAtlasData.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2012-2017 DragonBones team and other contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. using System.Collections.Generic;
  24. namespace DragonBones
  25. {
  26. /// <summary>
  27. /// - The texture atlas data.
  28. /// </summary>
  29. /// <version>DragonBones 3.0</version>
  30. /// <language>en_US</language>
  31. /// <summary>
  32. /// - 贴图集数据。
  33. /// </summary>
  34. /// <version>DragonBones 3.0</version>
  35. /// <language>zh_CN</language>
  36. public abstract class TextureAtlasData : BaseObject
  37. {
  38. /// <private/>
  39. public bool autoSearch;
  40. /// <private/>
  41. public uint width;
  42. /// <private/>
  43. public uint height;
  44. /// <private/>
  45. public float scale;
  46. /// <summary>
  47. /// - The texture atlas name.
  48. /// </summary>
  49. /// <version>DragonBones 3.0</version>
  50. /// <language>en_US</language>
  51. /// <summary>
  52. /// - 贴图集名称。
  53. /// </summary>
  54. /// <version>DragonBones 3.0</version>
  55. /// <language>zh_CN</language>
  56. public string name;
  57. /// <summary>
  58. /// - The image path of the texture atlas.
  59. /// </summary>
  60. /// <version>DragonBones 3.0</version>
  61. /// <language>en_US</language>
  62. /// <summary>
  63. /// - 贴图集图片路径。
  64. /// </summary>
  65. /// <version>DragonBones 3.0</version>
  66. /// <language>zh_CN</language>
  67. public string imagePath;
  68. /// <private/>
  69. public readonly Dictionary<string, TextureData> textures = new Dictionary<string, TextureData>();
  70. public TextureAtlasData()
  71. {
  72. }
  73. /// <inheritDoc/>
  74. protected override void _OnClear()
  75. {
  76. foreach (var value in this.textures.Values)
  77. {
  78. value.ReturnToPool();
  79. }
  80. this.autoSearch = false;
  81. this.width = 0;
  82. this.height = 0;
  83. this.scale = 1.0f;
  84. this.textures.Clear();
  85. this.name = "";
  86. this.imagePath = "";
  87. }
  88. /// <private/>
  89. public void CopyFrom(TextureAtlasData value)
  90. {
  91. this.autoSearch = value.autoSearch;
  92. this.scale = value.scale;
  93. this.width = value.width;
  94. this.height = value.height;
  95. this.name = value.name;
  96. this.imagePath = value.imagePath;
  97. foreach (var texture in this.textures.Values)
  98. {
  99. texture.ReturnToPool();
  100. }
  101. this.textures.Clear();
  102. foreach (var pair in value.textures)
  103. {
  104. var texture = CreateTexture();
  105. texture.CopyFrom(pair.Value);
  106. textures[pair.Key] = texture;
  107. }
  108. }
  109. /// <internal/>
  110. /// <private/>
  111. public abstract TextureData CreateTexture();
  112. /// <internal/>
  113. /// <private/>
  114. public void AddTexture(TextureData value)
  115. {
  116. if (value != null)
  117. {
  118. if (this.textures.ContainsKey(value.name))
  119. {
  120. Helper.Assert(false, "Same texture: " + value.name);
  121. this.textures[value.name].ReturnToPool();
  122. }
  123. value.parent = this;
  124. this.textures[value.name] = value;
  125. }
  126. }
  127. /// <private/>
  128. public TextureData GetTexture(string name)
  129. {
  130. return textures.ContainsKey(name) ? textures[name] : null;
  131. }
  132. }
  133. /// <internal/>
  134. /// <private/>
  135. public abstract class TextureData : BaseObject
  136. {
  137. public static Rectangle CreateRectangle()
  138. {
  139. return new Rectangle();
  140. }
  141. public bool rotated;
  142. public string name;
  143. public readonly Rectangle region = new Rectangle();
  144. public TextureAtlasData parent;
  145. public Rectangle frame = null; // Initial value.
  146. protected override void _OnClear()
  147. {
  148. this.rotated = false;
  149. this.name = "";
  150. this.region.Clear();
  151. this.parent = null; //
  152. this.frame = null;
  153. }
  154. public virtual void CopyFrom(TextureData value)
  155. {
  156. this.rotated = value.rotated;
  157. this.name = value.name;
  158. this.region.CopyFrom(value.region);
  159. this.parent = value.parent;
  160. if (this.frame == null && value.frame != null)
  161. {
  162. this.frame = TextureData.CreateRectangle();
  163. }
  164. else if (this.frame != null && value.frame == null)
  165. {
  166. this.frame = null;
  167. }
  168. if (this.frame != null && value.frame != null)
  169. {
  170. this.frame.CopyFrom(value.frame);
  171. }
  172. }
  173. }
  174. }