Fixed mouse delta handling.

This commit is contained in:
the_fiddler 2008-05-04 17:42:19 +00:00
parent b5fb1361ef
commit e9e9ee5f9c
5 changed files with 180 additions and 86 deletions

View file

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK.Input
{
/// <summary>
/// Enumerates all possible mouse buttons.
/// </summary>
public enum MouseButton
{
/// <summary>
/// The left mouse button.
/// </summary>
Left = 0,
/// <summary>
/// The middle mouse button.
/// </summary>
Middle,
/// <summary>
/// The right mouse button.
/// </summary>
Right,
/// <summary>
/// The first extra mouse button.
/// </summary>
Button1,
/// <summary>
/// The second extra mouse button.
/// </summary>
Button2,
/// <summary>
/// The third extra mouse button.
/// </summary>
Button3,
/// <summary>
/// The fourth extra mouse button.
/// </summary>
Button4,
/// <summary>
/// The fifth extra mouse button.
/// </summary>
Button5,
/// <summary>
/// The sixth extra mouse button.
/// </summary>
Button6,
/// <summary>
/// The seventh extra mouse button.
/// </summary>
Button7,
/// <summary>
/// The eigth extra mouse button.
/// </summary>
Button8,
/// <summary>
/// The ninth extra mouse button.
/// </summary>
Button9,
/// <summary>
/// Indicates the last available mouse button.
/// </summary>
LastButton
}
}

View file

