This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGlideTouch.cs
463 lines (395 loc) · 15.1 KB
/
GlideTouch.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
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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) GHI Electronics, LLC.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using GHI.Glide.Geom;
namespace GHI.Glide
{
/// <summary>
/// Touch event arguments.
/// </summary>
public class TouchEventArgs
{
/// <summary>
/// Indicates whether or not to continue processing the event.
/// </summary>
public bool Propagate = true;
/// <summary>
/// The point of contact.
/// </summary>
public Point Point;
/// <summary>
/// Creates a new TouchEventArgs.
/// </summary>
/// <param name="Touches">TouchInput</param>
public TouchEventArgs(TouchInput[] Touches)
{
Point = new Point(Touches[0].X, Touches[0].Y);
}
/// <summary>
/// Creates a new TouchEventArgs.
/// </summary>
/// <param name="point">Point</param>
public TouchEventArgs(Point point)
{
Point = point;
}
/// <summary>
/// Stops propagation.
/// </summary>
public void StopPropagation()
{
Propagate = false;
}
}
/// <summary>
/// The TouchGesture class defines gestures.
/// </summary>
public enum TouchGesture : uint
{
/// <summary>
/// No Gesture
/// </summary>
/// <remarks>Can be used to represent an error gesture or unknown gesture.</remarks>
NoGesture = 0,
/// <summary>
/// Begin
/// </summary>
/// <remarks>Used to identify the beginning of a Gesture Sequence; App can use this to highlight UIElement or some other sort of notification.</remarks>
Begin = 1,
/// <summary>
/// End
/// </summary>
/// <remarks>Used to identify the end of a gesture sequence; Fired when last finger involved in a gesture is removed.</remarks>
End = 2,
/// <summary>
/// Right
/// </summary>
Right = 3,
/// <summary>
/// Up Right
/// </summary>
UpRight = 4,
/// <summary>
/// Up
/// </summary>
Up = 5,
/// <summary>
/// Up Left
/// </summary>
UpLeft = 6,
/// <summary>
/// Left
/// </summary>
Left = 7,
/// <summary>
/// Down Left
/// </summary>
DownLeft = 8,
/// <summary>
/// Down
/// </summary>
Down = 9,
/// <summary>
/// Down Right
/// </summary>
DownRight = 10,
/// <summary>
/// Tap
/// </summary>
Tap = 11,
/// <summary>
/// Double Tap
/// </summary>
DoubleTap = 12,
/// <summary>
/// Zoom
/// </summary>
/// <remarks>Equivalent to your "Pinch" gesture.</remarks>
Zoom = 114,
/// <summary>
/// Pan
/// </summary>
/// <remarks>Equivalent to your "Scroll" gesture.</remarks>
Pan = 115,
/// <summary>
/// Rotate
/// </summary>
Rotate = 116,
/// <summary>
/// Two finger tap.
/// </summary>
TwoFingerTap = 117,
/// <summary>
/// Rollover
/// </summary>
Rollover = 118,
/// <summary>
/// Undefined
/// </summary>
/// <remarks>Additional touch gestures.</remarks>
UserDefined = 200,
}
/// <summary>
/// Touch gesture event arguments.
/// </summary>
public class TouchGestureEventArgs
{
/// <summary>
/// Indicates whether or not to continue processing the event.
/// </summary>
public bool Propagate = true;
/// <summary>
/// Time the event occured.
/// </summary>
public readonly DateTime Timestamp;
/// <summary>
/// X coordinate.
/// </summary>
/// <remarks>The X forms the center location of the gesture for multi-touch or the starting location for single touch.</remarks>
public readonly int X;
/// <summary>
/// Y coordinate.
/// </summary>
/// <remarks>The Y forms the center location of the gesture for multi-touch or the starting location for single touch.</remarks>
public readonly int Y;
/// <summary>
/// Indicates the gesture.
/// </summary>
public readonly TouchGesture Gesture;
// 2 bytes for gesture-specific arguments.
// TouchGesture.Zoom: Arguments = distance between fingers
// TouchGesture.Rotate: Arguments = angle in degrees (0-360)
/// <summary>
/// Touch gesture arguments.
/// </summary>
public readonly ushort Arguments;
/// <summary>
/// Creates a new TouchGestureEventArgs.
/// </summary>
/// <param name="gesture">Touch gesture.</param>
/// <param name="x">X-axis position.</param>
/// <param name="y">Y-axis position.</param>
/// <param name="arguments">Touch gesture arguments.</param>
/// <param name="timestamp">Time the event occured.</param>
public TouchGestureEventArgs(TouchGesture gesture, int x, int y, ushort arguments, DateTime timestamp)
{
Gesture = gesture;
X = x;
Y = y;
Arguments = arguments;
Timestamp = timestamp;
}
/// <summary>
/// Stops propagation.
/// </summary>
public void StopPropagation()
{
Propagate = false;
}
}
/// <summary>
/// Touch event handler.
/// </summary>
/// <param name="sender">Object associated with the event.</param>
/// <param name="e">Touch event arguments.</param>
public delegate void TouchEventHandler(object sender, TouchEventArgs e);
/// <summary>
/// Touch gesture event handler.
/// </summary>
/// <param name="sender">Object associated with the event.</param>
/// <param name="e">Touch event arguments.</param>
public delegate void TouchGestureEventHandler(object sender, TouchGestureEventArgs e);
/// <summary>
/// The GlideTouch class handles all touch functionality.
/// </summary>
public static class GlideTouch
{
/// <summary>
/// X-axis of the last touch point.
/// </summary>
public static int TouchX;
/// <summary>
/// Y-axis of the last touch point.
/// </summary>
public static int TouchY;
/// <summary>
/// Indicates whether all touch events are ignored or not.
/// </summary>
public static bool IgnoreAllEvents = false;
private class TouchConnection : IEventListener
{
public void InitializeForEventSource() { }
private bool wasMoveEvent = false;
public TouchConnection()
{
Calibrated = false;
}
public bool OnEvent(BaseEvent baseEvent)
{
if (IgnoreAllEvents)
return true;
if (baseEvent is TouchEvent)
{
TouchEvent e = (TouchEvent)baseEvent;
if (e.EventMessage == 3)
{
// If the same position -- do nothing
if (wasMoveEvent && e.Touches[0].X == TouchX && e.Touches[0].Y == TouchY)
{
// Nothing
}
else
{
wasMoveEvent = true;
TouchX = e.Touches[0].X;
TouchY = e.Touches[0].Y;
RaiseTouchMoveEvent(this, new TouchEventArgs(e.Touches));
}
}
else if (e.EventMessage == 1)
{
wasMoveEvent = false;
RaiseTouchDownEvent(this, new TouchEventArgs(e.Touches));
}
else if (e.EventMessage == 2)
{
wasMoveEvent = false;
RaiseTouchUpEvent(this, new TouchEventArgs(e.Touches));
}
}
else if (baseEvent is GenericEvent)
{
GenericEvent genericEvent = (GenericEvent)baseEvent;
if (genericEvent.EventCategory == (byte)EventCategory.Gesture)
RaiseTouchGestureEvent(this, new TouchGestureEventArgs((TouchGesture)genericEvent.EventMessage, genericEvent.X, genericEvent.Y, (ushort)genericEvent.EventData, genericEvent.Time));
}
return true;
}
}
private static TouchConnection touchConnection;
private static ExtendedWeakReference calWeakRef;
private static class GlideCalibration { }
/// <summary>
/// Initializes the touch panel.
/// </summary>
public static void Initialize()
{
touchConnection = new TouchConnection();
Touch.Initialize(touchConnection);
TouchCollectorConfiguration.CollectionMode = CollectionMode.InkAndGesture;
TouchCollectorConfiguration.CollectionMethod = CollectionMethod.Native;
TouchCollectorConfiguration.SamplingFrequency = 50;
calWeakRef = ExtendedWeakReference.RecoverOrCreate(typeof(GlideCalibration), 0, ExtendedWeakReference.c_SurvivePowerdown);
calWeakRef.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;
CalSettings = (CalibrationSettings)calWeakRef.Target;
if (CalSettings != null)
{
Touch.ActiveTouchPanel.SetCalibration(CalSettings.Points.Length, CalSettings.SX, CalSettings.SY, CalSettings.CX, CalSettings.CY);
Calibrated = true;
}
}
internal static void SaveCalibration(CalibrationSettings settings)
{
calWeakRef.Target = settings;
}
/// <summary>
/// Raises a touch down event.
/// </summary>
/// <param name="sender">Object associated with the event.</param>
/// <param name="e">Touch event arguments.</param>
/// <remarks>
/// Raises a touch down event. This event is handled by the Window currently assigned to Glide.MainWindow.
/// Once this Window receives a touch down event, it's passed on to it's children.
/// The first child to handle the event stops propagation to the remaining children.
/// </remarks>
public static void RaiseTouchDownEvent(object sender, TouchEventArgs e) { TouchDownEvent(sender, e); }
/// <summary>
/// Raises a touch up event.
/// </summary>
/// <param name="sender">Object associated with the event.</param>
/// <param name="e">Touch event arguments.</param>
/// <remarks>
/// Raises a touch up event. This event is handled by the Window currently assigned to Glide.MainWindow.
/// Once this Window receives a touch up event, it's passed on to it's children.
/// The first child to handle the event stops propagation to the remaining children.
/// </remarks>
public static void RaiseTouchUpEvent(object sender, TouchEventArgs e) { TouchUpEvent(sender, e); }
/// <summary>
/// Raises a touch move event.
/// </summary>
/// <param name="sender">Object associated with the event.</param>
/// <param name="e">Touch event arguments.</param>
/// <remarks>
/// Raises a touch move event. This event is handled by the Window currently assigned to Glide.MainWindow.
/// Once this Window receives a touch move event, it's passed on to it's children.
/// The first child to handle the event stops propagation to the remaining children.
/// </remarks>
public static void RaiseTouchMoveEvent(object sender, TouchEventArgs e) { TouchMoveEvent(sender, e); }
/// <summary>
/// Raises a touch gesture event.
/// </summary>
/// <param name="sender">Object associated with the event.</param>
/// <param name="e">Touch event arguments.</param>
public static void RaiseTouchGestureEvent(object sender, TouchGestureEventArgs e) { TouchGestureEvent(sender, e); }
/// <summary>
/// Touch down event handler.
/// </summary>
public static event TouchEventHandler TouchDownEvent = delegate { };
/// <summary>
/// Touch up event handler.
/// </summary>
public static event TouchEventHandler TouchUpEvent = delegate { };
/// <summary>
/// Touch move event handler.
/// </summary>
public static event TouchEventHandler TouchMoveEvent = delegate { };
/// <summary>
/// Touch gesture event handler.
/// </summary>
public static event TouchGestureEventHandler TouchGestureEvent = delegate { };
/// <summary>
/// Indicates whether or not the panel has been calibrated.
/// </summary>
public static bool Calibrated { get; internal set; }
/// <summary>
/// Current calibration settings if set othwerwise null.
/// </summary>
public static CalibrationSettings CalSettings;
}
/// <summary>
/// Calibration Settings
/// </summary>
[Serializable]
public sealed class CalibrationSettings
{
/// <summary>
/// Calibration Points
/// </summary>
public Point[] Points { get; set; }
/// <summary>
/// Screen X Buffer
/// </summary>
public short[] SX { get; set; }
/// <summary>
/// Screen Y Buffer
/// </summary>
public short[] SY { get; set; }
/// <summary>
/// Uncalibrated X Buffer
/// </summary>
public short[] CX { get; set; }
/// <summary>
/// Uncalibrated Y Buffer
/// </summary>
public short[] CY { get; set; }
}
}