-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUICenterOnChild.cs
193 lines (177 loc) · 4.73 KB
/
UICenterOnChild.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
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("NGUI/Interaction/Center Scroll View on Child")]
public class UICenterOnChild : MonoBehaviour
{
public delegate void OnCenterCallback(GameObject centeredObject);
public float springStrength = 8f;
public float nextPageThreshold;
public SpringPanel.OnFinished onFinished;
public OnCenterCallback onCenter;
private UIScrollView mScrollView;
private GameObject mCenteredObject;
public GameObject centeredObject => mCenteredObject;
private void Start()
{
Recenter();
}
private void OnEnable()
{
if ((bool)mScrollView)
{
mScrollView.centerOnChild = this;
Recenter();
}
}
private void OnDisable()
{
if ((bool)mScrollView)
{
mScrollView.centerOnChild = null;
}
}
private void OnDragFinished()
{
if (base.enabled)
{
Recenter();
}
}
private void OnValidate()
{
nextPageThreshold = Mathf.Abs(nextPageThreshold);
}
[ContextMenu("Execute")]
public void Recenter()
{
if (mScrollView == null)
{
mScrollView = NGUITools.FindInParents<UIScrollView>(base.gameObject);
if (mScrollView == null)
{
Debug.LogWarning(GetType() + " requires " + typeof(UIScrollView) + " on a parent object in order to work", this);
base.enabled = false;
return;
}
if ((bool)mScrollView)
{
mScrollView.centerOnChild = this;
mScrollView.onDragFinished = OnDragFinished;
}
if (mScrollView.horizontalScrollBar != null)
{
mScrollView.horizontalScrollBar.onDragFinished = OnDragFinished;
}
if (mScrollView.verticalScrollBar != null)
{
mScrollView.verticalScrollBar.onDragFinished = OnDragFinished;
}
}
if (!(mScrollView.panel == null))
{
Transform transform = base.transform;
if (transform.childCount != 0)
{
Vector3[] worldCorners = mScrollView.panel.worldCorners;
Vector3 vector = (worldCorners[2] + worldCorners[0]) * 0.5f;
Vector3 velocity = mScrollView.currentMomentum * mScrollView.momentumAmount;
Vector3 a = NGUIMath.SpringDampen(ref velocity, 9f, 2f);
Vector3 b = vector - a * 0.01f;
float num = 3.40282347E+38f;
Transform target = null;
int num2 = 0;
int i = 0;
for (int childCount = transform.childCount; i < childCount; i++)
{
Transform child = transform.GetChild(i);
if (child.gameObject.activeInHierarchy)
{
float num3 = Vector3.SqrMagnitude(child.position - b);
if (num3 < num)
{
num = num3;
target = child;
num2 = i;
}
}
}
if (nextPageThreshold > 0f && UICamera.currentTouch != null && mCenteredObject != null && mCenteredObject.transform == transform.GetChild(num2))
{
Vector2 totalDelta = UICamera.currentTouch.totalDelta;
float num4 = 0f;
switch (mScrollView.movement)
{
case UIScrollView.Movement.Horizontal:
num4 = totalDelta.x;
break;
case UIScrollView.Movement.Vertical:
num4 = totalDelta.y;
break;
default:
num4 = totalDelta.magnitude;
break;
}
if (Mathf.Abs(num4) > nextPageThreshold)
{
UIGrid component = GetComponent<UIGrid>();
if (component != null && component.sorting != 0)
{
List<Transform> childList = component.GetChildList();
if (num4 > nextPageThreshold)
{
target = ((num2 <= 0) ? childList[0] : childList[num2 - 1]);
}
else if (num4 < 0f - nextPageThreshold)
{
target = ((num2 >= childList.Count - 1) ? childList[childList.Count - 1] : childList[num2 + 1]);
}
}
else
{
Debug.LogWarning("Next Page Threshold requires a sorted UIGrid in order to work properly", this);
}
}
}
CenterOn(target, vector);
}
}
}
private void CenterOn(Transform target, Vector3 panelCenter)
{
if (target != null && mScrollView != null && mScrollView.panel != null)
{
Transform cachedTransform = mScrollView.panel.cachedTransform;
mCenteredObject = target.gameObject;
Vector3 a = cachedTransform.InverseTransformPoint(target.position);
Vector3 b = cachedTransform.InverseTransformPoint(panelCenter);
Vector3 b2 = a - b;
if (!mScrollView.canMoveHorizontally)
{
b2.x = 0f;
}
if (!mScrollView.canMoveVertically)
{
b2.y = 0f;
}
b2.z = 0f;
SpringPanel.Begin(mScrollView.panel.cachedGameObject, cachedTransform.localPosition - b2, springStrength).onFinished = onFinished;
}
else
{
mCenteredObject = null;
}
if (onCenter != null)
{
onCenter(mCenteredObject);
}
}
public void CenterOn(Transform target)
{
if (mScrollView != null && mScrollView.panel != null)
{
Vector3[] worldCorners = mScrollView.panel.worldCorners;
Vector3 panelCenter = (worldCorners[2] + worldCorners[0]) * 0.5f;
CenterOn(target, panelCenter);
}
}
}