Slightly More Complex Cursor Logic

This commit is contained in:
theGeekPirate 2017-10-27 13:55:15 -07:00 committed by GitHub
parent bc898ce1ee
commit 3f65b2c0c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 24 deletions

View File

@ -55,10 +55,11 @@ type WindowConfig struct {
type Window struct { type Window struct {
window *glfw.Window window *glfw.Window
bounds pixel.Rect bounds pixel.Rect
canvas *Canvas canvas *Canvas
vsync bool vsync bool
cursorVisible bool cursorVisible bool
cursorDisabled bool
// need to save these to correctly restore a fullscreen window // need to save these to correctly restore a fullscreen window
restore struct { restore struct {
@ -320,26 +321,16 @@ func (w *Window) VSync() bool {
// SetCursorVisible sets the visibility of the mouse cursor inside the Window client area. // SetCursorVisible sets the visibility of the mouse cursor inside the Window client area.
func (w *Window) SetCursorVisible(visible bool) { func (w *Window) SetCursorVisible(visible bool) {
w.cursorVisible = visible if !w.cursorDisabled {
mainthread.Call(func() { w.cursorVisible = visible
if visible { mainthread.Call(func() {
w.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal) if visible {
} else { w.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
w.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden) } 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)
}
})
} }
// CursorVisible returns the visibility status of the mouse cursor. // CursorVisible returns the visibility status of the mouse cursor.
@ -347,6 +338,28 @@ func (w *Window) CursorVisible() bool {
return w.cursorVisible 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. // Note: must be called inside the main thread.
func (w *Window) begin() { func (w *Window) begin() {
if currWin != w { if currWin != w {