Skip to content

Commit d9e0007

Browse files
committed
[Toolkit.Input] Add IEquatable and ==, != operator to KeyboardState, MouseState, PointerPoint
1 parent 9d13162 commit d9e0007

File tree

3 files changed

+202
-3
lines changed

3 files changed

+202
-3
lines changed

Source/Toolkit/SharpDX.Toolkit.Input/KeyboardState.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020

2121
using System;
2222
using System.Collections.Generic;
23+
using System.Runtime.InteropServices;
2324

2425
namespace SharpDX.Toolkit.Input
2526
{
2627
/// <summary>
2728
/// Represents the immediate state of keyboard (pressed keys)
2829
/// </summary>
2930
/// <remarks>The returned values from member methods require computation - it is advised to cache them when they needs to be reused</remarks>
30-
public struct KeyboardState
31+
[StructLayout(LayoutKind.Sequential)]
32+
public struct KeyboardState : IEquatable<KeyboardState>
3133
{
3234
// The key bit flag storage method was inspired from MonoGame and ExEn engines
3335

@@ -230,5 +232,62 @@ private static void AddKeysToArray(uint chunk, int arrayOffset, Keys[] pressedKe
230232
pressedKeys[index++] = (Keys)(arrayOffset + i);
231233
}
232234
}
235+
236+
/// <summary>
237+
/// Indicates whether the current object is equal to another object of the same type.
238+
/// </summary>
239+
/// <param name="other">An object to compare with this object.</param>
240+
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
241+
public bool Equals(KeyboardState other)
242+
{
243+
return data0 == other.data0 && data1 == other.data1 && data2 == other.data2 && data3 == other.data3 && data4 == other.data4 && data5 == other.data5 && data6 == other.data6 && data7 == other.data7;
244+
}
245+
246+
public override bool Equals(object obj)
247+
{
248+
if (ReferenceEquals(null, obj))
249+
{
250+
return false;
251+
}
252+
return obj is KeyboardState && Equals((KeyboardState)obj);
253+
}
254+
255+
public override int GetHashCode()
256+
{
257+
unchecked
258+
{
259+
int hashCode = (int)data0;
260+
hashCode = (hashCode * 397) ^ (int)data1;
261+
hashCode = (hashCode * 397) ^ (int)data2;
262+
hashCode = (hashCode * 397) ^ (int)data3;
263+
hashCode = (hashCode * 397) ^ (int)data4;
264+
hashCode = (hashCode * 397) ^ (int)data5;
265+
hashCode = (hashCode * 397) ^ (int)data6;
266+
hashCode = (hashCode * 397) ^ (int)data7;
267+
return hashCode;
268+
}
269+
}
270+
271+
/// <summary>
272+
/// Implements the ==.
273+
/// </summary>
274+
/// <param name="left">The left.</param>
275+
/// <param name="right">The right.</param>
276+
/// <returns>The result of the operator.</returns>
277+
public static bool operator ==(KeyboardState left, KeyboardState right)
278+
{
279+
return left.Equals(right);
280+
}
281+
282+
/// <summary>
283+
/// Implements the !=.
284+
/// </summary>
285+
/// <param name="left">The left.</param>
286+
/// <param name="right">The right.</param>
287+
/// <returns>The result of the operator.</returns>
288+
public static bool operator !=(KeyboardState left, KeyboardState right)
289+
{
290+
return !left.Equals(right);
291+
}
233292
}
234293
}