@ -20,8 +20,9 @@ namespace OpenTK.Input
private int numButtons, numWheels; private int numButtons, numWheels;
private IntPtr id; private IntPtr id;
private bool[] button = new bool[(int)MouseButton.LastButton]; private bool[] button = new bool[(int)MouseButton.LastButton];
private int wheel, last_wheel; private int wheel, wheel_last_accessed = 0;
private Point pos = new Point(); private Point pos = new Point();
private Point pos_last_accessed = new Point();
internal int last_x, last_y; internal int last_x, last_y;
#region --- IInputDevice Members --- #region --- IInputDevice Members ---
@ -47,6 +48,8 @@ namespace OpenTK.Input
#region --- Public Members --- #region --- Public Members ---
#region public int NumberOfButtons
/// <summary> /// <summary>
/// Gets an integer representing the number of buttons on this MouseDevice. /// Gets an integer representing the number of buttons on this MouseDevice.
/// </summary> /// </summary>
@ -56,6 +59,10 @@ namespace OpenTK.Input
internal set { numButtons = value; } internal set { numButtons = value; }
} }
#endregion
#region public int NumberOfWheels
/// <summary> /// <summary>
/// Gets an integer representing the number of wheels on this MouseDevice. /// Gets an integer representing the number of wheels on this MouseDevice.
/// </summary> /// </summary>
@ -65,6 +72,10 @@ namespace OpenTK.Input
internal set { numWheels = value; } internal set { numWheels = value; }
} }
#endregion
#region public IntPtr DeviceID
/// <summary> /// <summary>
/// Gets an IntPtr representing a device dependent ID. /// Gets an IntPtr representing a device dependent ID.
/// </summary> /// </summary>
@ -74,6 +85,10 @@ namespace OpenTK.Input
internal set { id = value; } internal set { id = value; }
} }
#endregion
#region public int Wheel
/// <summary> /// <summary>
/// Gets an integer representing the absolute wheel position. /// Gets an integer representing the absolute wheel position.
/// </summary> /// </summary>
@ -82,11 +97,16 @@ namespace OpenTK.Input
get { return wheel; } get { return wheel; }
internal set internal set
{ {
last_wheel = wheel;
wheel = value; wheel = value;
if (Move != null)
Move(this, EventArgs.Empty);
} }
} }
#endregion
#region public int WheelDelta
/// <summary> /// <summary>
/// Gets an integer representing the relative wheel movement. /// Gets an integer representing the relative wheel movement.
/// </summary> /// </summary>
@ -94,37 +114,40 @@ namespace OpenTK.Input
{ {
get get
{ {
return wheel - last_wheel; int result = wheel - wheel_last_accessed;
wheel_last_accessed = wheel;
return wheel;
} }
//internal set { wheel_delta = value; }
} }
#endregion
#region public int X
/// <summary> /// <summary>
/// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates. /// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates.
/// </summary> /// </summary>
public int X public int X
{ {
get { return pos.X; } get { return pos.X; }
internal set
{
last_x = pos.X;
pos.X = value;
}
} }
#endregion
#region public int Y
/// <summary> /// <summary>
/// Gets an integer representing the absolute y position of the pointer, in window pixel coordinates. /// Gets an integer representing the absolute y position of the pointer, in window pixel coordinates.
/// </summary> /// </summary>
public int Y public int Y
{ {
get { return pos.Y; } get { return pos.Y; }
internal set
{
last_y = pos.Y;
pos.Y = value;
}
} }
#endregion
#region public int XDelta
/// <summary> /// <summary>
/// Gets an integer representing the relative x movement of the pointer, in pixel coordinates. /// Gets an integer representing the relative x movement of the pointer, in pixel coordinates.
/// </summary> /// </summary>
@ -132,12 +155,16 @@ namespace OpenTK.Input
{ {
get get
{ {
//return delta_x; int result = pos.X - pos_last_accessed.X;
return pos.X - last_x; pos_last_accessed.X = pos.X;
return result;
} }
//internal set { delta_x = value; }
} }
#endregion
#region public int YDelta
/// <summary> /// <summary>
/// Gets an integer representing the relative y movement of the pointer, in pixel coordinates. /// Gets an integer representing the relative y movement of the pointer, in pixel coordinates.
/// </summary> /// </summary>
@ -145,25 +172,35 @@ namespace OpenTK.Input
{ {
get get
{ {
//return delta_y; int result = pos.Y - pos_last_accessed.Y;
return pos.Y - last_y; pos_last_accessed.Y = pos.Y;
return result;
} }
//internal set { delta_y = value; }
}
#region public Point Position
/// <summary>
/// Gets a System.Drawing.Point representing the absolute position of the pointer, in window pixel coordinates.
/// </summary>
public Point Position
{
get { return pos; }
} }
#endregion #endregion
//public event MouseMoveEvent Move; #region internal Point Position
/// <summary>
/// Sets a System.Drawing.Point representing the absolute position of the pointer, in window pixel coordinates.
/// </summary>
internal Point Position
{
set
{
pos = value;
if (Move != null)
Move(this, EventArgs.Empty);
}
}
#endregion
/// <summary>
/// Occurs when the mouse, or one of its wheels, is moved.
/// </summary>
public event MouseMoveEvent Move;
/// <summary> /// <summary>
/// Occurs when a button is pressed. /// Occurs when a button is pressed.
@ -202,6 +239,8 @@ namespace OpenTK.Input
#endregion #endregion
#region --- Overrides ---
public override int GetHashCode() public override int GetHashCode()
{ {
//return base.GetHashCode(); //return base.GetHashCode();
@ -215,33 +254,28 @@ namespace OpenTK.Input
} }
#endregion #endregion
#endregion
} }
public delegate void MouseMoveEvent(MouseDevice sender);
public delegate void MouseButtonDownEvent(MouseDevice sender, MouseButton button);
public delegate void MouseButtonUpEvent(MouseDevice sender, MouseButton button);
#region public enum MouseButton
/// <summary> /// <summary>
/// Enumerates all possible mouse buttons. /// Defines a MouseMove event.
/// </summary> /// </summary>
public enum MouseButton /// <param name="sender">The MouseDevice that generated this event.</param>
{ /// <param name="e">Not used.</param>
Left = 0, public delegate void MouseMoveEvent(MouseDevice sender, EventArgs e);
Middle,
Right,
Button1,
Button2,
Button3,
Button4,
Button5,
Button6,
Button7,
Button8,
Button9,
LastButton
}
#endregion /// <summary>
/// Defines the MouseButtonDown event.
/// </summary>
/// <param name="sender">The MouseDevice that generated this event.</param>
/// <param name="button">The MouseButton that was pressed.</param>
public delegate void MouseButtonDownEvent(MouseDevice sender, MouseButton button);
/// <summary>
/// Defines the MouseButtonUp event.
/// </summary>
/// <param name="sender">The MouseDevice that generated this event.</param>
/// <param name="button">The MouseButton that was released.</param>
public delegate void MouseButtonUpEvent(MouseDevice sender, MouseButton button);
} }

View file

