forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMobileInput.cs
More file actions
121 lines (110 loc) · 4.31 KB
/
Copy pathMobileInput.cs
File metadata and controls
121 lines (110 loc) · 4.31 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
/*
* @author Michael Holub
*/
using UnityEngine;
using System.Collections.Generic;
namespace TouchScript.InputSources
{
/// <summary>
/// Mobile Input Source
/// </summary>
[AddComponentMenu("TouchScript/Input Sources/Mobile Input")]
public class MobileInput : InputSource
{
#region Private variables
private Dictionary<int, TouchState> touchStates = new Dictionary<int, TouchState>();
private HashSet<int> touchIds = new HashSet<int>();
#endregion
#region Unity
/// <inheritdoc />
protected override void Update()
{
base.Update();
for (var i = 0; i < Input.touchCount; ++i)
{
var t = Input.GetTouch(i);
switch (t.phase)
{
case TouchPhase.Began:
if (touchIds.Contains(t.fingerId))
{
// ending previous touch (maybe we missed a frame)
endTouch(t.fingerId);
int id = beginTouch(t.position);
touchStates[t.fingerId] = new TouchState(id, t.phase, t.position);
} else
{
touchIds.Add(t.fingerId);
int id = beginTouch(t.position);
touchStates.Add(t.fingerId, new TouchState(id, t.phase, t.position));
}
break;
case TouchPhase.Moved:
if (touchIds.Contains(t.fingerId))
{
var ts = touchStates[t.fingerId];
touchStates[t.fingerId] = new TouchState(ts.Id, t.phase, t.position);
moveTouch(ts.Id, t.position);
} else
{
// maybe we missed began phase
touchIds.Add(t.fingerId);
int id = beginTouch(t.position);
touchStates.Add(t.fingerId, new TouchState(id, t.phase, t.position));
}
break;
case TouchPhase.Ended:
if (touchIds.Contains(t.fingerId))
{
var ts = touchStates[t.fingerId];
touchIds.Remove(t.fingerId);
touchStates.Remove(t.fingerId);
endTouch(ts.Id);
} else
{
// maybe we totally missed one finger begin-end transition
int id = beginTouch(t.position);
endTouch(id);
}
break;
case TouchPhase.Canceled:
if (touchIds.Contains(t.fingerId))
{
var ts = touchStates[t.fingerId];
touchIds.Remove(t.fingerId);
touchStates.Remove(t.fingerId);
endTouch(ts.Id);
} else
{
// maybe we totally missed one finger begin-end transition
int id = beginTouch(t.position);
cancelTouch(id);
}
break;
case TouchPhase.Stationary:
if (touchIds.Contains(t.fingerId))
{} else
{
touchIds.Add(t.fingerId);
int id = beginTouch(t.position);
touchStates.Add(t.fingerId, new TouchState(id, t.phase, t.position));
}
break;
}
}
}
#endregion
}
internal struct TouchState
{
public int Id;
public TouchPhase Phase;
public Vector2 Position;
public TouchState(int anId, TouchPhase aPhase, Vector2 aPosition)
{
Id = anId;
Phase = aPhase;
Position = aPosition;
}
}
}