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
186 lines (165 loc) · 6.32 KB
/
MobileInput.cs
File metadata and controls
186 lines (165 loc) · 6.32 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
/*
* @author Michael Holub
* @author Valentin Simonov / http://va.lent.in/
*/
using TouchScript.Utils.Attributes;
using UnityEngine;
using System.Collections.Generic;
namespace TouchScript.InputSources
{
/// <summary>
/// Mobile Input Source. Gathers touch input from built-in Unity's Input.Touches API. Though, should be used on mobile devices.
/// </summary>
[AddComponentMenu("TouchScript/Input Sources/Mobile Input")]
public sealed class MobileInput : InputSource
{
#region Public properties
/// <summary>
/// Indicates if this input source should be disabled on platforms which don't support touch input with Input.Touches.
/// </summary>
[ToggleLeft]
public bool DisableOnNonTouchPlatforms = true;
/// <summary>
/// Tags added to touches coming from this input.
/// </summary>
public Tags Tags = new Tags(Tags.INPUT_TOUCH);
#endregion
#region Private variables
private Dictionary<int, TouchState> touchStates = new Dictionary<int, TouchState>();
private HashSet<int> touchIds = new HashSet<int>();
#endregion
#region Unity methods
/// <inheritdoc />
protected override void OnEnable()
{
if (DisableOnNonTouchPlatforms)
{
switch (Application.platform)
{
case RuntimePlatform.Android:
case RuntimePlatform.IPhonePlayer:
case RuntimePlatform.MetroPlayerARM:
case RuntimePlatform.MetroPlayerX64:
case RuntimePlatform.MetroPlayerX86:
case RuntimePlatform.WP8Player:
break;
default:
// don't need mobile touch here
enabled = false;
return;
}
}
base.OnEnable();
touchStates.Clear();
touchIds.Clear();
}
/// <inheritdoc />
protected override void OnDisable()
{
foreach (var touchState in touchStates)
{
cancelTouch(touchState.Value.Id);
}
base.OnDisable();
}
/// <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).Id;
touchStates[t.fingerId] = new TouchState(id, t.phase, t.position);
}
else
{
touchIds.Add(t.fingerId);
int id = beginTouch(t.position).Id;
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).Id;
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).Id;
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).Id;
cancelTouch(id);
}
break;
case TouchPhase.Stationary:
if (touchIds.Contains(t.fingerId)) {}
else
{
touchIds.Add(t.fingerId);
int id = beginTouch(t.position).Id;
touchStates.Add(t.fingerId, new TouchState(id, t.phase, t.position));
}
break;
}
}
}
/// <inheritdoc />
protected override ITouch beginTouch(Vector2 position)
{
return beginTouch(position, new Tags(Tags));
}
#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;
}
}
}