@ -66,8 +66,8 @@ namespace OpenTK.Platform.Windows
// Mouse events: // Mouse events:
case WindowMessage.MOUSEMOVE: case WindowMessage.MOUSEMOVE:
//case WindowMessage.NCMOUSEMOVE: //case WindowMessage.NCMOUSEMOVE:
mouse.X = msg.LParam.ToInt32() & 0x0000FFFF; mouse.Position = new System.Drawing.Point(msg.LParam.ToInt32() & 0x0000FFFF,
mouse.Y = (int)(msg.LParam.ToInt32() & 0xFFFF0000) >> 16; (int)(msg.LParam.ToInt32() & 0xFFFF0000) >> 16);
return; return;
case WindowMessage.MOUSEWHEEL: case WindowMessage.MOUSEWHEEL:

View file

@ -155,10 +155,7 @@ namespace OpenTK.Platform.Windows
Debug.Print("Registered mouse {0}", mouse.ToString()); Debug.Print("Registered mouse {0}", mouse.ToString());
System.Drawing.Point p = new System.Drawing.Point(); System.Drawing.Point p = new System.Drawing.Point();
if (Functions.GetCursorPos(ref p)) if (Functions.GetCursorPos(ref p))
{ mouse.Position = p;
mouse.X = p.X;
mouse.Y = p.Y;
}
} }
} }
@ -200,13 +197,12 @@ namespace OpenTK.Platform.Windows
if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != 0) if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != 0)
{ {
mouse.X = rin.Data.Mouse.LastX; mouse.Position = new System.Drawing.Point(rin.Data.Mouse.LastX, rin.Data.Mouse.LastY);
mouse.Y = rin.Data.Mouse.LastY;
} }
else else
{ // Seems like MOUSE_MOVE_RELATIVE is the default, unless otherwise noted. { // Seems like MOUSE_MOVE_RELATIVE is the default, unless otherwise noted.
mouse.X += rin.Data.Mouse.LastX; mouse.Position = new System.Drawing.Point(mouse.X + rin.Data.Mouse.LastX,
mouse.Y += rin.Data.Mouse.LastY; mouse.Y + rin.Data.Mouse.LastY);
} }
if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_VIRTUAL_DESKTOP) != 0) if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_VIRTUAL_DESKTOP) != 0)

View file

