Applied ALyman's patch for MouseDevice.Move (http://www.opentk.com/node/652) with modifications.

This commit is contained in:
the_fiddler 2009-02-20 18:02:34 +00:00
parent f3a4da72ec
commit e44c93bae5

View file

@ -1,4 +1,6 @@
#region --- License --- #define COMPAT_REV1519 // Keeps compatibility with revision 1519
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos /* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info * See license.txt for license info
*/ */
@ -8,6 +10,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Drawing; using System.Drawing;
using System.ComponentModel;
namespace OpenTK.Input namespace OpenTK.Input
{ {
@ -16,14 +19,20 @@ namespace OpenTK.Input
/// </summary> /// </summary>
public sealed class MouseDevice : IInputDevice public sealed class MouseDevice : IInputDevice
{ {
private string description; #region --- Fields ---
private int numButtons, numWheels;
private IntPtr id; string description;
private bool[] button = new bool[(int)MouseButton.LastButton]; IntPtr id;
private int wheel, wheel_last_accessed = 0; int numButtons, numWheels;
private Point pos = new Point(); readonly bool[] button_state = new bool[Enum.GetValues(typeof(MouseButton)).Length];
private Point pos_last_accessed = new Point(); int wheel, last_wheel;
internal int last_x, last_y; 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 --- IInputDevice Members ---
@ -106,25 +115,17 @@ namespace OpenTK.Input
internal set internal set
{ {
wheel = value; wheel = value;
//if (Move != null) if (WheelChanged != null)
// Move(this, EventArgs.Empty); WheelChanged(
} this,
} new MouseWheelEventArgs(
pos.X,
#endregion pos.Y,
wheel,
#region public int WheelDelta wheel - last_wheel
)
/// <summary> );
/// Gets an integer representing the relative wheel movement. last_wheel = wheel;
/// </summary>
public int WheelDelta
{
get
{
int result = wheel - wheel_last_accessed;
wheel_last_accessed = wheel;
return result;
} }
} }
@ -154,40 +155,38 @@ namespace OpenTK.Input
#endregion #endregion
#region public int XDelta #region public bool this[MouseButton b]
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
public int XDelta /// <param name="key">The MouseButton to check.</param>
/// <returns>True if the MouseButton is pressed, false otherwise.</returns>
public bool this[MouseButton button]
{ {
get get
{ {
int result = pos.X - pos_last_accessed.X; return button_state[(int)button];
pos_last_accessed.X = pos.X; }
return result; 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 public int YDelta
/// <summary>
/// Gets an integer representing the relative y movement of the pointer, in pixel coordinates.
/// </summary>
public int YDelta
{
get
{
int result = pos.Y - pos_last_accessed.Y;
pos_last_accessed.Y = pos.Y;
return result;
}
}
#endregion #endregion
#region --- Internal Members ---
#region internal Point Position #region internal Point Position
/// <summary> /// <summary>
@ -198,54 +197,37 @@ namespace OpenTK.Input
set set
{ {
pos = value; pos = value;
//if (Move != null) last_pos = pos;
// Move(this, EventArgs.Empty);
Move(this, new MouseMoveEventArgs(pos.X, pos.Y, pos.X - last_pos.X, pos.Y - last_pos.Y));
} }
} }
#endregion #endregion
///// <summary> #endregion
/// Occurs when the mouse, or one of its wheels, is moved.
#region --- Events ---
/// <summary>
/// Occurs when the mouse's position is moved.
/// </summary> /// </summary>
//public event MouseMoveEvent Move; public event MouseMoveEventHandler Move = delegate(object sender, MouseMoveEventArgs e) { };
/// <summary> /// <summary>
/// Occurs when a button is pressed. /// Occurs when a button is pressed.
/// </summary> /// </summary>
public event MouseButtonDownEvent ButtonDown; public event MouseButtonEventHandler ButtonDown = delegate(object sender, MouseButtonEventArgs e) { };
/// <summary> /// <summary>
/// Occurs when a button is released. /// Occurs when a button is released.
/// </summary> /// </summary>
public event MouseButtonUpEvent ButtonUp; public event MouseButtonEventHandler ButtonUp = delegate(object sender, MouseButtonEventArgs e) { };
#region public bool this[MouseButton b]
/// <summary> /// <summary>
/// Gets a value indicating the status of the specified MouseButton. /// Occurs when one of the mouse wheels is moved.
/// </summary> /// </summary>
/// <param name="key">The MouseButton to check.</param> public event MouseWheelEventHandler WheelChanged = delegate(object sender, MouseWheelEventArgs e) { };
/// <returns>True if the MouseButton is pressed, false otherwise.</returns>
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
#region --- Overrides --- #region --- Overrides ---
@ -264,26 +246,278 @@ namespace OpenTK.Input
#endregion #endregion
#endregion #endregion
#region COMPAT_REV1519
#if COMPAT_REV1519
#region public int WheelDelta
/// <summary>
/// Gets an integer representing the relative wheel movement.
/// </summary>
[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
/// <summary>
/// Gets an integer representing the relative x movement of the pointer, in pixel coordinates.
/// </summary>
[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
/// <summary>
/// Gets an integer representing the relative y movement of the pointer, in pixel coordinates.
/// </summary>
[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
/// <summary>
/// Initializes a new instance of the <see cref="MouseMoveEventArgs"/> class.
/// </summary>
/// <param name="x">The X position.</param>
/// <param name="y">The Y position.</param>
/// <param name="xDelta">The change in X position produced by this event.</param>
/// <param name="yDelta">The change in Y position produced by this event.</param>
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
/// <summary>
/// Gets the X position of the mouse for the event.
/// </summary>
public int X { get { return x; } }
/// <summary>
/// Gets the Y position of the mouse for the event.
/// </summary>
public int Y { get { return y; } }
/// <summary>
/// Gets a System.Drawing.Points representing the location of the mouse for the event.
/// </summary>
public Point Position { get { return new Point(x, y); } }
/// <summary>
/// Gets the change in X position produced by this event.
/// </summary>
public int XDelta { get { return x_delta; } }
/// <summary>
/// Gets the change in Y position produced by this event.
/// </summary>
public int YDelta { get { return y_delta; } }
#endregion
} }
/// <summary> /// <summary>
/// Defines a MouseMove event. /// Provides data for the <see cref="MouseDevice.ButtonDown"/> and <see cref="MouseDevice.ButtonUp"/> events.
/// </summary> /// </summary>
/// <param name="sender">The MouseDevice that generated this event.</param> public struct MouseButtonEventArgs
/// <param name="e">Not used.</param> {
public delegate void MouseMoveEvent(MouseDevice sender, EventArgs e); #region Fields
MouseButton button;
bool pressed;
int x, y;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="MouseButtonEventArgs"/> class.
/// </summary>
/// <param name="x">The X position.</param>
/// <param name="y">The Y position.</param>
/// <param name="button">The mouse button for the event.</param>
/// <param name="pressed">The current state of the button.</param>
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
/// <summary>
/// Gets the X position of the mouse for the event.
/// </summary>
public int X { get { return x; } }
/// <summary>
/// Gets the Y position of the mouse for the event.
/// </summary>
public int Y { get { return y; } }
/// <summary>
/// Gets a System.Drawing.Points representing the location of the mouse for the event.
/// </summary>
public Point Position { get { return new Point(x, y); } }
/// <summary>
/// The mouse button for the event.
/// </summary>
public MouseButton Button { get { return this.button; } }
/// <summary>
/// Gets a System.Boolean representing the state of the mouse button for the event.
/// </summary>
public bool IsPressed { get { return pressed; } }
#endregion
}
/// <summary> /// <summary>
/// Defines the MouseButtonDown event. /// Provides data for the <see cref="MouseDevice.WheelChanged"/> event.
/// </summary> /// </summary>
/// <param name="sender">The MouseDevice that generated this event.</param> public struct MouseWheelEventArgs
/// <param name="button">The MouseButton that was pressed.</param> {
public delegate void MouseButtonDownEvent(MouseDevice sender, MouseButton button); #region Fields
int value;
int delta;
int x, y;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="MouseWheelEventArgs"/> class.
/// </summary>
/// <param name="x">The X position.</param>
/// <param name="y">The Y position.</param>
/// <param name="value">The value of the wheel.</param>
/// <param name="delta">The change in value of the wheel for this event.</param>
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
/// <summary>
/// Gets the X position of the mouse for the event.
/// </summary>
public int X { get { return x; } }
/// <summary>
/// Gets the Y position of the mouse for the event.
/// </summary>
public int Y { get { return y; } }
/// <summary>
/// Gets a System.Drawing.Points representing the location of the mouse for the event.
/// </summary>
public Point Position { get { return new Point(x, y); } }
/// <summary>
/// The value of the wheel.
/// </summary>
public int Value { get { return value; } }
/// <summary>
/// The change in value of the wheel for this event.
/// </summary>
public int Delta { get { return delta; } }
#endregion
}
#endregion
#region Event Handlers
/// <summary> /// <summary>
/// Defines the MouseButtonUp event. /// Defines a <see cref="MouseDevice.Move"/> event.
/// </summary> /// </summary>
/// <param name="sender">The MouseDevice that generated this event.</param> /// <param name="sender">The MouseDevice that generated this event.</param>
/// <param name="button">The MouseButton that was released.</param> /// <param name="e">A <see cref="MouseMoveEventArgs"/> that contains the event data.</param>
public delegate void MouseButtonUpEvent(MouseDevice sender, MouseButton button); public delegate void MouseMoveEventHandler(object sender, MouseMoveEventArgs e);
/// <summary>
/// Defines a <see cref="MouseDevice.WheelChanged"/> event.
/// </summary>
/// <param name="sender">The MouseDevice that generated this event.</param>
/// <param name="e">A <see>MouseWheelEventArgs</see> that contains the event data.</param>
public delegate void MouseWheelEventHandler(object sender, MouseWheelEventArgs e);
/// <summary>
/// Defines the <see cref="MouseDevice.ButtonDown"/> and <see cref="MouseDevice.ButtonUp"/> events.
/// </summary>
/// <param name="sender">The MouseDevice that generated this event.</param>
/// <param name="e">A <see cref="MouseButtonEventArgs"/> that contains the event data.</param>
public delegate void MouseButtonEventHandler(object sender, MouseButtonEventArgs e);
#endregion
} }