From e9e9ee5f9c90196812d6dd4bcb90dd51cd493eec Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 4 May 2008 17:42:19 +0000 Subject: [PATCH] Fixed mouse delta handling. --- Source/OpenTK/Input/MouseButton.cs | 65 ++++++++ Source/OpenTK/Input/MouseDevice.cs | 144 +++++++++++------- Source/OpenTK/Platform/Windows/WMInput.cs | 4 +- Source/OpenTK/Platform/Windows/WinRawMouse.cs | 12 +- Source/OpenTK/Platform/X11/X11Input.cs | 41 +++-- 5 files changed, 180 insertions(+), 86 deletions(-) create mode 100644 Source/OpenTK/Input/MouseButton.cs diff --git a/Source/OpenTK/Input/MouseButton.cs b/Source/OpenTK/Input/MouseButton.cs new file mode 100644 index 00000000..4f99a6b0 --- /dev/null +++ b/Source/OpenTK/Input/MouseButton.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenTK.Input +{ + /// + /// Enumerates all possible mouse buttons. + /// + public enum MouseButton + { + /// + /// The left mouse button. + /// + Left = 0, + /// + /// The middle mouse button. + /// + Middle, + /// + /// The right mouse button. + /// + Right, + /// + /// The first extra mouse button. + /// + Button1, + /// + /// The second extra mouse button. + /// + Button2, + /// + /// The third extra mouse button. + /// + Button3, + /// + /// The fourth extra mouse button. + /// + Button4, + /// + /// The fifth extra mouse button. + /// + Button5, + /// + /// The sixth extra mouse button. + /// + Button6, + /// + /// The seventh extra mouse button. + /// + Button7, + /// + /// The eigth extra mouse button. + /// + Button8, + /// + /// The ninth extra mouse button. + /// + Button9, + /// + /// Indicates the last available mouse button. + /// + LastButton + } +} diff --git a/Source/OpenTK/Input/MouseDevice.cs b/Source/OpenTK/Input/MouseDevice.cs index 62d5158a..0428cc7d 100644 --- a/Source/OpenTK/Input/MouseDevice.cs +++ b/Source/OpenTK/Input/MouseDevice.cs @@ -20,8 +20,9 @@ namespace OpenTK.Input private int numButtons, numWheels; private IntPtr id; 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_last_accessed = new Point(); internal int last_x, last_y; #region --- IInputDevice Members --- @@ -47,6 +48,8 @@ namespace OpenTK.Input #region --- Public Members --- + #region public int NumberOfButtons + /// /// Gets an integer representing the number of buttons on this MouseDevice. /// @@ -56,6 +59,10 @@ namespace OpenTK.Input internal set { numButtons = value; } } + #endregion + + #region public int NumberOfWheels + /// /// Gets an integer representing the number of wheels on this MouseDevice. /// @@ -65,6 +72,10 @@ namespace OpenTK.Input internal set { numWheels = value; } } + #endregion + + #region public IntPtr DeviceID + /// /// Gets an IntPtr representing a device dependent ID. /// @@ -74,6 +85,10 @@ namespace OpenTK.Input internal set { id = value; } } + #endregion + + #region public int Wheel + /// /// Gets an integer representing the absolute wheel position. /// @@ -82,11 +97,16 @@ namespace OpenTK.Input get { return wheel; } internal set { - last_wheel = wheel; wheel = value; + if (Move != null) + Move(this, EventArgs.Empty); } } + #endregion + + #region public int WheelDelta + /// /// Gets an integer representing the relative wheel movement. /// @@ -94,37 +114,40 @@ namespace OpenTK.Input { 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 + /// /// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates. /// public int X { get { return pos.X; } - internal set - { - last_x = pos.X; - pos.X = value; - } } + #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; } - internal set - { - last_y = pos.Y; - pos.Y = value; - } } + #endregion + + #region public int XDelta + /// /// Gets an integer representing the relative x movement of the pointer, in pixel coordinates. /// @@ -132,12 +155,16 @@ namespace OpenTK.Input { get { - //return delta_x; - return pos.X - last_x; + int result = pos.X - pos_last_accessed.X; + pos_last_accessed.X = pos.X; + return result; } - //internal set { delta_x = value; } } + #endregion + + #region public int YDelta + /// /// Gets an integer representing the relative y movement of the pointer, in pixel coordinates. /// @@ -145,25 +172,35 @@ namespace OpenTK.Input { get { - //return delta_y; - return pos.Y - last_y; + int result = pos.Y - pos_last_accessed.Y; + pos_last_accessed.Y = pos.Y; + return result; } - //internal set { delta_y = value; } - } - - #region public Point Position - - /// - /// Gets a System.Drawing.Point representing the absolute position of the pointer, in window pixel coordinates. - /// - public Point Position - { - get { return pos; } } #endregion - //public event MouseMoveEvent Move; + #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; + if (Move != null) + Move(this, EventArgs.Empty); + } + } + + #endregion + + /// + /// Occurs when the mouse, or one of its wheels, is moved. + /// + public event MouseMoveEvent Move; /// /// Occurs when a button is pressed. @@ -202,6 +239,8 @@ namespace OpenTK.Input #endregion + #region --- Overrides --- + public override int GetHashCode() { //return base.GetHashCode(); @@ -215,33 +254,28 @@ namespace OpenTK.Input } #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 - /// - /// Enumerates all possible mouse buttons. + /// Defines a MouseMove event. /// - public enum MouseButton - { - Left = 0, - Middle, - Right, - Button1, - Button2, - Button3, - Button4, - Button5, - Button6, - Button7, - Button8, - Button9, - LastButton - } + /// The MouseDevice that generated this event. + /// Not used. + public delegate void MouseMoveEvent(MouseDevice sender, EventArgs e); - #endregion + /// + /// Defines the MouseButtonDown event. + /// + /// The MouseDevice that generated this event. + /// The MouseButton that was pressed. + public delegate void MouseButtonDownEvent(MouseDevice sender, MouseButton button); + + /// + /// Defines the MouseButtonUp event. + /// + /// The MouseDevice that generated this event. + /// The MouseButton that was released. + public delegate void MouseButtonUpEvent(MouseDevice sender, MouseButton button); } diff --git a/Source/OpenTK/Platform/Windows/WMInput.cs b/Source/OpenTK/Platform/Windows/WMInput.cs index 75d5b094..2d9b1269 100644 --- a/Source/OpenTK/Platform/Windows/WMInput.cs +++ b/Source/OpenTK/Platform/Windows/WMInput.cs @@ -66,8 +66,8 @@ namespace OpenTK.Platform.Windows // Mouse events: case WindowMessage.MOUSEMOVE: //case WindowMessage.NCMOUSEMOVE: - mouse.X = msg.LParam.ToInt32() & 0x0000FFFF; - mouse.Y = (int)(msg.LParam.ToInt32() & 0xFFFF0000) >> 16; + mouse.Position = new System.Drawing.Point(msg.LParam.ToInt32() & 0x0000FFFF, + (int)(msg.LParam.ToInt32() & 0xFFFF0000) >> 16); return; case WindowMessage.MOUSEWHEEL: diff --git a/Source/OpenTK/Platform/Windows/WinRawMouse.cs b/Source/OpenTK/Platform/Windows/WinRawMouse.cs index 04103bff..32f158fd 100644 --- a/Source/OpenTK/Platform/Windows/WinRawMouse.cs +++ b/Source/OpenTK/Platform/Windows/WinRawMouse.cs @@ -155,10 +155,7 @@ namespace OpenTK.Platform.Windows Debug.Print("Registered mouse {0}", mouse.ToString()); System.Drawing.Point p = new System.Drawing.Point(); if (Functions.GetCursorPos(ref p)) - { - mouse.X = p.X; - mouse.Y = p.Y; - } + mouse.Position = p; } } @@ -200,13 +197,12 @@ namespace OpenTK.Platform.Windows if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != 0) { - mouse.X = rin.Data.Mouse.LastX; - mouse.Y = rin.Data.Mouse.LastY; + mouse.Position = new System.Drawing.Point(rin.Data.Mouse.LastX, rin.Data.Mouse.LastY); } else { // Seems like MOUSE_MOVE_RELATIVE is the default, unless otherwise noted. - mouse.X += rin.Data.Mouse.LastX; - mouse.Y += rin.Data.Mouse.LastY; + mouse.Position = new System.Drawing.Point(mouse.X + rin.Data.Mouse.LastX, + mouse.Y + rin.Data.Mouse.LastY); } if ((rin.Data.Mouse.Flags & RawMouseFlags.MOUSE_VIRTUAL_DESKTOP) != 0) diff --git a/Source/OpenTK/Platform/X11/X11Input.cs b/Source/OpenTK/Platform/X11/X11Input.cs index f87cf1db..bc020bd5 100644 --- a/Source/OpenTK/Platform/X11/X11Input.cs +++ b/Source/OpenTK/Platform/X11/X11Input.cs @@ -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 == 3) mouse[OpenTK.Input.MouseButton.Right] = true; else if (e.ButtonEvent.button == 4) 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 == 7) mouse[OpenTK.Input.MouseButton.Button2] = 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 == 10) mouse[OpenTK.Input.MouseButton.Button5] = 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 == 13) mouse[OpenTK.Input.MouseButton.Button8] = true; + 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 == 7) mouse[OpenTK.Input.MouseButton.Button2] = 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 == 10) mouse[OpenTK.Input.MouseButton.Button5] = 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 == 13) mouse[OpenTK.Input.MouseButton.Button8] = 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.Button5Mask) != 0) m.Wheel--; + //if ((e.state & (int)X11.MouseMask.Button5Mask) != 0) m.Wheel--; //Debug.Print("Button pressed: {0}", e.ButtonEvent.button); break; - case XEventName.ButtonRelease: + case XEventName.ButtonRelease: 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 == 3) mouse[OpenTK.Input.MouseButton.Right] = 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 == 8) mouse[OpenTK.Input.MouseButton.Button3] = 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 == 11) mouse[OpenTK.Input.MouseButton.Button6] = 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 == 6) mouse[OpenTK.Input.MouseButton.Button1] = 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 == 9) mouse[OpenTK.Input.MouseButton.Button4] = 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 == 12) mouse[OpenTK.Input.MouseButton.Button7] = 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; break; case XEventName.MotionNotify: - mouse.X = e.MotionEvent.x; - mouse.Y = e.MotionEvent.y; + mouse.Position = new System.Drawing.Point(mouse.X, mouse.Y); break; } }