UIWrapContent.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. [AddComponentMenu("NGUI/Interaction/Wrap Content")]
  6. public class UIWrapContent : MonoBehaviour
  7. {
  8. protected virtual void Start()
  9. {
  10. this.SortBasedOnScrollMovement();
  11. this.WrapContent();
  12. if (this.mScroll != null)
  13. {
  14. this.mScroll.GetComponent<UIPanel>().onClipMove = new UIPanel.OnClippingMoved(this.OnMove);
  15. }
  16. this.mFirstTime = false;
  17. }
  18. protected virtual void OnMove(UIPanel panel)
  19. {
  20. this.WrapContent();
  21. }
  22. [ContextMenu("Sort Based on Scroll Movement")]
  23. public void SortBasedOnScrollMovement()
  24. {
  25. if (!this.CacheScrollView())
  26. {
  27. return;
  28. }
  29. this.mChildren.Clear();
  30. for (int i = 0; i < this.mTrans.childCount; i++)
  31. {
  32. this.mChildren.Add(this.mTrans.GetChild(i));
  33. }
  34. if (this.mHorizontal)
  35. {
  36. List<Transform> list = this.mChildren;
  37. if (UIWrapContent.<>f__mg$cache0 == null)
  38. {
  39. UIWrapContent.<>f__mg$cache0 = new Comparison<Transform>(UIGrid.SortHorizontal);
  40. }
  41. list.Sort(UIWrapContent.<>f__mg$cache0);
  42. }
  43. else
  44. {
  45. List<Transform> list2 = this.mChildren;
  46. if (UIWrapContent.<>f__mg$cache1 == null)
  47. {
  48. UIWrapContent.<>f__mg$cache1 = new Comparison<Transform>(UIGrid.SortVertical);
  49. }
  50. list2.Sort(UIWrapContent.<>f__mg$cache1);
  51. }
  52. this.ResetChildPositions();
  53. }
  54. [ContextMenu("Sort Alphabetically")]
  55. public void SortAlphabetically()
  56. {
  57. if (!this.CacheScrollView())
  58. {
  59. return;
  60. }
  61. this.mChildren.Clear();
  62. for (int i = 0; i < this.mTrans.childCount; i++)
  63. {
  64. this.mChildren.Add(this.mTrans.GetChild(i));
  65. }
  66. List<Transform> list = this.mChildren;
  67. if (UIWrapContent.<>f__mg$cache2 == null)
  68. {
  69. UIWrapContent.<>f__mg$cache2 = new Comparison<Transform>(UIGrid.SortByName);
  70. }
  71. list.Sort(UIWrapContent.<>f__mg$cache2);
  72. this.ResetChildPositions();
  73. }
  74. protected bool CacheScrollView()
  75. {
  76. this.mTrans = base.transform;
  77. this.mPanel = NGUITools.FindInParents<UIPanel>(base.gameObject);
  78. this.mScroll = this.mPanel.GetComponent<UIScrollView>();
  79. if (this.mScroll == null)
  80. {
  81. return false;
  82. }
  83. if (this.mScroll.movement == UIScrollView.Movement.Horizontal)
  84. {
  85. this.mHorizontal = true;
  86. }
  87. else
  88. {
  89. if (this.mScroll.movement != UIScrollView.Movement.Vertical)
  90. {
  91. return false;
  92. }
  93. this.mHorizontal = false;
  94. }
  95. return true;
  96. }
  97. private void ResetChildPositions()
  98. {
  99. int i = 0;
  100. int count = this.mChildren.Count;
  101. while (i < count)
  102. {
  103. Transform transform = this.mChildren[i];
  104. transform.localPosition = ((!this.mHorizontal) ? new Vector3(0f, (float)(-(float)i * this.itemSize), 0f) : new Vector3((float)(i * this.itemSize), 0f, 0f));
  105. this.UpdateItem(transform, i);
  106. i++;
  107. }
  108. }
  109. public void WrapContent()
  110. {
  111. float num = (float)(this.itemSize * this.mChildren.Count) * 0.5f;
  112. Vector3[] worldCorners = this.mPanel.worldCorners;
  113. for (int i = 0; i < 4; i++)
  114. {
  115. Vector3 vector = worldCorners[i];
  116. vector = this.mTrans.InverseTransformPoint(vector);
  117. worldCorners[i] = vector;
  118. }
  119. Vector3 vector2 = Vector3.Lerp(worldCorners[0], worldCorners[2], 0.5f);
  120. bool flag = true;
  121. float num2 = num * 2f;
  122. if (this.mHorizontal)
  123. {
  124. float num3 = worldCorners[0].x - (float)this.itemSize;
  125. float num4 = worldCorners[2].x + (float)this.itemSize;
  126. int j = 0;
  127. int count = this.mChildren.Count;
  128. while (j < count)
  129. {
  130. Transform transform = this.mChildren[j];
  131. float num5 = transform.localPosition.x - vector2.x;
  132. if (num5 < -num)
  133. {
  134. Vector3 localPosition = transform.localPosition;
  135. localPosition.x += num2;
  136. num5 = localPosition.x - vector2.x;
  137. int num6 = Mathf.RoundToInt(localPosition.x / (float)this.itemSize);
  138. if (this.minIndex == this.maxIndex || (this.minIndex <= num6 && num6 <= this.maxIndex))
  139. {
  140. transform.localPosition = localPosition;
  141. this.UpdateItem(transform, j);
  142. }
  143. else
  144. {
  145. flag = false;
  146. }
  147. }
  148. else if (num5 > num)
  149. {
  150. Vector3 localPosition2 = transform.localPosition;
  151. localPosition2.x -= num2;
  152. num5 = localPosition2.x - vector2.x;
  153. int num7 = Mathf.RoundToInt(localPosition2.x / (float)this.itemSize);
  154. if (this.minIndex == this.maxIndex || (this.minIndex <= num7 && num7 <= this.maxIndex))
  155. {
  156. transform.localPosition = localPosition2;
  157. this.UpdateItem(transform, j);
  158. }
  159. else
  160. {
  161. flag = false;
  162. }
  163. }
  164. else if (this.mFirstTime)
  165. {
  166. this.UpdateItem(transform, j);
  167. }
  168. if (this.cullContent)
  169. {
  170. num5 += this.mPanel.clipOffset.x - this.mTrans.localPosition.x;
  171. if (!UICamera.IsPressed(transform.gameObject))
  172. {
  173. NGUITools.SetActive(transform.gameObject, num5 > num3 && num5 < num4, false);
  174. }
  175. }
  176. j++;
  177. }
  178. }
  179. else
  180. {
  181. float num8 = worldCorners[0].y - (float)this.itemSize;
  182. float num9 = worldCorners[2].y + (float)this.itemSize;
  183. int k = 0;
  184. int count2 = this.mChildren.Count;
  185. while (k < count2)
  186. {
  187. Transform transform2 = this.mChildren[k];
  188. float num10 = transform2.localPosition.y - vector2.y;
  189. if (num10 < -num)
  190. {
  191. Vector3 localPosition3 = transform2.localPosition;
  192. localPosition3.y += num2;
  193. num10 = localPosition3.y - vector2.y;
  194. int num11 = Mathf.RoundToInt(localPosition3.y / (float)this.itemSize);
  195. if (this.minIndex == this.maxIndex || (this.minIndex <= num11 && num11 <= this.maxIndex))
  196. {
  197. transform2.localPosition = localPosition3;
  198. this.UpdateItem(transform2, k);
  199. }
  200. else
  201. {
  202. flag = false;
  203. }
  204. }
  205. else if (num10 > num)
  206. {
  207. Vector3 localPosition4 = transform2.localPosition;
  208. localPosition4.y -= num2;
  209. num10 = localPosition4.y - vector2.y;
  210. int num12 = Mathf.RoundToInt(localPosition4.y / (float)this.itemSize);
  211. if (this.minIndex == this.maxIndex || (this.minIndex <= num12 && num12 <= this.maxIndex))
  212. {
  213. transform2.localPosition = localPosition4;
  214. this.UpdateItem(transform2, k);
  215. }
  216. else
  217. {
  218. flag = false;
  219. }
  220. }
  221. else if (this.mFirstTime)
  222. {
  223. this.UpdateItem(transform2, k);
  224. }
  225. if (this.cullContent)
  226. {
  227. num10 += this.mPanel.clipOffset.y - this.mTrans.localPosition.y;
  228. if (!UICamera.IsPressed(transform2.gameObject))
  229. {
  230. NGUITools.SetActive(transform2.gameObject, num10 > num8 && num10 < num9, false);
  231. }
  232. }
  233. k++;
  234. }
  235. }
  236. this.mScroll.restrictWithinPanel = !flag;
  237. }
  238. private void OnValidate()
  239. {
  240. if (this.maxIndex < this.minIndex)
  241. {
  242. this.maxIndex = this.minIndex;
  243. }
  244. if (this.minIndex > this.maxIndex)
  245. {
  246. this.maxIndex = this.minIndex;
  247. }
  248. }
  249. protected virtual void UpdateItem(Transform item, int index)
  250. {
  251. if (this.onInitializeItem != null)
  252. {
  253. int realIndex = (this.mScroll.movement != UIScrollView.Movement.Vertical) ? Mathf.RoundToInt(item.localPosition.x / (float)this.itemSize) : Mathf.RoundToInt(item.localPosition.y / (float)this.itemSize);
  254. this.onInitializeItem(item.gameObject, index, realIndex);
  255. }
  256. }
  257. public int itemSize = 100;
  258. public bool cullContent = true;
  259. public int minIndex;
  260. public int maxIndex;
  261. public UIWrapContent.OnInitializeItem onInitializeItem;
  262. private Transform mTrans;
  263. private UIPanel mPanel;
  264. private UIScrollView mScroll;
  265. private bool mHorizontal;
  266. private bool mFirstTime = true;
  267. private List<Transform> mChildren = new List<Transform>();
  268. [CompilerGenerated]
  269. private static Comparison<Transform> <>f__mg$cache0;
  270. [CompilerGenerated]
  271. private static Comparison<Transform> <>f__mg$cache1;
  272. [CompilerGenerated]
  273. private static Comparison<Transform> <>f__mg$cache2;
  274. public delegate void OnInitializeItem(GameObject go, int wrapIndex, int realIndex);
  275. }