@ -159,39 +159,38 @@ namespace OpenTK.Platform.X11
else if (e.ButtonEvent.button == 2) mouse[OpenTK.Input.MouseButton.Middle] = true; else if (e.ButtonEvent.button == 2) mouse[OpenTK.Input.MouseButton.Middle] = true;
else if (e.ButtonEvent.button == 3) mouse[OpenTK.Input.MouseButton.Right] = true; else if (e.ButtonEvent.button == 3) mouse[OpenTK.Input.MouseButton.Right] = true;
else if (e.ButtonEvent.button == 4) mouse.Wheel++; else if (e.ButtonEvent.button == 4) mouse.Wheel++;
else if (e.ButtonEvent.button == 5) mouse.Wheel--; else if (e.ButtonEvent.button == 5) mouse.Wheel--;
else if (e.ButtonEvent.button == 6) mouse[OpenTK.Input.MouseButton.Button1] = true; else if (e.ButtonEvent.button == 6) mouse[OpenTK.Input.MouseButton.Button1] = true;
else if (e.ButtonEvent.button == 7) mouse[OpenTK.Input.MouseButton.Button2] = true; else if (e.ButtonEvent.button == 7) mouse[OpenTK.Input.MouseButton.Button2] = true;
else if (e.ButtonEvent.button == 8) mouse[OpenTK.Input.MouseButton.Button3] = true; else if (e.ButtonEvent.button == 8) mouse[OpenTK.Input.MouseButton.Button3] = true;
else if (e.ButtonEvent.button == 9) mouse[OpenTK.Input.MouseButton.Button4] = true; else if (e.ButtonEvent.button == 9) mouse[OpenTK.Input.MouseButton.Button4] = true;
else if (e.ButtonEvent.button == 10) mouse[OpenTK.Input.MouseButton.Button5] = true; else if (e.ButtonEvent.button == 10) mouse[OpenTK.Input.MouseButton.Button5] = true;
else if (e.ButtonEvent.button == 11) mouse[OpenTK.Input.MouseButton.Button6] = true; else if (e.ButtonEvent.button == 11) mouse[OpenTK.Input.MouseButton.Button6] = true;
else if (e.ButtonEvent.button == 12) mouse[OpenTK.Input.MouseButton.Button7] = true; else if (e.ButtonEvent.button == 12) mouse[OpenTK.Input.MouseButton.Button7] = true;
else if (e.ButtonEvent.button == 13) mouse[OpenTK.Input.MouseButton.Button8] = true; else if (e.ButtonEvent.button == 13) mouse[OpenTK.Input.MouseButton.Button8] = true;
else if (e.ButtonEvent.button == 14) mouse[OpenTK.Input.MouseButton.Button9] = true; else if (e.ButtonEvent.button == 14) mouse[OpenTK.Input.MouseButton.Button9] = true;
//if ((e.state & (int)X11.MouseMask.Button4Mask) != 0) m.Wheel++; //if ((e.state & (int)X11.MouseMask.Button4Mask) != 0) m.Wheel++;
//if ((e.state & (int)X11.MouseMask.Button5Mask) != 0) m.Wheel--; //if ((e.state & (int)X11.MouseMask.Button5Mask) != 0) m.Wheel--;
//Debug.Print("Button pressed: {0}", e.ButtonEvent.button); //Debug.Print("Button pressed: {0}", e.ButtonEvent.button);
break; break;
case XEventName.ButtonRelease: case XEventName.ButtonRelease:
if (e.ButtonEvent.button == 1) mouse[OpenTK.Input.MouseButton.Left] = false; if (e.ButtonEvent.button == 1) mouse[OpenTK.Input.MouseButton.Left] = false;
else if (e.ButtonEvent.button == 2) mouse[OpenTK.Input.MouseButton.Middle] = false; else if (e.ButtonEvent.button == 2) mouse[OpenTK.Input.MouseButton.Middle] = false;
else if (e.ButtonEvent.button == 3) mouse[OpenTK.Input.MouseButton.Right] = false; else if (e.ButtonEvent.button == 3) mouse[OpenTK.Input.MouseButton.Right] = false;
else if (e.ButtonEvent.button == 6) mouse[OpenTK.Input.MouseButton.Button1] = false; else if (e.ButtonEvent.button == 6) mouse[OpenTK.Input.MouseButton.Button1] = false;
else if (e.ButtonEvent.button == 7) mouse[OpenTK.Input.MouseButton.Button2] = false; else if (e.ButtonEvent.button == 7) mouse[OpenTK.Input.MouseButton.Button2] = false;
else if (e.ButtonEvent.button == 8) mouse[OpenTK.Input.MouseButton.Button3] = false; else if (e.ButtonEvent.button == 8) mouse[OpenTK.Input.MouseButton.Button3] = false;
else if (e.ButtonEvent.button == 9) mouse[OpenTK.Input.MouseButton.Button4] = false; else if (e.ButtonEvent.button == 9) mouse[OpenTK.Input.MouseButton.Button4] = false;
else if (e.ButtonEvent.button == 10) mouse[OpenTK.Input.MouseButton.Button5] = false; else if (e.ButtonEvent.button == 10) mouse[OpenTK.Input.MouseButton.Button5] = false;
else if (e.ButtonEvent.button == 11) mouse[OpenTK.Input.MouseButton.Button6] = false; else if (e.ButtonEvent.button == 11) mouse[OpenTK.Input.MouseButton.Button6] = false;
else if (e.ButtonEvent.button == 12) mouse[OpenTK.Input.MouseButton.Button7] = false; else if (e.ButtonEvent.button == 12) mouse[OpenTK.Input.MouseButton.Button7] = false;
else if (e.ButtonEvent.button == 13) mouse[OpenTK.Input.MouseButton.Button8] = false; else if (e.ButtonEvent.button == 13) mouse[OpenTK.Input.MouseButton.Button8] = false;
else if (e.ButtonEvent.button == 14) mouse[OpenTK.Input.MouseButton.Button9] = false; else if (e.ButtonEvent.button == 14) mouse[OpenTK.Input.MouseButton.Button9] = false;
break; break;
case XEventName.MotionNotify: case XEventName.MotionNotify:
mouse.X = e.MotionEvent.x; mouse.Position = new System.Drawing.Point(mouse.X, mouse.Y);
mouse.Y = e.MotionEvent.y;
break; break;
} }
} }