#define COMPAT_REV1519 // Keeps compatibility with revision 1519 #region --- License --- /* Copyright (c) 2006, 2007 Stefanos Apostolopoulos * See license.txt for license info */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.ComponentModel; 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; readonly bool[] button_state = new bool[Enum.GetValues(typeof(MouseButton)).Length]; int wheel, last_wheel; Point pos = new Point(), last_pos = new Point(); #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 --- #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 an integer representing the absolute wheel position. /// public int Wheel { get { return wheel; } internal set { wheel = value; if (WheelChanged != null) WheelChanged( this, new MouseWheelEventArgs( pos.X, pos.Y, wheel, wheel - last_wheel ) ); last_wheel = wheel; } } #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 pos.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 pos.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 button_state[(int)button]; } internal set { bool previous_state = button_state[(int)button]; button_state[(int)button] = value; MouseButtonEventArgs e = new MouseButtonEventArgs(pos.X, pos.Y, button, value); if (value && !previous_state) ButtonDown(this, e); else if (!value && previous_state) ButtonUp(this, e); } } #endregion #endregion #region --- Internal Members --- #region internal Point Position /// /// Sets a System.Drawing.Point representing the absolute position of the pointer, in window pixel coordinates. /// internal Point Position { set { pos = value; last_pos = pos; Move(this, new MouseMoveEventArgs(pos.X, pos.Y, pos.X - last_pos.X, pos.Y - last_pos.Y)); } } #endregion #endregion #region --- Events --- /// /// Occurs when the mouse's position is moved. /// public event MouseMoveEventHandler Move = delegate(object sender, MouseMoveEventArgs e) { }; /// /// Occurs when a button is pressed. /// public event MouseButtonEventHandler ButtonDown = delegate(object sender, MouseButtonEventArgs e) { }; /// /// Occurs when a button is released. /// public event MouseButtonEventHandler ButtonUp = delegate(object sender, MouseButtonEventArgs e) { }; /// /// Occurs when one of the mouse wheels is moved. /// public event MouseWheelEventHandler WheelChanged = delegate(object sender, MouseWheelEventArgs e) { }; #region --- Overrides --- public override int GetHashCode() { //return base.GetHashCode(); return (int)(numButtons ^ numWheels ^ id.GetHashCode() ^ description.GetHashCode()); } 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 = wheel - wheel_last_accessed; wheel_last_accessed = 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 = pos.X - pos_last_accessed.X; pos_last_accessed.X = pos.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 = pos.Y - pos_last_accessed.Y; pos_last_accessed.Y = pos.Y; return result; } } #endregion #endif #endregion } #region Event Arguments public struct MouseMoveEventArgs { #region Fields int x, y; int x_delta, y_delta; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The X position. /// The Y position. /// The change in X position produced by this event. /// The change in Y position produced by this event. public MouseMoveEventArgs(int x, int y, int xDelta, int yDelta) { this.x = x; this.y = y; this.x_delta = xDelta; this.y_delta = yDelta; } #endregion #region Public Members /// /// Gets the X position of the mouse for the event. /// public int X { get { return x; } } /// /// Gets the Y position of the mouse for the event. /// public int Y { get { return y; } } /// /// Gets a System.Drawing.Points representing the location of the mouse for the event. /// public Point Position { get { return new Point(x, y); } } /// /// Gets the change in X position produced by this event. /// public int XDelta { get { return x_delta; } } /// /// Gets the change in Y position produced by this event. /// public int YDelta { get { return y_delta; } } #endregion } /// /// Provides data for the and events. /// public struct MouseButtonEventArgs { #region Fields MouseButton button; bool pressed; int x, y; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The X position. /// The Y position. /// The mouse button for the event. /// The current state of the button. internal MouseButtonEventArgs(int x, int y, MouseButton button, bool pressed) { this.x = x; this.y = y; this.button = button; this.pressed = pressed; } #endregion #region Public Members /// /// Gets the X position of the mouse for the event. /// public int X { get { return x; } } /// /// Gets the Y position of the mouse for the event. /// public int Y { get { return y; } } /// /// Gets a System.Drawing.Points representing the location of the mouse for the event. /// public Point Position { get { return new Point(x, y); } } /// /// The mouse button for the event. /// public MouseButton Button { get { return this.button; } } /// /// Gets a System.Boolean representing the state of the mouse button for the event. /// public bool IsPressed { get { return pressed; } } #endregion } /// /// Provides data for the event. /// public struct MouseWheelEventArgs { #region Fields int value; int delta; int x, y; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The X position. /// The Y position. /// The value of the wheel. /// The change in value of the wheel for this event. public MouseWheelEventArgs(int x, int y, int value, int delta) { this.x = x; this.y = y; this.value = value; this.delta = delta; } #endregion #region Public Members /// /// Gets the X position of the mouse for the event. /// public int X { get { return x; } } /// /// Gets the Y position of the mouse for the event. /// public int Y { get { return y; } } /// /// Gets a System.Drawing.Points representing the location of the mouse for the event. /// public Point Position { get { return new Point(x, y); } } /// /// The value of the wheel. /// public int Value { get { return value; } } /// /// The change in value of the wheel for this event. /// public int Delta { get { return delta; } } #endregion } #endregion #region Event Handlers /// /// Defines a event. /// /// The MouseDevice that generated this event. /// A that contains the event data. public delegate void MouseMoveEventHandler(object sender, MouseMoveEventArgs e); /// /// Defines a event. /// /// The MouseDevice that generated this event. /// A MouseWheelEventArgs that contains the event data. public delegate void MouseWheelEventHandler(object sender, MouseWheelEventArgs e); /// /// Defines the and events. /// /// The MouseDevice that generated this event. /// A that contains the event data. public delegate void MouseButtonEventHandler(object sender, MouseButtonEventArgs e); #endregion }