#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 OpenTK.Input { /// /// Encapsulates the state of a mouse device. /// public struct MouseState : IEquatable { #region Fields // Allocate enough ints to store all mouse buttons const int IntSize = sizeof(int); const int NumInts = ((int)MouseButton.LastButton + IntSize - 1) / IntSize; // The following line triggers bogus CS0214 in gmcs 2.0.1, sigh... unsafe fixed int Buttons[NumInts]; int x, y; float wheel; bool is_connected; #endregion #region Public Members /// /// Gets a indicating whether the specified /// is pressed. /// /// The to check. /// True if key is pressed; false otherwise. public bool this[MouseButton button] { get { return IsButtonDown(button); } internal set { if (value) EnableBit((int)button); else DisableBit((int)button); } } /// /// Gets a indicating whether this button is down. /// /// The to check. public bool IsButtonDown(MouseButton button) { return ReadBit((int)button); } /// /// Gets a indicating whether this button is up. /// /// The to check. public bool IsButtonUp(MouseButton button) { return !ReadBit((int)button); } /// /// Gets the absolute wheel position in integer units. /// To support high-precision mice, it is recommended to use instead. /// public int Wheel { get { return (int)Math.Round(wheel, MidpointRounding.AwayFromZero); } } /// /// Gets the absolute wheel position in floating-point units. /// public float WheelPrecise { get { return wheel; } internal set { wheel = value; } } /// /// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates. /// public int X { get { return x; } internal set { x = value; } } /// /// Gets an integer representing the absolute y position of the pointer, in window pixel coordinates. /// public int Y { get { return y; } internal set { y = value; } } /// /// Gets a indicating whether the left mouse button is pressed. /// This property is intended for XNA compatibility. /// public ButtonState LeftButton { get { return IsButtonDown(MouseButton.Left) ? ButtonState.Pressed : ButtonState.Released; } } /// /// Gets a indicating whether the middle mouse button is pressed. /// This property is intended for XNA compatibility. /// public ButtonState MiddleButton { get { return IsButtonDown(MouseButton.Middle) ? ButtonState.Pressed : ButtonState.Released; } } /// /// Gets a indicating whether the right mouse button is pressed. /// This property is intended for XNA compatibility. /// public ButtonState RightButton { get { return IsButtonDown(MouseButton.Right) ? ButtonState.Pressed : ButtonState.Released; } } /// /// Gets a indicating whether the first extra mouse button is pressed. /// This property is intended for XNA compatibility. /// public ButtonState XButton1 { get { return IsButtonDown(MouseButton.Button1) ? ButtonState.Pressed : ButtonState.Released; } } /// /// Gets a indicating whether the second extra mouse button is pressed. /// This property is intended for XNA compatibility. /// public ButtonState XButton2 { get { return IsButtonDown(MouseButton.Button2) ? ButtonState.Pressed : ButtonState.Released; } } /// /// 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 instead. /// public int ScrollWheelValue { get { return Wheel; } } /// /// Gets a value indicating whether this instance is connected. /// /// true if this instance is connected; otherwise, false. public bool IsConnected { get { return is_connected; } internal set { is_connected = value; } } /// /// Checks whether two instances are equal. /// /// /// A instance. /// /// /// A instance. /// /// /// True if both left is equal to right; false otherwise. /// public static bool operator ==(MouseState left, MouseState right) { return left.Equals(right); } /// /// Checks whether two instances are not equal. /// /// /// A instance. /// /// /// A instance. /// /// /// True if both left is not equal to right; false otherwise. /// public static bool operator !=(MouseState left, MouseState right) { return !left.Equals(right); } /// /// Compares to an object instance for equality. /// /// /// The to compare to. /// /// /// True if this instance is equal to obj; false otherwise. /// public override bool Equals(object obj) { if (obj is MouseState) { return this == (MouseState)obj; } else { return false; } } /// /// Generates a hashcode for the current instance. /// /// /// A represting the hashcode for this instance. /// public override int GetHashCode() { unsafe { fixed (int* b = Buttons) { return b->GetHashCode() ^ X.GetHashCode() ^ Y.GetHashCode() ^ WheelPrecise.GetHashCode(); } } } #endregion #region Internal Members internal bool ReadBit(int offset) { ValidateOffset(offset); int int_offset = offset / 32; int bit_offset = offset % 32; unsafe { fixed (int* b = Buttons) { return (*(b + int_offset) & (1 << bit_offset)) != 0u; } } } internal void EnableBit(int offset) { ValidateOffset(offset); int int_offset = offset / 32; int bit_offset = offset % 32; unsafe { fixed (int* b = Buttons) { *(b + int_offset) |= 1 << bit_offset; } } } internal void DisableBit(int offset) { ValidateOffset(offset); int int_offset = offset / 32; int bit_offset = offset % 32; unsafe { fixed (int* b = Buttons) { *(b + int_offset) &= ~(1 << bit_offset); } } } internal void MergeBits(MouseState other) { unsafe { int* b2 = other.Buttons; fixed (int* b1 = Buttons) { for (int i = 0; i < NumInts; i++) *(b1 + i) |= *(b2 + i); } WheelPrecise += other.WheelPrecise; X += other.X; Y += other.Y; IsConnected |= other.IsConnected; } } internal void SetIsConnected(bool value) { IsConnected = value; } #endregion #region Private Members static void ValidateOffset(int offset) { if (offset < 0 || offset >= NumInts * IntSize) throw new ArgumentOutOfRangeException("offset"); } #endregion #region IEquatable Members /// /// Compares two MouseState instances. /// /// The instance to compare two. /// True, if both instances are equal; false otherwise. public bool Equals(MouseState other) { bool equal = true; unsafe { int* b2 = other.Buttons; fixed (int* b1 = Buttons) { for (int i = 0; equal && i < NumInts; i++) equal &= *(b1 + i) == *(b2 + i); } equal &= X == other.X && Y == other.Y && WheelPrecise == other.WheelPrecise; } return equal; } #endregion } }