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
167 lines (147 loc) · 5.57 KB
/
Copy pathMobileInput.cs
File metadata and controls
167 lines (147 loc) · 5.57 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
/*
* @author Michael Holub
* @author Valentin Simonov / http://va.lent.in/
*/
using TouchScript.Utils.Editor.Attributes;
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 Public properties
[ToggleLeft]
public bool DisableOnNonTouchPlatforms = true;
#endregion
#region Private variables
private Dictionary<int, TouchState> touchStates = new Dictionary<int, TouchState>();
private HashSet<int> touchIds = new HashSet<int>();
#endregion
#region Unity methods
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();
}
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);
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;
}
}
}