123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using UnityEngine;
- [AddComponentMenu("NGUI/Interaction/Wrap Content")]
- public class UIWrapContent : MonoBehaviour
- {
- protected virtual void Start()
- {
- this.SortBasedOnScrollMovement();
- this.WrapContent();
- if (this.mScroll != null)
- {
- this.mScroll.GetComponent<UIPanel>().onClipMove = new UIPanel.OnClippingMoved(this.OnMove);
- }
- this.mFirstTime = false;
- }
- protected virtual void OnMove(UIPanel panel)
- {
- this.WrapContent();
- }
- [ContextMenu("Sort Based on Scroll Movement")]
- public void SortBasedOnScrollMovement()
- {
- if (!this.CacheScrollView())
- {
- return;
- }
- this.mChildren.Clear();
- for (int i = 0; i < this.mTrans.childCount; i++)
- {
- this.mChildren.Add(this.mTrans.GetChild(i));
- }
- if (this.mHorizontal)
- {
- List<Transform> list = this.mChildren;
- if (UIWrapContent.<>f__mg$cache0 == null)
- {
- UIWrapContent.<>f__mg$cache0 = new Comparison<Transform>(UIGrid.SortHorizontal);
- }
- list.Sort(UIWrapContent.<>f__mg$cache0);
- }
- else
- {
- List<Transform> list2 = this.mChildren;
- if (UIWrapContent.<>f__mg$cache1 == null)
- {
- UIWrapContent.<>f__mg$cache1 = new Comparison<Transform>(UIGrid.SortVertical);
- }
- list2.Sort(UIWrapContent.<>f__mg$cache1);
- }
- this.ResetChildPositions();
- }
- [ContextMenu("Sort Alphabetically")]
- public void SortAlphabetically()
- {
- if (!this.CacheScrollView())
- {
- return;
- }
- this.mChildren.Clear();
- for (int i = 0; i < this.mTrans.childCount; i++)
- {
- this.mChildren.Add(this.mTrans.GetChild(i));
- }
- List<Transform> list = this.mChildren;
- if (UIWrapContent.<>f__mg$cache2 == null)
- {
- UIWrapContent.<>f__mg$cache2 = new Comparison<Transform>(UIGrid.SortByName);
- }
- list.Sort(UIWrapContent.<>f__mg$cache2);
- this.ResetChildPositions();
- }
- protected bool CacheScrollView()
- {
- this.mTrans = base.transform;
- this.mPanel = NGUITools.FindInParents<UIPanel>(base.gameObject);
- this.mScroll = this.mPanel.GetComponent<UIScrollView>();
- if (this.mScroll == null)
- {
- return false;
- }
- if (this.mScroll.movement == UIScrollView.Movement.Horizontal)
- {
- this.mHorizontal = true;
- }
- else
- {
- if (this.mScroll.movement != UIScrollView.Movement.Vertical)
- {
- return false;
- }
- this.mHorizontal = false;
- }
- return true;
- }
- private void ResetChildPositions()
- {
- int i = 0;
- int count = this.mChildren.Count;
- while (i < count)
- {
- Transform transform = this.mChildren[i];
- transform.localPosition = ((!this.mHorizontal) ? new Vector3(0f, (float)(-(float)i * this.itemSize), 0f) : new Vector3((float)(i * this.itemSize), 0f, 0f));
- this.UpdateItem(transform, i);
- i++;
- }
- }
- public void WrapContent()
- {
- float num = (float)(this.itemSize * this.mChildren.Count) * 0.5f;
- Vector3[] worldCorners = this.mPanel.worldCorners;
- for (int i = 0; i < 4; i++)
- {
- Vector3 vector = worldCorners[i];
- vector = this.mTrans.InverseTransformPoint(vector);
- worldCorners[i] = vector;
- }
- Vector3 vector2 = Vector3.Lerp(worldCorners[0], worldCorners[2], 0.5f);
- bool flag = true;
- float num2 = num * 2f;
- if (this.mHorizontal)
- {
- float num3 = worldCorners[0].x - (float)this.itemSize;
- float num4 = worldCorners[2].x + (float)this.itemSize;
- int j = 0;
- int count = this.mChildren.Count;
- while (j < count)
- {
- Transform transform = this.mChildren[j];
- float num5 = transform.localPosition.x - vector2.x;
- if (num5 < -num)
- {
- Vector3 localPosition = transform.localPosition;
- localPosition.x += num2;
- num5 = localPosition.x - vector2.x;
- int num6 = Mathf.RoundToInt(localPosition.x / (float)this.itemSize);
- if (this.minIndex == this.maxIndex || (this.minIndex <= num6 && num6 <= this.maxIndex))
- {
- transform.localPosition = localPosition;
- this.UpdateItem(transform, j);
- }
- else
- {
- flag = false;
- }
- }
- else if (num5 > num)
- {
- Vector3 localPosition2 = transform.localPosition;
- localPosition2.x -= num2;
- num5 = localPosition2.x - vector2.x;
- int num7 = Mathf.RoundToInt(localPosition2.x / (float)this.itemSize);
- if (this.minIndex == this.maxIndex || (this.minIndex <= num7 && num7 <= this.maxIndex))
- {
- transform.localPosition = localPosition2;
- this.UpdateItem(transform, j);
- }
- else
- {
- flag = false;
- }
- }
- else if (this.mFirstTime)
- {
- this.UpdateItem(transform, j);
- }
- if (this.cullContent)
- {
- num5 += this.mPanel.clipOffset.x - this.mTrans.localPosition.x;
- if (!UICamera.IsPressed(transform.gameObject))
- {
- NGUITools.SetActive(transform.gameObject, num5 > num3 && num5 < num4, false);
- }
- }
- j++;
- }
- }
- else
- {
- float num8 = worldCorners[0].y - (float)this.itemSize;
- float num9 = worldCorners[2].y + (float)this.itemSize;
- int k = 0;
- int count2 = this.mChildren.Count;
- while (k < count2)
- {
- Transform transform2 = this.mChildren[k];
- float num10 = transform2.localPosition.y - vector2.y;
- if (num10 < -num)
- {
- Vector3 localPosition3 = transform2.localPosition;
- localPosition3.y += num2;
- num10 = localPosition3.y - vector2.y;
- int num11 = Mathf.RoundToInt(localPosition3.y / (float)this.itemSize);
- if (this.minIndex == this.maxIndex || (this.minIndex <= num11 && num11 <= this.maxIndex))
- {
- transform2.localPosition = localPosition3;
- this.UpdateItem(transform2, k);
- }
- else
- {
- flag = false;
- }
- }
- else if (num10 > num)
- {
- Vector3 localPosition4 = transform2.localPosition;
- localPosition4.y -= num2;
- num10 = localPosition4.y - vector2.y;
- int num12 = Mathf.RoundToInt(localPosition4.y / (float)this.itemSize);
- if (this.minIndex == this.maxIndex || (this.minIndex <= num12 && num12 <= this.maxIndex))
- {
- transform2.localPosition = localPosition4;
- this.UpdateItem(transform2, k);
- }
- else
- {
- flag = false;
- }
- }
- else if (this.mFirstTime)
- {
- this.UpdateItem(transform2, k);
- }
- if (this.cullContent)
- {
- num10 += this.mPanel.clipOffset.y - this.mTrans.localPosition.y;
- if (!UICamera.IsPressed(transform2.gameObject))
- {
- NGUITools.SetActive(transform2.gameObject, num10 > num8 && num10 < num9, false);
- }
- }
- k++;
- }
- }
- this.mScroll.restrictWithinPanel = !flag;
- }
- private void OnValidate()
- {
- if (this.maxIndex < this.minIndex)
- {
- this.maxIndex = this.minIndex;
- }
- if (this.minIndex > this.maxIndex)
- {
- this.maxIndex = this.minIndex;
- }
- }
- protected virtual void UpdateItem(Transform item, int index)
- {
- if (this.onInitializeItem != null)
- {
- int realIndex = (this.mScroll.movement != UIScrollView.Movement.Vertical) ? Mathf.RoundToInt(item.localPosition.x / (float)this.itemSize) : Mathf.RoundToInt(item.localPosition.y / (float)this.itemSize);
- this.onInitializeItem(item.gameObject, index, realIndex);
- }
- }
- public int itemSize = 100;
- public bool cullContent = true;
- public int minIndex;
- public int maxIndex;
- public UIWrapContent.OnInitializeItem onInitializeItem;
- private Transform mTrans;
- private UIPanel mPanel;
- private UIScrollView mScroll;
- private bool mHorizontal;
- private bool mFirstTime = true;
- private List<Transform> mChildren = new List<Transform>();
- [CompilerGenerated]
- private static Comparison<Transform> <>f__mg$cache0;
- [CompilerGenerated]
- private static Comparison<Transform> <>f__mg$cache1;
- [CompilerGenerated]
- private static Comparison<Transform> <>f__mg$cache2;
- public delegate void OnInitializeItem(GameObject go, int wrapIndex, int realIndex);
- }
|