-
Notifications
You must be signed in to change notification settings - Fork 1
/
NiceTouchForwarder.cs
205 lines (169 loc) · 7.44 KB
/
NiceTouchForwarder.cs
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
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NiceTouch.GestureReceiving;
namespace NiceTouch
{
public partial class NiceTouchForwarder : IGestureReceiver
{
// todo: raw version of each gesture that is unclaimed, fruit ninja style with typical godot bindings
// these classes are made, just need to create and call them
readonly Dictionary<Touch, HashSet<IGestureInterpreter>> _claimedTouches =
new Dictionary<Touch, HashSet<IGestureInterpreter>>();
readonly Dictionary<IGestureInterpreter, HashSet<Touch>> _touchesClaimedByInterpreters =
new Dictionary<IGestureInterpreter, HashSet<Touch>>();
readonly HashSet<Touch> _unclaimedTouches = new HashSet<Touch>();
readonly Stack<HashSet<IGestureInterpreter>> _recycledInterpreterCollections =
new Stack<HashSet<IGestureInterpreter>>();
readonly Stack<HashSet<Touch>> _recycledTouchCollections = new Stack<HashSet<Touch>>();
InterpreterWithGestureTouchesPool _interpreterWithGestureTouchesPool = new InterpreterWithGestureTouchesPool();
public void OnSingleTouch(object sender, TouchData touchData)
{
Touch touch = touchData.Touch;
_unclaimedTouches.Add(touch);
if (touchData.Pressed)
{
BeginSingleTouch(touch);
}
else
{
EndSingleTouch(touch);
}
}
public void OnSingleDrag(object sender, Touch touch)
{
HashSet<IGestureInterpreter> touchers = _claimedTouches[touch];
foreach (var g in touchers)
g.OnSingleDrag(touch);
}
public void OnSingleLongPress(object sender, Touch touch)
{
HashSet<IGestureInterpreter> touchers = _claimedTouches[touch];
foreach (var g in touchers)
g.OnSingleTap(touch);
}
public void OnSingleSwipe(object sender, Touch touch)
{
HashSet<IGestureInterpreter> touchers = _claimedTouches[touch];
foreach (var g in touchers)
g.OnSingleSwipe(touch);
}
public void OnSingleTap(object sender, Touch touch)
{
HashSet<IGestureInterpreter> touchers = _claimedTouches[touch];
foreach (var g in touchers)
g.OnSingleTap(touch);
}
// comprised of one drag + one drag/hold
public void OnTwist(object sender, TwistData gesture)
{
FilterTouches(ref gesture,
out List<IGestureInterpreter> fullReceivers,
out List<InterpreterWithGestureTouches> partialReceivers);
foreach (IGestureInterpreter g in fullReceivers)
g.OnTwist(gesture);
foreach (InterpreterWithGestureTouches receiver in partialReceivers)
{
Debug.Assert(receiver.JustOneTouch); //twists only have two touches so this must be one
Touch touch = receiver.Touches[0];
receiver.Interpreter.OnSingleDrag(touch);
}
}
// comprised of one drag + one drag/hold
public void OnPinch(object sender, PinchData gesture)
{
FilterTouches(ref gesture,
out List<IGestureInterpreter> fullReceivers,
out List<InterpreterWithGestureTouches> partialReceivers);
foreach (IGestureInterpreter g in fullReceivers)
g.OnPinch(gesture);
foreach (InterpreterWithGestureTouches receiver in partialReceivers)
{
Debug.Assert(receiver.JustOneTouch); //pinches only have two touches so this must be one
Touch touch = receiver.Touches[0];
receiver.Interpreter.OnSingleDrag(touch);
// todo: pinches and twists should be allowed to contain a stationary touch
}
}
public void OnMultiDrag(object sender, MultiDragData gesture)
{
FilterTouches(ref gesture,
out List<IGestureInterpreter> fullReceivers,
out List<InterpreterWithGestureTouches> partialReceivers);
foreach (IGestureInterpreter g in fullReceivers)
g.OnMultiDrag(gesture);
foreach (InterpreterWithGestureTouches receiver in partialReceivers)
{
if (receiver.JustOneTouch)
{
receiver.Interpreter.OnSingleDrag(receiver.Touches[0]);
continue;
}
var multiGesture = new MultiDragData(receiver.TouchListCopy);
receiver.Interpreter.OnMultiDrag(multiGesture);
}
}
public void OnMultiLongPress(object sender, MultiLongPressData gesture)
{
FilterTouches(ref gesture,
out List<IGestureInterpreter> fullReceivers,
out List<InterpreterWithGestureTouches> partialReceivers);
foreach (IGestureInterpreter g in fullReceivers)
g.OnMultiLongPress(gesture);
foreach (InterpreterWithGestureTouches receiver in partialReceivers)
{
if (receiver.JustOneTouch)
{
receiver.Interpreter.OnSingleLongPress(receiver.Touches[0]);
continue;
}
var multiGesture = new MultiLongPressData(receiver.TouchListCopy);
receiver.Interpreter.OnMultiLongPress(multiGesture);
}
}
public void OnMultiSwipe(object sender, MultiSwipeData gesture)
{
FilterTouches(ref gesture,
out List<IGestureInterpreter> fullReceivers,
out List<InterpreterWithGestureTouches> partialReceivers);
foreach (IGestureInterpreter g in fullReceivers)
g.OnMultiSwipe(gesture);
foreach (InterpreterWithGestureTouches receiver in partialReceivers)
{
if (receiver.JustOneTouch)
{
receiver.Interpreter.OnSingleSwipe(receiver.Touches[0]);
continue;
}
var multiGesture = new MultiSwipeData(receiver.TouchListCopy);
receiver.Interpreter.OnMultiSwipe(multiGesture);
}
}
public void OnMultiTap(object sender, MultiTapData gesture)
{
FilterTouches(ref gesture,
out List<IGestureInterpreter> fullReceivers,
out List<InterpreterWithGestureTouches> partialReceivers);
foreach (IGestureInterpreter g in fullReceivers)
g.OnMultiTap(gesture);
foreach (InterpreterWithGestureTouches receiver in partialReceivers)
{
if (receiver.JustOneTouch)
{
receiver.Interpreter.OnSingleTap(receiver.Touches[0]);
continue;
}
var multiGesture = new MultiTapData(receiver.TouchListCopy);
receiver.Interpreter.OnMultiTap(multiGesture);
}
}
public void OnRawMultiDrag(object sender, RawMultiDragData e)
{
return;
}
public void OnRawPinchTwist(object sender, RawTwoFingerDragData e)
{
return;
}
}
}