1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace Crow
{
/// <summary>
/// Encapsulates the state of a mouse device.
/// </summary>
public struct MouseState : IEquatable<MouseState>
{
#region Fields
public const int MaxButtons = 16; // we are storing in an ushort
Point position;
MouseScroll scroll;
ushort buttons;
bool is_connected;
#endregion
#region Public Members
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether the specified
/// <see cref="OpenTK.Input.MouseButton"/> is pressed.
/// </summary>
/// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
/// <returns>True if key is pressed; false otherwise.</returns>
public bool this[MouseButton button]
{
get { return IsButtonDown(button); }
internal set
{
if (value)
EnableBit((int)button);
else
DisableBit((int)button);
}
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether this button is down.
/// </summary>
/// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
public bool IsButtonDown(MouseButton button)
{
return ReadBit((int)button);
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether this button is up.
/// </summary>
/// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
public bool IsButtonUp(MouseButton button)
{
return !ReadBit((int)button);
}
/// <summary>
/// Gets the absolute wheel position in integer units.
/// To support high-precision mice, it is recommended to use <see cref="WheelPrecise"/> instead.
/// </summary>
public int Wheel
{
get { return (int)Math.Round(scroll.Y, MidpointRounding.AwayFromZero); }
}
/// <summary>
/// Gets the absolute wheel position in floating-point units.
/// </summary>
public float WheelPrecise
{
get { return scroll.Y; }
}
/// <summary>
/// Gets a <see cref="OpenTK.Input.MouseScroll"/> instance,
/// representing the current state of the mouse scroll wheel.
/// </summary>
public MouseScroll Scroll
{
get { return scroll; }
}
/// <summary>
/// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates.
/// </summary>
public int X
{
get { return position.X; }
set { position.X = value; }
}
/// <summary>
/// Gets an integer representing the absolute y position of the pointer, in window pixel coordinates.
/// </summary>
public int Y
{
get { return position.Y; }
set { position.Y = value; }
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether the left mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState LeftButton
{
get { return IsButtonDown(MouseButton.Left) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether the middle mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState MiddleButton
{
get { return IsButtonDown(MouseButton.Middle) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether the right mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState RightButton
{
get { return IsButtonDown(MouseButton.Right) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether the first extra mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState XButton1
{
get { return IsButtonDown(MouseButton.Button1) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether the second extra mouse button is pressed.
/// This property is intended for XNA compatibility.
/// </summary>
public ButtonState XButton2
{
get { return IsButtonDown(MouseButton.Button2) ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets the absolute wheel position in integer units. This property is intended for XNA compatibility.
/// To support high-precision mice, it is recommended to use <see cref="WheelPrecise"/> instead.
/// </summary>
public int ScrollWheelValue
{
get { return Wheel; }
}
/// <summary>
/// Gets a value indicating whether this instance is connected.
/// </summary>
/// <value><c>true</c> if this instance is connected; otherwise, <c>false</c>.</value>
public bool IsConnected
{
get { return is_connected; }
internal set { is_connected = value; }
}
/// <summary>
/// Checks whether two <see cref="MouseState" /> instances are equal.
/// </summary>
/// <param name="left">
/// A <see cref="MouseState"/> instance.
/// </param>
/// <param name="right">
/// A <see cref="MouseState"/> instance.
/// </param>
/// <returns>
/// True if both left is equal to right; false otherwise.
/// </returns>
public static bool operator ==(MouseState left, MouseState right)
{
return left.Equals(right);
}
/// <summary>
/// Checks whether two <see cref="MouseState" /> instances are not equal.
/// </summary>
/// <param name="left">
/// A <see cref="MouseState"/> instance.
/// </param>
/// <param name="right">
/// A <see cref="MouseState"/> instance.
/// </param>
/// <returns>
/// True if both left is not equal to right; false otherwise.
/// </returns>
public static bool operator !=(MouseState left, MouseState right)
{
return !left.Equals(right);
}
/// <summary>
/// Compares to an object instance for equality.
/// </summary>
/// <param name="obj">
/// The <see cref="System.Object"/> to compare to.
/// </param>
/// <returns>
/// True if this instance is equal to obj; false otherwise.
/// </returns>
public override bool Equals(object obj)
{
if (obj is MouseState)
{
return this == (MouseState)obj;
}
else
{
return false;
}
}
/// <summary>
/// Generates a hashcode for the current instance.
/// </summary>
/// <returns>
/// A <see cref="System.Int32"/> represting the hashcode for this instance.
/// </returns>
public override int GetHashCode()
{
return buttons.GetHashCode() ^ X.GetHashCode() ^ Y.GetHashCode() ^ scroll.GetHashCode();
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="OpenTK.Input.MouseState"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="OpenTK.Input.MouseState"/>.</returns>
public override string ToString()
{
string b = Convert.ToString(buttons, 2).PadLeft(10, '0');
return String.Format("[X={0}, Y={1}, Scroll={2}, Buttons={3}, IsConnected={4}]",
X, Y, Scroll, b, IsConnected);
}
#endregion
#region Internal Members
internal Point Position
{
get { return position; }
set { position = value; }
}
internal bool ReadBit(int offset)
{
ValidateOffset(offset);
return (buttons & (1 << offset)) != 0;
}
public void EnableBit(int offset)
{
ValidateOffset(offset);
buttons |= unchecked((ushort)(1 << offset));
}
public void DisableBit(int offset)
{
ValidateOffset(offset);
buttons &= unchecked((ushort)(~(1 << offset)));
}
internal void MergeBits(MouseState other)
{
buttons |= other.buttons;
SetScrollRelative(other.scroll.X, other.scroll.Y);
X += other.X;
Y += other.Y;
IsConnected |= other.IsConnected;
}
internal void SetIsConnected(bool value)
{
IsConnected = value;
}
#region Internal Members
internal void SetScrollAbsolute(float x, float y)
{
scroll.X = x;
scroll.Y = y;
}
internal void SetScrollRelative(float x, float y)
{
scroll.X += x;
scroll.Y += y;
}
#endregion
#endregion
#region Private Members
static void ValidateOffset(int offset)
{
if (offset < 0 || offset >= 16)
throw new ArgumentOutOfRangeException("offset");
}
#endregion
#region IEquatable<MouseState> Members
/// <summary>
/// Compares two MouseState instances.
/// </summary>
/// <param name="other">The instance to compare two.</param>
/// <returns>True, if both instances are equal; false otherwise.</returns>
public bool Equals(MouseState other)
{
return
buttons == other.buttons &&
X == other.X &&
Y == other.Y &&
Scroll == other.Scroll;
}
#endregion
}
}
|