-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchInputProvider.cs
120 lines (103 loc) · 4.78 KB
/
TouchInputProvider.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
using System;
using System.Collections;
using System.Device.Gpio;
using nanoFramework.UI.Input;
using nanoFramework.UI.Threading;
using nanoFramework.Presentation;
namespace OpenHalo
{
public sealed class TouchInputProvider
{
public readonly Dispatcher Dispatcher;
private ArrayList buttons;
private DispatcherOperationCallback callback;
private InputProviderSite site;
private PresentationSource source;
/// <summary>
/// Maps Touches to Buttons that can be processed by
/// nanoFramework.Presentation.
/// </summary>
/// <param name="source"></param>
public TouchInputProvider(PresentationSource source)
{
// Set the input source.
this.source = source;
// Register our object as an input source with the input manager and
// get back an InputProviderSite object which forwards the input
// report to the input manager, which then places the input in the
// staging area.
site = InputManager.CurrentInputManager.RegisterInputProvider(this);
// Create a delegate that refers to the InputProviderSite object's
// ReportInput method.
callback = new DispatcherOperationCallback(delegate (object report)
{
InputReportArgs args = (InputReportArgs)report;
return site.ReportInput(args.Device, args.Report);
});
Dispatcher = Dispatcher.CurrentDispatcher;
this.buttons = new ArrayList();
}
/// <summary>
/// Add a Touch gesture as a specific Button
/// </summary>
/// <param name="touch">Touch driver</param>
/// <param name="button">Button that this gesture represents</param>
/// <param name="gestureToReact">Gesture from CST to use</param>
public void AddTouch(Button button, TekuSP.Drivers.DriverBase.Interfaces.ITouchSensor touch, TekuSP.Drivers.CST816D.Gesture gestureToReact)
{
this.buttons.Add(new TouchPad(this, button, touch, gestureToReact));
}
/// <summary>
/// Represents a button pad on the emulated device, containing five
/// buttons for user input.
/// </summary>
internal class TouchPad : IDisposable
{
private readonly Button button;
private readonly TouchInputProvider sink;
private readonly ButtonDevice buttonDevice = InputManager.CurrentInputManager.ButtonDevice;
private readonly TekuSP.Drivers.CST816D.Gesture gest;
/// <summary>
/// Constructs a ButtonPad object that handles the
/// hardware's button interrupts.
/// </summary>
/// <param name="sink"></param>
/// <param name="button"></param>
/// <param name="pin"></param>
public TouchPad(TouchInputProvider sink, Button button, TekuSP.Drivers.DriverBase.Interfaces.ITouchSensor touch, TekuSP.Drivers.CST816D.Gesture gestureToReact = TekuSP.Drivers.CST816D.Gesture.GEST_NONE)
{
this.sink = sink;
this.button = button;
gest = gestureToReact;
touch.OnStateChanged += Cts_OnStateChanged;
}
private void Cts_OnStateChanged(object sender, TekuSP.Drivers.DriverBase.Interfaces.ITouchData data)
{
DateTime time = DateTime.UtcNow;
if (gest == TekuSP.Drivers.CST816D.Gesture.GEST_NONE)
{
sink.Dispatcher.BeginInvoke(sink.callback, new InputReportArgs(buttonDevice, new RawTouchInputReport(sink.source, time, data.Gesture, new nanoFramework.UI.TouchInput[] { new nanoFramework.UI.TouchInput() { X = data.X, Y = data.Y, ContactHeight = 15, ContactWidth = 15 } })));
return;
}
if (data.Gesture != (byte)gest)
return;
RawButtonInputReport report = new RawButtonInputReport(sink.source, time, button, RawButtonActions.ButtonDown);
RawButtonInputReport report2 = new RawButtonInputReport(sink.source, time, button, RawButtonActions.ButtonUp);
// Queue the button press to the input provider site.
sink.Dispatcher.BeginInvoke(sink.callback, new InputReportArgs(buttonDevice, report));
sink.Dispatcher.BeginInvoke(sink.callback, new InputReportArgs(buttonDevice, report2));
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
}