#define COMPAT_REV1519 // Keeps compatibility with revision 1519 #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.ComponentModel; #if !MINIMAL using System.Drawing; #endif using System.Text; namespace OpenTK.Input { /// /// Represents a mouse device and provides methods to query its status. /// public sealed class MouseDevice : IInputDevice { #region --- Fields --- string description; IntPtr id; int numButtons, numWheels; MouseState state; #if COMPAT_REV1519 int wheel_last_accessed = 0; Point pos_last_accessed = new Point(); #endif #endregion #region --- IInputDevice Members --- #region public string Description /// /// Gets a string describing this MouseDevice. /// public string Description { get { return description; } internal set { description = value; } } #endregion #region public InputDeviceType DeviceType /// /// Gets a value indicating the InputDeviceType of this InputDevice. /// public InputDeviceType DeviceType { get { return InputDeviceType.Mouse; } } #endregion #endregion #region --- Public Members --- /// /// Retrieves the combined hardware for all specified mouse devices. /// This method is equivalent to . /// /// A structure representing the state for the specified mouse device. /// public MouseState GetState() { return Mouse.GetState(); } /// /// Retrieves the hardware for the specified mouse device. /// This method is equivalent to . /// /// The index of the mouse device. /// A structure representing the state for the specified mouse device. /// public MouseState GetState(int index) { return Mouse.GetState(index); } /// /// Retreves the for the mouse cursor. /// This method is equivalent to . /// /// A structure representing the state of the mouse cursor. /// public MouseState GetCursorState() { return Mouse.GetCursorState(); } #region public int NumberOfButtons /// /// Gets an integer representing the number of buttons on this MouseDevice. /// public int NumberOfButtons { get { return numButtons; } internal set { numButtons = value; } } #endregion #region public int NumberOfWheels /// /// Gets an integer representing the number of wheels on this MouseDevice. /// public int NumberOfWheels { get { return numWheels; } internal set { numWheels = value; } } #endregion #region public IntPtr DeviceID /// /// Gets an IntPtr representing a device dependent ID. /// public IntPtr DeviceID { get { return id; } internal set { id = value; } } #endregion #region public int Wheel /// /// Gets the absolute wheel position in integer units. /// To support high-precision mice, it is recommended to use instead. /// public int Wheel { get { return state.Wheel; } } /// /// Gets the absolute wheel position in floating-point units. /// public float WheelPrecise { get { return state.WheelPrecise; } } #endregion #region public int X /// /// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates. /// public int X { get { return state.X; } } #endregion #region public int Y /// /// Gets an integer representing the absolute y position of the pointer, in window pixel coordinates. /// public int Y { get { return state.Y; } } #endregion #region public bool this[MouseButton b] /// /// Gets a System.Boolean indicating the state of the specified MouseButton. /// /// The MouseButton to check. /// True if the MouseButton is pressed, false otherwise. public bool this[MouseButton button] { get { return state[button]; } internal set { state[button] = value; } } #endregion #endregion #region --- Internal Members --- internal void HandleMouseDown(object sender, MouseButtonEventArgs e) { state = e.Mouse; ButtonDown(this, e); } internal void HandleMouseUp(object sender, MouseButtonEventArgs e) { state = e.Mouse; ButtonUp(this, e); } internal void HandleMouseMove(object sender, MouseMoveEventArgs e) { state = e.Mouse; Move(this, e); } internal void HandleMouseWheel(object sender, MouseWheelEventArgs e) { state = e.Mouse; WheelChanged(this, e); } #endregion #region --- Events --- /// /// Occurs when the mouse's position is moved. /// public event EventHandler Move = delegate { }; /// /// Occurs when a button is pressed. /// public event EventHandler ButtonDown = delegate { }; /// /// Occurs when a button is released. /// public event EventHandler ButtonUp = delegate { }; /// /// Occurs when one of the mouse wheels is moved. /// public event EventHandler WheelChanged = delegate { }; #region --- Overrides --- /// /// Calculates the hash code for this instance. /// /// public override int GetHashCode() { return (int)(numButtons ^ numWheels ^ id.GetHashCode() ^ description.GetHashCode()); } /// /// Returns a that describes this instance. /// /// A that describes this instance. public override string ToString() { return String.Format("ID: {0} ({1}). Buttons: {2}, Wheels: {3}", DeviceID, Description, NumberOfButtons, NumberOfWheels); } #endregion #endregion #region COMPAT_REV1519 #if COMPAT_REV1519 #region public int WheelDelta /// /// Gets an integer representing the relative wheel movement. /// [Obsolete("WheelDelta is only defined for a single WheelChanged event. Use the OpenTK.Input.MouseWheelEventArgs::Delta property with the OpenTK.Input.MouseDevice::WheelChanged event.", false)] public int WheelDelta { get { int result = (int)Math.Round(state.WheelPrecise - wheel_last_accessed, MidpointRounding.AwayFromZero); wheel_last_accessed = state.Wheel; return result; } } #endregion #region public int XDelta /// /// Gets an integer representing the relative x movement of the pointer, in pixel coordinates. /// [Obsolete("XDelta is only defined for a single Move event. Use the OpenTK.Input.MouseMoveEventArgs::Delta property with the OpenTK.Input.MouseDevice::Move event.", false)] public int XDelta { get { int result = state.X - pos_last_accessed.X; pos_last_accessed.X = state.X; return result; } } #endregion #region public int YDelta /// /// Gets an integer representing the relative y movement of the pointer, in pixel coordinates. /// [Obsolete("YDelta is only defined for a single Move event. Use the OpenTK.Input.MouseMoveEventArgs::Delta property with the OpenTK.Input.MouseDevice::Move event.", false)] public int YDelta { get { int result = state.Y - pos_last_accessed.Y; pos_last_accessed.Y = state.Y; return result; } } #endregion #endif #endregion } }