Repository navigation
Expand file tree
/
Copy pathTouchClickManager.cs
More file actions
267 lines (216 loc) · 8.69 KB
/
Copy pathTouchClickManager.cs
File metadata and controls
267 lines (216 loc) · 8.69 KB
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
using System;
using UnityEngine;
namespace AsagiHandyScripts
{
public class TouchClickManager : MonoBehaviour
{
public class BaseTouchClickEvent : EventArgs
{
public float time;
public Vector2 position;
public bool isMouse;
public BaseTouchClickEvent(float _time, Vector2 _pos, bool _isMouse)
{
time = _time;
position = _pos;
isMouse = _isMouse;
}
}
public class TapEventArgs : BaseTouchClickEvent
{
public TapEventArgs(float _time, Vector2 _pos, bool _isMouse) : base(_time, _pos, _isMouse) {}
}
public event EventHandler<TapEventArgs> Tap;
public class TapHoldEventArgs : BaseTouchClickEvent
{
public float holdTime;
public TapHoldEventArgs(float _time, float _holdTime, Vector2 _pos, bool _isMouse) : base(_time, _pos, _isMouse)
{
holdTime = _holdTime;
}
}
public event EventHandler<TapHoldEventArgs> TapHold;
public class TapHoldInEventArgs : BaseTouchClickEvent
{
public TapHoldInEventArgs(float _time, Vector2 _pos, bool _isMouse) : base(_time, _pos, _isMouse) {}
}
public event EventHandler<TapHoldInEventArgs> TapHoldIn;
public class TapHoldOutEventArgs : BaseTouchClickEvent
{
public TapHoldOutEventArgs(float _time, Vector2 _pos, bool _isMouse) : base (_time, _pos, _isMouse) {}
}
public event EventHandler<TapHoldOutEventArgs> TapHoldOut;
public class TapHoldCancelEventArgs : BaseTouchClickEvent
{
public TapHoldCancelEventArgs(float _time, Vector2 _pos, bool _isMouse) : base(_time, _pos, _isMouse) { }
}
public event EventHandler<TapHoldCancelEventArgs> TapHoldCancel;
public class TouchInEventArgs : BaseTouchClickEvent
{
public TouchInEventArgs(float _time, Vector2 _pos, bool _isMouse) : base(_time, _pos, _isMouse) { }
}
public event EventHandler<TouchInEventArgs> TouchIn;
public class TouchOutEventArgs : BaseTouchClickEvent
{
public TouchOutEventArgs(float _time, Vector2 _pos, bool _isMouse) : base(_time, _pos, _isMouse) { }
}
public event EventHandler<TouchOutEventArgs> TouchOut;
public class SwipeEventArgs : BaseTouchClickEvent
{
public Vector2 moves;
public SwipeEventArgs(float _time, Vector2 _pos, Vector2 _moves, bool _isMouse) : base(_time, _pos, _isMouse)
{
moves = _moves;
}
}
public event EventHandler<SwipeEventArgs> Swipe;
public class SwipeStartEventArgs : BaseTouchClickEvent
{
public SwipeStartEventArgs(float _time, Vector2 _pos, bool _isMouse) : base(_time, _pos, _isMouse) { }
}
public event EventHandler<SwipeStartEventArgs> SwipeStart;
public class SwipeEndEventArgs : BaseTouchClickEvent
{
public SwipeEndEventArgs(float _time, Vector2 _pos, bool _isMouse) : base(_time, _pos, _isMouse) { }
}
public event EventHandler<SwipeEndEventArgs> SwipeEnd;
#region public Variables
public bool Tapped { get; private set; }
public bool Swiped { get; private set; }
public bool Held { get; private set; }
public bool Cancelled { get; private set; }
#endregion
#region serialized fields
[SerializeField]
float tapToSwipeMoves = 0.1f;
[SerializeField]
float timeHoldDetection = 1.0f;
[SerializeField]
bool debug;
#endregion
#region private variables
float tapStartTime = 0;
Vector2 tapStartPos = Vector2.zero;
Vector2 tapNowPos = Vector2.zero;
float lastTime;
#endregion
// Use this for initialization
void Start()
{
lastTime = Time.time;
Tapped = false;
Swiped = false;
Held = false;
Cancelled = false;
}
// Update is called once per frame
void Update()
{
bool mouseClick = Input.GetMouseButton(0);
bool isTouch = Input.touchCount > 0;
bool anyTap = (mouseClick || isTouch);
if (!Cancelled && anyTap)
{
float timeNow = Time.time;
Vector2 lastTouchPos = tapNowPos;
tapNowPos = GetPointerPosition(mouseClick);
DebugLog("tap now: " + tapNowPos.ToString());
float distanceTapPos = Vector2.Distance(tapNowPos, tapStartPos);
if (!Tapped)
{
tapStartPos = tapNowPos;
tapStartTime = Time.time;
Tapped = true;
if (TouchIn != null)
TouchIn(this, new TouchInEventArgs(timeNow, tapNowPos, mouseClick));
DebugLog("TouchIn : TouchClickManager : " + timeNow);
}
else if (!Held && distanceTapPos < tapToSwipeMoves && timeNow - tapStartTime >= timeHoldDetection)
{
Held = true;
if (TapHoldIn != null)
TapHoldIn(this, new TapHoldInEventArgs(timeNow, tapNowPos, mouseClick));
DebugLog("TapHoldIn : TouchClickManager : " + timeNow);
}
else if (!Held && !Swiped && distanceTapPos >= tapToSwipeMoves)
{
Swiped = true;
if (SwipeStart != null)
SwipeStart(this, new SwipeStartEventArgs(timeNow, tapStartPos, mouseClick));
DebugLog("SwipeStart : TouchClickManager : " + timeNow);
}
else if (Swiped)
{
if (Swipe != null)
Swipe(this, new SwipeEventArgs(timeNow, tapNowPos, tapNowPos - lastTouchPos, mouseClick));
DebugLog("Swiping : TouchClickManager : " + timeNow);
}
else if (Held)
{
if (TapHold != null)
TapHold(this, new TapHoldEventArgs(timeNow, timeNow - tapStartTime, tapNowPos, mouseClick));
DebugLog("Holding : TouchClickManager : " + timeNow);
}
else if (Held && !Swiped && distanceTapPos >= tapToSwipeMoves)
{
Tapped = false;
Swiped = false;
Held = false;
Cancelled = true;
if (TapHoldCancel != null)
TapHoldCancel(this, new TapHoldCancelEventArgs(timeNow, tapNowPos, mouseClick));
DebugLog("TapHoldCancelled : TouchClickManager : " + timeNow);
}
}
else if (!anyTap && (Tapped || Swiped || Held) && !Cancelled)
{
float timeNow = Time.time;
if (TouchOut != null)
TouchOut(this, new TouchOutEventArgs(timeNow, tapNowPos, mouseClick));
DebugLog("TouchOut : TouchClickManager : " + timeNow);
if (Swiped)
{
if (SwipeEnd != null)
SwipeEnd(this, new SwipeEndEventArgs(timeNow, tapNowPos, mouseClick));
DebugLog("SwipeIsEnd : TouchClickManager : " + timeNow);
}
else if (Held)
{
if (TapHoldOut != null)
TapHoldOut(this, new TapHoldOutEventArgs(timeNow, tapNowPos, mouseClick));
DebugLog("TapHeld : TouchClickManager : " + timeNow);
}
else if (Tapped)
{
if (Tap != null)
Tap(this, new TapEventArgs(timeNow, tapNowPos, mouseClick));
DebugLog("Tapped : TouchClickManager : " + timeNow);
}
ResetFlag();
}
else if (Cancelled && !anyTap)
{
Cancelled = false;
}
}
public Vector2 GetPointerPosition(bool isMouse)
{
if (isMouse)
return Input.mousePosition;
else
return Input.GetTouch(0).position;
}
void ResetFlag()
{
Tapped = false;
Swiped = false;
Held = false;
Cancelled = false;
}
void DebugLog(string str)
{
if (debug)
Debug.Log(str);
}
}
}