-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUIGrid.cs
324 lines (288 loc) · 6.43 KB
/
UIGrid.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("NGUI/Interaction/Grid")]
public class UIGrid : UIWidgetContainer
{
public enum Arrangement
{
Horizontal,
Vertical,
CellSnap
}
public enum Sorting
{
None,
Alphabetic,
Horizontal,
Vertical,
Custom
}
public delegate void OnReposition();
public Arrangement arrangement;
public 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 OnReposition onReposition;
public Comparison<Transform> onCustomSort;
[HideInInspector]
[SerializeField]
private bool sorted;
protected bool mReposition;
protected UIPanel mPanel;
protected bool mInitDone;
public bool repositionNow
{
set
{
if (value)
{
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 (!hideInactive || ((bool)child && NGUITools.GetActive(child.gameObject)))
{
list.Add(child);
}
}
if (sorting != 0 && arrangement != Arrangement.CellSnap)
{
if (sorting == Sorting.Alphabetic)
{
list.Sort(SortByName);
}
else if (sorting == Sorting.Horizontal)
{
list.Sort(SortHorizontal);
}
else if (sorting == Sorting.Vertical)
{
list.Sort(SortVertical);
}
else if (onCustomSort != null)
{
list.Sort(onCustomSort);
}
else
{
Sort(list);
}
}
return list;
}
public Transform GetChild(int index)
{
List<Transform> childList = GetChildList();
return (index >= childList.Count) ? null : childList[index];
}
public int GetIndex(Transform trans)
{
return GetChildList().IndexOf(trans);
}
public void AddChild(Transform trans)
{
AddChild(trans, sort: true);
}
public void AddChild(Transform trans, bool sort)
{
if (trans != null)
{
trans.parent = base.transform;
ResetPosition(GetChildList());
}
}
public bool RemoveChild(Transform t)
{
List<Transform> childList = GetChildList();
if (childList.Remove(t))
{
ResetPosition(childList);
return true;
}
return false;
}
protected virtual void Init()
{
mInitDone = true;
mPanel = NGUITools.FindInParents<UIPanel>(base.gameObject);
}
protected virtual void Start()
{
if (!mInitDone)
{
Init();
}
bool flag = animateSmoothly;
animateSmoothly = false;
Reposition();
animateSmoothly = flag;
base.enabled = false;
}
protected virtual void Update()
{
Reposition();
base.enabled = false;
}
private void OnValidate()
{
if (!Application.isPlaying && NGUITools.GetActive(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)
{
Vector3 localPosition = a.localPosition;
ref float x = ref localPosition.x;
Vector3 localPosition2 = b.localPosition;
return x.CompareTo(localPosition2.x);
}
public static int SortVertical(Transform a, Transform b)
{
Vector3 localPosition = b.localPosition;
ref float y = ref localPosition.y;
Vector3 localPosition2 = a.localPosition;
return y.CompareTo(localPosition2.y);
}
protected virtual void Sort(List<Transform> list)
{
}
[ContextMenu("Execute")]
public virtual void Reposition()
{
if (Application.isPlaying && !mInitDone && NGUITools.GetActive(base.gameObject))
{
Init();
}
if (sorted)
{
sorted = false;
if (sorting == Sorting.None)
{
sorting = Sorting.Alphabetic;
}
NGUITools.SetDirty(this);
}
List<Transform> childList = GetChildList();
ResetPosition(childList);
if (keepWithinPanel)
{
ConstrainWithinPanel();
}
if (onReposition != null)
{
onReposition();
}
}
public void ConstrainWithinPanel()
{
if (mPanel != null)
{
mPanel.ConstrainTargetToBounds(base.transform, immediate: true);
UIScrollView component = mPanel.GetComponent<UIScrollView>();
if (component != null)
{
component.UpdateScrollbars(recalculateBounds: true);
}
}
}
protected void ResetPosition(List<Transform> list)
{
mReposition = false;
int num = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
Transform transform = base.transform;
int i = 0;
for (int count = list.Count; i < count; i++)
{
Transform transform2 = list[i];
Vector3 vector = transform2.localPosition;
float z = vector.z;
if (arrangement == Arrangement.CellSnap)
{
if (cellWidth > 0f)
{
vector.x = Mathf.Round(vector.x / cellWidth) * cellWidth;
}
if (cellHeight > 0f)
{
vector.y = Mathf.Round(vector.y / cellHeight) * cellHeight;
}
}
else
{
vector = ((arrangement != 0) ? new Vector3(cellWidth * (float)num2, (0f - cellHeight) * (float)num, z) : new Vector3(cellWidth * (float)num, (0f - cellHeight) * (float)num2, z));
}
if (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 >= maxPerLine && maxPerLine > 0)
{
num = 0;
num2++;
}
}
if (pivot != 0)
{
Vector2 pivotOffset = NGUIMath.GetPivotOffset(pivot);
float num5;
float num6;
if (arrangement == Arrangement.Horizontal)
{
num5 = Mathf.Lerp(0f, (float)num3 * cellWidth, pivotOffset.x);
num6 = Mathf.Lerp((float)(-num4) * cellHeight, 0f, pivotOffset.y);
}
else
{
num5 = Mathf.Lerp(0f, (float)num4 * cellWidth, pivotOffset.x);
num6 = Mathf.Lerp((float)(-num3) * 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)
{
component.target.x -= num5;
component.target.y -= num6;
}
else
{
Vector3 localPosition = child.localPosition;
localPosition.x -= num5;
localPosition.y -= num6;
child.localPosition = localPosition;
}
}
}
}
}