UITable.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. [AddComponentMenu("NGUI/Interaction/Table")]
  6. public class UITable : UIWidgetContainer
  7. {
  8. public bool repositionNow
  9. {
  10. set
  11. {
  12. if (value)
  13. {
  14. this.mReposition = true;
  15. base.enabled = true;
  16. }
  17. }
  18. }
  19. public List<Transform> GetChildList()
  20. {
  21. Transform transform = base.transform;
  22. List<Transform> list = new List<Transform>();
  23. for (int i = 0; i < transform.childCount; i++)
  24. {
  25. Transform child = transform.GetChild(i);
  26. if (!this.hideInactive || (child && NGUITools.GetActive(child.gameObject)))
  27. {
  28. list.Add(child);
  29. }
  30. }
  31. if (this.sorting != UITable.Sorting.None)
  32. {
  33. if (this.sorting == UITable.Sorting.Alphabetic)
  34. {
  35. List<Transform> list2 = list;
  36. if (UITable.<>f__mg$cache0 == null)
  37. {
  38. UITable.<>f__mg$cache0 = new Comparison<Transform>(UIGrid.SortByName);
  39. }
  40. list2.Sort(UITable.<>f__mg$cache0);
  41. }
  42. else if (this.sorting == UITable.Sorting.Horizontal)
  43. {
  44. List<Transform> list3 = list;
  45. if (UITable.<>f__mg$cache1 == null)
  46. {
  47. UITable.<>f__mg$cache1 = new Comparison<Transform>(UIGrid.SortHorizontal);
  48. }
  49. list3.Sort(UITable.<>f__mg$cache1);
  50. }
  51. else if (this.sorting == UITable.Sorting.Vertical)
  52. {
  53. List<Transform> list4 = list;
  54. if (UITable.<>f__mg$cache2 == null)
  55. {
  56. UITable.<>f__mg$cache2 = new Comparison<Transform>(UIGrid.SortVertical);
  57. }
  58. list4.Sort(UITable.<>f__mg$cache2);
  59. }
  60. else if (this.onCustomSort != null)
  61. {
  62. list.Sort(this.onCustomSort);
  63. }
  64. else
  65. {
  66. this.Sort(list);
  67. }
  68. }
  69. return list;
  70. }
  71. protected virtual void Sort(List<Transform> list)
  72. {
  73. if (UITable.<>f__mg$cache3 == null)
  74. {
  75. UITable.<>f__mg$cache3 = new Comparison<Transform>(UIGrid.SortByName);
  76. }
  77. list.Sort(UITable.<>f__mg$cache3);
  78. }
  79. protected virtual void Start()
  80. {
  81. this.Init();
  82. this.Reposition();
  83. base.enabled = false;
  84. }
  85. protected virtual void Init()
  86. {
  87. this.mInitDone = true;
  88. this.mPanel = NGUITools.FindInParents<UIPanel>(base.gameObject);
  89. }
  90. protected virtual void LateUpdate()
  91. {
  92. if (this.mReposition)
  93. {
  94. this.Reposition();
  95. }
  96. base.enabled = false;
  97. }
  98. private void OnValidate()
  99. {
  100. if (!Application.isPlaying && NGUITools.GetActive(this))
  101. {
  102. this.Reposition();
  103. }
  104. }
  105. protected void RepositionVariableSize(List<Transform> children)
  106. {
  107. float num = 0f;
  108. float num2 = 0f;
  109. int num3 = (this.columns <= 0) ? 1 : (children.Count / this.columns + 1);
  110. int num4 = (this.columns <= 0) ? children.Count : this.columns;
  111. Bounds[,] array = new Bounds[num3, num4];
  112. Bounds[] array2 = new Bounds[num4];
  113. Bounds[] array3 = new Bounds[num3];
  114. int num5 = 0;
  115. int num6 = 0;
  116. int i = 0;
  117. int count = children.Count;
  118. while (i < count)
  119. {
  120. Transform transform = children[i];
  121. Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(transform, !this.hideInactive);
  122. Vector3 localScale = transform.localScale;
  123. bounds.min = Vector3.Scale(bounds.min, localScale);
  124. bounds.max = Vector3.Scale(bounds.max, localScale);
  125. array[num6, num5] = bounds;
  126. array2[num5].Encapsulate(bounds);
  127. array3[num6].Encapsulate(bounds);
  128. if (++num5 >= this.columns && this.columns > 0)
  129. {
  130. num5 = 0;
  131. num6++;
  132. }
  133. i++;
  134. }
  135. num5 = 0;
  136. num6 = 0;
  137. Vector2 pivotOffset = NGUIMath.GetPivotOffset(this.cellAlignment);
  138. int j = 0;
  139. int count2 = children.Count;
  140. while (j < count2)
  141. {
  142. Transform transform2 = children[j];
  143. Bounds bounds2 = array[num6, num5];
  144. Bounds bounds3 = array2[num5];
  145. Bounds bounds4 = array3[num6];
  146. Vector3 localPosition = transform2.localPosition;
  147. localPosition.x = num + bounds2.extents.x - bounds2.center.x;
  148. localPosition.x -= Mathf.Lerp(0f, bounds2.max.x - bounds2.min.x - bounds3.max.x + bounds3.min.x, pivotOffset.x) - this.padding.x;
  149. if (this.direction == UITable.Direction.Down)
  150. {
  151. localPosition.y = -num2 - bounds2.extents.y - bounds2.center.y;
  152. localPosition.y += Mathf.Lerp(bounds2.max.y - bounds2.min.y - bounds4.max.y + bounds4.min.y, 0f, pivotOffset.y) - this.padding.y;
  153. }
  154. else
  155. {
  156. localPosition.y = num2 + bounds2.extents.y - bounds2.center.y;
  157. localPosition.y -= Mathf.Lerp(0f, bounds2.max.y - bounds2.min.y - bounds4.max.y + bounds4.min.y, pivotOffset.y) - this.padding.y;
  158. }
  159. num += bounds3.size.x + this.padding.x * 2f;
  160. transform2.localPosition = localPosition;
  161. if (++num5 >= this.columns && this.columns > 0)
  162. {
  163. num5 = 0;
  164. num6++;
  165. num = 0f;
  166. num2 += bounds4.size.y + this.padding.y * 2f;
  167. }
  168. j++;
  169. }
  170. if (this.pivot != UIWidget.Pivot.TopLeft)
  171. {
  172. pivotOffset = NGUIMath.GetPivotOffset(this.pivot);
  173. Bounds bounds5 = NGUIMath.CalculateRelativeWidgetBounds(base.transform);
  174. float num7 = Mathf.Lerp(0f, bounds5.size.x, pivotOffset.x);
  175. float num8 = Mathf.Lerp(-bounds5.size.y, 0f, pivotOffset.y);
  176. Transform transform3 = base.transform;
  177. for (int k = 0; k < transform3.childCount; k++)
  178. {
  179. Transform child = transform3.GetChild(k);
  180. SpringPosition component = child.GetComponent<SpringPosition>();
  181. if (component != null)
  182. {
  183. SpringPosition springPosition = component;
  184. springPosition.target.x = springPosition.target.x - num7;
  185. SpringPosition springPosition2 = component;
  186. springPosition2.target.y = springPosition2.target.y - num8;
  187. }
  188. else
  189. {
  190. Vector3 localPosition2 = child.localPosition;
  191. localPosition2.x -= num7;
  192. localPosition2.y -= num8;
  193. child.localPosition = localPosition2;
  194. }
  195. }
  196. }
  197. }
  198. [ContextMenu("Execute")]
  199. public virtual void Reposition()
  200. {
  201. if (Application.isPlaying && !this.mInitDone && NGUITools.GetActive(this))
  202. {
  203. this.Init();
  204. }
  205. this.mReposition = false;
  206. Transform transform = base.transform;
  207. List<Transform> childList = this.GetChildList();
  208. if (childList.Count > 0)
  209. {
  210. this.RepositionVariableSize(childList);
  211. }
  212. if (this.keepWithinPanel && this.mPanel != null)
  213. {
  214. this.mPanel.ConstrainTargetToBounds(transform, true);
  215. UIScrollView component = this.mPanel.GetComponent<UIScrollView>();
  216. if (component != null)
  217. {
  218. component.UpdateScrollbars(true);
  219. }
  220. }
  221. if (this.onReposition != null)
  222. {
  223. this.onReposition();
  224. }
  225. }
  226. public int columns;
  227. public UITable.Direction direction;
  228. public UITable.Sorting sorting;
  229. public UIWidget.Pivot pivot;
  230. public UIWidget.Pivot cellAlignment;
  231. public bool hideInactive = true;
  232. public bool keepWithinPanel;
  233. public Vector2 padding = Vector2.zero;
  234. public UITable.OnReposition onReposition;
  235. public Comparison<Transform> onCustomSort;
  236. protected UIPanel mPanel;
  237. protected bool mInitDone;
  238. protected bool mReposition;
  239. [CompilerGenerated]
  240. private static Comparison<Transform> <>f__mg$cache0;
  241. [CompilerGenerated]
  242. private static Comparison<Transform> <>f__mg$cache1;
  243. [CompilerGenerated]
  244. private static Comparison<Transform> <>f__mg$cache2;
  245. [CompilerGenerated]
  246. private static Comparison<Transform> <>f__mg$cache3;
  247. public delegate void OnReposition();
  248. public enum Direction
  249. {
  250. Down,
  251. Up
  252. }
  253. public enum Sorting
  254. {
  255. None,
  256. Alphabetic,
  257. Horizontal,
  258. Vertical,
  259. Custom
  260. }
  261. }