From 4769e7dbbd8c4c66816426f4eda9ad51508a70dd Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 4 Sep 2015 06:55:12 +1000 Subject: [PATCH] Implement cursor hiding on Windows to partially address #36. (Will do Linux backend next) --- ClassicalSharp/2D/Screens/ChatScreen.cs | 3 +++ ClassicalSharp/2D/Screens/NormalScreen.cs | 10 +++++++--- OpenTK/INativeWindow.cs | 4 ++++ OpenTK/NativeWindow.cs | 7 ++++++- OpenTK/Platform/MacOS/CarbonGLNative.cs | 5 +++++ OpenTK/Platform/Windows/API.cs | 5 ++++- OpenTK/Platform/Windows/WinGLNative.cs | 8 ++++++-- OpenTK/Platform/X11/X11GLNative.cs | 5 +++++ 8 files changed, 40 insertions(+), 7 deletions(-) diff --git a/ClassicalSharp/2D/Screens/ChatScreen.cs b/ClassicalSharp/2D/Screens/ChatScreen.cs index 7d7d9eb82..b474c5d70 100644 --- a/ClassicalSharp/2D/Screens/ChatScreen.cs +++ b/ClassicalSharp/2D/Screens/ChatScreen.cs @@ -94,6 +94,7 @@ namespace ClassicalSharp { public override void Dispose() { if( !textInput.chatInputText.Empty ) { + game.CursorVisible = false; game.chatInInputBuffer = textInput.chatInputText.ToString(); } chatFont.Dispose(); @@ -145,6 +146,7 @@ namespace ClassicalSharp { } void OpenTextInputBar( string initialText ) { + game.CursorVisible = true; suppressNextPress = true; HandlesAllInput = true; textInput.chatInputText.Clear(); @@ -158,6 +160,7 @@ namespace ClassicalSharp { if( HandlesAllInput ) { // text input bar if( key == game.Keys[KeyMapping.SendChat] ) { HandlesAllInput = false; + game.CursorVisible = false; game.Camera.RegrabMouse(); textInput.SendTextInBufferAndReset(); } else if( key == Key.PageUp ) { diff --git a/ClassicalSharp/2D/Screens/NormalScreen.cs b/ClassicalSharp/2D/Screens/NormalScreen.cs index 212f601a9..9a0606982 100644 --- a/ClassicalSharp/2D/Screens/NormalScreen.cs +++ b/ClassicalSharp/2D/Screens/NormalScreen.cs @@ -25,12 +25,14 @@ namespace ClassicalSharp { playerList.Dispose(); playerList = null; } + graphicsApi.Texturing = false; + } else { + graphicsApi.Texturing = false; + DrawCrosshairs(); } - graphicsApi.Texturing = false; - DrawCrosshairs(); } - const int crosshairExtent = 20, crosshairWeight = 2; + const int crosshairExtent = 15, crosshairWeight = 2; void DrawCrosshairs() { int curCol = 150 + (int)( 50 * Math.Abs( Math.Sin( game.accumulator ) ) ); FastColour col = new FastColour( curCol, curCol, curCol ); @@ -48,6 +50,7 @@ namespace ClassicalSharp { if( playerList != null ) { playerList.Dispose(); } + game.CursorVisible = true; } public override void OnResize( int oldWidth, int oldHeight, int width, int height ) { @@ -68,6 +71,7 @@ namespace ClassicalSharp { chat.Init(); hotbar = new BlockHotbarWidget( game ); hotbar.Init(); + game.CursorVisible = false; } public override bool HandlesAllInput { diff --git a/OpenTK/INativeWindow.cs b/OpenTK/INativeWindow.cs index 6b2572524..6515b0821 100644 --- a/OpenTK/INativeWindow.cs +++ b/OpenTK/INativeWindow.cs @@ -111,7 +111,11 @@ namespace OpenTK { /// Gets the available MouseDevice. MouseDevice Mouse { get; } + /// Gets or sets the cursor position in screen coordinates. Point DesktopCursorPos { get; set; } + + /// Sets whether the cursor is visible in the window. + bool CursorVisible { set; } /// Occurs whenever the window is moved. event EventHandler Move; diff --git a/OpenTK/NativeWindow.cs b/OpenTK/NativeWindow.cs index d2fee6e53..3b24fea07 100644 --- a/OpenTK/NativeWindow.cs +++ b/OpenTK/NativeWindow.cs @@ -237,7 +237,12 @@ namespace OpenTK { get { return implementation.Mouse; } } - /// Gets the primary Mouse device, or null if no Mouse exists. + /// Sets whether the cursor is visible in the window. + public bool CursorVisible { + set { implementation.CursorVisible = value; } + } + + /// Gets or sets the cursor position in screen coordinates. public Point DesktopCursorPos { get { return implementation.DesktopCursorPos; } set { implementation.DesktopCursorPos = value; } diff --git a/OpenTK/Platform/MacOS/CarbonGLNative.cs b/OpenTK/Platform/MacOS/CarbonGLNative.cs index d82ee2f8a..a25d64bb2 100644 --- a/OpenTK/Platform/MacOS/CarbonGLNative.cs +++ b/OpenTK/Platform/MacOS/CarbonGLNative.cs @@ -1066,6 +1066,11 @@ namespace OpenTK.Platform.MacOS set { System.Windows.Forms.Cursor.Position = value; } } + // TODO: Hide and show cursor + public bool CursorVisible { + set { } + } + #endregion } } diff --git a/OpenTK/Platform/Windows/API.cs b/OpenTK/Platform/Windows/API.cs index f39898b60..78579c9c0 100644 --- a/OpenTK/Platform/Windows/API.cs +++ b/OpenTK/Platform/Windows/API.cs @@ -154,10 +154,13 @@ namespace OpenTK.Platform.Windows { public static extern bool TrackMouseEvent(ref TrackMouseEventStructure lpEventTrack); [DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity] - internal static extern bool GetCursorPos(ref Point point); + internal static extern bool GetCursorPos(ref POINT point); [DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity] internal static extern bool SetCursorPos(int x, int y); + + [DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity] + internal static extern bool ShowCursor( int value ); } internal struct Constants { diff --git a/OpenTK/Platform/Windows/WinGLNative.cs b/OpenTK/Platform/Windows/WinGLNative.cs index cf30bf0c4..689148d33 100644 --- a/OpenTK/Platform/Windows/WinGLNative.cs +++ b/OpenTK/Platform/Windows/WinGLNative.cs @@ -803,14 +803,18 @@ namespace OpenTK.Platform.Windows public Point DesktopCursorPos { get { - Point pos = default( Point ); + POINT pos = default( POINT ); API.GetCursorPos( ref pos ); - return pos; + return new Point( pos.X, pos.Y ); } set { API.SetCursorPos( value.X, value.Y ); } } + + public bool CursorVisible { + set { API.ShowCursor( value ? 1 : 0 ); } + } public void Dispose() { Dispose(true); diff --git a/OpenTK/Platform/X11/X11GLNative.cs b/OpenTK/Platform/X11/X11GLNative.cs index bf4a4dd18..f4d56117d 100644 --- a/OpenTK/Platform/X11/X11GLNative.cs +++ b/OpenTK/Platform/X11/X11GLNative.cs @@ -820,6 +820,11 @@ namespace OpenTK.Platform.X11 { set { System.Windows.Forms.Cursor.Position = value; } } + // TODO: Hide and show cursor + public bool CursorVisible { + set { } + } + /// Returns true if a render window/context exists. public bool Exists { get { return exists; }