Source/Toolkit/SharpDX.Toolkit.Input/MouseState.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
22+
using System.Runtime.InteropServices;
23+
2124
namespace SharpDX.Toolkit.Input
2225
{
2326
/// <summary>
2427
/// The <see cref="MouseState"/> structure represents a snapshot of mouse state.
2528
/// </summary>
2629
/// <remarks>Is inmutable.</remarks>
27-
public struct MouseState
30+
[StructLayout(LayoutKind.Sequential)]
31+
public struct MouseState : IEquatable<MouseState>
2832
{
2933
private readonly ButtonState left;
3034
private readonly ButtonState middle;
@@ -97,5 +101,62 @@ public MouseState(ButtonState left, ButtonState middle, ButtonState right, Butto
97101
/// Gets the cumulative mouse scroll wheel value since the game was started.
98102
/// </summary>
99103
public int WheelDelta { get { return wheelDelta; } }
104+
105+
/// <summary>
106+
/// Indicates whether the current object is equal to another object of the same type.
107+
/// </summary>
108+
/// <param name="other">An object to compare with this object.</param>
109+
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
110+
public bool Equals(MouseState other)
111+
{
112+
return left == other.left && middle == other.middle && right == other.right && xButton1 == other.xButton1 && xButton2 == other.xButton2 && MathUtil.WithinEpsilon(x, other.x) && MathUtil.WithinEpsilon(y, other.y) && wheelDelta == other.wheelDelta;
113+
}
114+
115+
public override bool Equals(object obj)
116+
{
117+
if (ReferenceEquals(null, obj))
118+
{
119+
return false;
120+
}
121+
return obj is MouseState && Equals((MouseState)obj);
122+
}
123+
124+
public override int GetHashCode()
125+
{
126+
unchecked
127+
{
128+
int hashCode = (int)left;
129+
hashCode = (hashCode * 397) ^ (int)middle;
130+
hashCode = (hashCode * 397) ^ (int)right;
131+
hashCode = (hashCode * 397) ^ (int)xButton1;
132+
hashCode = (hashCode * 397) ^ (int)xButton2;
133+
hashCode = (hashCode * 397) ^ x.GetHashCode();
134+
hashCode = (hashCode * 397) ^ y.GetHashCode();
135+
hashCode = (hashCode * 397) ^ wheelDelta;
136+
return hashCode;
137+
}
138+
}
139+
140+
/// <summary>
141+
/// Implements the ==.
142+
/// </summary>
143+
/// <param name="left">The left.</param>
144+
/// <param name="right">The right.</param>
145+
/// <returns>The result of the operator.</returns>
146+
public static bool operator ==(MouseState left, MouseState right)
147+
{
148+
return left.Equals(right);
149+
}
150+
151+
/// <summary>
152+
/// Implements the !=.
153+
/// </summary>
154+
/// <param name="left">The left.</param>
155+
/// <param name="right">The right.</param>
156+
/// <returns>The result of the operator.</returns>
157+
public static bool operator !=(MouseState left, MouseState right)
158+
{
159+
return !left.Equals(right);
160+
}
100161
}
101162
}

Source/Toolkit/SharpDX.Toolkit.Input/PointerPoint.cs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
22+
using System.Runtime.InteropServices;
23+
2124
namespace SharpDX.Toolkit.Input
2225
{
2326
/// <summary>
2427
/// Represents a platform-independent information about a pointer event.
2528
/// </summary>
26-
public struct PointerPoint
29+
[StructLayout(LayoutKind.Sequential)]
30+
public struct PointerPoint : IEquatable<PointerPoint>
2731
{
2832
/// <summary>
2933
/// The type of event that represents current pointer point
@@ -154,5 +158,80 @@ public struct PointerPoint
154158
/// Indicates the kind of pointer state change.
155159
/// </summary>
156160
public PointerUpdateKind PointerUpdateKind { get; internal set; }
161+
162+
/// <summary>
163+
/// Indicates whether the current object is equal to another object of the same type.
164+
/// </summary>
165+
/// <param name="other">An object to compare with this object.</param>
166+
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
167+
public bool Equals(PointerPoint other)
168+
{
169+
return EventType == other.EventType && DeviceType == other.DeviceType && PointerId == other.PointerId && Position.Equals(other.Position) && Timestamp == other.Timestamp && KeyModifiers == other.KeyModifiers && ContactRect.Equals(other.ContactRect) && IsBarrelButtonPresset.Equals(other.IsBarrelButtonPresset) && IsCanceled.Equals(other.IsCanceled) && IsEraser.Equals(other.IsEraser) && IsHorizontalMouseWheel.Equals(other.IsHorizontalMouseWheel) && IsInRange.Equals(other.IsInRange) && IsInverted.Equals(other.IsInverted) && IsLeftButtonPressed.Equals(other.IsLeftButtonPressed) && IsMiddleButtonPressed.Equals(other.IsMiddleButtonPressed) && IsRightButtonPressed.Equals(other.IsRightButtonPressed) && IsXButton1Pressed.Equals(other.IsXButton1Pressed) && IsXButton2Pressed.Equals(other.IsXButton2Pressed) && IsPrimary.Equals(other.IsPrimary) && MouseWheelDelta == other.MouseWheelDelta && Orientation.Equals(other.Orientation) && TouchConfidence.Equals(other.TouchConfidence) && Twist.Equals(other.Twist) && XTilt.Equals(other.XTilt) && YTilt.Equals(other.YTilt) && PointerUpdateKind == other.PointerUpdateKind;
170+
}
171+
172+
public override bool Equals(object obj)
173+
{
174+
if (ReferenceEquals(null, obj))
175+
{
176+
return false;
177+
}
178+
return obj is PointerPoint && Equals((PointerPoint)obj);
179+
}
180+
181+
public override int GetHashCode()
182+
{
183+
unchecked
184+
{
185+
int hashCode = (int)EventType;
186+
hashCode = (hashCode * 397) ^ (int)DeviceType;
187+
hashCode = (hashCode * 397) ^ (int)PointerId;
188+
hashCode = (hashCode * 397) ^ Position.GetHashCode();
189+
hashCode = (hashCode * 397) ^ Timestamp.GetHashCode();
190+
hashCode = (hashCode * 397) ^ (int)KeyModifiers;
191+
hashCode = (hashCode * 397) ^ ContactRect.GetHashCode();
192+
hashCode = (hashCode * 397) ^ IsBarrelButtonPresset.GetHashCode();
193+
hashCode = (hashCode * 397) ^ IsCanceled.GetHashCode();
194+
hashCode = (hashCode * 397) ^ IsEraser.GetHashCode();
195+
hashCode = (hashCode * 397) ^ IsHorizontalMouseWheel.GetHashCode();
196+
hashCode = (hashCode * 397) ^ IsInRange.GetHashCode();
197+
hashCode = (hashCode * 397) ^ IsInverted.GetHashCode();
198+
hashCode = (hashCode * 397) ^ IsLeftButtonPressed.GetHashCode();
199+
hashCode = (hashCode * 397) ^ IsMiddleButtonPressed.GetHashCode();
200+
hashCode = (hashCode * 397) ^ IsRightButtonPressed.GetHashCode();
201+
hashCode = (hashCode * 397) ^ IsXButton1Pressed.GetHashCode();
202+
hashCode = (hashCode * 397) ^ IsXButton2Pressed.GetHashCode();
203+
hashCode = (hashCode * 397) ^ IsPrimary.GetHashCode();
204+
hashCode = (hashCode * 397) ^ MouseWheelDelta;
205+
hashCode = (hashCode * 397) ^ Orientation.GetHashCode();
206+
hashCode = (hashCode * 397) ^ TouchConfidence.GetHashCode();
207+
hashCode = (hashCode * 397) ^ Twist.GetHashCode();
208+
hashCode = (hashCode * 397) ^ XTilt.GetHashCode();
209+
hashCode = (hashCode * 397) ^ YTilt.GetHashCode();
210+
hashCode = (hashCode * 397) ^ (int)PointerUpdateKind;
211+
return hashCode;
212+
}
213+
}
214+
215+
/// <summary>
216+
/// Implements the ==.
217+
/// </summary>
218+
/// <param name="left">The left.</param>
219+
/// <param name="right">The right.</param>
220+
/// <returns>The result of the operator.</returns>
221+
public static bool operator ==(PointerPoint left, PointerPoint right)
222+
{
223+
return left.Equals(right);
224+
}
225+
226+
/// <summary>
227+
/// Implements the !=.
228+
/// </summary>
229+
/// <param name="left">The left.</param>
230+
/// <param name="right">The right.</param>
231+
/// <returns>The result of the operator.</returns>
232+
public static bool operator !=(PointerPoint left, PointerPoint right)
233+
{
234+
return !left.Equals(right);
235+
}
157236
}
158237
}

0 commit comments

Comments
 (0)