blob: b45379b51d64380b28416dd18245a2e3f2790eda (
plain)
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
|
using System;
using System.Collections.Generic;
using UnityEngine;
public class PassiveButtonManager : DestroyableSingleton<PassiveButtonManager>
{
public List<PassiveButton> Buttons = new List<PassiveButton>();
private List<IFocusHolder> FocusHolders = new List<IFocusHolder>();
private PassiveButton currentOver;
public Controller Controller = new Controller();
private PassiveButton currentDown;
private Collider2D[] results = new Collider2D[40];
private class DepthComparer : IComparer<PassiveButton>
{
public static readonly PassiveButtonManager.DepthComparer Instance = new PassiveButtonManager.DepthComparer();
public int Compare(PassiveButton x, PassiveButton y)
{
if (x == null)
{
return 1;
}
if (y == null)
{
return -1;
}
return x.transform.position.z.CompareTo(y.transform.position.z);
}
}
public void RegisterOne(PassiveButton button)
{
this.Buttons.Add(button);
}
public void RemoveOne(PassiveButton passiveButton)
{
this.Buttons.Remove(passiveButton);
}
public void RegisterOne(IFocusHolder focusHolder)
{
this.FocusHolders.Add(focusHolder);
}
public void RemoveOne(IFocusHolder focusHolder)
{
this.FocusHolders.Remove(focusHolder);
}
public void Update()
{
this.Controller.Update();
for (int i = 1; i < this.Buttons.Count; i++)
{
if (PassiveButtonManager.DepthComparer.Instance.Compare(this.Buttons[i - 1], this.Buttons[i]) > 0)
{
this.Buttons.Sort(PassiveButtonManager.DepthComparer.Instance);
break;
}
}
Vector2 position = this.Controller.Touches[0].Position;
int num = Physics2D.OverlapPointNonAlloc(position, this.results);
bool flag = false;
for (int j = 0; j < this.Buttons.Count; j++)
{
PassiveButton passiveButton = this.Buttons[j];
if (!passiveButton)
{
this.Buttons.RemoveAt(j);
j--;
}
else if (passiveButton.isActiveAndEnabled)
{
bool flag2 = false;
for (int k = 0; k < num; k++)
{
if (this.results[k].gameObject == passiveButton.gameObject)
{
flag2 = true;
break;
}
}
if (flag2)
{
flag = true;
if (passiveButton != this.currentOver)
{
if (this.currentOver)
{
this.currentOver.OnMouseOut.Invoke();
}
this.currentOver = passiveButton;
this.currentDown = null;
this.currentOver.OnMouseOver.Invoke();
break;
}
break;
}
}
}
if (!flag && this.currentOver)
{
this.currentOver.OnMouseOut.Invoke();
this.currentOver = null;
this.currentDown = null;
}
if (this.Controller.AnyTouchDown)
{
if (this.currentOver)
{
this.currentDown = this.currentOver;
if (this.currentOver.OnDown)
{
this.currentOver.DoClick();
}
}
this.HandleFocus(position);
return;
}
if (this.Controller.AnyTouchUp && this.currentDown)
{
if (this.currentDown.OnUp)
{
this.currentDown.DoClick();
}
this.currentDown = null;
}
}
private void CheckForDown()
{
Vector2 touch = this.GetTouch(true);
for (int i = 0; i < this.Buttons.Count; i++)
{
PassiveButton passiveButton = this.Buttons[i];
if (!passiveButton)
{
this.Buttons.RemoveAt(i);
i--;
}
else if (passiveButton.isActiveAndEnabled)
{
for (int j = 0; j < passiveButton.Colliders.Length; j++)
{
Collider2D collider2D = passiveButton.Colliders[j];
if (collider2D && collider2D.OverlapPoint(touch))
{
this.currentDown = passiveButton;
if (passiveButton.OnDown)
{
passiveButton.DoClick();
}
return;
}
}
}
}
this.HandleFocus(touch);
}
private void HandleFocus(Vector2 pt)
{
bool flag = false;
for (int i = 0; i < this.FocusHolders.Count; i++)
{
IFocusHolder focusHolder = this.FocusHolders[i];
if (!(focusHolder as MonoBehaviour))
{
this.FocusHolders.RemoveAt(i);
i--;
}
else if (focusHolder.CheckCollision(pt))
{
flag = true;
focusHolder.GiveFocus();
for (int j = 0; j < this.FocusHolders.Count; j++)
{
if (j != i)
{
this.FocusHolders[j].LoseFocus();
}
}
break;
}
}
if (!flag)
{
for (int k = 0; k < this.FocusHolders.Count; k++)
{
this.FocusHolders[k].LoseFocus();
}
}
}
private void HandleMouseOut(PassiveButton button)
{
if (this.currentOver == button)
{
button.OnMouseOut.Invoke();
this.currentOver = null;
}
}
private void CheckForUp()
{
if (!this.currentDown)
{
return;
}
PassiveButton passiveButton = this.currentDown;
this.currentDown = null;
if (!passiveButton.OnUp)
{
return;
}
Vector2 touch = this.GetTouch(false);
for (int i = 0; i < passiveButton.Colliders.Length; i++)
{
if (passiveButton.Colliders[i].OverlapPoint(touch))
{
if (passiveButton.OnUp)
{
passiveButton.DoClick();
}
return;
}
}
}
private Vector2 GetTouch(bool downOrUp)
{
if (downOrUp)
{
if (this.Controller.Touches[0].TouchStart)
{
return this.Controller.Touches[0].Position;
}
return this.Controller.Touches[1].Position;
}
else
{
if (this.Controller.Touches[0].TouchEnd)
{
return this.Controller.Touches[0].Position;
}
return this.Controller.Touches[1].Position;
}
}
}
|