forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinFormsGameWindow.cs
684 lines (567 loc) · 23.1 KB
/
WinFormsGameWindow.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Windows;
using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Point = System.Drawing.Point;
using Rectangle = Microsoft.Xna.Framework.Rectangle;
using XnaPoint = Microsoft.Xna.Framework.Point;
namespace MonoGame.Framework
{
class WinFormsGameWindow : GameWindow, IDisposable
{
internal WinFormsGameForm Form;
static private ReaderWriterLockSlim _allWindowsReaderWriterLockSlim = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
static private List<WinFormsGameWindow> _allWindows = new List<WinFormsGameWindow>();
private WinFormsGamePlatform _platform;
private bool _isResizable;
private bool _isBorderless;
private bool _isMouseHidden;
private bool _isMouseInBounds;
private Point _locationBeforeFullScreen;
// flag to indicate that we're switching to/from full screen and should ignore resize events
private bool _switchingFullScreen;
// true if window position was moved either through code or by dragging/resizing the form
private bool _wasMoved;
private bool _isResizeTickEnabled;
private readonly System.Timers.Timer _resizeTickTimer;
#region Internal Properties
internal Game Game { get; private set; }
#endregion
#region Public Properties
public override IntPtr Handle { get { return Form.Handle; } }
public override string ScreenDeviceName { get { return String.Empty; } }
public override Rectangle ClientBounds
{
get
{
var position = Form.PointToScreen(Point.Empty);
var size = Form.ClientSize;
return new Rectangle(position.X, position.Y, size.Width, size.Height);
}
}
public override bool AllowUserResizing
{
get { return _isResizable; }
set
{
if (_isResizable != value)
{
_isResizable = value;
Form.MaximizeBox = _isResizable;
}
else
return;
if (_isBorderless)
return;
Form.FormBorderStyle = _isResizable ? FormBorderStyle.Sizable : FormBorderStyle.FixedSingle;
}
}
public override bool AllowAltF4
{
get { return base.AllowAltF4; }
set
{
Form.AllowAltF4 = value;
base.AllowAltF4 = value;
}
}
public override DisplayOrientation CurrentOrientation
{
get { return DisplayOrientation.Default; }
}
public override XnaPoint Position
{
get { return new XnaPoint(Form.Location.X, Form.Location.Y); }
set
{
_wasMoved = true;
Form.Location = new Point(value.X, value.Y);
RefreshAdapter();
}
}
protected internal override void SetSupportedOrientations(DisplayOrientation orientations)
{
}
public override bool IsBorderless
{
get { return _isBorderless; }
set
{
if (_isBorderless != value)
_isBorderless = value;
else
return;
if (_isBorderless)
Form.FormBorderStyle = FormBorderStyle.None;
else
Form.FormBorderStyle = _isResizable ? FormBorderStyle.Sizable : FormBorderStyle.FixedSingle;
}
}
public bool IsFullScreen { get; private set; }
public bool HardwareModeSwitch { get; private set; }
#endregion
internal WinFormsGameWindow(WinFormsGamePlatform platform)
{
_platform = platform;
Game = platform.Game;
Form = new WinFormsGameForm(this);
ChangeClientSize(new Size(GraphicsDeviceManager.DefaultBackBufferWidth, GraphicsDeviceManager.DefaultBackBufferHeight));
SetIcon();
Title = Utilities.AssemblyHelper.GetDefaultWindowTitle();
Form.MaximizeBox = false;
Form.FormBorderStyle = FormBorderStyle.FixedSingle;
Form.StartPosition = FormStartPosition.Manual;
// Capture mouse events.
Mouse.WindowHandle = Form.Handle;
Form.MouseWheel += OnMouseScroll;
Form.MouseHorizontalWheel += OnMouseHorizontalScroll;
Form.MouseEnter += OnMouseEnter;
Form.MouseLeave += OnMouseLeave;
_resizeTickTimer = new System.Timers.Timer(1) { SynchronizingObject = Form, AutoReset = false };
_resizeTickTimer.Elapsed += OnResizeTick;
Form.Activated += OnActivated;
Form.Deactivate += OnDeactivate;
Form.Resize += OnResize;
Form.ResizeBegin += OnResizeBegin;
Form.ResizeEnd += OnResizeEnd;
Form.KeyPress += OnKeyPress;
RegisterToAllWindows();
}
[StructLayout(LayoutKind.Sequential)]
internal struct POINTSTRUCT
{
public int X;
public int Y;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto, BestFitMapping = false)]
private static extern IntPtr ExtractIcon(IntPtr hInst, string exeFileName, int iconIndex);
[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
[return: MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
internal static extern bool GetCursorPos(out POINTSTRUCT pt);
[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
internal static extern int MapWindowPoints(HandleRef hWndFrom, HandleRef hWndTo, out POINTSTRUCT pt, int cPoints);
private void SetIcon()
{
// When running unit tests this can return null.
var assembly = Assembly.GetEntryAssembly();
if (assembly == null)
return;
var handle = ExtractIcon(IntPtr.Zero, assembly.Location, 0);
if (handle != IntPtr.Zero)
Form.Icon = Icon.FromHandle(handle);
}
~WinFormsGameWindow()
{
Dispose(false);
}
private void RegisterToAllWindows()
{
_allWindowsReaderWriterLockSlim.EnterWriteLock();
try
{
_allWindows.Add(this);
}
finally
{
_allWindowsReaderWriterLockSlim.ExitWriteLock();
}
}
private void UnregisterFromAllWindows()
{
_allWindowsReaderWriterLockSlim.EnterWriteLock();
try
{
_allWindows.Remove(this);
}
finally
{
_allWindowsReaderWriterLockSlim.ExitWriteLock();
}
}
private void OnActivated(object sender, EventArgs eventArgs)
{
_platform.IsActive = true;
Keyboard.SetActive(true);
}
private void OnDeactivate(object sender, EventArgs eventArgs)
{
// If in exclusive mode full-screen, force it out of exclusive mode and minimize the window
if( IsFullScreen && _platform.Game.GraphicsDevice.PresentationParameters.HardwareModeSwitch ) {
// This is true when the user presses the Windows key while game window has focus
if( Form.WindowState == FormWindowState.Minimized )
MinimizeFullScreen();
}
_platform.IsActive = false;
Keyboard.SetActive(false);
}
private void OnMouseScroll(object sender, MouseEventArgs mouseEventArgs)
{
MouseState.ScrollWheelValue += mouseEventArgs.Delta;
}
private void OnMouseHorizontalScroll(object sender, HorizontalMouseWheelEventArgs mouseEventArgs)
{
MouseState.HorizontalScrollWheelValue += mouseEventArgs.Delta;
}
private void UpdateMouseState()
{
// If we call the form client functions before the form has
// been made visible it will cause the wrong window size to
// be applied at startup.
if (!Form.Visible)
return;
POINTSTRUCT pos;
GetCursorPos(out pos);
MapWindowPoints(new HandleRef(null, IntPtr.Zero), new HandleRef(Form, Form.Handle), out pos, 1);
var clientPos = new System.Drawing.Point(pos.X, pos.Y);
var withinClient = Form.ClientRectangle.Contains(clientPos);
var buttons = Control.MouseButtons;
var previousState = MouseState.LeftButton;
MouseState.X = clientPos.X;
MouseState.Y = clientPos.Y;
MouseState.LeftButton = (buttons & MouseButtons.Left) == MouseButtons.Left ? ButtonState.Pressed : ButtonState.Released;
MouseState.MiddleButton = (buttons & MouseButtons.Middle) == MouseButtons.Middle ? ButtonState.Pressed : ButtonState.Released;
MouseState.RightButton = (buttons & MouseButtons.Right) == MouseButtons.Right ? ButtonState.Pressed : ButtonState.Released;
MouseState.XButton1 = (buttons & MouseButtons.XButton1) == MouseButtons.XButton1 ? ButtonState.Pressed : ButtonState.Released;
MouseState.XButton2 = (buttons & MouseButtons.XButton2) == MouseButtons.XButton2 ? ButtonState.Pressed : ButtonState.Released;
// Don't process touch state if we're not active
// and the mouse is within the client area.
if (!_platform.IsActive || !withinClient)
{
if (MouseState.LeftButton == ButtonState.Pressed)
{
// Release mouse TouchLocation
var touchX = MathHelper.Clamp(MouseState.X, 0, Form.ClientRectangle.Width-1);
var touchY = MathHelper.Clamp(MouseState.Y, 0, Form.ClientRectangle.Height-1);
TouchPanelState.AddEvent(0, TouchLocationState.Released, new Vector2(touchX, touchY), true);
}
return;
}
TouchLocationState? touchState = null;
if (MouseState.LeftButton == ButtonState.Pressed)
if (previousState == ButtonState.Released)
touchState = TouchLocationState.Pressed;
else
touchState = TouchLocationState.Moved;
else if (previousState == ButtonState.Pressed)
touchState = TouchLocationState.Released;
if (touchState.HasValue)
TouchPanelState.AddEvent(0, touchState.Value, new Vector2(MouseState.X, MouseState.Y), true);
}
private void OnMouseEnter(object sender, EventArgs e)
{
_isMouseInBounds = true;
if (!_platform.IsMouseVisible && !_isMouseHidden)
{
_isMouseHidden = true;
Cursor.Hide();
}
}
private void OnMouseLeave(object sender, EventArgs e)
{
_isMouseInBounds = false;
if (_isMouseHidden)
{
_isMouseHidden = false;
Cursor.Show();
}
}
[DllImport("user32.dll")]
private static extern short VkKeyScanEx(char ch, IntPtr dwhkl);
private void OnKeyPress(object sender, KeyPressEventArgs e)
{
var key = (Keys) (VkKeyScanEx(e.KeyChar, InputLanguage.CurrentInputLanguage.Handle) & 0xff);
OnTextInput(sender, new TextInputEventArgs(e.KeyChar, key));
}
internal void Initialize(int width, int height)
{
ChangeClientSize(new Size(width, height));
}
internal void Initialize(PresentationParameters pp)
{
ChangeClientSize(new Size(pp.BackBufferWidth, pp.BackBufferHeight));
if (pp.IsFullScreen)
{
EnterFullScreen(pp);
if (!pp.HardwareModeSwitch)
_platform.Game.GraphicsDevice.OnPresentationChanged();
}
}
private FormWindowState _lastFormState;
private void OnResize(object sender, EventArgs eventArgs)
{
if (_switchingFullScreen || Form.IsResizing)
return;
// this event can be triggered when moving the window through Windows hotkeys
// in that case we should no longer center the window after resize
if (_lastFormState == Form.WindowState)
_wasMoved = true;
if (Game.Window == this && Form.WindowState != FormWindowState.Minimized) {
// we may need to restore full screen when coming back from a minimized window
if (_lastFormState == FormWindowState.Minimized)
_platform.Game.GraphicsDevice.SetHardwareFullscreen();
UpdateBackBufferSize();
}
_lastFormState = Form.WindowState;
OnClientSizeChanged();
}
private void OnResizeBegin(object sender, EventArgs e)
{
_isResizeTickEnabled = true;
_resizeTickTimer.Enabled = true;
}
private void OnResizeTick(object sender, System.Timers.ElapsedEventArgs e)
{
if (!_isResizeTickEnabled)
return;
UpdateWindows();
Game.Tick();
_resizeTickTimer.Enabled = true;
}
private void OnResizeEnd(object sender, EventArgs eventArgs)
{
_isResizeTickEnabled = false;
_resizeTickTimer.Enabled = false;
_wasMoved = true;
if (Game.Window == this)
{
UpdateBackBufferSize();
RefreshAdapter();
}
OnClientSizeChanged();
}
private void RefreshAdapter()
{
// the display that the window is on might have changed, so we need to
// check and possibly update the Adapter of the GraphicsDevice
if (Game.GraphicsDevice != null)
Game.GraphicsDevice.RefreshAdapter();
}
private void UpdateBackBufferSize()
{
var manager = Game.graphicsDeviceManager;
if (manager.GraphicsDevice == null)
return;
var newSize = Form.ClientSize;
if (newSize.Width == manager.PreferredBackBufferWidth
&& newSize.Height == manager.PreferredBackBufferHeight)
return;
// Set the default new back buffer size
manager.PreferredBackBufferWidth = newSize.Width;
manager.PreferredBackBufferHeight = newSize.Height;
manager.ApplyChanges();
}
protected override void SetTitle(string title)
{
Form.Text = title;
}
internal void RunLoop()
{
Application.Idle += TickOnIdle;
Application.Run(Form);
Application.Idle -= TickOnIdle;
// We need to remove the WM_QUIT message in the message
// pump as it will keep us from restarting on this
// same thread.
//
// This is critical for some NUnit runners which
// typically will run all the tests on the same
// process/thread.
var msg = new NativeMessage();
do
{
if (msg.msg == WM_QUIT)
break;
Thread.Sleep(100);
}
while (PeekMessage(out msg, IntPtr.Zero, 0, 1 << 5, 1));
}
// Run game loop when the app becomes Idle.
private void TickOnIdle(object sender, EventArgs e)
{
var nativeMsg = new NativeMessage();
do
{
UpdateWindows();
Game.Tick();
}
while (!PeekMessage(out nativeMsg, IntPtr.Zero, 0, 0, 0) && Form != null && Form.IsDisposed == false);
}
internal void UpdateWindows()
{
_allWindowsReaderWriterLockSlim.EnterReadLock();
try
{
// Update the mouse state for each window.
foreach (var window in _allWindows)
if (window.Game == Game)
window.UpdateMouseState();
}
finally
{
_allWindowsReaderWriterLockSlim.ExitReadLock();
}
}
private const uint WM_QUIT = 0x12;
[StructLayout(LayoutKind.Sequential)]
public struct NativeMessage
{
public IntPtr handle;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
internal void ChangeClientSize(Size clientBounds)
{
var prevIsResizing = Form.IsResizing;
// make sure we don't see the events from this as a user resize
Form.IsResizing = true;
if(this.Form.ClientSize != clientBounds)
this.Form.ClientSize = clientBounds;
// if the window wasn't moved manually and it's resized, it should be centered
if (!_wasMoved)
Form.CenterOnPrimaryMonitor();
Form.IsResizing = prevIsResizing;
}
[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern bool PeekMessage(out NativeMessage msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
#region Public Methods
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (disposing)
{
if (Form != null)
{
UnregisterFromAllWindows();
Form.Dispose();
Form = null;
}
}
_platform = null;
Game = null;
Mouse.WindowHandle = IntPtr.Zero;
}
public override void BeginScreenDeviceChange(bool willBeFullScreen)
{
}
public override void EndScreenDeviceChange(string screenDeviceName, int clientWidth, int clientHeight)
{
}
public void MouseVisibleToggled()
{
if (_platform.IsMouseVisible)
{
if (_isMouseHidden)
{
Cursor.Show();
_isMouseHidden = false;
}
}
else if (!_isMouseHidden && _isMouseInBounds)
{
Cursor.Hide();
_isMouseHidden = true;
}
}
internal void OnPresentationChanged(PresentationParameters pp)
{
var raiseClientSizeChanged = false;
if (pp.IsFullScreen && pp.HardwareModeSwitch && IsFullScreen && HardwareModeSwitch)
{
if( _platform.IsActive ) {
// stay in hardware full screen, need to call ResizeTargets so the displaymode can be switched
_platform.Game.GraphicsDevice.ResizeTargets();
} else {
// This needs to be called in case the user presses the Windows key while the focus is on the second monitor,
// which (sometimes) causes the window to exit fullscreen mode, but still keeps it visible
MinimizeFullScreen();
}
}
else if (pp.IsFullScreen && (!IsFullScreen || pp.HardwareModeSwitch != HardwareModeSwitch))
{
EnterFullScreen(pp);
raiseClientSizeChanged = true;
}
else if (!pp.IsFullScreen && IsFullScreen)
{
ExitFullScreen();
raiseClientSizeChanged = true;
}
ChangeClientSize(new Size(pp.BackBufferWidth, pp.BackBufferHeight));
if (raiseClientSizeChanged)
OnClientSizeChanged();
}
#endregion
private void EnterFullScreen(PresentationParameters pp)
{
_switchingFullScreen = true;
// store the location of the window so we can restore it later
if (!IsFullScreen)
_locationBeforeFullScreen = Form.Location;
_platform.Game.GraphicsDevice.SetHardwareFullscreen();
if (!pp.HardwareModeSwitch)
{
// FIXME: setting the WindowState to Maximized when the form is not shown will not update the ClientBounds
// this causes the back buffer to be the wrong size when initializing in soft full screen
// we show the form to bypass the issue
Form.Show();
IsBorderless = true;
Form.WindowState = FormWindowState.Maximized;
_lastFormState = FormWindowState.Maximized;
}
IsFullScreen = true;
HardwareModeSwitch = pp.HardwareModeSwitch;
_switchingFullScreen = false;
}
[DllImport("user32.dll")]
static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);
private void ExitFullScreen()
{
_switchingFullScreen = true;
_platform.Game.GraphicsDevice.ClearHardwareFullscreen();
IsBorderless = false;
Form.WindowState = FormWindowState.Normal;
_lastFormState = FormWindowState.Normal;
Form.Location = _locationBeforeFullScreen;
IsFullScreen = false;
// Windows does not always correctly redraw the desktop when exiting soft full screen, so force a redraw
if (!HardwareModeSwitch)
RedrawWindow(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 1);
_switchingFullScreen = false;
}
private void MinimizeFullScreen()
{
_switchingFullScreen = true;
_platform.Game.GraphicsDevice.ClearHardwareFullscreen();
IsBorderless = false;
Form.WindowState = FormWindowState.Minimized;
_lastFormState = FormWindowState.Minimized;
Form.Location = _locationBeforeFullScreen;
IsFullScreen = false;
// Windows does not always correctly redraw the desktop when exiting soft full screen, so force a redraw
if (!HardwareModeSwitch)
RedrawWindow(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 1);
_switchingFullScreen = false;
}
}
}