From ed05d8e12c9f06d27dbc956081461a6e7ed74ef6 Mon Sep 17 00:00:00 2001 From: kanato Date: Sat, 14 Nov 2009 18:40:56 +0000 Subject: [PATCH] MacOS: Several minor fixes: * Implement MouseWheel event * Implement KeyPress event * Fix generation of MouseMove events * Fix right mouse button up event --- .../MacOS/CarbonBindings/CarbonAPI.cs | 19 +++ .../MacOS/CarbonBindings/MacOSKeys.cs | 3 - .../OpenTK/Platform/MacOS/CarbonGLNative.cs | 145 +++++++++++------- 3 files changed, 105 insertions(+), 62 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs b/Source/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs index 117dc84f..e91f0ebd 100644 --- a/Source/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs +++ b/Source/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs @@ -640,6 +640,25 @@ namespace OpenTK.Platform.MacOS.Carbon return (MouseButton)button; } + static internal int GetEventMouseWheelDelta(IntPtr inEvent) + { + int delta; + + unsafe + { + int* d = δ + + OSStatus result = API.GetEventParameter(inEvent, + EventParamName.MouseWheelDelta, EventParamType.typeSInt32, + IntPtr.Zero, (uint)sizeof(int), IntPtr.Zero, (IntPtr)d); + + if (result != OSStatus.NoError) + throw new MacOSException(result); + } + + return delta; + } + static internal OSStatus GetEventWindowMouseLocation(IntPtr inEvent, out HIPoint pt) { HIPoint point; diff --git a/Source/OpenTK/Platform/MacOS/CarbonBindings/MacOSKeys.cs b/Source/OpenTK/Platform/MacOS/CarbonBindings/MacOSKeys.cs index 95fb6cf1..90868408 100644 --- a/Source/OpenTK/Platform/MacOS/CarbonBindings/MacOSKeys.cs +++ b/Source/OpenTK/Platform/MacOS/CarbonBindings/MacOSKeys.cs @@ -121,7 +121,4 @@ namespace OpenTK.Platform.MacOS.Carbon Command = 0x0100, // Open-Apple - Windows key Option = 0x0800, // Option key is same position as the alt key on non-mac keyboards. } - partial class API - { - } } diff --git a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs index 47044bfc..7cca609a 100644 --- a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs +++ b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs @@ -64,6 +64,8 @@ namespace OpenTK.Platform.MacOS static Dictionary mWindows = new Dictionary(); + KeyPressEventArgs mKeyPressArgs = new KeyPressEventArgs((char)0); + #endregion #region AGL Device Hack @@ -331,25 +333,35 @@ namespace OpenTK.Platform.MacOS private OSStatus ProcessKeyboardEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) { System.Diagnostics.Debug.Assert(evt.EventClass == EventClass.Keyboard); - MacOSKeyCode code; - char charCode; + MacOSKeyCode code = (MacOSKeyCode)0; + char charCode = '\0'; + + //Debug.Print("Processing keyboard event {0}", evt.KeyboardEventKind); + + switch (evt.KeyboardEventKind) + { + case KeyboardEventKind.RawKeyDown: + case KeyboardEventKind.RawKeyRepeat: + case KeyboardEventKind.RawKeyUp: + GetCharCodes(inEvent, out code, out charCode); + mKeyPressArgs.KeyChar = charCode; + break; + } switch (evt.KeyboardEventKind) { case KeyboardEventKind.RawKeyRepeat: - GetCharCodes(inEvent, out code, out charCode); InputDriver.Keyboard[0].KeyRepeat = true; goto case KeyboardEventKind.RawKeyDown; case KeyboardEventKind.RawKeyDown: - GetCharCodes(inEvent, out code, out charCode); + OnKeyPress(mKeyPressArgs); InputDriver.Keyboard[0][Keymap[code]] = true; return OSStatus.EventNotHandled; case KeyboardEventKind.RawKeyUp: - GetCharCodes(inEvent, out code, out charCode); InputDriver.Keyboard[0][Keymap[code]] = false; - + return OSStatus.EventNotHandled; case KeyboardEventKind.RawKeyModifiersChanged: @@ -362,6 +374,7 @@ namespace OpenTK.Platform.MacOS } + private OSStatus ProcessWindowEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) { System.Diagnostics.Debug.Assert(evt.EventClass == EventClass.Window); @@ -426,21 +439,6 @@ namespace OpenTK.Platform.MacOS } } - - if (this.windowState == WindowState.Fullscreen) - { - InputDriver.Mouse[0].Position = new Point((int)pt.X, (int)pt.Y); - } - else - { - // ignore clicks in the title bar - if (pt.Y < mTitlebarHeight) - return OSStatus.EventNotHandled; - - InputDriver.Mouse[0].Position = - new Point((int)pt.X, (int)(pt.Y - mTitlebarHeight)); - } - switch (evt.MouseEventKind) { case MouseEventKind.MouseDown: @@ -462,9 +460,11 @@ namespace OpenTK.Platform.MacOS } - break; + return OSStatus.NoError; case MouseEventKind.MouseUp: + button = API.GetEventMouseButton(inEvent); + switch (button) { case MouseButton.Primary: @@ -482,12 +482,43 @@ namespace OpenTK.Platform.MacOS button = API.GetEventMouseButton(inEvent); - break; + return OSStatus.NoError; + + case MouseEventKind.WheelMoved: + + int delta = API.GetEventMouseWheelDelta(inEvent) / 3; + + InputDriver.Mouse[0].Wheel += delta; + + return OSStatus.NoError; case MouseEventKind.MouseMoved: case MouseEventKind.MouseDragged: - //Debug.Print("MouseMoved: {0}", InputDriver.Mouse[0].Position); + if (this.windowState == WindowState.Fullscreen) + { + Point mousePosInClient = new Point((int)pt.X, (int)pt.Y); + + if (mousePosInClient.X != InputDriver.Mouse[0].X || + mousePosInClient.Y != InputDriver.Mouse[0].Y) + { + InputDriver.Mouse[0].Position = mousePosInClient; + } + } + else + { + // ignore clicks in the title bar + if (pt.Y < mTitlebarHeight) + return OSStatus.EventNotHandled; + + Point mousePosInClient = new Point((int)pt.X, (int)(pt.Y - mTitlebarHeight)); + + if (mousePosInClient.X != InputDriver.Mouse[0].X || + mousePosInClient.Y != InputDriver.Mouse[0].Y) + { + InputDriver.Mouse[0].Position = mousePosInClient; + } + } return OSStatus.EventNotHandled; @@ -496,8 +527,6 @@ namespace OpenTK.Platform.MacOS return OSStatus.EventNotHandled; } - - return OSStatus.EventNotHandled; } private static void GetCharCodes(IntPtr inEvent, out MacOSKeyCode code, out char charCode) @@ -598,18 +627,6 @@ namespace OpenTK.Platform.MacOS bounds = GetRegion().ToRectangle(); } - protected virtual void OnClosing(CancelEventArgs e) - { - if (Closing != null) - Closing(this, e); - } - - protected virtual void OnClosed() - { - if (Closed != null) - Closed(this, EventArgs.Empty); - } - #endregion #region INativeGLWindow Members @@ -894,8 +911,7 @@ namespace OpenTK.Platform.MacOS windowState = value; - if (WindowStateChanged != null) - WindowStateChanged(this, EventArgs.Empty); + OnWindowStateChanged(); OnResize(); } @@ -928,44 +944,55 @@ namespace OpenTK.Platform.MacOS WindowBorderChanged(this, EventArgs.Empty); } } - } + } - public event EventHandler Idle; + #region --- Event wrappers --- + private void OnKeyPress(KeyPressEventArgs keyPressArgs) + { + if (KeyPress != null) + KeyPress(this, keyPressArgs); + } + + + private void OnWindowStateChanged() + { + if (WindowStateChanged != null) + WindowStateChanged(this, EventArgs.Empty); + } + + protected virtual void OnClosing(CancelEventArgs e) + { + if (Closing != null) + Closing(this, e); + } + + protected virtual void OnClosed() + { + if (Closed != null) + Closed(this, EventArgs.Empty); + } + + #endregion + + public event EventHandler Idle; public event EventHandler Load; - public event EventHandler Unload; - public event EventHandler Move; - public event EventHandler Resize; - public event EventHandler Closing; - public event EventHandler Closed; - public event EventHandler Disposed; - public event EventHandler IconChanged; - public event EventHandler TitleChanged; - public event EventHandler ClientSizeChanged; - public event EventHandler VisibleChanged; - public event EventHandler WindowInfoChanged; - public event EventHandler FocusedChanged; - public event EventHandler WindowBorderChanged; - public event EventHandler WindowStateChanged; - public event EventHandler KeyPress; - public event EventHandler MouseEnter; - public event EventHandler MouseLeave; #endregion