Implemented structural equality

This commit is contained in:
Stefanos A. 2013-12-24 15:01:10 +01:00
parent 42e6a96a43
commit 7bab950cc0
2 changed files with 101 additions and 11 deletions

View file

@ -33,7 +33,7 @@ using System.Text;
namespace OpenTK.Input namespace OpenTK.Input
{ {
public struct JoystickCapabilities public struct JoystickCapabilities : IEquatable<JoystickCapabilities>
{ {
byte axis_count; byte axis_count;
byte button_count; byte button_count;
@ -69,6 +69,33 @@ namespace OpenTK.Input
get { return button_count; } 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 #endregion
#region Private Members #region Private Members
@ -80,10 +107,16 @@ namespace OpenTK.Input
#endregion #endregion
public bool IsConnected #region IEquatable<JoystickCapabilities> Members
public bool Equals(JoystickCapabilities other)
{ {
get { return is_connected; } return
AxisCount == other.AxisCount &&
ButtonCount == other.ButtonCount &&
IsConnected == other.IsConnected;
} }
#endregion
} }
} }

View file

@ -34,7 +34,7 @@ using System.Text;
namespace OpenTK.Input namespace OpenTK.Input
{ {
public struct JoystickState public struct JoystickState : IEquatable<JoystickState>
{ {
// If we ever add more values to JoystickAxis or JoystickButton // If we ever add more values to JoystickAxis or JoystickButton
// then we'll need to increase these limits. // then we'll need to increase these limits.
@ -59,13 +59,7 @@ namespace OpenTK.Input
float value = 0.0f; float value = 0.0f;
if (axis >= 0 && axis < MaxAxes) if (axis >= 0 && axis < MaxAxes)
{ {
unsafe value = GetAxisUnsafe(axis) * ConversionFactor;
{
fixed (short* paxis = axes)
{
value = *(paxis + axis) * ConversionFactor;
}
}
} }
else else
{ {
@ -94,6 +88,38 @@ namespace OpenTK.Input
get { return is_connected; } 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 #endregion
#region Internal Members #region Internal Members
@ -132,5 +158,36 @@ namespace OpenTK.Input
} }
#endregion #endregion
#region Private Members
short GetAxisUnsafe(int index)
{
unsafe
{
fixed (short* paxis = axes)
{
return *(paxis + index);
}
}
}
#endregion
#region IEquatable<JoystickState> 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
} }
} }