From 7bab950cc025c5a7afbeae2bbc3c99312c76b15f Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Tue, 24 Dec 2013 15:01:10 +0100 Subject: [PATCH] Implemented structural equality --- Source/OpenTK/Input/JoystickCapabilities.cs | 39 ++++++++++- Source/OpenTK/Input/JoystickState.cs | 73 ++++++++++++++++++--- 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/Source/OpenTK/Input/JoystickCapabilities.cs b/Source/OpenTK/Input/JoystickCapabilities.cs index b25fa69b..80036211 100644 --- a/Source/OpenTK/Input/JoystickCapabilities.cs +++ b/Source/OpenTK/Input/JoystickCapabilities.cs @@ -33,7 +33,7 @@ using System.Text; namespace OpenTK.Input { - public struct JoystickCapabilities + public struct JoystickCapabilities : IEquatable { byte axis_count; byte button_count; @@ -69,6 +69,33 @@ namespace OpenTK.Input get { return button_count; } } + public bool IsConnected + { + get { return is_connected; } + } + + public override string ToString() + { + return String.Format( + "{{Axes: {0}; Buttons: {1}; IsConnected: {2}}}", + AxisCount, ButtonCount, IsConnected); + } + + public override int GetHashCode() + { + return + AxisCount.GetHashCode() ^ + ButtonCount.GetHashCode() ^ + IsConnected.GetHashCode(); + } + + public override bool Equals(object obj) + { + return + obj is JoystickCapabilities && + Equals((JoystickCapabilities)obj); + } + #endregion #region Private Members @@ -80,10 +107,16 @@ namespace OpenTK.Input #endregion - public bool IsConnected + #region IEquatable Members + + public bool Equals(JoystickCapabilities other) { - get { return is_connected; } + return + AxisCount == other.AxisCount && + ButtonCount == other.ButtonCount && + IsConnected == other.IsConnected; } + #endregion } } diff --git a/Source/OpenTK/Input/JoystickState.cs b/Source/OpenTK/Input/JoystickState.cs index 8a233df6..1b2a1299 100644 --- a/Source/OpenTK/Input/JoystickState.cs +++ b/Source/OpenTK/Input/JoystickState.cs @@ -34,7 +34,7 @@ using System.Text; namespace OpenTK.Input { - public struct JoystickState + public struct JoystickState : IEquatable { // If we ever add more values to JoystickAxis or JoystickButton // then we'll need to increase these limits. @@ -59,13 +59,7 @@ namespace OpenTK.Input float value = 0.0f; if (axis >= 0 && axis < MaxAxes) { - unsafe - { - fixed (short* paxis = axes) - { - value = *(paxis + axis) * ConversionFactor; - } - } + value = GetAxisUnsafe(axis) * ConversionFactor; } else { @@ -94,6 +88,38 @@ namespace OpenTK.Input get { return is_connected; } } + public override string ToString() + { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < MaxAxes; i++) + { + sb.Append(" "); + sb.Append(GetAxis(i)); + } + return String.Format( + "{{Axes:{0}; Buttons: {1}; IsConnected: {2}}}", + sb.ToString(), + Convert.ToString((int)buttons, 2), + IsConnected); + } + + public override int GetHashCode() + { + int hash = buttons.GetHashCode() ^ IsConnected.GetHashCode(); + for (int i = 0; i < MaxAxes; i++) + { + hash ^= GetAxisUnsafe(i).GetHashCode(); + } + return hash; + } + + public override bool Equals(object obj) + { + return + obj is JoystickState && + Equals((JoystickState)obj); + } + #endregion #region Internal Members @@ -132,5 +158,36 @@ namespace OpenTK.Input } #endregion + + #region Private Members + + short GetAxisUnsafe(int index) + { + unsafe + { + fixed (short* paxis = axes) + { + return *(paxis + index); + } + } + } + + #endregion + + #region IEquatable Members + + public bool Equals(JoystickState other) + { + bool equals = + buttons == other.buttons && + IsConnected == other.IsConnected; + for (int i = 0; equals && i < MaxAxes; i++) + { + equals &= GetAxisUnsafe(i) == other.GetAxisUnsafe(i); + } + return equals; + } + + #endregion } }