ui: Take the Input driver

This commit is contained in:
Hajime Hoshi 2019-04-06 23:03:21 +09:00
parent de9f54fd9d
commit a980de8326
5 changed files with 32 additions and 20 deletions

View File

@ -147,7 +147,7 @@ func (i *Input) setMouseCursor(x, y int) {
i.cursorX, i.cursorY = x, y
}
func (i *Input) Update() {
func (i *Input) UpdateGamepads() {
nav := js.Global().Get("navigator")
if nav.Get("getGamepads") == js.Undefined() {
return

View File

@ -30,7 +30,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/glfw"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/input"
"github.com/hajimehoshi/ebiten/internal/mainthread"
)
@ -65,7 +64,8 @@ type userInterface struct {
reqWidth int
reqHeight int
driver driver.Graphics
graphics driver.Graphics
input driver.Input
m sync.Mutex
}
@ -568,12 +568,13 @@ func DeviceScaleFactor() float64 {
return f
}
func Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool, driver driver.Graphics) error {
func Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool, graphics driver.Graphics, input driver.Input) error {
u := currentUI
_ = mainthread.Run(func() error {
u.driver = driver
u.graphics = graphics
u.input = input
if driver.IsGL() {
if graphics.IsGL() {
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
} else {
@ -600,7 +601,7 @@ func Run(width, height int, scale float64, title string, g GraphicsContext, main
}
u.window = window
if driver.IsGL() {
if graphics.IsGL() {
u.window.MakeContextCurrent()
}
@ -675,7 +676,7 @@ func Run(width, height int, scale float64, title string, g GraphicsContext, main
w = u.nativeWindow()
return nil
})
driver.SetWindow(w)
graphics.SetWindow(w)
return u.loop(g)
}
@ -766,7 +767,7 @@ func (u *userInterface) update(g GraphicsContext) error {
Update(window *glfw.Window, scale float64)
}
input.Get().(updater).Update(u.window, u.getScale()*glfwScale())
u.input.(updater).Update(u.window, u.getScale()*glfwScale())
defer hooks.ResumeAudio()
@ -830,7 +831,7 @@ func (u *userInterface) loop(g GraphicsContext) error {
// swapBuffers must be called from the main thread.
func (u *userInterface) swapBuffers() {
if u.driver.IsGL() {
if u.graphics.IsGL() {
u.window.SwapBuffers()
}
}
@ -928,7 +929,7 @@ func (u *userInterface) forceSetScreenSize(width, height int, scale float64, ful
u.window.SetTitle(u.title)
}
if u.driver.IsGL() {
if u.graphics.IsGL() {
// SwapInterval is affected by the current monitor of the window.
// This needs to be called at least after SetMonitor.
// Without SwapInterval after SetMonitor, vsynch doesn't work (#375).
@ -942,7 +943,7 @@ func (u *userInterface) forceSetScreenSize(width, height int, scale float64, ful
glfw.SwapInterval(0)
}
}
u.driver.SetVsyncEnabled(vsync)
u.graphics.SetVsyncEnabled(vsync)
u.toChangeSize = true
}

View File

@ -32,6 +32,12 @@ import (
var canvas js.Value
type inputDriver interface {
driver.Input
UpdateGamepads()
}
type userInterface struct {
width int
height int
@ -46,6 +52,8 @@ type userInterface struct {
contextLost bool
lastActualScale float64
input inputDriver
}
var currentUI = &userInterface{
@ -203,11 +211,7 @@ func (u *userInterface) update(g GraphicsContext) error {
}
hooks.ResumeAudio()
type updater interface {
Update()
}
input.Get().(updater).Update()
u.input.UpdateGamepads()
u.updateGraphicsContext(g)
if err := g.Update(func() {
u.updateGraphicsContext(g)
@ -361,8 +365,10 @@ func Loop(ch <-chan error) error {
return <-ch
}
func Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool, driver driver.Graphics) error {
func Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool, graphics driver.Graphics, input driver.Input) error {
u := currentUI
u.input = input.(inputDriver)
document.Set("title", title)
u.setScreenSize(width, height, scale, u.fullscreen)
canvas.Call("focus")

View File

@ -148,7 +148,11 @@ func appMain(a app.App) {
}
}
func Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool, driver driver.Graphics) error {
func Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool, graphics driver.Graphics, input driver.Input) error {
if graphics != opengl.Get() {
panic("ui: graphics driver must be OpenGL")
}
u := currentUI
u.m.Lock()

3
run.go
View File

@ -19,6 +19,7 @@ import (
"sync/atomic"
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/input"
"github.com/hajimehoshi/ebiten/internal/ui"
"github.com/hajimehoshi/ebiten/internal/web"
)
@ -96,7 +97,7 @@ func run(width, height int, scale float64, title string, g *graphicsContext, mai
if !web.IsGopherJS() {
defer atomic.StoreInt32(&isRunning, 0)
}
if err := ui.Run(width, height, scale, title, g, mainloop, graphicsDriver()); err != nil {
if err := ui.Run(width, height, scale, title, g, mainloop, graphicsDriver(), input.Get()); err != nil {
if err == ui.RegularTermination {
return nil
}