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 i.cursorX, i.cursorY = x, y
} }
func (i *Input) Update() { func (i *Input) UpdateGamepads() {
nav := js.Global().Get("navigator") nav := js.Global().Get("navigator")
if nav.Get("getGamepads") == js.Undefined() { if nav.Get("getGamepads") == js.Undefined() {
return return

View File

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

View File

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

3
run.go
View File

@ -19,6 +19,7 @@ import (
"sync/atomic" "sync/atomic"
"github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/input"
"github.com/hajimehoshi/ebiten/internal/ui" "github.com/hajimehoshi/ebiten/internal/ui"
"github.com/hajimehoshi/ebiten/internal/web" "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() { if !web.IsGopherJS() {
defer atomic.StoreInt32(&isRunning, 0) 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 { if err == ui.RegularTermination {
return nil return nil
} }