internal/uidriver/mobile: Separate UpdateInput into UpdateInput and UpdateGamepads

This commit is contained in:
Hajime Hoshi 2021-10-16 20:37:01 +09:00
parent ab5220ea4c
commit 2d9349824f
3 changed files with 30 additions and 12 deletions

View File

@ -191,7 +191,7 @@ func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool {
return false return false
} }
func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []Touch, gamepads []Gamepad) { func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []Touch) {
i.ui.m.Lock() i.ui.m.Lock()
defer i.ui.m.Unlock() defer i.ui.m.Unlock()
@ -210,7 +210,9 @@ func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []Tou
i.touches = i.touches[:0] i.touches = i.touches[:0]
i.touches = append(i.touches, touches...) i.touches = append(i.touches, touches...)
}
func (i *Input) updateGamepads(gamepads []Gamepad) {
i.gamepads = i.gamepads[:0] i.gamepads = i.gamepads[:0]
i.gamepads = append(i.gamepads, gamepads...) i.gamepads = append(i.gamepads, gamepads...)
} }

View File

@ -218,7 +218,7 @@ func (u *UserInterface) appMain(a app.App) {
for _, t := range touches { for _, t := range touches {
ts = append(ts, t) ts = append(ts, t)
} }
u.input.update(keys, runes, ts, nil) u.input.update(keys, runes, ts)
} }
} }
} }
@ -468,8 +468,17 @@ type Gamepad struct {
AxisNum int AxisNum int
} }
func (u *UserInterface) UpdateInput(keys map[driver.Key]struct{}, runes []rune, touches []Touch, gamepads []Gamepad) { func (u *UserInterface) UpdateInput(keys map[driver.Key]struct{}, runes []rune, touches []Touch) {
u.input.update(keys, runes, touches, gamepads) u.input.update(keys, runes, touches)
if u.fpsMode == driver.FPSModeVsyncOffMinimum {
u.renderRequester.RequestRenderIfNeeded()
}
}
// UpdateGamepads updates the gamepad states.
// UpdateGamepads is called when the gamepad states are given from outside like Android.
func (u *UserInterface) UpdateGamepads(gamepads []Gamepad) {
u.input.updateGamepads(gamepads)
if u.fpsMode == driver.FPSModeVsyncOffMinimum { if u.fpsMode == driver.FPSModeVsyncOffMinimum {
u.renderRequester.RequestRenderIfNeeded() u.renderRequester.RequestRenderIfNeeded()
} }

View File

@ -18,6 +18,8 @@
package ebitenmobileview package ebitenmobileview
import ( import (
"runtime"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/uidriver/mobile" "github.com/hajimehoshi/ebiten/v2/internal/uidriver/mobile"
) )
@ -31,6 +33,8 @@ var (
keys = map[driver.Key]struct{}{} keys = map[driver.Key]struct{}{}
runes []rune runes []rune
touches = map[driver.TouchID]position{} touches = map[driver.TouchID]position{}
// gamepads is updated only at Android.
gamepads = map[driver.GamepadID]mobile.Gamepad{} gamepads = map[driver.GamepadID]mobile.Gamepad{}
) )
@ -49,10 +53,13 @@ func updateInput() {
}) })
} }
mobile.Get().UpdateInput(keys, runes, touchSlice)
if runtime.GOOS == "android" {
gamepadSlice = gamepadSlice[:0] gamepadSlice = gamepadSlice[:0]
for _, g := range gamepads { for _, g := range gamepads {
gamepadSlice = append(gamepadSlice, g) gamepadSlice = append(gamepadSlice, g)
} }
mobile.Get().UpdateGamepads(gamepadSlice)
mobile.Get().UpdateInput(keys, runes, touchSlice, gamepadSlice) }
} }