-
Notifications
You must be signed in to change notification settings - Fork 1
/
TouchHelper.cs
167 lines (149 loc) · 7.27 KB
/
TouchHelper.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
using System;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
namespace Helpers {
public enum WTouchAction {
Entered,
Pressed,
Moved,
Released,
Cancelled,
Exited,
}
public enum WTouchDeviceType {
Touch,
Mouse,
Pen,
}
public enum WMouseButton {
Unknown,
Left,
Middle,
Right,
}
public class WTouchEventArgs {
public Pointer Pointer { get; }
public bool Handled { get; set; }
public long Id { get; }
public WTouchAction ActionType { get; }
public WTouchDeviceType DeviceType { get; }
public WMouseButton MouseButton { get; }
public Point Location { get; }
public bool InContact { get; }
public WTouchEventArgs(long id, WTouchAction type, WMouseButton mouseButton, WTouchDeviceType deviceType, Point location, bool inContact, Pointer pointer, bool handled) {
Id = id;
ActionType = type;
DeviceType = deviceType;
MouseButton = mouseButton;
Location = location;
InContact = inContact;
Pointer = pointer;
Handled = handled;
}
}
public static class TouchHelper {
public static void SetTouchEventHandler(UIElement control, Action<WTouchEventArgs> callback) {
var e = new EventHandler<WTouchEventArgs>((sender, args) => callback(args));
control.Holding += (sender, args) => {
WTouchEventArgs wTouch;
switch (args.HoldingState) {
case HoldingState.Started:
wTouch = new WTouchEventArgs(-10, WTouchAction.Pressed, WMouseButton.Right, WTouchDeviceType.Touch, args.GetPosition(control), true, null, args.Handled);
break;
default:
wTouch = new WTouchEventArgs(-10, WTouchAction.Released, WMouseButton.Right, WTouchDeviceType.Touch, args.GetPosition(control), false, null, args.Handled);
break;
}
e.Invoke(control, wTouch);
};
control.PointerEntered += (sender, args) => {
var pointer = args.GetCurrentPoint((UIElement) sender);
var pos = new Point(pointer.Position.X, pointer.Position.Y);
var skTouch = new WTouchEventArgs(pointer.PointerId, WTouchAction.Entered, GetMouseButton(pointer), GetTouchDevice(pointer), pos, pointer.IsInContact, args.Pointer, args.Handled);
e.Invoke(control, skTouch);
};
control.PointerPressed += (sender, args) => {
var pointer = args.GetCurrentPoint((UIElement) sender);
var pos = new Point(pointer.Position.X, pointer.Position.Y);
var skTouch = new WTouchEventArgs(pointer.PointerId, WTouchAction.Pressed, GetMouseButton(pointer), GetTouchDevice(pointer), pos, pointer.IsInContact, args.Pointer, args.Handled);
e.Invoke(control, skTouch);
};
control.PointerMoved += (sender, args) => {
var pointer = args.GetCurrentPoint((UIElement) sender);
var pos = new Point(pointer.Position.X, pointer.Position.Y);
var skTouch = new WTouchEventArgs(pointer.PointerId, WTouchAction.Moved, GetMouseButton(pointer), GetTouchDevice(pointer), pos, pointer.IsInContact, args.Pointer, args.Handled);
e.Invoke(control, skTouch);
};
control.PointerReleased += (sender, args) => {
var pointer = args.GetCurrentPoint((UIElement) sender);
var pos = new Point(pointer.Position.X, pointer.Position.Y);
var skTouch = new WTouchEventArgs(pointer.PointerId, WTouchAction.Released, GetMouseButton(pointer), GetTouchDevice(pointer), pos, pointer.IsInContact, args.Pointer, args.Handled);
e.Invoke(control, skTouch);
};
control.PointerExited += (sender, args) => {
var pointer = args.GetCurrentPoint((UIElement) sender);
var pos = new Point(pointer.Position.X, pointer.Position.Y);
var skTouch = new WTouchEventArgs(pointer.PointerId, WTouchAction.Exited, GetMouseButton(pointer), GetTouchDevice(pointer), pos, pointer.IsInContact, args.Pointer, args.Handled);
e.Invoke(control, skTouch);
};
control.PointerCanceled += (sender, args) => {
var pointer = args.GetCurrentPoint((UIElement) sender);
var pos = new Point(pointer.Position.X, pointer.Position.Y);
var skTouch = new WTouchEventArgs(pointer.PointerId, WTouchAction.Cancelled, GetMouseButton(pointer), GetTouchDevice(pointer), pos, pointer.IsInContact, args.Pointer, args.Handled);
e.Invoke(control, skTouch);
};
control.PointerCaptureLost += (sender, args) => {
var pointer = args.GetCurrentPoint((UIElement) sender);
var pos = new Point(pointer.Position.X, pointer.Position.Y);
var skTouch = new WTouchEventArgs(pointer.PointerId, WTouchAction.Cancelled, GetMouseButton(pointer), GetTouchDevice(pointer), pos, pointer.IsInContact, args.Pointer, args.Handled);
e(control, skTouch);
};
}
public static WTouchDeviceType GetTouchDevice(PointerPoint pointer) {
WTouchDeviceType device;
switch (pointer.PointerDevice.PointerDeviceType) {
case PointerDeviceType.Pen:
device = WTouchDeviceType.Pen;
break;
case PointerDeviceType.Mouse:
device = WTouchDeviceType.Mouse;
break;
case PointerDeviceType.Touch:
device = WTouchDeviceType.Touch;
break;
default:
device = (WTouchDeviceType) 3;
break;
}
return device;
}
public static WMouseButton GetMouseButton(PointerPoint pointer) {
var properties = pointer.Properties;
var mouse = WMouseButton.Unknown;
if (properties.IsLeftButtonPressed)
mouse = WMouseButton.Left;
else if (properties.IsMiddleButtonPressed)
mouse = WMouseButton.Middle;
else if (properties.IsRightButtonPressed) mouse = WMouseButton.Right;
switch (properties.PointerUpdateKind) {
case PointerUpdateKind.LeftButtonPressed:
case PointerUpdateKind.LeftButtonReleased:
mouse = WMouseButton.Left;
break;
case PointerUpdateKind.RightButtonPressed:
case PointerUpdateKind.RightButtonReleased:
mouse = WMouseButton.Right;
break;
case PointerUpdateKind.MiddleButtonPressed:
case PointerUpdateKind.MiddleButtonReleased:
mouse = WMouseButton.Middle;
break;
}
if (properties.IsEraser) mouse = WMouseButton.Middle;
return mouse;
}
}
}