From 3f65b2c0c8e510adb1ff559569e6d18c564ff88d Mon Sep 17 00:00:00 2001 From: theGeekPirate Date: Fri, 27 Oct 2017 13:55:15 -0700 Subject: [PATCH] Slightly More Complex Cursor Logic --- pixelgl/window.go | 61 ++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/pixelgl/window.go b/pixelgl/window.go index 62c4bb7..30cee7e 100644 --- a/pixelgl/window.go +++ b/pixelgl/window.go @@ -55,10 +55,11 @@ type WindowConfig struct { type Window struct { window *glfw.Window - bounds pixel.Rect - canvas *Canvas - vsync bool - cursorVisible bool + bounds pixel.Rect + canvas *Canvas + vsync bool + cursorVisible bool + cursorDisabled bool // need to save these to correctly restore a fullscreen window restore struct { @@ -320,26 +321,16 @@ func (w *Window) VSync() bool { // SetCursorVisible sets the visibility of the mouse cursor inside the Window client area. func (w *Window) SetCursorVisible(visible bool) { - w.cursorVisible = visible - mainthread.Call(func() { - if visible { - w.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal) - } else { - w.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden) - } - }) -} - -// SetCursorDisabled both hides the cursor, as well as limits cursor movement to the Window client area. -func (w *Window) SetCursorDisabled(disabled bool) { - w.cursorVisible = !disabled - mainthread.Call(func() { - if disabled { - w.window.SetInputMode(glfw.CursorMode, glfw.CursorDisabled) - } else { - w.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal) - } - }) + if !w.cursorDisabled { + w.cursorVisible = visible + mainthread.Call(func() { + if visible { + w.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal) + } else { + w.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden) + } + }) + } } // CursorVisible returns the visibility status of the mouse cursor. @@ -347,6 +338,28 @@ func (w *Window) CursorVisible() bool { return w.cursorVisible } +// SetCursorDisabled both hides the cursor, as well as limits cursor movement to the Window client area. +func (w *Window) SetCursorDisabled(disabled bool) { + w.cursorDisabled = disabled + mainthread.Call(func() { + if disabled { + w.cursorVisible = false + w.window.SetInputMode(glfw.CursorMode, glfw.CursorDisabled) + } else { + if w.cursorVisible { + w.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal) + } else { + w.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden) + } + } + }) +} + +// CursorDisabled returns the disabled status of the mouse cursor. +func (w *Window) CursorDisabled() bool { + return w.cursorDisabled +} + // Note: must be called inside the main thread. func (w *Window) begin() { if currWin != w {