-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUIRect.cs
548 lines (478 loc) · 10.8 KB
/
UIRect.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
using System;
using UnityEngine;
public abstract class UIRect : MonoBehaviour
{
[Serializable]
public class AnchorPoint
{
public Transform target;
public float relative;
public int absolute;
[NonSerialized]
public UIRect rect;
[NonSerialized]
public Camera targetCam;
public AnchorPoint()
{
}
public AnchorPoint(float relative)
{
this.relative = relative;
}
public void Set(float relative, float absolute)
{
this.relative = relative;
this.absolute = Mathf.FloorToInt(absolute + 0.5f);
}
public void Set(Transform target, float relative, float absolute)
{
this.target = target;
this.relative = relative;
this.absolute = Mathf.FloorToInt(absolute + 0.5f);
}
public void SetToNearest(float abs0, float abs1, float abs2)
{
SetToNearest(0f, 0.5f, 1f, abs0, abs1, abs2);
}
public void SetToNearest(float rel0, float rel1, float rel2, float abs0, float abs1, float abs2)
{
float num = Mathf.Abs(abs0);
float num2 = Mathf.Abs(abs1);
float num3 = Mathf.Abs(abs2);
if (num < num2 && num < num3)
{
Set(rel0, abs0);
}
else if (num2 < num && num2 < num3)
{
Set(rel1, abs1);
}
else
{
Set(rel2, abs2);
}
}
public void SetHorizontal(Transform parent, float localPos)
{
if ((bool)rect)
{
Vector3[] sides = rect.GetSides(parent);
float num = Mathf.Lerp(sides[0].x, sides[2].x, relative);
absolute = Mathf.FloorToInt(localPos - num + 0.5f);
}
else
{
Vector3 position = target.position;
if (parent != null)
{
position = parent.InverseTransformPoint(position);
}
absolute = Mathf.FloorToInt(localPos - position.x + 0.5f);
}
}
public void SetVertical(Transform parent, float localPos)
{
if ((bool)rect)
{
Vector3[] sides = rect.GetSides(parent);
float num = Mathf.Lerp(sides[3].y, sides[1].y, relative);
absolute = Mathf.FloorToInt(localPos - num + 0.5f);
}
else
{
Vector3 position = target.position;
if (parent != null)
{
position = parent.InverseTransformPoint(position);
}
absolute = Mathf.FloorToInt(localPos - position.y + 0.5f);
}
}
public Vector3[] GetSides(Transform relativeTo)
{
if (target != null)
{
if (rect != null)
{
return rect.GetSides(relativeTo);
}
if (target.GetComponent<Camera>() != null)
{
return target.GetComponent<Camera>().GetSides(relativeTo);
}
}
return null;
}
}
public enum AnchorUpdate
{
OnEnable,
OnUpdate,
OnStart
}
public AnchorPoint leftAnchor = new AnchorPoint();
public AnchorPoint rightAnchor = new AnchorPoint(1f);
public AnchorPoint bottomAnchor = new AnchorPoint();
public AnchorPoint topAnchor = new AnchorPoint(1f);
public AnchorUpdate updateAnchors = AnchorUpdate.OnUpdate;
protected GameObject mGo;
protected Transform mTrans;
protected BetterList<UIRect> mChildren = new BetterList<UIRect>();
protected bool mChanged = true;
protected bool mStarted;
protected bool mParentFound;
[NonSerialized]
private bool mUpdateAnchors = true;
[NonSerialized]
private int mUpdateFrame = -1;
[NonSerialized]
private bool mAnchorsCached;
[NonSerialized]
public float finalAlpha = 1f;
private UIRoot mRoot;
private UIRect mParent;
private bool mRootSet;
protected Camera mCam;
protected static Vector3[] mSides = new Vector3[4];
public GameObject cachedGameObject
{
get
{
if (mGo == null)
{
mGo = base.gameObject;
}
return mGo;
}
}
public Transform cachedTransform
{
get
{
if (mTrans == null)
{
mTrans = base.transform;
}
return mTrans;
}
}
public Camera anchorCamera
{
get
{
if (!mAnchorsCached)
{
ResetAnchors();
}
return mCam;
}
}
public bool isFullyAnchored => (bool)leftAnchor.target && (bool)rightAnchor.target && (bool)topAnchor.target && (bool)bottomAnchor.target;
public virtual bool isAnchoredHorizontally => (bool)leftAnchor.target || (bool)rightAnchor.target;
public virtual bool isAnchoredVertically => (bool)bottomAnchor.target || (bool)topAnchor.target;
public virtual bool canBeAnchored => true;
public UIRect parent
{
get
{
if (!mParentFound)
{
mParentFound = true;
mParent = NGUITools.FindInParents<UIRect>(cachedTransform.parent);
}
return mParent;
}
}
public UIRoot root
{
get
{
if (parent != null)
{
return mParent.root;
}
if (!mRootSet)
{
mRootSet = true;
mRoot = NGUITools.FindInParents<UIRoot>(cachedTransform);
}
return mRoot;
}
}
public bool isAnchored => ((bool)leftAnchor.target || (bool)rightAnchor.target || (bool)topAnchor.target || (bool)bottomAnchor.target) && canBeAnchored;
public abstract float alpha
{
get;
set;
}
public abstract Vector3[] localCorners
{
get;
}
public abstract Vector3[] worldCorners
{
get;
}
protected float cameraRayDistance
{
get
{
if (anchorCamera == null)
{
return 0f;
}
if (!mCam.orthographic)
{
Transform cachedTransform = this.cachedTransform;
Transform transform = mCam.transform;
Plane plane = new Plane(cachedTransform.rotation * Vector3.back, cachedTransform.position);
Ray ray = new Ray(transform.position, transform.rotation * Vector3.forward);
if (plane.Raycast(ray, out float enter))
{
return enter;
}
}
return Mathf.Lerp(mCam.nearClipPlane, mCam.farClipPlane, 0.5f);
}
}
public abstract float CalculateFinalAlpha(int frameID);
public virtual void Invalidate(bool includeChildren)
{
mChanged = true;
if (includeChildren)
{
for (int i = 0; i < mChildren.size; i++)
{
mChildren.buffer[i].Invalidate(includeChildren: true);
}
}
}
public virtual Vector3[] GetSides(Transform relativeTo)
{
if (anchorCamera != null)
{
return mCam.GetSides(cameraRayDistance, relativeTo);
}
Vector3 position = cachedTransform.position;
for (int i = 0; i < 4; i++)
{
mSides[i] = position;
}
if (relativeTo != null)
{
for (int j = 0; j < 4; j++)
{
mSides[j] = relativeTo.InverseTransformPoint(mSides[j]);
}
}
return mSides;
}
protected Vector3 GetLocalPos(AnchorPoint ac, Transform trans)
{
if (anchorCamera == null || ac.targetCam == null)
{
return cachedTransform.localPosition;
}
Vector3 vector = mCam.ViewportToWorldPoint(ac.targetCam.WorldToViewportPoint(ac.target.position));
if (trans != null)
{
vector = trans.InverseTransformPoint(vector);
}
vector.x = Mathf.Floor(vector.x + 0.5f);
vector.y = Mathf.Floor(vector.y + 0.5f);
return vector;
}
protected virtual void OnEnable()
{
mUpdateFrame = -1;
if (updateAnchors == AnchorUpdate.OnEnable)
{
mAnchorsCached = false;
mUpdateAnchors = true;
}
if (mStarted)
{
OnInit();
}
mUpdateFrame = -1;
}
protected virtual void OnInit()
{
mChanged = true;
mRootSet = false;
mParentFound = false;
if (parent != null)
{
mParent.mChildren.Add(this);
}
}
protected virtual void OnDisable()
{
if ((bool)mParent)
{
mParent.mChildren.Remove(this);
}
mParent = null;
mRoot = null;
mRootSet = false;
mParentFound = false;
}
protected void Start()
{
mStarted = true;
OnInit();
OnStart();
}
public void Update()
{
if (!mAnchorsCached)
{
ResetAnchors();
}
int frameCount = Time.frameCount;
if (mUpdateFrame != frameCount)
{
if (updateAnchors == AnchorUpdate.OnUpdate || mUpdateAnchors)
{
mUpdateFrame = frameCount;
mUpdateAnchors = false;
bool flag = false;
if ((bool)leftAnchor.target)
{
flag = true;
if (leftAnchor.rect != null && leftAnchor.rect.mUpdateFrame != frameCount)
{
leftAnchor.rect.Update();
}
}
if ((bool)bottomAnchor.target)
{
flag = true;
if (bottomAnchor.rect != null && bottomAnchor.rect.mUpdateFrame != frameCount)
{
bottomAnchor.rect.Update();
}
}
if ((bool)rightAnchor.target)
{
flag = true;
if (rightAnchor.rect != null && rightAnchor.rect.mUpdateFrame != frameCount)
{
rightAnchor.rect.Update();
}
}
if ((bool)topAnchor.target)
{
flag = true;
if (topAnchor.rect != null && topAnchor.rect.mUpdateFrame != frameCount)
{
topAnchor.rect.Update();
}
}
if (flag)
{
OnAnchor();
}
}
OnUpdate();
}
}
public void UpdateAnchors()
{
if (isAnchored && updateAnchors != AnchorUpdate.OnStart)
{
OnAnchor();
}
}
protected abstract void OnAnchor();
public void SetAnchor(Transform t)
{
leftAnchor.target = t;
rightAnchor.target = t;
topAnchor.target = t;
bottomAnchor.target = t;
ResetAnchors();
UpdateAnchors();
}
public void SetAnchor(GameObject go)
{
Transform target = (!(go != null)) ? null : go.transform;
leftAnchor.target = target;
rightAnchor.target = target;
topAnchor.target = target;
bottomAnchor.target = target;
ResetAnchors();
UpdateAnchors();
}
public void SetAnchor(GameObject go, int left, int bottom, int right, int top)
{
Transform target = (!(go != null)) ? null : go.transform;
leftAnchor.target = target;
rightAnchor.target = target;
topAnchor.target = target;
bottomAnchor.target = target;
leftAnchor.relative = 0f;
rightAnchor.relative = 1f;
bottomAnchor.relative = 0f;
topAnchor.relative = 1f;
leftAnchor.absolute = left;
rightAnchor.absolute = right;
bottomAnchor.absolute = bottom;
topAnchor.absolute = top;
ResetAnchors();
UpdateAnchors();
}
public void ResetAnchors()
{
mAnchorsCached = true;
leftAnchor.rect = ((!(bool)leftAnchor.target) ? null : leftAnchor.target.GetComponent<UIRect>());
bottomAnchor.rect = ((!(bool)bottomAnchor.target) ? null : bottomAnchor.target.GetComponent<UIRect>());
rightAnchor.rect = ((!(bool)rightAnchor.target) ? null : rightAnchor.target.GetComponent<UIRect>());
topAnchor.rect = ((!(bool)topAnchor.target) ? null : topAnchor.target.GetComponent<UIRect>());
mCam = NGUITools.FindCameraForLayer(cachedGameObject.layer);
FindCameraFor(leftAnchor);
FindCameraFor(bottomAnchor);
FindCameraFor(rightAnchor);
FindCameraFor(topAnchor);
mUpdateAnchors = true;
}
public void ResetAndUpdateAnchors()
{
ResetAnchors();
UpdateAnchors();
}
public abstract void SetRect(float x, float y, float width, float height);
private void FindCameraFor(AnchorPoint ap)
{
if (ap.target == null || ap.rect != null)
{
ap.targetCam = null;
}
else
{
ap.targetCam = NGUITools.FindCameraForLayer(ap.target.gameObject.layer);
}
}
public virtual void ParentHasChanged()
{
mParentFound = false;
UIRect y = NGUITools.FindInParents<UIRect>(cachedTransform.parent);
if (mParent != y)
{
if ((bool)mParent)
{
mParent.mChildren.Remove(this);
}
mParent = y;
if ((bool)mParent)
{
mParent.mChildren.Add(this);
}
mRootSet = false;
}
}
protected abstract void OnStart();
protected virtual void OnUpdate()
{
}
}