internal/uidriver/glfw: Refactoring

This commit is contained in:
Hajime Hoshi 2021-04-15 01:59:31 +09:00
parent 264274b43b
commit 71e899acf3

View File

@ -34,6 +34,19 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/thread" "github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
func driverCursorModeToGLFWCursorMode(mode driver.CursorMode) int {
switch mode {
case driver.CursorModeVisible:
return glfw.CursorNormal
case driver.CursorModeHidden:
return glfw.CursorHidden
case driver.CursorModeCaptured:
return glfw.CursorDisabled
default:
panic(fmt.Sprintf("glfw: invalid driver.CursorMode: %d", mode))
}
}
type UserInterface struct { type UserInterface struct {
context driver.UIContext context driver.UIContext
title string title string
@ -526,7 +539,7 @@ func (u *UserInterface) CursorMode() driver.CursorMode {
case glfw.CursorDisabled: case glfw.CursorDisabled:
v = driver.CursorModeCaptured v = driver.CursorModeCaptured
default: default:
panic(fmt.Sprintf("invalid cursor mode: %d", mode)) panic(fmt.Sprintf("glfw: invalid GLFW cursor mode: %d", mode))
} }
return nil return nil
}) })
@ -539,18 +552,7 @@ func (u *UserInterface) SetCursorMode(mode driver.CursorMode) {
return return
} }
_ = u.t.Call(func() error { _ = u.t.Call(func() error {
var c int u.window.SetInputMode(glfw.CursorMode, driverCursorModeToGLFWCursorMode(mode))
switch mode {
case driver.CursorModeVisible:
c = glfw.CursorNormal
case driver.CursorModeHidden:
c = glfw.CursorHidden
case driver.CursorModeCaptured:
c = glfw.CursorDisabled
default:
panic(fmt.Sprintf("invalid cursor mode: %d", mode))
}
u.window.SetInputMode(glfw.CursorMode, c)
return nil return nil
}) })
} }
@ -612,15 +614,7 @@ func (u *UserInterface) createWindow() error {
u.window.SetInputMode(glfw.StickyMouseButtonsMode, glfw.True) u.window.SetInputMode(glfw.StickyMouseButtonsMode, glfw.True)
u.window.SetInputMode(glfw.StickyKeysMode, glfw.True) u.window.SetInputMode(glfw.StickyKeysMode, glfw.True)
u.window.SetInputMode(glfw.CursorMode, driverCursorModeToGLFWCursorMode(u.getInitCursorMode()))
mode := glfw.CursorNormal
switch u.getInitCursorMode() {
case driver.CursorModeHidden:
mode = glfw.CursorHidden
case driver.CursorModeCaptured:
mode = glfw.CursorDisabled
}
u.window.SetInputMode(glfw.CursorMode, mode)
u.window.SetTitle(u.title) u.window.SetTitle(u.title)
// TODO: Set icons // TODO: Set icons