-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouchObserver.cs
More file actions
305 lines (263 loc) · 7.63 KB
/
TouchObserver.cs
File metadata and controls
305 lines (263 loc) · 7.63 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
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
using UnityEngine;
using System.Collections;
namespace Uzu
{
/// <summary>
/// Monitors 'touch' input events.
/// Many of the touch event phases supplied by Unity are
/// not reliable, as they may or may not be fired depending on the
/// frame rate.
/// </summary>
public class TouchObserver
{
/// <summary>
/// Wrapper class for encapsulating touch data whether it
/// comes from an actual touch or a mouse or some other input.
/// Allows the application to handle the input without knowing
/// from what source it came from.
/// </summary>
public struct TouchWrapper
{
public int touchId;
public Vector2 position;
public float deltaTime;
}
public const int LMB_TOUCH_ID = -1;
public const int RMB_TOUCH_ID = -2;
public delegate void OnTouchBeginDelegate (TouchWrapper touch);
public delegate void OnTouchUpdateDelegate (TouchWrapper touch);
public delegate void OnTouchEndDelegate (int touchId, Vector2 lastPosition);
public OnTouchBeginDelegate OnTouchBegin { get; set; }
public OnTouchUpdateDelegate OnTouchUpdate { get; set; }
public OnTouchEndDelegate OnTouchEnd { get; set; }
/// <summary>
/// Creates a new TouchObserver.
/// maxTouchCount # of touches will be monitored.
/// Any touches over this number will be ignored.
/// </summary>
public TouchObserver (int maxTouchCount)
{
MAX_TOUCH_TRACKER_COUNT = maxTouchCount;
// Allocate.
{
_trackers = new TouchTracker[MAX_TOUCH_TRACKER_COUNT];
for (int i = 0; i < MAX_TOUCH_TRACKER_COUNT; i++) {
_trackers [i] = new TouchTracker ();
}
}
}
/// <summary>
/// Manually clear all currently active touches.
/// </summary>
public void ClearTouches ()
{
for (int i = 0; i < _trackers.Length; i++) {
TouchTracker tracker = _trackers [i];
if (tracker.IsActive) {
DoTrackingEnd (tracker);
}
}
}
/// <summary>
/// Updates the states of all touches.
/// Call once per frame.
/// </summary>
public void Update ()
{
// Clear all active trackers dirty state.
for (int i = 0; i < _trackers.Length; i++) {
TouchTracker tracker = _trackers [i];
if (tracker.IsActive) {
tracker.IsDirty = false;
}
}
// Process all touches...
int touchCount = Input.touches.Length;
for (int i = 0; i < touchCount; i++) {
Touch touch = Input.touches [i];
TouchWrapper touchWrapper = TouchToTouchWrapper (touch);
// Are we already tracking this finger?
TouchTracker tracker = GetExistingTracker (touch.fingerId);
if (touch.phase == TouchPhase.Began) {
// If the tracker for this finger is already existing,
// but the touch phase is just beginning, then this is
// a different touch event. End the previous event, and detect
// a new event.
if (tracker != null) {
DoTrackingEnd (tracker);
tracker = null;
}
// New finger detected - start tracking if possible.
if (tracker == null) {
tracker = GetNewTracker ();
if (tracker != null) {
DoTrackingBegin (tracker, touchWrapper);
}
}
}
else {
// Update the tracker.
if (tracker != null) {
DoTrackingUpdate (tracker, touchWrapper);
}
}
}
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
// Mouse.
{
DoMouseProcessing (0, LMB_TOUCH_ID, ref _LMBDownStartTime, LMBToTouchWrapper);
DoMouseProcessing (1, RMB_TOUCH_ID, ref _RMBDownStartTime, RMBToTouchWrapper);
}
#endif
// Deactivate all active trackers that weren't updated this frame so they can be re-used.
// Ideally, TouchPhase.Ended could be used to do this, but it is not reliable.
for (int i = 0; i < _trackers.Length; i++) {
TouchTracker tracker = _trackers [i];
if (tracker.IsActive && !tracker.IsDirty) {
DoTrackingEnd (tracker);
}
}
}
private void DoMouseProcessing (int buttonIndex, int touchId, ref float downStartTime, System.Func <TouchWrapper> touchWrapperFunc)
{
if (Input.GetMouseButtonDown (buttonIndex)) {
TouchTracker tracker = GetExistingTracker (touchId);
if (tracker != null) {
Debug.LogWarning ("Mouse button [" + buttonIndex + "] pressed down twice without an up!?");
DoTrackingEnd (tracker);
}
tracker = GetNewTracker ();
if (tracker != null) {
downStartTime = Time.time;
TouchWrapper touch = touchWrapperFunc ();
DoTrackingBegin (tracker, touch);
}
}
else if (Input.GetMouseButtonUp (buttonIndex)) {
TouchTracker tracker = GetExistingTracker (touchId);
if (tracker != null) {
DoTrackingEnd (tracker);
}
}
else if (Input.GetMouseButton (buttonIndex)) {
TouchTracker tracker = GetExistingTracker (touchId);
if (tracker != null) {
TouchWrapper touch = touchWrapperFunc ();
DoTrackingUpdate (tracker, touch);
}
}
}
#region Implementation.
private readonly int MAX_TOUCH_TRACKER_COUNT;
private TouchTracker[] _trackers;
/// <summary>
/// Tracks a single touch.
/// </summary>
private class TouchTracker
{
private Vector2 _lastPosition;
private int _fingerId;
private bool _isActive = false;
private bool _isDirty = false;
public int FingerId {
get { return _fingerId; }
}
public bool IsActive {
get { return _isActive; }
}
public bool IsDirty {
get { return _isDirty; }
set { _isDirty = value; }
}
public Vector2 LastPosition {
get { return _lastPosition; }
}
public void BeginTracking (TouchWrapper touch)
{
_isActive = true;
_isDirty = true;
_fingerId = touch.touchId;
_lastPosition = touch.position;
}
public void Update (TouchWrapper touch)
{
_isDirty = true;
_lastPosition = touch.position;
}
public void EndTracking ()
{
_isActive = false;
}
}
private static TouchWrapper TouchToTouchWrapper (Touch t)
{
TouchWrapper touch = new TouchWrapper ();
touch.position = t.position;
touch.touchId = t.fingerId;
touch.deltaTime = t.deltaTime;
return touch;
}
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
private static float _LMBDownStartTime;
private static float _RMBDownStartTime;
private static TouchWrapper LMBToTouchWrapper ()
{
TouchWrapper touch = new TouchWrapper ();
touch.position = Input.mousePosition;
touch.touchId = LMB_TOUCH_ID;
touch.deltaTime = Time.time - _LMBDownStartTime;
return touch;
}
private static TouchWrapper RMBToTouchWrapper ()
{
TouchWrapper touch = new TouchWrapper ();
touch.position = Input.mousePosition;
touch.touchId = RMB_TOUCH_ID;
touch.deltaTime = Time.time - _RMBDownStartTime;
return touch;
}
#endif
private void DoTrackingBegin (TouchTracker tracker, TouchWrapper touch)
{
tracker.BeginTracking (touch);
if (OnTouchBegin != null) {
OnTouchBegin (touch);
}
}
private void DoTrackingUpdate (TouchTracker tracker, TouchWrapper touch)
{
tracker.Update (touch);
if (OnTouchUpdate != null) {
OnTouchUpdate (touch);
}
}
private void DoTrackingEnd (TouchTracker tracker)
{
if (OnTouchEnd != null) {
OnTouchEnd (tracker.FingerId, tracker.LastPosition);
}
tracker.EndTracking ();
}
private TouchTracker GetExistingTracker (int fingerId)
{
for (int i = 0; i < _trackers.Length; i++) {
TouchTracker tracker = _trackers [i];
if (tracker.IsActive && tracker.FingerId == fingerId) {
return tracker;
}
}
return null;
}
private TouchTracker GetNewTracker ()
{
for (int i = 0; i < _trackers.Length; i++) {
TouchTracker tracker = _trackers [i];
if (!tracker.IsActive) {
return tracker;
}
}
return null;
}
#endregion
}
}