Skip to content

Commit

Permalink
Implemented IEquatable for NativeWindow class
Browse files Browse the repository at this point in the history
Made NativeWindow's protected Window field readonly
  • Loading branch information
ForeverZer0 committed Jun 6, 2019
1 parent 288600c commit 39acf7a
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 30 deletions.
145 changes: 116 additions & 29 deletions GLFW.NET/NativeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ namespace GLFW
/// 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
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>
Expand All @@ -38,12 +49,46 @@ 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 Window Window;
protected readonly Window Window;

private string title;

Expand Down Expand Up @@ -134,7 +179,10 @@ public Size ClientSize
/// request automatically.
/// </para>
/// </summary>
public void RequestAttention() { Glfw.RequestWindowAttention(handle); }
public void RequestAttention()
{
Glfw.RequestWindowAttention(handle);
}

/// <summary>
/// Gets or sets a string to the system clipboard.
Expand Down Expand Up @@ -318,14 +366,6 @@ public Point Position

/// <summary>
/// Gets or sets the size of the window, in screen coordinates, including border, titlebar, etc.
/// <para>
/// BUG: On Windows, as of GLFW 3.2.1, the frame size values may be incorrect, resulting in incorrect values for
/// this property.
/// </para>
/// <para>
/// This is due to how the values are retrieved from the OS by the underlying library. Typically the values will
/// be larger than expected.
/// </para>
/// </summary>
/// <value>
/// A <see cref="System.Drawing.Size" /> in screen coordinates that represents the size of the window.
Expand Down Expand Up @@ -451,7 +491,10 @@ public bool Visible
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator Window(NativeWindow nativeWindow) { return nativeWindow.Window; }
public static implicit operator Window(NativeWindow nativeWindow)
{
return nativeWindow.Window;
}

/// <summary>
/// Performs an implicit conversion from <see cref="NativeWindow" /> to <see cref="IntPtr" />.
Expand All @@ -460,7 +503,10 @@ public bool Visible
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator IntPtr(NativeWindow nativeWindow) { return nativeWindow.Window; }
public static implicit operator IntPtr(NativeWindow nativeWindow)
{
return nativeWindow.Window;
}

#endregion

Expand All @@ -471,16 +517,20 @@ public bool Visible
/// <summary>
/// Initializes a new instance of the <see cref="NativeWindow" /> class.
/// </summary>
public NativeWindow() : this(800, 600, string.Empty, Monitor.None, Window.None) { }
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)
{ }
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.
Expand Down Expand Up @@ -535,41 +585,62 @@ public void CenterOnScreen()
/// <summary>
/// Focuses this form to receive input and events.
/// </summary>
public void Focus() { Glfw.FocusWindow(Window); }
public void Focus()
{
Glfw.FocusWindow(Window);
}

/// <summary>
/// Sets the window fullscreen on the primary monitor.
/// </summary>
public void Fullscreen() { Fullscreen(Glfw.PrimaryMonitor); }
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); }
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); }
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); }
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); }
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); }
public void Restore()
{
Glfw.RestoreWindow(Window);
}

/// <summary>
/// Sets the aspect ratio to maintain for the window.
Expand All @@ -587,7 +658,10 @@ public void SetAspectRatio(int numerator, int denominator)
/// <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([NotNull] params Image[] images) { Glfw.SetWindowIcon(Window, images.Length, images); }
public void SetIcons([NotNull] params Image[] images)
{
Glfw.SetWindowIcon(Window, images.Length, images);
}

/// <summary>
/// Sets the window monitor.
Expand All @@ -602,7 +676,8 @@ public void SetAspectRatio(int numerator, int denominator)
/// <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)
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);
}
Expand Down Expand Up @@ -635,7 +710,10 @@ public void SetSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeig
/// 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); }
public void SwapBuffers()
{
Glfw.SwapBuffers(Window);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
Expand Down Expand Up @@ -832,7 +910,10 @@ protected virtual void OnCharacterInput(uint codePoint, ModifierKeys mods)
/// <summary>
/// Raises the <see cref="Closed" /> event.
/// </summary>
protected virtual void OnClosed() { Closed?.Invoke(this, EventArgs.Empty); }
protected virtual void OnClosed()
{
Closed?.Invoke(this, EventArgs.Empty);
}

/// <summary>
/// Raises the <see cref="Closing" /> event.
Expand Down Expand Up @@ -866,7 +947,10 @@ protected virtual void OnFileDrop([NotNull] string[] paths)
/// </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); }
protected virtual void OnFocusChanged(bool focusing)
{
FocusChanged?.Invoke(this, EventArgs.Empty);
}

/// <summary>
/// Raises the <see cref="FramebufferSizeChanged" /> event.
Expand Down Expand Up @@ -950,7 +1034,10 @@ protected virtual void OnMouseScroll(double x, double y)
/// <param name="x">The new position on the x-axis.</param>
/// <param name="y">The new position on the y-axis.</param>
[SuppressMessage("ReSharper", "UnusedParameter.Global")]
protected virtual void OnPositionChanged(double x, double y) { PositionChanged?.Invoke(this, EventArgs.Empty); }
protected virtual void OnPositionChanged(double x, double y)
{
PositionChanged?.Invoke(this, EventArgs.Empty);
}

/// <summary>
/// Raises the <see cref="SizeChanged" /> event.
Expand Down
4 changes: 3 additions & 1 deletion GLFW.NET/Vulkan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using JetBrains.Annotations;

namespace GLFW
{
Expand Down Expand Up @@ -77,7 +78,7 @@ public static extern int
/// <param name="vulkan">The vulkan instance.</param>
/// <param name="procName">Name of the function.</param>
/// <returns>The address of the function, or <see cref="IntPtr.Zero" /> if an error occurred.</returns>
public static IntPtr GetInstanceProcAddress(IntPtr vulkan, string procName)
public static IntPtr GetInstanceProcAddress(IntPtr vulkan, [NotNull] string procName)
{
return GetInstanceProcAddress(vulkan, Encoding.ASCII.GetBytes(procName));
}
Expand All @@ -96,6 +97,7 @@ public static IntPtr GetInstanceProcAddress(IntPtr vulkan, string procName)
/// </para>
/// </summary>
/// <returns>An array of extension names.</returns>
[NotNull]
public static string[] GetRequiredInstanceExtensions()
{
var ptr = GetRequiredInstanceExtensions(out var count);
Expand Down

0 comments on commit 39acf7a

Please sign in to comment.