diff --git a/Source/OpenTK/Input/MouseDevice.cs b/Source/OpenTK/Input/MouseDevice.cs index 2e8be4c4..bd1861b8 100644 --- a/Source/OpenTK/Input/MouseDevice.cs +++ b/Source/OpenTK/Input/MouseDevice.cs @@ -1,4 +1,6 @@ -#region --- License --- +#define COMPAT_REV1519 // Keeps compatibility with revision 1519 + +#region --- License --- /* Copyright (c) 2006, 2007 Stefanos Apostolopoulos * See license.txt for license info */ @@ -8,6 +10,7 @@ using System; using System.Collections.Generic; using System.Text; using System.Drawing; +using System.ComponentModel; namespace OpenTK.Input { @@ -16,14 +19,20 @@ namespace OpenTK.Input /// public sealed class MouseDevice : IInputDevice { - private string description; - private int numButtons, numWheels; - private IntPtr id; - private bool[] button = new bool[(int)MouseButton.LastButton]; - private int wheel, wheel_last_accessed = 0; - private Point pos = new Point(); - private Point pos_last_accessed = new Point(); - internal int last_x, last_y; + #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 --- @@ -106,25 +115,17 @@ namespace OpenTK.Input internal set { wheel = value; - //if (Move != null) - // Move(this, EventArgs.Empty); - } - } - - #endregion - - #region public int WheelDelta - - /// - /// Gets an integer representing the relative wheel movement. - /// - public int WheelDelta - { - get - { - int result = wheel - wheel_last_accessed; - wheel_last_accessed = wheel; - return result; + if (WheelChanged != null) + WheelChanged( + this, + new MouseWheelEventArgs( + pos.X, + pos.Y, + wheel, + wheel - last_wheel + ) + ); + last_wheel = wheel; } } @@ -154,40 +155,38 @@ namespace OpenTK.Input #endregion - #region public int XDelta + #region public bool this[MouseButton b] /// - /// Gets an integer representing the relative x movement of the pointer, in pixel coordinates. + /// Gets a System.Boolean indicating the state of the specified MouseButton. /// - public int XDelta + /// The MouseButton to check. + /// True if the MouseButton is pressed, false otherwise. + public bool this[MouseButton button] { get { - int result = pos.X - pos_last_accessed.X; - pos_last_accessed.X = pos.X; - return result; + 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 - #region public int YDelta - - /// - /// Gets an integer representing the relative y movement of the pointer, in pixel coordinates. - /// - public int YDelta - { - get - { - int result = pos.Y - pos_last_accessed.Y; - pos_last_accessed.Y = pos.Y; - return result; - } - } - #endregion + #region --- Internal Members --- + #region internal Point Position /// @@ -198,54 +197,37 @@ namespace OpenTK.Input set { pos = value; - //if (Move != null) - // Move(this, EventArgs.Empty); + last_pos = pos; + + Move(this, new MouseMoveEventArgs(pos.X, pos.Y, pos.X - last_pos.X, pos.Y - last_pos.Y)); } } #endregion - ///// - /// Occurs when the mouse, or one of its wheels, is moved. + #endregion + + #region --- Events --- + + /// + /// Occurs when the mouse's position is moved. /// - //public event MouseMoveEvent Move; + public event MouseMoveEventHandler Move = delegate(object sender, MouseMoveEventArgs e) { }; /// /// Occurs when a button is pressed. /// - public event MouseButtonDownEvent ButtonDown; + public event MouseButtonEventHandler ButtonDown = delegate(object sender, MouseButtonEventArgs e) { }; /// /// Occurs when a button is released. /// - public event MouseButtonUpEvent ButtonUp; - - #region public bool this[MouseButton b] + public event MouseButtonEventHandler ButtonUp = delegate(object sender, MouseButtonEventArgs e) { }; /// - /// Gets a value indicating the status of the specified MouseButton. + /// Occurs when one of the mouse wheels is moved. /// - /// The MouseButton to check. - /// True if the MouseButton is pressed, false otherwise. - public bool this[MouseButton b] - { - get - { - return button[(int)b]; - } - internal set - { - if (ButtonDown != null && value && !button[(int)b]) - ButtonDown(this, b); - else if (ButtonUp != null && !value && button[(int)b]) - ButtonUp(this, b); - button[(int)b] = value; - - //System.Diagnostics.Debug.Print("Mouse button {0} {1}", b, value ? "down" : "up"); - } - } - - #endregion + public event MouseWheelEventHandler WheelChanged = delegate(object sender, MouseWheelEventArgs e) { }; #region --- Overrides --- @@ -264,26 +246,278 @@ namespace OpenTK.Input #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 } /// - /// Defines a MouseMove event. + /// Provides data for the and events. /// - /// The MouseDevice that generated this event. - /// Not used. - public delegate void MouseMoveEvent(MouseDevice sender, EventArgs e); + 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 + } /// - /// Defines the MouseButtonDown event. + /// Provides data for the event. /// - /// The MouseDevice that generated this event. - /// The MouseButton that was pressed. - public delegate void MouseButtonDownEvent(MouseDevice sender, MouseButton button); + 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 the MouseButtonUp event. + /// Defines a event. /// /// The MouseDevice that generated this event. - /// The MouseButton that was released. - public delegate void MouseButtonUpEvent(MouseDevice sender, MouseButton button); + /// 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 }