-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
387 lines (334 loc) · 12.2 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
387 lines (334 loc) · 12.2 KB
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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using Cursors = System.Windows.Input.Cursors;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
using Point = System.Windows.Point;
using UserControl = System.Windows.Controls.UserControl;
namespace BabySmash
{
public partial class MainWindow : Window
{
private readonly Controller controller;
public Controller Controller { get { return controller; } }
private const int VkShift = 0x10;
private const int VkControl = 0x11;
private const int VkMenu = 0x12;
private const int VkLeftWindows = 0x5B;
private const int VkRightWindows = 0x5C;
private const int VkO = 0x4F;
private readonly DispatcherTimer optionsGestureTimer;
private readonly Stopwatch optionsGestureStopwatch = new();
private readonly HashSet<Key> pressedKeys = new();
private bool optionsGestureRequiresRelease;
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int virtualKey);
private UserControl customCursor;
public UserControl CustomCursor { get { return customCursor; } set { customCursor = value; } }
// FPS counter
private int _frameCount;
private readonly Stopwatch _fpsStopwatch = new();
private bool _showFps;
public void AddFigure(UserControl c)
{
this.figuresCanvas.Children.Add(c);
}
public void RemoveFigure(UserControl c)
{
this.figuresCanvas.Children.Remove(c);
}
public MainWindow(Controller c, bool showFps = false)
{
this.controller = c;
this.DataContext = controller;
_showFps = showFps;
InitializeComponent();
optionsGestureTimer = new DispatcherTimer(DispatcherPriority.Input)
{
Interval = TimeSpan.FromMilliseconds(50)
};
optionsGestureTimer.Tick += OptionsGestureTimer_Tick;
// Initialize cursor early to prevent NullReferenceException in mouse events (OnMouseEnter, OnMouseLeave, OnMouseMove)
AssertCursor();
if (_showFps)
{
fpsLabel.Visibility = Visibility.Visible;
CompositionTarget.Rendering += OnRendering;
_fpsStopwatch.Start();
}
}
private void OnRendering(object sender, EventArgs e)
{
_frameCount++;
if (_fpsStopwatch.ElapsedMilliseconds >= 1000)
{
var fps = _frameCount * 1000.0 / _fpsStopwatch.ElapsedMilliseconds;
fpsLabel.Text = $"FPS: {fps:F0} | Shapes: {figuresCanvas.Children.Count}";
_frameCount = 0;
_fpsStopwatch.Restart();
}
}
protected override void OnClosed(EventArgs e)
{
ResetOptionsGesture();
optionsGestureTimer.Tick -= OptionsGestureTimer_Tick;
if (_showFps)
{
CompositionTarget.Rendering -= OnRendering;
}
base.OnClosed(e);
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
controller.MouseWheel(this, e);
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
controller.MouseUp(this, e);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
controller.MouseDown(this, e);
}
protected override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
AssertCursor();
CustomCursor.Visibility = Visibility.Visible;
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
CustomCursor.Visibility = Visibility.Hidden;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (controller.isOptionsDialogShown == false)
{
CustomCursor.Visibility = Visibility.Visible;
Point p = e.GetPosition(mouseDragCanvas);
double pX = p.X;
double pY = p.Y;
Cursor = Cursors.None;
Canvas.SetTop(CustomCursor, pY);
Canvas.SetLeft(CustomCursor, pX);
Canvas.SetZIndex(CustomCursor, int.MaxValue);
}
controller.MouseMove(this, e);
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
Key key = e.Key == Key.System ? e.SystemKey : e.Key;
pressedKeys.Remove(key);
if (IsOptionsGestureKey(key) &&
(optionsGestureStopwatch.IsRunning || optionsGestureRequiresRelease))
{
if (IsOptionsGestureReleased())
{
ResetOptionsGesture();
}
e.Handled = true;
return;
}
e.Handled = true;
controller.ProcessKey(this, e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
Key key = e.Key == Key.System ? e.SystemKey : e.Key;
pressedKeys.Add(key);
if (optionsGestureRequiresRelease)
{
if (IsOptionsGestureKey(key))
{
e.Handled = true;
}
return;
}
if (key == Key.O && IsExactOptionsGestureHeld())
{
if (!optionsGestureStopwatch.IsRunning)
{
optionsGestureStopwatch.Restart();
optionsGestureTimer.Start();
}
e.Handled = true;
return;
}
if (optionsGestureStopwatch.IsRunning)
{
CancelOptionsGestureUntilRelease();
}
}
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (optionsGestureStopwatch.IsRunning || optionsGestureRequiresRelease)
{
ResetOptionsGestureAfterDialog();
}
else
{
pressedKeys.Clear();
}
}
protected override void OnDeactivated(EventArgs e)
{
bool gestureWasActive = optionsGestureStopwatch.IsRunning || optionsGestureRequiresRelease;
ResetOptionsGesture();
pressedKeys.Clear();
if (gestureWasActive && !IsOptionsGestureReleased())
{
optionsGestureRequiresRelease = true;
optionsGestureTimer.Start();
}
base.OnDeactivated(e);
}
protected override void OnStateChanged(EventArgs e)
{
base.OnStateChanged(e);
if (IsLoaded && !controller.isOptionsDialogShown)
{
Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
new Action(() => controller.RestoreKioskState()));
}
}
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
controller.LostMouseCapture(this, e);
}
internal void AssertCursor()
{
try
{
mouseCursorCanvas.Children.Clear();
CustomCursor = Utils.GetCursor();
if (CustomCursor.Parent != null)
{
((Canvas)CustomCursor.Parent).Children.Remove(CustomCursor);
}
CustomCursor.RenderTransform = new ScaleTransform(0.5, 0.5);
CustomCursor.Name = "customCursor";
mouseCursorCanvas.Children.Insert(0, CustomCursor); //in front!
CustomCursor.Visibility = Visibility.Hidden;
}
catch (System.NotSupportedException)
{
//we can die here if we ALT-F4 while in the Options Dialog
}
}
internal void RestoreCursor()
{
AssertCursor();
Cursor = Cursors.None;
if (CustomCursor == null)
{
return;
}
Point position = Mouse.GetPosition(mouseDragCanvas);
Canvas.SetTop(CustomCursor, position.Y);
Canvas.SetLeft(CustomCursor, position.X);
Canvas.SetZIndex(CustomCursor, int.MaxValue);
CustomCursor.Visibility = IsMouseOver ? Visibility.Visible : Visibility.Hidden;
}
private void OptionsGestureTimer_Tick(object sender, EventArgs e)
{
if (optionsGestureRequiresRelease)
{
if (IsOptionsGestureReleased())
{
ResetOptionsGesture();
}
return;
}
if (!optionsGestureStopwatch.IsRunning)
{
optionsGestureTimer.Stop();
return;
}
if (!IsExactOptionsGestureHeld())
{
CancelOptionsGestureUntilRelease();
return;
}
if (optionsGestureStopwatch.Elapsed >= TimeSpan.FromSeconds(3))
{
optionsGestureStopwatch.Reset();
optionsGestureTimer.Stop();
optionsGestureRequiresRelease = true;
controller.ShowOptionsDialog();
}
}
internal void ResetOptionsGestureAfterDialog()
{
ResetOptionsGesture();
optionsGestureRequiresRelease = !IsOptionsGestureReleased();
if (optionsGestureRequiresRelease)
{
optionsGestureTimer.Start();
}
}
internal void CancelOptionsGestureFromHook()
{
if (optionsGestureStopwatch.IsRunning)
{
CancelOptionsGestureUntilRelease();
}
}
internal bool IsOptionsGestureArmed => optionsGestureStopwatch.IsRunning;
private void CancelOptionsGestureUntilRelease()
{
optionsGestureStopwatch.Reset();
optionsGestureRequiresRelease = true;
optionsGestureTimer.Start();
}
private void ResetOptionsGesture()
{
optionsGestureStopwatch.Reset();
optionsGestureTimer.Stop();
optionsGestureRequiresRelease = false;
}
private static bool IsOptionsGestureKey(Key key)
{
return key == Key.O ||
key == Key.LeftCtrl || key == Key.RightCtrl ||
key == Key.LeftAlt || key == Key.RightAlt ||
key == Key.LeftShift || key == Key.RightShift;
}
private bool IsExactOptionsGestureHeld()
{
return IsKeyDown(VkO) &&
IsKeyDown(VkControl) &&
IsKeyDown(VkMenu) &&
IsKeyDown(VkShift) &&
!IsKeyDown(VkLeftWindows) &&
!IsKeyDown(VkRightWindows) &&
pressedKeys.All(IsOptionsGestureKey);
}
private static bool IsOptionsGestureReleased()
{
return !IsKeyDown(VkO) &&
!IsKeyDown(VkControl) &&
!IsKeyDown(VkMenu) &&
!IsKeyDown(VkShift);
}
private static bool IsKeyDown(int virtualKey)
{
return (GetAsyncKeyState(virtualKey) & 0x8000) != 0;
}
}
}