diff --git a/build/props/common.props b/build/props/common.props index 68a61ff44d..880a3d2df9 100644 --- a/build/props/common.props +++ b/build/props/common.props @@ -5,18 +5,18 @@ MIT 8.0 - Silk.NET September 2020 Update + Silk.NET October 2020 Update - - Add a byte* overload to Glfw.GetError - - Make GlfwProvider throw if Glfw.Init fails - - Update to latest specifications - - Miscellaneous bug fixes and improvements + - Add a BorderSize property and GetFullSize extension method to IWindow for working with window borders. + - Add a Center extension method which will center the window to a monitor. + - Update to latest specifications. + - Miscellaneous bug fixes and improvements. true $(MSBuildThisFileDirectory)/../output_packages https://github.com/Ultz/Silk.NET Git - 1.6.0 + 1.7.0 Silk.NET is a high-speed, advanced library, providing bindings to popular low-level APIs such as OpenGL, OpenCL, OpenAL, GLFW, and Vulkan. diff --git a/src/Lab/MonitorPlayground/Program.cs b/src/Lab/MonitorPlayground/Program.cs index 59c37b4a0f..50c8d139eb 100644 --- a/src/Lab/MonitorPlayground/Program.cs +++ b/src/Lab/MonitorPlayground/Program.cs @@ -67,6 +67,11 @@ static void Main(string[] args) } } + if (input.Keyboards[0].IsKeyPressed(Key.Space)) + { + window.Center(); + } + if (input.Keyboards[0].IsKeyPressed(Key.Escape)) { window.Close(); diff --git a/src/Windowing/Silk.NET.Windowing.Common/Interfaces/IWindow.cs b/src/Windowing/Silk.NET.Windowing.Common/Interfaces/IWindow.cs index 38a2917f82..293164ae18 100644 --- a/src/Windowing/Silk.NET.Windowing.Common/Interfaces/IWindow.cs +++ b/src/Windowing/Silk.NET.Windowing.Common/Interfaces/IWindow.cs @@ -33,6 +33,17 @@ public interface IWindow : IWindowProperties, IWindowHost, IView /// new bool IsClosing { get; set; } + /// + /// Gets the distances in screen coordinates from the edges of the content area to the corresponding edges of + /// the full window. + /// + /// + /// Because these are distances and not coordinates, they are always zero or positive. + /// + /// + // This is in IWindow because it can't be configured. + Rectangle BorderSize { get; } + /// /// Raised when the window is moved. /// diff --git a/src/Windowing/Silk.NET.Windowing.Common/WindowExtensions.cs b/src/Windowing/Silk.NET.Windowing.Common/WindowExtensions.cs index 75f743391d..079154eb49 100644 --- a/src/Windowing/Silk.NET.Windowing.Common/WindowExtensions.cs +++ b/src/Windowing/Silk.NET.Windowing.Common/WindowExtensions.cs @@ -4,6 +4,7 @@ // of the MIT license. See the LICENSE file for details. using System; +using System.Drawing; using Silk.NET.Windowing.Common.Structs; namespace Silk.NET.Windowing.Common @@ -61,6 +62,34 @@ public static void Run(this IView view) view.Reset(); } + /// + /// Gets the full size of the given window including its borders. + /// + /// The window to get size information from. + /// The full size of the window (including both content area and borders) + public static Size GetFullSize(this IWindow window) => Size.Add(window.Size, window.BorderSize.Size); + + /// + /// Centers this window to the given monitor or, if null, the current monitor the window's on. + /// + /// The window to center. + /// The specific monitor to center the window to, if any. + public static void Center(this IWindow window, IMonitor? monitor = null) + { + monitor ??= window.Monitor; + var monitorBounds = monitor.Bounds; + var windowFullSize = window.GetFullSize(); + window.Position = Point.Add + ( + monitorBounds.Location, + new Size + ( + monitorBounds.Size.Width / 2 - windowFullSize.Width / 2, + monitorBounds.Size.Height / 2 - windowFullSize.Height / 2 + ) + ); + } + /// /// Sets the window icon to default on the given window. /// diff --git a/src/Windowing/Silk.NET.Windowing.Desktop/GlfwWindow.cs b/src/Windowing/Silk.NET.Windowing.Desktop/GlfwWindow.cs index 81f0b4649b..de45be0bc1 100644 --- a/src/Windowing/Silk.NET.Windowing.Desktop/GlfwWindow.cs +++ b/src/Windowing/Silk.NET.Windowing.Desktop/GlfwWindow.cs @@ -119,13 +119,23 @@ public GlfwWindow(WindowOptions options, GlfwWindow parent, GlfwMonitor monitor) /// public int RunningSlowTolerance { get; set; } - /// + /// public unsafe bool IsClosing { get => _glfw.WindowShouldClose(_windowPtr); set => _glfw.SetWindowShouldClose(_windowPtr, value); } + /// + public unsafe Rectangle BorderSize + { + get + { + _glfw.GetWindowFrameSize(_windowPtr, out var l, out var t, out var r, out var b); + return Rectangle.FromLTRB(l, t, r, b); + } + } + /// public bool IsRunningSlowly => _isRunningSlowlyTries > RunningSlowTolerance;