Relations.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using FairyGUI.Utils;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class Relations
  10. {
  11. GObject _owner;
  12. List<RelationItem> _items;
  13. public GObject handling;
  14. public Relations(GObject owner)
  15. {
  16. _owner = owner;
  17. _items = new List<RelationItem>();
  18. }
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. /// <param name="target"></param>
  23. /// <param name="relationType"></param>
  24. public void Add(GObject target, RelationType relationType)
  25. {
  26. Add(target, relationType, false);
  27. }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. /// <param name="target"></param>
  32. /// <param name="relationType"></param>
  33. /// <param name="usePercent"></param>
  34. public void Add(GObject target, RelationType relationType, bool usePercent)
  35. {
  36. int cnt = _items.Count;
  37. for (int i = 0; i < cnt; i++)
  38. {
  39. RelationItem item = _items[i];
  40. if (item.target == target)
  41. {
  42. item.Add(relationType, usePercent);
  43. return;
  44. }
  45. }
  46. RelationItem newItem = new RelationItem(_owner);
  47. newItem.target = target;
  48. newItem.Add(relationType, usePercent);
  49. _items.Add(newItem);
  50. }
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. /// <param name="target"></param>
  55. /// <param name="relationType"></param>
  56. public void Remove(GObject target, RelationType relationType)
  57. {
  58. int cnt = _items.Count;
  59. int i = 0;
  60. while (i < cnt)
  61. {
  62. RelationItem item = _items[i];
  63. if (item.target == target)
  64. {
  65. item.Remove(relationType);
  66. if (item.isEmpty)
  67. {
  68. item.Dispose();
  69. _items.RemoveAt(i);
  70. cnt--;
  71. continue;
  72. }
  73. else
  74. i++;
  75. }
  76. i++;
  77. }
  78. }
  79. /// <summary>
  80. ///
  81. /// </summary>
  82. /// <param name="target"></param>
  83. /// <returns></returns>
  84. public bool Contains(GObject target)
  85. {
  86. int cnt = _items.Count;
  87. for (int i = 0; i < cnt; i++)
  88. {
  89. RelationItem item = _items[i];
  90. if (item.target == target)
  91. return true;
  92. }
  93. return false;
  94. }
  95. /// <summary>
  96. ///
  97. /// </summary>
  98. /// <param name="target"></param>
  99. public void ClearFor(GObject target)
  100. {
  101. int cnt = _items.Count;
  102. int i = 0;
  103. while (i < cnt)
  104. {
  105. RelationItem item = _items[i];
  106. if (item.target == target)
  107. {
  108. item.Dispose();
  109. _items.RemoveAt(i);
  110. cnt--;
  111. }
  112. else
  113. i++;
  114. }
  115. }
  116. /// <summary>
  117. ///
  118. /// </summary>
  119. public void ClearAll()
  120. {
  121. int cnt = _items.Count;
  122. for (int i = 0; i < cnt; i++)
  123. {
  124. RelationItem item = _items[i];
  125. item.Dispose();
  126. }
  127. _items.Clear();
  128. }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. /// <param name="source"></param>
  133. public void CopyFrom(Relations source)
  134. {
  135. ClearAll();
  136. List<RelationItem> arr = source._items;
  137. foreach (RelationItem ri in arr)
  138. {
  139. RelationItem item = new RelationItem(_owner);
  140. item.CopyFrom(ri);
  141. _items.Add(item);
  142. }
  143. }
  144. /// <summary>
  145. ///
  146. /// </summary>
  147. public void Dispose()
  148. {
  149. ClearAll();
  150. handling = null;
  151. }
  152. /// <summary>
  153. ///
  154. /// </summary>
  155. /// <param name="dWidth"></param>
  156. /// <param name="dHeight"></param>
  157. /// <param name="applyPivot"></param>
  158. public void OnOwnerSizeChanged(float dWidth, float dHeight, bool applyPivot)
  159. {
  160. int cnt = _items.Count;
  161. if (cnt == 0)
  162. return;
  163. for (int i = 0; i < cnt; i++)
  164. _items[i].ApplyOnSelfSizeChanged(dWidth, dHeight, applyPivot);
  165. }
  166. /// <summary>
  167. ///
  168. /// </summary>
  169. public bool isEmpty
  170. {
  171. get
  172. {
  173. return _items.Count == 0;
  174. }
  175. }
  176. public void Setup(ByteBuffer buffer, bool parentToChild)
  177. {
  178. int cnt = buffer.ReadByte();
  179. GObject target;
  180. for (int i = 0; i < cnt; i++)
  181. {
  182. int targetIndex = buffer.ReadShort();
  183. if (targetIndex == -1)
  184. target = _owner.parent;
  185. else if (parentToChild)
  186. target = ((GComponent)_owner).GetChildAt(targetIndex);
  187. else
  188. target = _owner.parent.GetChildAt(targetIndex);
  189. RelationItem newItem = new RelationItem(_owner);
  190. newItem.target = target;
  191. _items.Add(newItem);
  192. int cnt2 = buffer.ReadByte();
  193. for (int j = 0; j < cnt2; j++)
  194. {
  195. RelationType rt = (RelationType)buffer.ReadByte();
  196. bool usePercent = buffer.ReadBool();
  197. newItem.InternalAdd(rt, usePercent);
  198. }
  199. }
  200. }
  201. }
  202. }