UIGrid.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. [AddComponentMenu("NGUI/Interaction/Grid")]
  6. public class UIGrid : 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 != UIGrid.Sorting.None && this.arrangement != UIGrid.Arrangement.CellSnap)
  32. {
  33. if (this.sorting == UIGrid.Sorting.Alphabetic)
  34. {
  35. List<Transform> list2 = list;
  36. if (UIGrid.<>f__mg$cache0 == null)
  37. {
  38. UIGrid.<>f__mg$cache0 = new Comparison<Transform>(UIGrid.SortByName);
  39. }
  40. list2.Sort(UIGrid.<>f__mg$cache0);
  41. }
  42. else if (this.sorting == UIGrid.Sorting.Horizontal)
  43. {
  44. List<Transform> list3 = list;
  45. if (UIGrid.<>f__mg$cache1 == null)
  46. {
  47. UIGrid.<>f__mg$cache1 = new Comparison<Transform>(UIGrid.SortHorizontal);
  48. }
  49. list3.Sort(UIGrid.<>f__mg$cache1);
  50. }
  51. else if (this.sorting == UIGrid.Sorting.Vertical)
  52. {
  53. List<Transform> list4 = list;
  54. if (UIGrid.<>f__mg$cache2 == null)
  55. {
  56. UIGrid.<>f__mg$cache2 = new Comparison<Transform>(UIGrid.SortVertical);
  57. }
  58. list4.Sort(UIGrid.<>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. public Transform GetChild(int index)
  72. {
  73. List<Transform> childList = this.GetChildList();
  74. return (index >= childList.Count) ? null : childList[index];
  75. }
  76. public int GetIndex(Transform trans)
  77. {
  78. return this.GetChildList().IndexOf(trans);
  79. }
  80. public void AddChild(Transform trans)
  81. {
  82. this.AddChild(trans, true);
  83. }
  84. public void AddChild(Transform trans, bool sort)
  85. {
  86. if (trans != null)
  87. {
  88. trans.parent = base.transform;
  89. this.ResetPosition(this.GetChildList());
  90. }
  91. }
  92. public bool RemoveChild(Transform t)
  93. {
  94. List<Transform> childList = this.GetChildList();
  95. if (childList.Remove(t))
  96. {
  97. this.ResetPosition(childList);
  98. return true;
  99. }
  100. return false;
  101. }
  102. protected virtual void Init()
  103. {
  104. this.mInitDone = true;
  105. this.mPanel = NGUITools.FindInParents<UIPanel>(base.gameObject);
  106. }
  107. protected virtual void Start()
  108. {
  109. if (!this.mInitDone)
  110. {
  111. this.Init();
  112. }
  113. bool flag = this.animateSmoothly;
  114. this.animateSmoothly = false;
  115. this.Reposition();
  116. this.animateSmoothly = flag;
  117. base.enabled = false;
  118. }
  119. protected virtual void Update()
  120. {
  121. this.Reposition();
  122. base.enabled = false;
  123. }
  124. private void OnValidate()
  125. {
  126. if (!Application.isPlaying && NGUITools.GetActive(this))
  127. {
  128. this.Reposition();
  129. }
  130. }
  131. public static int SortByName(Transform a, Transform b)
  132. {
  133. return string.Compare(a.name, b.name);
  134. }
  135. public static int SortHorizontal(Transform a, Transform b)
  136. {
  137. return a.localPosition.x.CompareTo(b.localPosition.x);
  138. }
  139. public static int SortVertical(Transform a, Transform b)
  140. {
  141. return b.localPosition.y.CompareTo(a.localPosition.y);
  142. }
  143. protected virtual void Sort(List<Transform> list)
  144. {
  145. }
  146. [ContextMenu("Execute")]
  147. public virtual void Reposition()
  148. {
  149. if (Application.isPlaying && !this.mInitDone && NGUITools.GetActive(base.gameObject))
  150. {
  151. this.Init();
  152. }
  153. if (this.sorted)
  154. {
  155. this.sorted = false;
  156. if (this.sorting == UIGrid.Sorting.None)
  157. {
  158. this.sorting = UIGrid.Sorting.Alphabetic;
  159. }
  160. NGUITools.SetDirty(this);
  161. }
  162. List<Transform> childList = this.GetChildList();
  163. this.ResetPosition(childList);
  164. if (this.keepWithinPanel)
  165. {
  166. this.ConstrainWithinPanel();
  167. }
  168. if (this.onReposition != null)
  169. {
  170. this.onReposition();
  171. }
  172. }
  173. public void ConstrainWithinPanel()
  174. {
  175. if (this.mPanel != null)
  176. {
  177. this.mPanel.ConstrainTargetToBounds(base.transform, true);
  178. UIScrollView component = this.mPanel.GetComponent<UIScrollView>();
  179. if (component != null)
  180. {
  181. component.UpdateScrollbars(true);
  182. }
  183. }
  184. }
  185. protected virtual void ResetPosition(List<Transform> list)
  186. {
  187. this.mReposition = false;
  188. int num = 0;
  189. int num2 = 0;
  190. int num3 = 0;
  191. int num4 = 0;
  192. Transform transform = base.transform;
  193. int i = 0;
  194. int count = list.Count;
  195. while (i < count)
  196. {
  197. Transform transform2 = list[i];
  198. Vector3 vector = transform2.localPosition;
  199. float z = vector.z;
  200. if (this.arrangement == UIGrid.Arrangement.CellSnap)
  201. {
  202. if (this.cellWidth > 0f)
  203. {
  204. vector.x = Mathf.Round(vector.x / this.cellWidth) * this.cellWidth;
  205. }
  206. if (this.cellHeight > 0f)
  207. {
  208. vector.y = Mathf.Round(vector.y / this.cellHeight) * this.cellHeight;
  209. }
  210. }
  211. else
  212. {
  213. vector = ((this.arrangement != UIGrid.Arrangement.Horizontal) ? new Vector3(this.cellWidth * (float)num2, -this.cellHeight * (float)num, z) : new Vector3(this.cellWidth * (float)num, -this.cellHeight * (float)num2, z));
  214. }
  215. if (this.animateSmoothly && Application.isPlaying && Vector3.SqrMagnitude(transform2.localPosition - vector) >= 0.0001f)
  216. {
  217. SpringPosition springPosition = SpringPosition.Begin(transform2.gameObject, vector, 15f);
  218. springPosition.updateScrollView = true;
  219. springPosition.ignoreTimeScale = true;
  220. }
  221. else
  222. {
  223. transform2.localPosition = vector;
  224. }
  225. num3 = Mathf.Max(num3, num);
  226. num4 = Mathf.Max(num4, num2);
  227. if (++num >= this.maxPerLine && this.maxPerLine > 0)
  228. {
  229. num = 0;
  230. num2++;
  231. }
  232. i++;
  233. }
  234. if (this.pivot != UIWidget.Pivot.TopLeft)
  235. {
  236. Vector2 pivotOffset = NGUIMath.GetPivotOffset(this.pivot);
  237. float num5;
  238. float num6;
  239. if (this.arrangement == UIGrid.Arrangement.Horizontal)
  240. {
  241. num5 = Mathf.Lerp(0f, (float)num3 * this.cellWidth, pivotOffset.x);
  242. num6 = Mathf.Lerp((float)(-(float)num4) * this.cellHeight, 0f, pivotOffset.y);
  243. }
  244. else
  245. {
  246. num5 = Mathf.Lerp(0f, (float)num4 * this.cellWidth, pivotOffset.x);
  247. num6 = Mathf.Lerp((float)(-(float)num3) * this.cellHeight, 0f, pivotOffset.y);
  248. }
  249. for (int j = 0; j < transform.childCount; j++)
  250. {
  251. Transform child = transform.GetChild(j);
  252. SpringPosition component = child.GetComponent<SpringPosition>();
  253. if (component != null)
  254. {
  255. SpringPosition springPosition2 = component;
  256. springPosition2.target.x = springPosition2.target.x - num5;
  257. SpringPosition springPosition3 = component;
  258. springPosition3.target.y = springPosition3.target.y - num6;
  259. }
  260. else
  261. {
  262. Vector3 localPosition = child.localPosition;
  263. localPosition.x -= num5;
  264. localPosition.y -= num6;
  265. child.localPosition = localPosition;
  266. }
  267. }
  268. }
  269. }
  270. public UIGrid.Arrangement arrangement;
  271. public UIGrid.Sorting sorting;
  272. public UIWidget.Pivot pivot;
  273. public int maxPerLine;
  274. public float cellWidth = 200f;
  275. public float cellHeight = 200f;
  276. public bool animateSmoothly;
  277. public bool hideInactive;
  278. public bool keepWithinPanel;
  279. public UIGrid.OnReposition onReposition;
  280. public Comparison<Transform> onCustomSort;
  281. [HideInInspector]
  282. [SerializeField]
  283. private bool sorted;
  284. protected bool mReposition;
  285. protected UIPanel mPanel;
  286. protected bool mInitDone;
  287. [CompilerGenerated]
  288. private static Comparison<Transform> <>f__mg$cache0;
  289. [CompilerGenerated]
  290. private static Comparison<Transform> <>f__mg$cache1;
  291. [CompilerGenerated]
  292. private static Comparison<Transform> <>f__mg$cache2;
  293. public delegate void OnReposition();
  294. public enum Arrangement
  295. {
  296. Horizontal,
  297. Vertical,
  298. CellSnap
  299. }
  300. public enum Sorting
  301. {
  302. None,
  303. Alphabetic,
  304. Horizontal,
  305. Vertical,
  306. Custom
  307. }
  308. }