-
Notifications
You must be signed in to change notification settings - Fork 6
/
TouchPanel.xaml.cs
258 lines (228 loc) · 8.35 KB
/
TouchPanel.xaml.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
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfMaiTouchEmulator.Managers;
namespace WpfMaiTouchEmulator;
/// <summary>
/// Interaction logic for TouchPanel.xaml
/// </summary>
public partial class TouchPanel : Window
{
internal Action<TouchValue>? onTouch;
internal Action<TouchValue>? onRelease;
internal Action? onInitialReposition;
private readonly Dictionary<int, Polygon> activeTouches = [];
private readonly TouchPanelPositionManager _positionManager;
private List<Polygon> buttons = [];
private bool isDebugEnabled = Properties.Settings.Default.IsDebugEnabled;
private bool isRingButtonEmulationEnabled = Properties.Settings.Default.IsRingButtonEmulationEnabled;
private bool hasRepositioned = false;
private enum ResizeDirection
{
BottomRight = 8,
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool ReleaseCapture();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public TouchPanel()
{
InitializeComponent();
Topmost = true;
_positionManager = new TouchPanelPositionManager();
Loaded += Window_Loaded;
StateCheckLoop();
}
private async void StateCheckLoop()
{
while (true)
{
if (activeTouches.Count != 0 && !TouchesOver.Any())
{
await Task.Delay(100);
if (activeTouches.Count != 0 && !TouchesOver.Any())
{
DeselectAllItems();
}
}
await Task.Delay(100);
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
buttons = VisualTreeHelperExtensions.FindVisualChildren<Polygon>(this);
DeselectAllItems();
}
public void PositionTouchPanel()
{
var position = _positionManager.GetSinMaiWindowPosition();
if (position != null &&
(Top != position.Value.Top || Left != position.Value.Left || Width != position.Value.Width || Height != position.Value.Height)
)
{
Logger.Info("Touch panel not over sinmai window, repositioning");
Top = position.Value.Top;
Left = position.Value.Left;
Width = position.Value.Width;
Height = position.Value.Height;
if (!hasRepositioned)
{
hasRepositioned = true;
onInitialReposition?.Invoke();
}
}
}
private void DragBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// This event is for the draggable bar, it calls DragMove to move the window
DragMove();
}
private void ResizeGrip_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
ResizeWindow(ResizeDirection.BottomRight);
}
}
private void ResizeWindow(ResizeDirection direction)
{
ReleaseCapture();
SendMessage(new System.Windows.Interop.WindowInteropHelper(this).Handle,
0x112, // WM_SYSCOMMAND message
(IntPtr)(0xF000 + direction),
IntPtr.Zero);
}
private void Element_TouchDown(object sender, TouchEventArgs e)
{
// Cast the sender to a Border to ensure it's the correct element type.
if (sender is Polygon element)
{
// Highlight the element and add it to the active touches tracking.
HighlightElement(element, true);
var touchValue = (TouchValue)element.Tag;
if (isRingButtonEmulationEnabled && RingButtonEmulator.HasRingButtonMapping((TouchValue)element.Tag))
{
RingButtonEmulator.PressButton((TouchValue)element.Tag);
}
else
{
onTouch?.Invoke((TouchValue)element.Tag);
}
activeTouches[e.TouchDevice.Id] = element;
}
e.Handled = true;
}
private void Element_TouchMove(object sender, TouchEventArgs e)
{
// Attempt to find the element under the current touch point.
var touchPoint = e.GetTouchPoint(this).Position;
var hitTestResult = VisualTreeHelper.HitTest(this, touchPoint);
if (hitTestResult != null && hitTestResult.VisualHit is Polygon newElement)
{
// If this touch point is already tracking another element, unhighlight the previous one.
if (activeTouches.TryGetValue(e.TouchDevice.Id, out var previousElement) && previousElement != newElement)
{
Task.Delay(50)
.ContinueWith(t =>
{
HighlightElement(previousElement, false);
Application.Current.Dispatcher.Invoke(() =>
{
onRelease?.Invoke((TouchValue)previousElement.Tag);
});
});
}
// Highlight the new element and update the tracking.
HighlightElement(newElement, true);
onTouch?.Invoke((TouchValue)newElement.Tag);
activeTouches[e.TouchDevice.Id] = newElement;
}
e.Handled = true;
}
private void Element_TouchUp(object sender, TouchEventArgs e)
{
// When touch is lifted, unhighlight the associated element and remove it from tracking.
if (activeTouches.TryGetValue(e.TouchDevice.Id, out var element))
{
HighlightElement(element, false);
RingButtonEmulator.ReleaseButton((TouchValue)element.Tag);
onRelease?.Invoke((TouchValue)element.Tag);
activeTouches.Remove(e.TouchDevice.Id);
}
e.Handled = true;
}
private void DeselectAllItems()
{
// Logic to deselect all items or the last touched item
foreach (var element in activeTouches.Values)
{
HighlightElement(element, false);
onRelease?.Invoke((TouchValue)element.Tag);
}
activeTouches.Clear();
RingButtonEmulator.ReleaseAllButtons();
}
public void SetDebugMode(bool enabled)
{
isDebugEnabled = enabled;
buttons.ForEach(button =>
{
button.Opacity = enabled ? 0.3 : 0;
});
}
public void SetBorderMode(BorderSetting borderSetting, string borderColour)
{
if (borderSetting == BorderSetting.Rainbow)
{
var rotateTransform = new RotateTransform { CenterX = 0.5, CenterY = 0.5 };
touchPanelBorder.BorderBrush = new ImageBrush {
ImageSource = new BitmapImage(new Uri(@"pack://application:,,,/Assets/conicalGradient.png")),
ViewportUnits = BrushMappingMode.RelativeToBoundingBox,
Viewport = new Rect(0, 0, 1, 1),
TileMode = TileMode.Tile,
RelativeTransform = rotateTransform,
};
var animation = new DoubleAnimation
{
From = 0,
To = 360,
Duration = new Duration(TimeSpan.FromSeconds(10)),
RepeatBehavior = RepeatBehavior.Forever
};
rotateTransform.BeginAnimation(RotateTransform.AngleProperty, animation);
return;
}
else if (borderSetting == BorderSetting.Solid)
{
try
{
var colour = (Color)ColorConverter.ConvertFromString(borderColour);
touchPanelBorder.BorderBrush = new SolidColorBrush { Color = colour };
return;
}
catch (Exception ex)
{
Logger.Error("Failed to parse solid colour", ex);
}
}
touchPanelBorder.BorderBrush = null;
}
public void SetEmulateRingButton(bool enabled)
{
isRingButtonEmulationEnabled = enabled;
}
private void HighlightElement(Polygon element, bool highlight)
{
if (isDebugEnabled)
{
Application.Current.Dispatcher.Invoke(() =>
{
element.Opacity = highlight ? 0.8 : 0.3;
});
}
}
}