123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using UnityEngine;
- [AddComponentMenu("NGUI/Interaction/Grid")]
- public class UIGrid : UIWidgetContainer
- {
- public bool repositionNow
- {
- set
- {
- if (value)
- {
- this.mReposition = true;
- base.enabled = true;
- }
- }
- }
- public List<Transform> GetChildList()
- {
- Transform transform = base.transform;
- List<Transform> list = new List<Transform>();
- for (int i = 0; i < transform.childCount; i++)
- {
- Transform child = transform.GetChild(i);
- if (!this.hideInactive || (child && NGUITools.GetActive(child.gameObject)))
- {
- list.Add(child);
- }
- }
- if (this.sorting != UIGrid.Sorting.None && this.arrangement != UIGrid.Arrangement.CellSnap)
- {
- if (this.sorting == UIGrid.Sorting.Alphabetic)
- {
- List<Transform> list2 = list;
- if (UIGrid.<>f__mg$cache0 == null)
- {
- UIGrid.<>f__mg$cache0 = new Comparison<Transform>(UIGrid.SortByName);
- }
- list2.Sort(UIGrid.<>f__mg$cache0);
- }
- else if (this.sorting == UIGrid.Sorting.Horizontal)
- {
- List<Transform> list3 = list;
- if (UIGrid.<>f__mg$cache1 == null)
- {
- UIGrid.<>f__mg$cache1 = new Comparison<Transform>(UIGrid.SortHorizontal);
- }
- list3.Sort(UIGrid.<>f__mg$cache1);
- }
- else if (this.sorting == UIGrid.Sorting.Vertical)
- {
- List<Transform> list4 = list;
- if (UIGrid.<>f__mg$cache2 == null)
- {
- UIGrid.<>f__mg$cache2 = new Comparison<Transform>(UIGrid.SortVertical);
- }
- list4.Sort(UIGrid.<>f__mg$cache2);
- }
- else if (this.onCustomSort != null)
- {
- list.Sort(this.onCustomSort);
- }
- else
- {
- this.Sort(list);
- }
- }
- return list;
- }
- public Transform GetChild(int index)
- {
- List<Transform> childList = this.GetChildList();
- return (index >= childList.Count) ? null : childList[index];
- }
- public int GetIndex(Transform trans)
- {
- return this.GetChildList().IndexOf(trans);
- }
- public void AddChild(Transform trans)
- {
- this.AddChild(trans, true);
- }
- public void AddChild(Transform trans, bool sort)
- {
- if (trans != null)
- {
- trans.parent = base.transform;
- this.ResetPosition(this.GetChildList());
- }
- }
- public bool RemoveChild(Transform t)
- {
- List<Transform> childList = this.GetChildList();
- if (childList.Remove(t))
- {
- this.ResetPosition(childList);
- return true;
- }
- return false;
- }
- protected virtual void Init()
- {
- this.mInitDone = true;
- this.mPanel = NGUITools.FindInParents<UIPanel>(base.gameObject);
- }
- protected virtual void Start()
- {
- if (!this.mInitDone)
- {
- this.Init();
- }
- bool flag = this.animateSmoothly;
- this.animateSmoothly = false;
- this.Reposition();
- this.animateSmoothly = flag;
- base.enabled = false;
- }
- protected virtual void Update()
- {
- this.Reposition();
- base.enabled = false;
- }
- private void OnValidate()
- {
- if (!Application.isPlaying && NGUITools.GetActive(this))
- {
- this.Reposition();
- }
- }
- public static int SortByName(Transform a, Transform b)
- {
- return string.Compare(a.name, b.name);
- }
- public static int SortHorizontal(Transform a, Transform b)
- {
- return a.localPosition.x.CompareTo(b.localPosition.x);
- }
- public static int SortVertical(Transform a, Transform b)
- {
- return b.localPosition.y.CompareTo(a.localPosition.y);
- }
- protected virtual void Sort(List<Transform> list)
- {
- }
- [ContextMenu("Execute")]
- public virtual void Reposition()
- {
- if (Application.isPlaying && !this.mInitDone && NGUITools.GetActive(base.gameObject))
- {
- this.Init();
- }
- if (this.sorted)
- {
- this.sorted = false;
- if (this.sorting == UIGrid.Sorting.None)
- {
- this.sorting = UIGrid.Sorting.Alphabetic;
- }
- NGUITools.SetDirty(this);
- }
- List<Transform> childList = this.GetChildList();
- this.ResetPosition(childList);
- if (this.keepWithinPanel)
- {
- this.ConstrainWithinPanel();
- }
- if (this.onReposition != null)
- {
- this.onReposition();
- }
- }
- public void ConstrainWithinPanel()
- {
- if (this.mPanel != null)
- {
- this.mPanel.ConstrainTargetToBounds(base.transform, true);
- UIScrollView component = this.mPanel.GetComponent<UIScrollView>();
- if (component != null)
- {
- component.UpdateScrollbars(true);
- }
- }
- }
- protected virtual void ResetPosition(List<Transform> list)
- {
- this.mReposition = false;
- int num = 0;
- int num2 = 0;
- int num3 = 0;
- int num4 = 0;
- Transform transform = base.transform;
- int i = 0;
- int count = list.Count;
- while (i < count)
- {
- Transform transform2 = list[i];
- Vector3 vector = transform2.localPosition;
- float z = vector.z;
- if (this.arrangement == UIGrid.Arrangement.CellSnap)
- {
- if (this.cellWidth > 0f)
- {
- vector.x = Mathf.Round(vector.x / this.cellWidth) * this.cellWidth;
- }
- if (this.cellHeight > 0f)
- {
- vector.y = Mathf.Round(vector.y / this.cellHeight) * this.cellHeight;
- }
- }
- else
- {
- 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));
- }
- if (this.animateSmoothly && Application.isPlaying && Vector3.SqrMagnitude(transform2.localPosition - vector) >= 0.0001f)
- {
- SpringPosition springPosition = SpringPosition.Begin(transform2.gameObject, vector, 15f);
- springPosition.updateScrollView = true;
- springPosition.ignoreTimeScale = true;
- }
- else
- {
- transform2.localPosition = vector;
- }
- num3 = Mathf.Max(num3, num);
- num4 = Mathf.Max(num4, num2);
- if (++num >= this.maxPerLine && this.maxPerLine > 0)
- {
- num = 0;
- num2++;
- }
- i++;
- }
- if (this.pivot != UIWidget.Pivot.TopLeft)
- {
- Vector2 pivotOffset = NGUIMath.GetPivotOffset(this.pivot);
- float num5;
- float num6;
- if (this.arrangement == UIGrid.Arrangement.Horizontal)
- {
- num5 = Mathf.Lerp(0f, (float)num3 * this.cellWidth, pivotOffset.x);
- num6 = Mathf.Lerp((float)(-(float)num4) * this.cellHeight, 0f, pivotOffset.y);
- }
- else
- {
- num5 = Mathf.Lerp(0f, (float)num4 * this.cellWidth, pivotOffset.x);
- num6 = Mathf.Lerp((float)(-(float)num3) * this.cellHeight, 0f, pivotOffset.y);
- }
- for (int j = 0; j < transform.childCount; j++)
- {
- Transform child = transform.GetChild(j);
- SpringPosition component = child.GetComponent<SpringPosition>();
- if (component != null)
- {
- SpringPosition springPosition2 = component;
- springPosition2.target.x = springPosition2.target.x - num5;
- SpringPosition springPosition3 = component;
- springPosition3.target.y = springPosition3.target.y - num6;
- }
- else
- {
- Vector3 localPosition = child.localPosition;
- localPosition.x -= num5;
- localPosition.y -= num6;
- child.localPosition = localPosition;
- }
- }
- }
- }
- public UIGrid.Arrangement arrangement;
- public UIGrid.Sorting sorting;
- public UIWidget.Pivot pivot;
- public int maxPerLine;
- public float cellWidth = 200f;
- public float cellHeight = 200f;
- public bool animateSmoothly;
- public bool hideInactive;
- public bool keepWithinPanel;
- public UIGrid.OnReposition onReposition;
- public Comparison<Transform> onCustomSort;
- [HideInInspector]
- [SerializeField]
- private bool sorted;
- protected bool mReposition;
- protected UIPanel mPanel;
- protected bool mInitDone;
- [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 OnReposition();
- public enum Arrangement
- {
- Horizontal,
- Vertical,
- CellSnap
- }
- public enum Sorting
- {
- None,
- Alphabetic,
- Horizontal,
- Vertical,
- Custom
- }
- }
|