From 1fc5e96a253057df1877f5484b27db76750751f1 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 18 Oct 2010 15:25:25 +0000 Subject: [PATCH] * Source/OpenTK/NativeWindow.cs: * Source/OpenTK/INativeWindow.cs: * Source/OpenTK/Platform/X11/API.cs: * Source/OpenTK/Platform/X11/X11GLNative.cs: * Source/OpenTK/Platform/Windows/WinGLNative.cs: * Source/OpenTK/Platform/MacOS/CarbonGLNative.cs: * Source/Examples/OpenTK/Test/GameWindowStates.cs: Initial implementation of CursorVisible API. See issue [#1560]. --- .../Examples/OpenTK/Test/GameWindowStates.cs | 33 +++++++++++++++++-- Source/OpenTK/INativeWindow.cs | 10 ++++++ Source/OpenTK/NativeWindow.cs | 18 ++++++++++ .../OpenTK/Platform/MacOS/CarbonGLNative.cs | 6 ++++ Source/OpenTK/Platform/Windows/WinGLNative.cs | 10 ++++++ Source/OpenTK/Platform/X11/API.cs | 20 +++++++++++ Source/OpenTK/Platform/X11/X11GLNative.cs | 32 ++++++++++++++++++ 7 files changed, 127 insertions(+), 2 deletions(-) diff --git a/Source/Examples/OpenTK/Test/GameWindowStates.cs b/Source/Examples/OpenTK/Test/GameWindowStates.cs index c15c0c21..8d6b9b67 100644 --- a/Source/Examples/OpenTK/Test/GameWindowStates.cs +++ b/Source/Examples/OpenTK/Test/GameWindowStates.cs @@ -23,6 +23,7 @@ namespace Examples.Tests bool mouse_in_window = false; bool viewport_changed = true; bool refresh_text = true; + bool move_window = false; public GameWindowStates() : base(800, 600) @@ -38,7 +39,8 @@ namespace Examples.Tests Resize += delegate { refresh_text = true; }; WindowBorderChanged += delegate { refresh_text = true; }; WindowStateChanged += delegate { refresh_text = true; }; - Mouse.Move += delegate { refresh_text = true; }; + Mouse.Move += MouseMoveHandler; + Mouse.ButtonDown += MouseButtonHandler; } void KeyDownHandler(object sender, KeyboardKeyEventArgs e) @@ -68,7 +70,33 @@ namespace Examples.Tests case Key.Minus: Size -= new Size(16, 16); break; } } - + + void MouseMoveHandler(object sender, MouseMoveEventArgs e) + { + refresh_text = true; + + if (move_window) + { + Location = new Point(X + e.XDelta, Y + e.YDelta); + } + } + + void MouseButtonHandler(object sender, MouseButtonEventArgs e) + { + refresh_text = true; + + if (e.IsPressed) + { + CursorVisible = false; + move_window = true; + } + else + { + CursorVisible = true; + move_window = false; + } + } + static int Clamp(int val, int min, int max) { return val > max ? max : val < min ? min : val; @@ -97,6 +125,7 @@ namespace Examples.Tests DrawString(gfx, String.Format("Focused: {0}.", this.Focused), line++); DrawString(gfx, String.Format("Mouse {0} window.", mouse_in_window ? "inside" : "outside of"), line++); DrawString(gfx, String.Format("Mouse position: {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.Wheel)), line++); + DrawString(gfx, String.Format("Mouse visible: {0}", CursorVisible), line++); DrawString(gfx, String.Format("Window.Bounds: {0}", Bounds), line++); DrawString(gfx, String.Format("Window.Location: {0}, Size: {1}", Location, Size), line++); DrawString(gfx, String.Format("Window.{{X={0}, Y={1}, Width={2}, Height={3}}}", X, Y, Width, Height), line++); diff --git a/Source/OpenTK/INativeWindow.cs b/Source/OpenTK/INativeWindow.cs index 6bb3b33b..03927607 100644 --- a/Source/OpenTK/INativeWindow.cs +++ b/Source/OpenTK/INativeWindow.cs @@ -132,6 +132,16 @@ namespace OpenTK [Obsolete] OpenTK.Input.IInputDriver InputDriver { get; } + /// + /// Gets or sets a value, indicating whether the mouse cursor is visible. + /// + bool CursorVisible { get; set; } + +// /// +// /// Gets or sets a value, indicating whether the mouse cursor is confined inside the window size. +// /// +// bool CursorGrabbed { get; set; } + /// /// Closes this window. /// diff --git a/Source/OpenTK/NativeWindow.cs b/Source/OpenTK/NativeWindow.cs index f6f00857..681eb3ef 100644 --- a/Source/OpenTK/NativeWindow.cs +++ b/Source/OpenTK/NativeWindow.cs @@ -49,6 +49,7 @@ namespace OpenTK private readonly INativeWindow implementation; private bool disposed, events; + private bool cursor_visible = true; #endregion @@ -541,6 +542,23 @@ namespace OpenTK #endregion + #region CursorVisible + + /// + /// Gets or sets a value indicating whether the mouse cursor is visible. + /// + public bool CursorVisible + { + get { return cursor_visible; } + set + { + cursor_visible = value; + implementation.CursorVisible = value; + } + } + + #endregion + #endregion #region Events diff --git a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs index aa483365..2919ca54 100644 --- a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs +++ b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs @@ -925,6 +925,12 @@ namespace OpenTK.Platform.MacOS } } + public bool CursorVisible + { + get { return true; } + set { } + } + public void Close() { CancelEventArgs e = new CancelEventArgs(); diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index 438b49cf..442e02f9 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -836,6 +836,16 @@ namespace OpenTK.Platform.Windows public bool Exists { get { return exists; } } + #endregion + + #region CursorVisible + + public bool CursorVisible + { + get { return true; } + set { } + } + #endregion #region Close diff --git a/Source/OpenTK/Platform/X11/API.cs b/Source/OpenTK/Platform/X11/API.cs index 442842e5..eaa80214 100644 --- a/Source/OpenTK/Platform/X11/API.cs +++ b/Source/OpenTK/Platform/X11/API.cs @@ -1562,6 +1562,26 @@ XF86VidModeGetGammaRampSize( #endregion #endregion + + public static Pixmap XCreateBitmapFromData(Display display, Window d, byte[,] data) + { + if (data == null) + throw new ArgumentNullException("data"); + + unsafe + { + fixed (byte* pdata = data) + { + return XCreateBitmapFromData(display, d, pdata, data.GetLength(0), data.GetLength(1)); + } + } + } + + [DllImport(X11Library)] + unsafe public static extern Pixmap XCreateBitmapFromData(Display display, Window d, byte* data, int width, int height); + + [DllImport("libX11", EntryPoint = "XAllocColor")] + public static extern Status XAllocNamedColor(Display display, Colormap colormap, string color_name, out XColor screen_def_return, out XColor exact_def_return); } /* [StructLayout(LayoutKind.Sequential)] diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index 7119095f..054b7c66 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -1300,6 +1300,38 @@ namespace OpenTK.Platform.X11 #endregion + public bool CursorVisible + { + get { return true; } + set + { + if (value) + { + using (new XLock(window.Display)) + { + Functions.XUndefineCursor(window.Display, window.WindowHandle); + } + } + else + { + using (new XLock(window.Display)) + { + XColor black, dummy; + IntPtr cmap = Functions.XDefaultColormap(window.Display, window.Screen); + Functions.XAllocNamedColor(window.Display, cmap, "black", out black, out dummy); + IntPtr bmp_empty = Functions.XCreateBitmapFromData(window.Display, + window.WindowHandle, new byte[,] { { 0 } }); + IntPtr cursor_empty = Functions.XCreatePixmapCursor(window.Display, + bmp_empty, bmp_empty, ref black, ref black, 0, 0); + + Functions.XDefineCursor(window.Display, window.WindowHandle, cursor_empty); + + Functions.XFreeCursor(window.Display, cursor_empty); + } + } + } + } + #endregion #region --- INativeGLWindow Members ---