-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathNativeWindow.cs
1093 lines (983 loc) · 41.3 KB
/
NativeWindow.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using Microsoft.Win32.SafeHandles;
namespace GLFW
{
/// <summary>
/// Provides a simplified interface for creating and using a GLFW window with properties, events, etc.
/// </summary>
/// <seealso cref="Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid" />
public class NativeWindow : SafeHandleZeroOrMinusOneIsInvalid, IEquatable<NativeWindow>
{
/// <summary>
/// Determines whether the specified <paramref name="window" /> is equal to this instance.
/// </summary>
/// <param name="window">A <see cref="NativeWindow" /> instance to compare for equality.</param>
/// <returns><c>true</c> if objects represent the same window, otherwise <c>false</c>.</returns>
public bool Equals(NativeWindow window)
{
if (ReferenceEquals(null, window)) return false;
return ReferenceEquals(this, window) || Window.Equals(window.Window);
}
/// <summary>
/// Raises the <see cref="Maximized" /> event.
/// </summary>
/// <param name="maximized">Flag indicating if window is being maximized or restored.</param>
protected virtual void OnMaximizeChanged(bool maximized)
{
MaximizeChanged?.Invoke(this, new MaximizeEventArgs(maximized));
}
/// <summary>
/// Occurs when the content scale has been changed.
/// </summary>
public event EventHandler<ContentScaleEventArgs> ContentScaleChanged;
/// <summary>
/// Raises the <see cref="ContentScaleChanged" /> event.
/// </summary>
/// <param name="xScale">The new scale on the x-axis.</param>
/// <param name="yScale">The new scale on the y-axis.</param>
protected virtual void OnContentScaleChanged(float xScale, float yScale)
{
ContentScaleChanged?.Invoke(this, new ContentScaleEventArgs(xScale, yScale));
}
/// <inheritdoc cref="Object.Equals(object)" />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is NativeWindow other && Equals(other);
}
/// <inheritdoc cref="Object.GetHashCode" />
public override int GetHashCode()
{
return Window.GetHashCode();
}
/// <summary>
/// Determines whether the specified window is equal to this instance.
/// </summary>
/// <param name="left">This instance.</param>
/// <param name="right">A <see cref="NativeWindow" /> instance to compare for equality.</param>
/// <returns><c>true</c> if objects represent the same window, otherwise <c>false</c>.</returns>
public static bool operator ==(NativeWindow left, NativeWindow right)
{
return Equals(left, right);
}
/// <summary>
/// Determines whether the specified window is not equal to this instance.
/// </summary>
/// <param name="left">This instance.</param>
/// <param name="right">A <see cref="NativeWindow" /> instance to compare for equality.</param>
/// <returns><c>true</c> if objects do not represent the same window, otherwise <c>false</c>.</returns>
public static bool operator !=(NativeWindow left, NativeWindow right)
{
return !Equals(left, right);
}
#region Fields and Constants
/// <summary>
/// The window instance this object wraps.
/// </summary>
protected readonly Window Window;
private string title;
private PositionCallback windowPositionCallback;
private SizeCallback windowSizeCallback, framebufferSizeCallback;
private FocusCallback windowFocusCallback;
private WindowCallback closeCallback, windowRefreshCallback;
private FileDropCallback dropCallback;
private MouseCallback cursorPositionCallback, scrollCallback;
private MouseEnterCallback cursorEnterCallback;
private MouseButtonCallback mouseButtonCallback;
private CharModsCallback charModsCallback;
private KeyCallback keyCallback;
private WindowMaximizedCallback windowMaximizeCallback;
private WindowContentsScaleCallback windowContentScaleCallback;
#endregion
#region Properties
/// <summary>
/// Gets or sets the size and location of the window including its non-client elements (borders, title bar, etc.), in
/// screen coordinates.
/// </summary>
/// <value>
/// A <see cref="Rectangle" /> in screen coordinates relative to the parent control that represents the size and
/// location of the control including its non-client elements.
/// </value>
public Rectangle Bounds
{
get => new Rectangle(Position, Size);
set
{
Size = value.Size;
Position = value.Location;
}
}
/// <summary>
/// Gets the ratio between the current DPI and the platform's default DPI.
/// </summary>
/// <seealso cref="Glfw.GetWindowContentScale" />
public PointF ContentScale
{
get
{
Glfw.GetWindowContentScale(handle, out var x, out var y);
return new PointF(x, y);
}
}
/// <summary>
/// Gets the size and location of the client area of the window, in screen coordinates.
/// </summary>
/// <value>
/// A <see cref="Rectangle" /> in screen coordinates that represents the size and location of the window's client area.
/// </value>
public Rectangle ClientBounds
{
get => new Rectangle(Position, ClientSize);
set
{
Glfw.SetWindowPosition(Window, value.X, value.Y);
Glfw.SetWindowSize(Window, value.Width, value.Height);
}
}
/// <summary>
/// Gets or sets the width of the client area of the window, in screen coordinates.
/// </summary>
/// <exception cref="Exception">Thrown when specified value is less than 1.</exception>
public int ClientWidth
{
get
{
Glfw.GetWindowSize(Window, out var width, out var dummy);
return width;
}
set
{
if (value < 1)
throw new Exception("Window width muts be greater than 0.");
Glfw.GetWindowSize(Window, out var dummy, out var height);
Glfw.SetWindowSize(Window, value, height);
}
}
/// <summary>
/// Gets or sets the height of the client area of the window, in screen coordinates.
/// </summary>
/// <exception cref="Exception">Thrown when specified value is less than 1.</exception>
public int ClientHeight
{
get
{
Glfw.GetWindowSize(Window, out var dummy, out var height);
return height;
}
set
{
if (value < 1)
throw new Exception("Window height muts be greater than 0.");
Glfw.GetWindowSize(Window, out var width, out var dummy);
Glfw.SetWindowSize(Window, width, value);
}
}
/// <summary>
/// Gets or sets the size of the client area of the window, in screen coordinates.
/// </summary>
/// <value>
/// A <see cref="System.Drawing.Size" /> in screen coordinates that represents the size of the window's client area.
/// </value>
public Size ClientSize
{
get
{
Glfw.GetWindowSize(Window, out var width, out var height);
return new Size(width, height);
}
set => Glfw.SetWindowSize(Window, value.Width, value.Height);
}
/// <summary>
/// Requests user-attention to this window on platforms that support it,
/// <para>
/// Once the user has given attention, usually by focusing the window or application, the system will end the
/// request automatically.
/// </para>
/// </summary>
public void RequestAttention()
{
Glfw.RequestWindowAttention(handle);
}
/// <summary>
/// Gets or sets a string to the system clipboard.
/// </summary>
/// <value>
/// The clipboard string.
/// </value>
[JetBrains.Annotations.NotNull]
public string Clipboard
{
get => Glfw.GetClipboardString(Window);
set => Glfw.SetClipboardString(Window, value);
}
/// <summary>
/// Gets or sets the behavior of the mouse cursor.
/// </summary>
/// <value>
/// The cursor mode.
/// </value>
public CursorMode CursorMode
{
get => (CursorMode) Glfw.GetInputMode(Window, InputMode.Cursor);
set => Glfw.SetInputMode(Window, InputMode.Cursor, (int) value);
}
/// <summary>
/// Gets the underlying pointer used by GLFW for this window instance.
/// </summary>
/// <value>
/// The GLFW window handle.
/// </value>
public IntPtr Handle => handle;
/// <summary>
/// Gets the Window's HWND for this window.
/// <para>WARNING: Windows only.</para>
/// </summary>
/// <value>
/// The HWND pointer.
/// </value>
// ReSharper disable once IdentifierTypo
public IntPtr Hwnd
{
get
{
try
{
return Native.GetWin32Window(Window);
}
catch (Exception)
{
return IntPtr.Zero;
}
}
}
/// <summary>
/// Gets a value indicating whether this instance is closing.
/// </summary>
/// <value>
/// <c>true</c> if this instance is closing; otherwise, <c>false</c>.
/// </value>
public bool IsClosing => Glfw.WindowShouldClose(Window);
/// <summary>
/// Gets a value indicating whether this instance is decorated.
/// </summary>
/// <value>
/// <c>true</c> if this instance is decorated; otherwise, <c>false</c>.
/// </value>
public bool IsDecorated => Glfw.GetWindowAttribute(Window, WindowAttribute.Decorated);
/// <summary>
/// Gets a value indicating whether this instance is floating (top-most, always-on-top).
/// </summary>
/// <value>
/// <c>true</c> if this instance is floating; otherwise, <c>false</c>.
/// </value>
public bool IsFloating => Glfw.GetWindowAttribute(Window, WindowAttribute.Floating);
/// <summary>
/// Gets a value indicating whether this instance is focused.
/// </summary>
/// <value>
/// <c>true</c> if this instance is focused; otherwise, <c>false</c>.
/// </value>
public bool IsFocused => Glfw.GetWindowAttribute(Window, WindowAttribute.Focused);
/// <summary>
/// Gets a value indicating whether this instance is resizable.
/// </summary>
/// <value>
/// <c>true</c> if this instance is resizable; otherwise, <c>false</c>.
/// </value>
public bool IsResizable => Glfw.GetWindowAttribute(Window, WindowAttribute.Resizable);
/// <summary>
/// Gets or sets a value indicating whether this <see cref="NativeWindow" /> is maximized.
/// <para>Has no effect on fullscreen windows.</para>
/// </summary>
/// <value>
/// <c>true</c> if maximized; otherwise, <c>false</c>.
/// </value>
public bool Maximized
{
get => Glfw.GetWindowAttribute(Window, WindowAttribute.Maximized);
set
{
if (value)
Glfw.MaximizeWindow(Window);
else
Glfw.RestoreWindow(Window);
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="NativeWindow" /> is minimized.
/// <para>If window is already minimized, does nothing.</para>
/// </summary>
/// <value>
/// <c>true</c> if minimized; otherwise, <c>false</c>.
/// </value>
public bool Minimized
{
get => Glfw.GetWindowAttribute(Window, WindowAttribute.AutoIconify);
set
{
if (value)
Glfw.IconifyWindow(Window);
else
Glfw.RestoreWindow(Window);
}
}
/// <summary>
/// Gets the monitor this window is fullscreen on.
/// <para>Returns <see cref="GLFW.Monitor.None" /> if window is not fullscreen.</para>
/// </summary>
/// <value>
/// The monitor.
/// </value>
public Monitor Monitor => Glfw.GetWindowMonitor(Window);
/// <summary>
/// Gets or sets the mouse position in screen-coordinates relative to the client area of the window.
/// </summary>
/// <value>
/// The mouse position.
/// </value>
public Point MousePosition
{
get
{
Glfw.GetCursorPosition(Window, out var x, out var y);
return new Point(Convert.ToInt32(x), Convert.ToInt32(y));
}
set => Glfw.SetCursorPosition(Window, value.X, value.Y);
}
/// <summary>
/// Gets or sets the position of the window in screen coordinates, including border, titlebar, etc..
/// </summary>
/// <value>
/// The position.
/// </value>
public Point Position
{
get
{
Glfw.GetWindowPosition(Window, out var x, out var y);
Glfw.GetWindowFrameSize(Window, out var l, out var t, out var dummy1, out var dummy2);
return new Point(x - l, y - t);
}
set
{
Glfw.GetWindowFrameSize(Window, out var l, out var t, out var dummy1, out var dummy2);
Glfw.SetWindowPosition(Window, value.X + l, value.Y + t);
}
}
/// <summary>
/// Gets or sets the size of the window, in screen coordinates, including border, titlebar, etc.
/// </summary>
/// <value>
/// A <see cref="System.Drawing.Size" /> in screen coordinates that represents the size of the window.
/// </value>
public Size Size
{
get
{
Glfw.GetWindowSize(Window, out var width, out var height);
Glfw.GetWindowFrameSize(Window, out var l, out var t, out var r, out var b);
return new Size(width + l + r, height + t + b);
}
set
{
Glfw.GetWindowFrameSize(Window, out var l, out var t, out var r, out var b);
Glfw.SetWindowSize(Window, value.Width - l - r, value.Height - t - b);
}
}
/// <summary>
/// Sets the sticky keys input mode.
/// <para>
/// Set to <c>true</c> to enable sticky keys, or <c>false</c> to disable it. If sticky keys are enabled, a key
/// press will ensure that <see cref="Glfw.GetKey" /> returns <see cref="InputState.Press" /> the next time it is
/// called even if the key had been released before the call. This is useful when you are only interested in
/// whether keys have been pressed but not when or in which order.
/// </para>
/// </summary>
public bool StickyKeys
{
get => Glfw.GetInputMode(Window, InputMode.StickyKeys) == (int) Constants.True;
set =>
Glfw.SetInputMode(Window, InputMode.StickyKeys, value ? (int) Constants.True : (int) Constants.False);
}
/// <summary>
/// Gets or sets the sticky mouse button input mode.
/// <para>
/// Set to <c>true</c> to enable sticky mouse buttons, or <c>false</c> to disable it. If sticky mouse buttons are
/// enabled, a mouse button press will ensure that <see cref="Glfw.GetMouseButton" /> returns
/// <see cref="InputState.Press" /> the next time it is called even if the mouse button had been released before
/// the call. This is useful when you are only interested in whether mouse buttons have been pressed but not when
/// or in which order.
/// </para>
/// </summary>
public bool StickyMouseButtons
{
get => Glfw.GetInputMode(Window, InputMode.StickyMouseButton) == (int) Constants.True;
set =>
Glfw.SetInputMode(Window, InputMode.StickyMouseButton,
value ? (int) Constants.True : (int) Constants.False);
}
/// <summary>
/// Gets or sets the window title or caption.
/// </summary>
/// <value>
/// The title.
/// </value>
[CanBeNull]
public string Title
{
get => title;
set
{
title = value;
Glfw.SetWindowTitle(Window, value ?? string.Empty);
}
}
/// <summary>
/// Gets or sets a user-defined pointer for GLFW to retain for this instance.
/// </summary>
/// <value>
/// The user-defined pointer.
/// </value>
public IntPtr UserPointer
{
get => Glfw.GetWindowUserPointer(Window);
set => Glfw.SetWindowUserPointer(Window, value);
}
/// <summary>
/// Gets the video mode for the monitor this window is fullscreen on.
/// <para>If window is not fullscreen, returns the <see cref="GLFW.VideoMode" /> for the primary monitor.</para>
/// </summary>
/// <value>
/// The video mode.
/// </value>
public VideoMode VideoMode
{
get
{
var monitor = Monitor;
return Glfw.GetVideoMode(monitor == Monitor.None ? Glfw.PrimaryMonitor : monitor);
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="NativeWindow" /> is visible.
/// </summary>
/// <value>
/// <c>true</c> if visible; otherwise, <c>false</c>.
/// </value>
public bool Visible
{
get => Glfw.GetWindowAttribute(Window, WindowAttribute.Visible);
set
{
if (value)
Glfw.ShowWindow(Window);
else
Glfw.HideWindow(Window);
}
}
#region Operators
/// <summary>
/// Performs an implicit conversion from <see cref="NativeWindow" /> to <see cref="GLFW.Window" />.
/// </summary>
/// <param name="nativeWindow">The game window.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator Window(NativeWindow nativeWindow)
{
return nativeWindow.Window;
}
/// <summary>
/// Performs an implicit conversion from <see cref="NativeWindow" /> to <see cref="IntPtr" />.
/// </summary>
/// <param name="nativeWindow">The game window.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator IntPtr(NativeWindow nativeWindow)
{
return nativeWindow.Window;
}
#endregion
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="NativeWindow" /> class.
/// </summary>
public NativeWindow() : this(800, 600, string.Empty, Monitor.None, Window.None)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NativeWindow" /> class.
/// </summary>
/// <param name="width">The desired width, in screen coordinates, of the window. This must be greater than zero.</param>
/// <param name="height">The desired height, in screen coordinates, of the window. This must be greater than zero.</param>
/// <param name="title">The initial window title.</param>
public NativeWindow(int width, int height, [CanBeNull] string title) : this(width, height, title, Monitor.None,
Window.None)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NativeWindow" /> class.
/// </summary>
/// <param name="width">The desired width, in screen coordinates, of the window. This must be greater than zero.</param>
/// <param name="height">The desired height, in screen coordinates, of the window. This must be greater than zero.</param>
/// <param name="title">The initial window title.</param>
/// <param name="monitor">The monitor to use for full screen mode, or <see cref="GLFW.Monitor.None" /> for windowed mode.</param>
/// <param name="share">
/// A window instance whose context to share resources with, or <see cref="GLFW.Window.None" /> to not share
/// resources..
/// </param>
public NativeWindow(int width, int height, [CanBeNull] string title, Monitor monitor, Window share) : base(true)
{
this.title = title ?? string.Empty;
Window = Glfw.CreateWindow(width, height, title ?? string.Empty, monitor, share);
SetHandle(Window);
if (Glfw.GetClientApi(this) != ClientApi.None)
MakeCurrent();
BindCallbacks();
}
#endregion
#region Methods
/// <summary>
/// Centers the on window on the screen.
/// <para>Has no effect on fullscreen or maximized windows.</para>
/// </summary>
public void CenterOnScreen()
{
if (Maximized)
return;
var monitor = Monitor == Monitor.None ? Glfw.PrimaryMonitor : Monitor;
var videoMode = Glfw.GetVideoMode(monitor);
var size = Size;
Position = new Point((videoMode.Width - size.Width) / 2, (videoMode.Height - size.Height) / 2);
}
/// <summary>
/// Closes this instance.
/// <para>This invalidates the window, but does not free its resources.</para>
/// </summary>
public new void Close()
{
Glfw.SetWindowShouldClose(Window, true);
OnClosing();
base.Close();
}
/// <summary>
/// Focuses this form to receive input and events.
/// </summary>
public void Focus()
{
Glfw.FocusWindow(Window);
}
/// <summary>
/// Sets the window fullscreen on the primary monitor.
/// </summary>
public void Fullscreen()
{
Fullscreen(Glfw.PrimaryMonitor);
}
/// <summary>
/// Sets the window fullscreen on the specified monitor.
/// </summary>
/// <param name="monitor">The monitor to display the window fullscreen.</param>
public void Fullscreen(Monitor monitor)
{
Glfw.SetWindowMonitor(Window, monitor, 0, 0, 0, 0, -1);
}
/// <summary>
/// Makes window and its context the current.
/// </summary>
public void MakeCurrent()
{
Glfw.MakeContextCurrent(Window);
}
/// <summary>
/// Maximizes this window to fill the screen.
/// <para>Has no effect if window is already maximized.</para>
/// </summary>
public void Maximize()
{
Glfw.MaximizeWindow(Window);
}
/// <summary>
/// Minimizes this window.
/// <para>Has no effect if window is already minimized.</para>
/// </summary>
public void Minimize()
{
Glfw.IconifyWindow(Window);
}
/// <summary>
/// Restores a minimized window to its previous state.
/// <para>Has no effect if window was already restored.</para>
/// </summary>
public void Restore()
{
Glfw.RestoreWindow(Window);
}
/// <summary>
/// Sets the aspect ratio to maintain for the window.
/// <para>This function is ignored for fullscreen windows.</para>
/// </summary>
/// <param name="numerator">The numerator of the desired aspect ratio.</param>
/// <param name="denominator">The denominator of the desired aspect ratio.</param>
public void SetAspectRatio(int numerator, int denominator)
{
Glfw.SetWindowAspectRatio(Window, numerator, denominator);
}
/// <summary>
/// Sets the icon(s) used for the titlebar, taskbar, etc.
/// <para>Standard sizes are 16x16, 32x32, and 48x48.</para>
/// </summary>
/// <param name="images">One or more images to set as an icon.</param>
public void SetIcons([JetBrains.Annotations.NotNull] params Image[] images)
{
Glfw.SetWindowIcon(Window, images.Length, images);
}
/// <summary>
/// Sets the window monitor.
/// <para>
/// If <paramref name="monitor" /> is not <see cref="GLFW.Monitor.None" />, the window will be full-screened and
/// dimensions ignored.
/// </para>
/// </summary>
/// <param name="monitor">The desired monitor, or <see cref="GLFW.Monitor.None" /> to set windowed mode.</param>
/// <param name="x">The desired x-coordinate of the upper-left corner of the client area.</param>
/// <param name="y">The desired y-coordinate of the upper-left corner of the client area.</param>
/// <param name="width">The desired width, in screen coordinates, of the client area or video mode.</param>
/// <param name="height">The desired height, in screen coordinates, of the client area or video mode.</param>
/// <param name="refreshRate">The desired refresh rate, in Hz, of the video mode, or <see cref="Constants.Default" />.</param>
public void SetMonitor(Monitor monitor, int x, int y, int width, int height,
int refreshRate = (int) Constants.Default)
{
Glfw.SetWindowMonitor(Window, monitor, x, y, width, height, refreshRate);
}
/// <summary>
/// Sets the limits of the client size area of the window.
/// </summary>
/// <param name="minSize">The minimum size of the client area.</param>
/// <param name="maxSize">The maximum size of the client area.</param>
public void SetSizeLimits(Size minSize, Size maxSize)
{
SetSizeLimits(minSize.Width, minSize.Height, maxSize.Width, maxSize.Height);
}
/// <summary>
/// Sets the limits of the client size area of the window.
/// </summary>
/// <param name="minWidth">The minimum width of the client area.</param>
/// <param name="minHeight">The minimum height of the client area.</param>
/// <param name="maxWidth">The maximum width of the client area.</param>
/// <param name="maxHeight">The maximum height of the client area.</param>
public void SetSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeight)
{
Glfw.SetWindowSizeLimits(Window, minWidth, minHeight, maxWidth, maxHeight);
}
/// <summary>
/// Swaps the front and back buffers when rendering with OpenGL or OpenGL ES.
/// <para>
/// This should not be called on a window that is not using an OpenGL or OpenGL ES context (.i.e. Vulkan).
/// </para>
/// </summary>
public void SwapBuffers()
{
Glfw.SwapBuffers(Window);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only
/// unmanaged resources.
/// </param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Disposed?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Releases the internal GLFW handle.
/// </summary>
/// <returns><c>true</c> if handle was released successfully, otherwise <c>false</c>.</returns>
protected override bool ReleaseHandle()
{
try
{
Glfw.DestroyWindow(Window);
return true;
}
catch (Exception)
{
return false;
}
}
private void BindCallbacks()
{
windowPositionCallback = (_, x, y) => OnPositionChanged(x, y);
windowSizeCallback = (_, w, h) => OnSizeChanged(w, h);
windowFocusCallback = (_, focusing) => OnFocusChanged(focusing);
closeCallback = _ => OnClosing();
dropCallback = (_, count, arrayPtr) => OnFileDrop(count, arrayPtr);
cursorPositionCallback = (_, x, y) => OnMouseMove(x, y);
cursorEnterCallback = (_, entering) => OnMouseEnter(entering);
mouseButtonCallback = (_, button, state, mod) => OnMouseButton(button, state, mod);
scrollCallback = (_, x, y) => OnMouseScroll(x, y);
charModsCallback = (_, cp, mods) => OnCharacterInput(cp, mods);
framebufferSizeCallback = (_, w, h) => OnFramebufferSizeChanged(w, h);
windowRefreshCallback = _ => Refreshed?.Invoke(this, EventArgs.Empty);
keyCallback = (_, key, code, state, mods) => OnKey(key, code, state, mods);
windowMaximizeCallback = (_, maximized) => OnMaximizeChanged(maximized);
windowContentScaleCallback = (_, x, y) => OnContentScaleChanged(x, y);
Glfw.SetWindowPositionCallback(Window, windowPositionCallback);
Glfw.SetWindowSizeCallback(Window, windowSizeCallback);
Glfw.SetWindowFocusCallback(Window, windowFocusCallback);
Glfw.SetCloseCallback(Window, closeCallback);
Glfw.SetDropCallback(Window, dropCallback);
Glfw.SetCursorPositionCallback(Window, cursorPositionCallback);
Glfw.SetCursorEnterCallback(Window, cursorEnterCallback);
Glfw.SetMouseButtonCallback(Window, mouseButtonCallback);
Glfw.SetScrollCallback(Window, scrollCallback);
Glfw.SetCharModsCallback(Window, charModsCallback);
Glfw.SetFramebufferSizeCallback(Window, framebufferSizeCallback);
Glfw.SetWindowRefreshCallback(Window, windowRefreshCallback);
Glfw.SetKeyCallback(Window, keyCallback);
Glfw.SetWindowMaximizeCallback(Window, windowMaximizeCallback);
Glfw.SetWindowContentScaleCallback(Window, windowContentScaleCallback);
}
private void OnFileDrop(int count, IntPtr pointer)
{
var paths = new string[count];
var offset = 0;
for (var i = 0; i < count; i++, offset += IntPtr.Size)
{
var ptr = Marshal.ReadIntPtr(pointer + offset);
paths[i] = Util.PtrToStringUTF8(ptr);
}
OnFileDrop(paths);
}
#endregion
#region Delegates and Events
/// <summary>
/// Occurs when the window is maximized or restored.
/// </summary>
public event EventHandler<MaximizeEventArgs> MaximizeChanged;
/// <summary>
/// Occurs when the window receives character input.
/// </summary>
public event EventHandler<CharEventArgs> CharacterInput;
/// <summary>
/// Occurs when the window is closed.
/// </summary>
public event EventHandler Closed;
/// <summary>
/// Occurs when the form is closing, and provides subscribers means of canceling the action..
/// </summary>
public event CancelEventHandler Closing;
/// <summary>
/// Occurs when the window is disposed.
/// </summary>
public event EventHandler Disposed;
/// <summary>
/// Occurs when files are dropped onto the window client area with a drag-drop event.
/// </summary>
public event EventHandler<FileDropEventArgs> FileDrop;
/// <summary>
/// Occurs when the window gains or loses focus.
/// </summary>
public event EventHandler FocusChanged;
/// <summary>
/// Occurs when the size of the internal framebuffer is changed.
/// </summary>
public event EventHandler<SizeChangeEventArgs> FramebufferSizeChanged;
/// <summary>
/// Occurs when a key is pressed, released, or repeated.
/// </summary>
public event EventHandler<KeyEventArgs> KeyAction;
/// <summary>
/// Occurs when a key is pressed.
/// </summary>
public event EventHandler<KeyEventArgs> KeyPress;
/// <summary>
/// Occurs when a key is released.
/// </summary>
public event EventHandler<KeyEventArgs> KeyRelease;
/// <summary>
/// Occurs when a key is held long enough to raise a repeat event.
/// </summary>
public event EventHandler<KeyEventArgs> KeyRepeat;
/// <summary>
/// Occurs when a mouse button is pressed or released.
/// </summary>
public event EventHandler<MouseButtonEventArgs> MouseButton;
/// <summary>
/// Occurs when the mouse cursor enters the client area of the window.
/// </summary>
public event EventHandler MouseEnter;
/// <summary>
/// Occurs when the mouse cursor leaves the client area of the window.
/// </summary>
public event EventHandler MouseLeave;
/// <summary>
/// Occurs when mouse cursor is moved.
/// </summary>
public event EventHandler<MouseMoveEventArgs> MouseMoved;
/// <summary>
/// Occurs when mouse is scrolled.
/// </summary>
public event EventHandler<MouseMoveEventArgs> MouseScroll;
/// <summary>
/// Occurs when position of the <see cref="NativeWindow" /> is changed.
/// </summary>
public event EventHandler PositionChanged;
/// <summary>
/// Occurs when window is refreshed.
/// </summary>
public event EventHandler Refreshed;
/// <summary>
/// Occurs when size of the <see cref="NativeWindow" /> is changed.
/// </summary>
public event EventHandler<SizeChangeEventArgs> SizeChanged;
/// <summary>
/// Raises the <see cref="CharacterInput" /> event.
/// </summary>
/// <param name="codePoint">The Unicode code point.</param>
/// <param name="mods">The modifier keys present.</param>
protected virtual void OnCharacterInput(uint codePoint, ModifierKeys mods)
{
CharacterInput?.Invoke(this, new CharEventArgs(codePoint, mods));
}
/// <summary>
/// Raises the <see cref="Closed" /> event.
/// </summary>
protected virtual void OnClosed()
{
Closed?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Raises the <see cref="Closing" /> event.
/// </summary>
protected virtual void OnClosing()
{
var args = new CancelEventArgs();
Closing?.Invoke(this, args);
if (args.Cancel)
{
Glfw.SetWindowShouldClose(Window, false);
}
else
{
base.Close();
OnClosed();
}
}
/// <summary>
/// Raises the <see cref="FileDrop" /> event.
/// </summary>
/// <param name="paths">The filenames of the dropped files.</param>
protected virtual void OnFileDrop([JetBrains.Annotations.NotNull] string[] paths)
{
FileDrop?.Invoke(this, new FileDropEventArgs(paths));
}
/// <summary>
/// Raises the <see cref="FocusChanged" /> event.
/// </summary>
/// <param name="focusing"><c>true</c> if window is gaining focus, otherwise <c>false</c>.</param>
// ReSharper disable once UnusedParameter.Global
protected virtual void OnFocusChanged(bool focusing)
{
FocusChanged?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Raises the <see cref="FramebufferSizeChanged" /> event.
/// </summary>
/// <param name="width">The new width.</param>
/// <param name="height">The new height.</param>
protected virtual void OnFramebufferSizeChanged(int width, int height)
{