From 00311cdb137afb53ed13486181b2b089cf9cb539 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 19 Oct 2010 09:20:59 +0000 Subject: [PATCH] Added initial code for mouse grabbing. Modified GameWindowStates to test this. --- .../Examples/OpenTK/Test/GameWindowStates.cs | 20 ++++++--------- Source/OpenTK/Platform/Windows/API.cs | 3 +++ Source/OpenTK/Platform/Windows/WinGLNative.cs | 25 +++++++++++++++++-- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Source/Examples/OpenTK/Test/GameWindowStates.cs b/Source/Examples/OpenTK/Test/GameWindowStates.cs index 202d1d5b..b2e33b2b 100644 --- a/Source/Examples/OpenTK/Test/GameWindowStates.cs +++ b/Source/Examples/OpenTK/Test/GameWindowStates.cs @@ -48,7 +48,12 @@ namespace Examples.Tests { switch (e.Key) { - case OpenTK.Input.Key.Escape: this.Exit(); break; + case OpenTK.Input.Key.Escape: + if (!CursorVisible) + CursorVisible = true; + else + Exit(); + break; case Key.Number1: WindowState = WindowState.Normal; break; case Key.Number2: WindowState = WindowState.Maximized; break; @@ -86,18 +91,9 @@ namespace Examples.Tests { refresh_text = true; - if (e.Button == MouseButton.Left) + if (e.Button == MouseButton.Left && e.IsPressed) { - if (e.IsPressed) - { - CursorVisible = false; - move_window = true; - } - else - { - CursorVisible = true; - move_window = false; - } + CursorVisible = false; } } diff --git a/Source/OpenTK/Platform/Windows/API.cs b/Source/OpenTK/Platform/Windows/API.cs index ed39f0f2..20cb38a7 100644 --- a/Source/OpenTK/Platform/Windows/API.cs +++ b/Source/OpenTK/Platform/Windows/API.cs @@ -957,6 +957,9 @@ namespace OpenTK.Platform.Windows [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool ClipCursor(ref RECT rcClip); + [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] + public static extern bool ClipCursor(IntPtr rcClip); + #region Async input #region GetCursorPos diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index 99c3efa3..2efd2c78 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -93,6 +93,8 @@ namespace OpenTK.Platform.Windows KeyPressEventArgs key_press = new KeyPressEventArgs((char)0); + int cursor_visible_count = 0; + #endregion #region Contructors @@ -842,8 +844,27 @@ namespace OpenTK.Platform.Windows public bool CursorVisible { - get { return true; } - set { Functions.ShowCursor(value); } + get { return cursor_visible_count > 0; } + set + { + if (value && cursor_visible_count < 0) + { + cursor_visible_count = Functions.ShowCursor(true); + + if (!Functions.ClipCursor(IntPtr.Zero)) + Debug.WriteLine(String.Format("Failed to grab cursor. Error: {0}", + Marshal.GetLastWin32Error())); + } + else if (!value && cursor_visible_count >= 0) + { + cursor_visible_count = Functions.ShowCursor(false); + + Win32Rectangle rect = Win32Rectangle.From(ClientRectangle); + if (!Functions.ClipCursor(ref rect)) + Debug.WriteLine(String.Format("Failed to grab cursor. Error: {0}", + Marshal.GetLastWin32Error())); + } + } } #endregion