From 1acf8a807b374c6d5e24c354a34a5085f5f85f9c Mon Sep 17 00:00:00 2001 From: Stefanos A Date: Sun, 22 Dec 2013 22:01:04 +0100 Subject: [PATCH] Implemented IEquatable<> interface --- Source/OpenTK/Input/GamePadButtons.cs | 30 ++++++++++++- Source/OpenTK/Input/GamePadCapabilities.cs | 46 ++++++++++++++++++- Source/OpenTK/Input/GamePadDPad.cs | 51 +++++++++++++++++++++- Source/OpenTK/Input/GamePadState.cs | 32 +++++++++++++- 4 files changed, 155 insertions(+), 4 deletions(-) diff --git a/Source/OpenTK/Input/GamePadButtons.cs b/Source/OpenTK/Input/GamePadButtons.cs index 199e593f..5d423ddb 100644 --- a/Source/OpenTK/Input/GamePadButtons.cs +++ b/Source/OpenTK/Input/GamePadButtons.cs @@ -107,9 +107,37 @@ namespace OpenTK.Input return !left.Equals(right); } + public override string ToString() + { + return String.Format( + "{{ABXYLR: {0}{1}{2}{3}{4}{5}; Back: {6}; BigButton: {7}; LStick: {8}; RStick: {9}}}", + A == ButtonState.Pressed ? "1" : "0", + B == ButtonState.Pressed ? "1" : "0", + X == ButtonState.Pressed ? "1" : "0", + Y == ButtonState.Pressed ? "1" : "0", + LeftShoulder == ButtonState.Pressed ? "1" : "0", + RightShoulder == ButtonState.Pressed ? "1" : "0", + Back == ButtonState.Pressed ? "1" : "0", + BigButton == ButtonState.Pressed ? "1" : "0", + LeftStick == ButtonState.Pressed ? "1" : "0", + RightStick == ButtonState.Pressed ? "1" : "0"); + } + + public override int GetHashCode() + { + return buttons.GetHashCode(); + } + + public override bool Equals(object obj) + { + return + obj is GamePadButtons && + Equals((GamePadButtons)obj); + } + #endregion - #region IEquatable Members + #region IEquatable Members public bool Equals(GamePadButtons other) { diff --git a/Source/OpenTK/Input/GamePadCapabilities.cs b/Source/OpenTK/Input/GamePadCapabilities.cs index 23c56cd3..767e0198 100644 --- a/Source/OpenTK/Input/GamePadCapabilities.cs +++ b/Source/OpenTK/Input/GamePadCapabilities.cs @@ -32,7 +32,7 @@ using System; namespace OpenTK.Input { - public struct GamePadCapabilities + public struct GamePadCapabilities : IEquatable { byte axis_count; byte button_count; @@ -62,6 +62,50 @@ namespace OpenTK.Input get { return trackball_count; } internal set { trackball_count = (byte)value; } } + + public static bool operator ==(GamePadCapabilities left, GamePadCapabilities right) + { + return left.Equals(right); + } + + public static bool operator !=(GamePadCapabilities left, GamePadCapabilities right) + { + return !left.Equals(right); + } + + public override string ToString() + { + return String.Format( + "{{Axes: {0}; Buttons: {1}; DPads: {2}; Trackballs: {3}}}", + AxisCount, ButtonCount, DPadCount, TrackballCount); + } + + public override int GetHashCode() + { + return + AxisCount.GetHashCode() ^ ButtonCount.GetHashCode() ^ + DPadCount.GetHashCode() ^ TrackballCount.GetHashCode(); + } + + public override bool Equals(object obj) + { + return + obj is GamePadCapabilities && + Equals((GamePadCapabilities)obj); + } + + #region IEquatable Members + + public bool Equals(GamePadCapabilities other) + { + return + AxisCount == other.AxisCount && + ButtonCount == other.ButtonCount && + DPadCount == other.DPadCount && + TrackballCount == other.TrackballCount; + } + + #endregion } } diff --git a/Source/OpenTK/Input/GamePadDPad.cs b/Source/OpenTK/Input/GamePadDPad.cs index 72389b00..06ba2262 100644 --- a/Source/OpenTK/Input/GamePadDPad.cs +++ b/Source/OpenTK/Input/GamePadDPad.cs @@ -31,7 +31,7 @@ using System; namespace OpenTK.Input { - public struct GamePadDPad + public struct GamePadDPad : IEquatable { [Flags] enum DPadButtons : byte @@ -44,6 +44,8 @@ namespace OpenTK.Input DPadButtons buttons; + #region Public Members + internal GamePadDPad(Buttons state) { // DPad butons are stored in the lower 4bits @@ -75,6 +77,42 @@ namespace OpenTK.Input internal set { SetButton(DPadButtons.Right, value); } } + public static bool operator ==(GamePadDPad left, GamePadDPad right) + { + return left.Equals(right); + } + + public static bool operator !=(GamePadDPad left, GamePadDPad right) + { + return !left.Equals(right); + } + + public override string ToString() + { + return String.Format( + "{{ULDR: {0}{1}{2}{3}}}", + IsUp ? "1" : "0", + IsLeft ? "1" : "0", + IsDown ? "1" : "0", + IsRight ? "1" : "0"); + } + + public override int GetHashCode() + { + return buttons.GetHashCode(); + } + + public override bool Equals(object obj) + { + return + obj is GamePadDPad && + Equals((GamePadDPad)obj); + } + + #endregion + + #region Private Members + void SetButton(DPadButtons button, bool value) { if (value) @@ -86,5 +124,16 @@ namespace OpenTK.Input buttons &= ~button; } } + + #endregion + + #region IEquatable Members + + public bool Equals(GamePadDPad other) + { + return buttons == other.buttons; + } + + #endregion } } diff --git a/Source/OpenTK/Input/GamePadState.cs b/Source/OpenTK/Input/GamePadState.cs index d45787bb..e85187f8 100644 --- a/Source/OpenTK/Input/GamePadState.cs +++ b/Source/OpenTK/Input/GamePadState.cs @@ -32,7 +32,7 @@ namespace OpenTK.Input /// /// Encapsulates the state of a GamePad device. /// - public struct GamePadState /*: IEquatable*/ + public struct GamePadState : IEquatable { const float RangeMultiplier = 1.0f / (short.MaxValue + 1); @@ -62,6 +62,36 @@ namespace OpenTK.Input get { return is_connected; } } + public override string ToString() + { + return String.Format( + "{{Buttons: {0}; DPad: {1}; IsConnected: {2}", + Buttons, DPad, IsConnected); + } + + public override int GetHashCode() + { + return Buttons.GetHashCode() ^ DPad.GetHashCode() ^ IsConnected.GetHashCode(); + } + + public override bool Equals(object obj) + { + return + obj is GamePadState && + Equals((GamePadState)obj); + } + + #endregion + + #region IEquatable Members + public bool Equals(GamePadState other) + { + return + Buttons == other.Buttons && + DPad == other.DPad && + IsConnected == other.IsConnected; + } + #endregion #region Internal Members