driver: Add Input

This commit is contained in:
Hajime Hoshi 2019-04-06 21:21:16 +09:00
parent bf165ae9d4
commit de9f54fd9d
8 changed files with 38 additions and 7 deletions

View File

@ -14,6 +14,22 @@
package driver
type Input interface {
CursorPosition() (x, y int)
GamepadAxis(id int, axis int) float64
GamepadAxisNum(id int) int
GamepadButtonNum(id int) int
GamepadIDs() []int
IsGamepadButtonPressed(id int, button GamepadButton) bool
IsKeyPressed(key Key) bool
IsMouseButtonPressed(button MouseButton) bool
ResetForFrame()
RuneBuffer() []rune
TouchIDs() []int
TouchPosition(id int) (x, y int)
Wheel() (xoff, yoff float64)
}
// TODO: Remove this for API simplicity.
type Touch struct {
ID int

View File

@ -25,7 +25,7 @@ type pos struct {
Y int
}
func Get() *Input {
func Get() driver.Input {
return theInput
}

View File

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

View File

@ -46,7 +46,7 @@ func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool {
return false
}
func (i *Input) SetTouches(touches []*driver.Touch) {
func (i *Input) Update(touches []*driver.Touch) {
i.m.Lock()
i.touches = map[int]pos{}
for _, t := range touches {

View File

@ -761,7 +761,12 @@ func (u *userInterface) update(g GraphicsContext) error {
_ = mainthread.Run(func() error {
glfw.PollEvents()
input.Get().Update(u.window, u.getScale()*glfwScale())
type updater interface {
Update(window *glfw.Window, scale float64)
}
input.Get().(updater).Update(u.window, u.getScale()*glfwScale())
defer hooks.ResumeAudio()

View File

@ -203,7 +203,11 @@ func (u *userInterface) update(g GraphicsContext) error {
}
hooks.ResumeAudio()
input.Get().UpdateGamepads()
type updater interface {
Update()
}
input.Get().(updater).Update()
u.updateGraphicsContext(g)
if err := g.Update(func() {
u.updateGraphicsContext(g)

View File

@ -140,7 +140,10 @@ func appMain(a app.App) {
for _, t := range touches {
ts = append(ts, t)
}
input.Get().SetTouches(ts)
type updater interface {
Update(touches []*driver.Touch)
}
input.Get().(updater).Update(ts)
}
}
}

View File

@ -39,5 +39,8 @@ func updateTouches() {
Y: position.y,
})
}
input.Get().SetTouches(ts)
type updater interface {
Update(touches []*driver.Touch)
}
input.Get().(updater).Update(ts)
}