ui: Add SetCursorVisibility (#258)

This commit is contained in:
Hajime Hoshi 2016-09-03 17:17:54 +09:00
parent 417112c586
commit cd885ce7c5
4 changed files with 33 additions and 0 deletions

View File

@ -142,6 +142,20 @@ func ScreenScale() float64 {
return s return s
} }
func SetCursorVisibility(visible bool) {
// This can be called before Run: change the state asyncly.
go func() {
_ = currentUI.runOnMainThread(func() error {
c := glfw.CursorNormal
if !visible {
c = glfw.CursorHidden
}
currentUI.window.SetInputMode(glfw.CursorMode, c)
return nil
})
}()
}
func Run(width, height int, scale float64, title string, g GraphicsContext) error { func Run(width, height int, scale float64, title string, g GraphicsContext) error {
u := currentUI u := currentUI
// GLContext must be created before setting the screen size, which requires // GLContext must be created before setting the screen size, which requires

View File

@ -57,6 +57,14 @@ func ScreenScale() float64 {
return currentUI.scale return currentUI.scale
} }
func SetCursorVisibility(visibility bool) {
if visibility {
canvas.Get("style").Set("cursor", "auto")
} else {
canvas.Get("style").Set("cursor", "none")
}
}
func (u *userInterface) actualScreenScale() float64 { func (u *userInterface) actualScreenScale() float64 {
return u.scale * u.deviceScale return u.scale * u.deviceScale
} }

View File

@ -112,6 +112,10 @@ func ScreenScale() float64 {
return currentUI.scale return currentUI.scale
} }
func SetCursorVisibility(visibility bool) {
// Do nothing
}
func (u *userInterface) actualScreenScale() float64 { func (u *userInterface) actualScreenScale() float64 {
return u.scale * deviceScale() return u.scale * deviceScale()
} }

7
run.go
View File

@ -153,3 +153,10 @@ func ScreenScale() float64 {
} }
return ui.ScreenScale() return ui.ScreenScale()
} }
// SetCursorVisibility changes the state of cursor visiblity.
//
// This function is concurrent-safe.
func SetCursorVisibility(visible bool) {
ui.SetCursorVisibility(visible)
}