forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouchManagerInstance.cs
More file actions
540 lines (466 loc) · 15.8 KB
/
Copy pathTouchManagerInstance.cs
File metadata and controls
540 lines (466 loc) · 15.8 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
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
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using TouchScript.Devices.Display;
using TouchScript.Hit;
using TouchScript.InputSources;
using TouchScript.Layers;
using UnityEngine;
namespace TouchScript
{
/// <summary>
/// Default implementation of <see cref="ITouchManager"/>.
/// </summary>
internal sealed class TouchManagerInstance : MonoBehaviour, ITouchManager
{
#region Events
/// <inheritdoc />
public event EventHandler FrameStarted
{
add { frameStartedInvoker += value; }
remove { frameStartedInvoker -= value; }
}
/// <inheritdoc />
public event EventHandler FrameFinished
{
add { frameFinishedInvoker += value; }
remove { frameFinishedInvoker -= value; }
}
/// <inheritdoc />
public event EventHandler<TouchEventArgs> TouchesBegan
{
add { touchesBeganInvoker += value; }
remove { touchesBeganInvoker -= value; }
}
/// <inheritdoc />
public event EventHandler<TouchEventArgs> TouchesMoved
{
add { touchesMovedInvoker += value; }
remove { touchesMovedInvoker -= value; }
}
/// <inheritdoc />
public event EventHandler<TouchEventArgs> TouchesEnded
{
add { touchesEndedInvoker += value; }
remove { touchesEndedInvoker -= value; }
}
/// <inheritdoc />
public event EventHandler<TouchEventArgs> TouchesCancelled
{
add { touchesCancelledInvoker += value; }
remove { touchesCancelledInvoker -= value; }
}
// iOS Events AOT hack
private EventHandler<TouchEventArgs> touchesBeganInvoker, touchesMovedInvoker,
touchesEndedInvoker, touchesCancelledInvoker;
private EventHandler frameStartedInvoker, frameFinishedInvoker;
#endregion
#region Public properties
public static TouchManagerInstance Instance
{
get
{
if (shuttingDown) return null;
if (instance == null)
{
if (!Application.isPlaying) return null;
var objects = FindObjectsOfType<TouchManagerInstance>();
if (objects.Length == 0)
{
var go = new GameObject("TouchManager Instance");
go.hideFlags = HideFlags.HideInHierarchy;
DontDestroyOnLoad(go);
instance = go.AddComponent<TouchManagerInstance>();
}
else if (objects.Length >= 1)
{
instance = objects[0];
}
}
return instance;
}
}
/// <inheritdoc />
public IDisplayDevice DisplayDevice
{
get
{
if (displayDevice == null)
{
displayDevice = ScriptableObject.CreateInstance<GenericDisplayDevice>();
}
return displayDevice;
}
set
{
if (value == null)
{
displayDevice = ScriptableObject.CreateInstance<GenericDisplayDevice>();
}
else
{
displayDevice = value;
}
updateDPI();
}
}
/// <inheritdoc />
public float DPI
{
get { return dpi; }
}
/// <inheritdoc />
public IList<TouchLayer> Layers
{
get { return new ReadOnlyCollection<TouchLayer>(layers); }
}
/// <inheritdoc />
public float DotsPerCentimeter
{
get { return dotsPerCentimeter; }
}
/// <inheritdoc />
public int NumberOfTouches
{
get { return touches.Count; }
}
/// <inheritdoc />
public IList<ITouch> ActiveTouches
{
get { return touches.Cast<ITouch>().ToList(); }
}
#endregion
#region Private variables
private static bool shuttingDown = false;
private static TouchManagerInstance instance;
private IDisplayDevice displayDevice;
private float dpi = 96;
private float dotsPerCentimeter = TouchManager.CM_TO_INCH * 96;
private List<TouchLayer> layers = new List<TouchLayer>(10);
private List<TouchPoint> touches = new List<TouchPoint>(30);
private Dictionary<int, TouchPoint> idToTouch = new Dictionary<int, TouchPoint>(30);
// Upcoming changes
private List<TouchPoint> touchesBegan = new List<TouchPoint>(30);
private HashSet<int> touchesUpdated = new HashSet<int>();
private HashSet<int> touchesEnded = new HashSet<int>();
private HashSet<int> touchesCancelled = new HashSet<int>();
private int nextTouchId = 0;
#endregion
#region Public methods
/// <inheritdoc />
public bool AddLayer(TouchLayer layer)
{
if (layer == null) return false;
if (layers.Contains(layer)) return true;
layers.Add(layer);
return true;
}
/// <inheritdoc />
public bool AddLayer(TouchLayer layer, int index)
{
if (layer == null) return false;
if (index >= layers.Count) return AddLayer(layer);
var i = layers.IndexOf(layer);
if (i == -1)
{
layers.Insert(index, layer);
}
else
{
if (index == i || i == index - 1) return true;
layers.RemoveAt(i);
if (index < i) layers.Insert(index, layer);
else layers.Insert(index - 1, layer);
}
return true;
}
/// <inheritdoc />
public bool RemoveLayer(TouchLayer layer)
{
if (layer == null) return false;
var result = layers.Remove(layer);
return result;
}
/// <inheritdoc />
public void ChangeLayerIndex(int at, int to)
{
if (at < 0 || at >= layers.Count) return;
if (to < 0 || to >= layers.Count) return;
var data = layers[at];
layers.RemoveAt(at);
layers.Insert(to, data);
}
/// <inheritdoc />
public Transform GetHitTarget(Vector2 position)
{
ITouchHit hit;
TouchLayer layer;
if (GetHitTarget(position, out hit, out layer)) return hit.Transform;
return null;
}
/// <inheritdoc />
public bool GetHitTarget(Vector2 position, out ITouchHit hit)
{
TouchLayer layer;
return GetHitTarget(position, out hit, out layer);
}
/// <inheritdoc />
public bool GetHitTarget(Vector2 position, out ITouchHit hit, out TouchLayer layer)
{
hit = null;
layer = null;
foreach (var touchLayer in layers)
{
if (touchLayer == null) continue;
ITouchHit _hit;
if (touchLayer.Hit(position, out _hit) == TouchLayer.LayerHitResult.Hit)
{
hit = _hit;
layer = touchLayer;
return true;
}
}
return false;
}
#endregion
#region Internal methods
internal ITouch BeginTouch(Vector2 position)
{
return BeginTouch(position, null);
}
internal ITouch BeginTouch(Vector2 position, Tags tags)
{
TouchPoint touch;
lock (touchesBegan)
{
touch = new TouchPoint(nextTouchId++, position, tags);
touchesBegan.Add(touch);
}
return touch;
}
internal void UpdateTouch(int id)
{
lock (touchesUpdated)
{
if (idToTouch.ContainsKey(id)) touchesUpdated.Add(id);
}
}
internal void MoveTouch(int id, Vector2 position)
{
lock (touchesUpdated)
{
TouchPoint touch;
if (idToTouch.TryGetValue(id, out touch))
{
touch.SetPosition(position);
touchesUpdated.Add(id);
}
}
}
/// <inheritdoc />
internal void EndTouch(int id)
{
lock (touchesEnded)
{
TouchPoint touch;
if (!idToTouch.TryGetValue(id, out touch))
{
// This touch was added this frame
touch = touchesBegan.Find((t) => t.Id == id);
// No touch with such id
if (touch == null) return;
}
touchesEnded.Add(touch.Id);
}
}
/// <inheritdoc />
internal void CancelTouch(int id)
{
lock (touchesCancelled)
{
TouchPoint touch;
if (!idToTouch.TryGetValue(id, out touch))
{
// This touch was added this frame
touch = touchesBegan.Find((t) => t.Id == id);
// No touch with such id
if (touch == null) return;
}
touchesCancelled.Add(touch.Id);
}
}
#endregion
#region Unity
private void Awake()
{
if (instance == null) instance = this;
updateDPI();
StopAllCoroutines();
StartCoroutine("lateAwake");
}
private void OnLevelWasLoaded(int value)
{
StopAllCoroutines();
StartCoroutine("lateAwake");
}
private IEnumerator lateAwake()
{
yield return null;
updateLayers();
createCameraLayer();
createTouchInput();
}
private void Update()
{
updateTouches();
}
private void OnApplicationQuit()
{
shuttingDown = true;
}
#endregion
#region Private functions
private void updateDPI()
{
dpi = DisplayDevice == null ? 96 : DisplayDevice.DPI;
dotsPerCentimeter = TouchManager.CM_TO_INCH * dpi;
}
private void updateLayers()
{
// filter empty layers
layers = layers.FindAll(l => l != null);
}
private void createCameraLayer()
{
if (layers.Count == 0)
{
Debug.LogWarning("No camera layers, adding CameraLayer for the main camera. (this message is harmless)");
if (Camera.main != null)
{
var layer = Camera.main.GetComponent<TouchLayer>();
if (layer == null) layer = Camera.main.gameObject.AddComponent<CameraLayer>();
AddLayer(layer);
}
}
}
private void createTouchInput()
{
var inputs = FindObjectsOfType(typeof(InputSource));
if (inputs.Length == 0)
{
GameObject obj = null;
var objects = FindObjectsOfType<TouchManager>();
if (objects.Length == 0)
{
obj = GameObject.Find("TouchScript");
if (obj == null) obj = new GameObject("TouchScript");
}
else
{
obj = objects[0].gameObject;
}
switch (Application.platform)
{
case RuntimePlatform.IPhonePlayer:
case RuntimePlatform.Android:
case RuntimePlatform.BlackBerryPlayer:
case RuntimePlatform.MetroPlayerARM:
case RuntimePlatform.MetroPlayerX64:
case RuntimePlatform.MetroPlayerX86:
case RuntimePlatform.WP8Player:
obj.AddComponent<MobileInput>();
break;
default:
obj.AddComponent<MouseInput>();
break;
}
}
}
private void updateBegan()
{
if (touchesBegan.Count > 0)
{
foreach (var touch in touchesBegan)
{
touches.Add(touch);
idToTouch.Add(touch.Id, touch);
foreach (var touchLayer in layers)
{
if (touchLayer == null) continue;
if (touchLayer.BeginTouch(touch)) break;
}
}
if (touchesBeganInvoker != null) touchesBeganInvoker(this, new TouchEventArgs(touchesBegan.ConvertAll((t) => t as ITouch)));
touchesBegan.Clear();
}
}
private void updateUpdated()
{
if (touchesUpdated.Count > 0)
{
var updated = new List<ITouch>(touchesUpdated.Count);
// Need to loop through all touches to reset those which did not move
foreach (var touch in touches)
{
touch.ResetPosition();
if (touchesUpdated.Contains(touch.Id))
{
updated.Add(touch);
if (touch.Layer != null) touch.Layer.UpdateTouch(touch);
}
}
if (touchesMovedInvoker != null) touchesMovedInvoker(this, new TouchEventArgs(updated));
touchesUpdated.Clear();
}
}
private void updateEnded()
{
if (touchesEnded.Count > 0)
{
var updated = new List<ITouch>(touchesEnded.Count);
foreach (var id in touchesEnded)
{
var touch = idToTouch[id];
idToTouch.Remove(touch.Id);
touches.Remove(touch);
updated.Add(touch);
if (touch.Layer != null) touch.Layer.EndTouch(touch);
}
if (touchesEndedInvoker != null) touchesEndedInvoker(this, new TouchEventArgs(updated));
touchesEnded.Clear();
}
}
private void updateCancelled()
{
if (touchesCancelled.Count > 0)
{
var updated = new List<ITouch>(touchesCancelled.Count);
foreach (var id in touchesCancelled)
{
var touch = idToTouch[id];
idToTouch.Remove(touch.Id);
touches.Remove(touch);
updated.Add(touch);
if (touch.Layer != null) touch.Layer.CancelTouch(touch);
}
if (touchesCancelledInvoker != null) touchesCancelledInvoker(this, new TouchEventArgs(updated));
touchesCancelled.Clear();
}
}
private void updateTouches()
{
if (frameStartedInvoker != null) frameStartedInvoker(this, EventArgs.Empty);
lock (touchesBegan) updateBegan();
lock (touchesUpdated) updateUpdated();
lock (touchesEnded) updateEnded();
lock (touchesCancelled) updateCancelled();
if (frameFinishedInvoker != null) frameFinishedInvoker(this, EventArgs.Empty);
}
#endregion
}
}