2 // The Open Toolkit Library License
4 // Copyright (c) 2006 - 2009 the Open Toolkit library.
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights to
9 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 // the Software, and to permit persons to whom the Software is furnished to do
11 // so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
28 namespace OpenTK.Input
31 /// Encapsulates the state of a mouse device.
33 public struct MouseState : IEquatable<MouseState>
35 internal const int MaxButtons = 16; // we are storing in an ushort
36 private Vector2 position;
37 private MouseScroll scroll;
38 private ushort buttons;
41 /// Gets a <see cref="System.Boolean"/> indicating whether the specified
42 /// <see cref="OpenTK.Input.MouseButton"/> is pressed.
44 /// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
45 /// <returns>True if key is pressed; false otherwise.</returns>
46 public bool this[MouseButton button]
48 get { return IsButtonDown(button); }
53 EnableBit((int)button);
57 DisableBit((int)button);
63 /// Gets a <see cref="System.Boolean"/> indicating whether this button is down.
65 /// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
66 public bool IsButtonDown(MouseButton button)
68 return ReadBit((int)button);
72 /// Gets a <see cref="System.Boolean"/> indicating whether this button is up.
74 /// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
75 public bool IsButtonUp(MouseButton button)
77 return !ReadBit((int)button);
81 /// Gets the absolute wheel position in integer units.
82 /// To support high-precision mice, it is recommended to use <see cref="WheelPrecise"/> instead.
86 get { return (int)Math.Round(scroll.Y, MidpointRounding.AwayFromZero); }
90 /// Gets the absolute wheel position in floating-point units.
92 public float WheelPrecise
94 get { return scroll.Y; }
98 /// Gets a <see cref="OpenTK.Input.MouseScroll"/> instance,
99 /// representing the current state of the mouse scroll wheel.
101 public MouseScroll Scroll
103 get { return scroll; }
107 /// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates.
111 get { return (int)Math.Round(position.X); }
112 internal set { position.X = value; }
116 /// Gets an integer representing the absolute y position of the pointer, in window pixel coordinates.
120 get { return (int)Math.Round(position.Y); }
121 internal set { position.Y = value; }
125 /// Gets a <see cref="System.Boolean"/> indicating whether the left mouse button is pressed.
126 /// This property is intended for XNA compatibility.
128 public ButtonState LeftButton
130 get { return IsButtonDown(MouseButton.Left) ? ButtonState.Pressed : ButtonState.Released; }
134 /// Gets a <see cref="System.Boolean"/> indicating whether the middle mouse button is pressed.
135 /// This property is intended for XNA compatibility.
137 public ButtonState MiddleButton
139 get { return IsButtonDown(MouseButton.Middle) ? ButtonState.Pressed : ButtonState.Released; }
143 /// Gets a <see cref="System.Boolean"/> indicating whether the right mouse button is pressed.
144 /// This property is intended for XNA compatibility.
146 public ButtonState RightButton
148 get { return IsButtonDown(MouseButton.Right) ? ButtonState.Pressed : ButtonState.Released; }
152 /// Gets a <see cref="System.Boolean"/> indicating whether the first extra mouse button is pressed.
153 /// This property is intended for XNA compatibility.
155 public ButtonState XButton1
157 get { return IsButtonDown(MouseButton.Button1) ? ButtonState.Pressed : ButtonState.Released; }
161 /// Gets a <see cref="System.Boolean"/> indicating whether the second extra mouse button is pressed.
162 /// This property is intended for XNA compatibility.
164 public ButtonState XButton2
166 get { return IsButtonDown(MouseButton.Button2) ? ButtonState.Pressed : ButtonState.Released; }
170 /// Gets a value indicating whether any button is down.
172 /// <value><c>true</c> if any button is down; otherwise, <c>false</c>.</value>
173 public bool IsAnyButtonDown
177 // If any bit is set then a button is down.
183 /// Gets the absolute wheel position in integer units. This property is intended for XNA compatibility.
184 /// To support high-precision mice, it is recommended to use <see cref="WheelPrecise"/> instead.
186 public int ScrollWheelValue
188 get { return Wheel; }
192 /// Gets a value indicating whether this instance is connected.
194 /// <value><c>true</c> if this instance is connected; otherwise, <c>false</c>.</value>
195 public bool IsConnected { get; internal set; }
198 /// Checks whether two <see cref="MouseState" /> instances are equal.
200 /// <param name="left">
201 /// A <see cref="MouseState"/> instance.
203 /// <param name="right">
204 /// A <see cref="MouseState"/> instance.
207 /// True if both left is equal to right; false otherwise.
209 public static bool operator ==(MouseState left, MouseState right)
211 return left.Equals(right);
215 /// Checks whether two <see cref="MouseState" /> instances are not equal.
217 /// <param name="left">
218 /// A <see cref="MouseState"/> instance.
220 /// <param name="right">
221 /// A <see cref="MouseState"/> instance.
224 /// True if both left is not equal to right; false otherwise.
226 public static bool operator !=(MouseState left, MouseState right)
228 return !left.Equals(right);
232 /// Compares to an object instance for equality.
234 /// <param name="obj">
235 /// The <see cref="System.Object"/> to compare to.
238 /// True if this instance is equal to obj; false otherwise.
240 public override bool Equals(object obj)
242 if (obj is MouseState)
244 return this == (MouseState)obj;
253 /// Generates a hashcode for the current instance.
256 /// A <see cref="System.Int32"/> represting the hashcode for this instance.
258 public override int GetHashCode()
260 return buttons.GetHashCode() ^ X.GetHashCode() ^ Y.GetHashCode() ^ scroll.GetHashCode();
264 /// Returns a <see cref="System.String"/> that represents the current <see cref="OpenTK.Input.MouseState"/>.
266 /// <returns>A <see cref="System.String"/> that represents the current <see cref="OpenTK.Input.MouseState"/>.</returns>
267 public override string ToString()
269 string b = Convert.ToString(buttons, 2).PadLeft(10, '0');
270 return String.Format("[X={0}, Y={1}, Scroll={2}, Buttons={3}, IsConnected={4}]",
271 X, Y, Scroll, b, IsConnected);
274 internal Vector2 Position
276 get { return position; }
277 set { position = value; }
280 internal bool ReadBit(int offset)
282 ValidateOffset(offset);
283 return (buttons & (1 << offset)) != 0;
286 internal void EnableBit(int offset)
288 ValidateOffset(offset);
289 buttons |= unchecked((ushort)(1 << offset));
292 internal void DisableBit(int offset)
294 ValidateOffset(offset);
295 buttons &= unchecked((ushort)(~(1 << offset)));
298 internal void MergeBits(MouseState other)
300 buttons |= other.buttons;
301 SetScrollRelative(other.scroll.X, other.scroll.Y);
304 IsConnected |= other.IsConnected;
307 internal void SetIsConnected(bool value)
312 internal void SetScrollAbsolute(float x, float y)
318 internal void SetScrollRelative(float x, float y)
324 private static void ValidateOffset(int offset)
326 if (offset < 0 || offset >= 16)
328 throw new ArgumentOutOfRangeException("offset");
333 /// Compares two MouseState instances.
335 /// <param name="other">The instance to compare two.</param>
336 /// <returns>True, if both instances are equal; false otherwise.</returns>
337 public bool Equals(MouseState other)
340 buttons == other.buttons &&
343 Scroll == other.Scroll;