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
}
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()
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 = append(i.touches, touches...)
}
func (i *Input) updateGamepads(gamepads []Gamepad) {
i.gamepads = i.gamepads[:0]
i.gamepads = append(i.gamepads, gamepads...)
}

View File

@ -218,7 +218,7 @@ func (u *UserInterface) appMain(a app.App) {
for _, t := range touches {
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
}
func (u *UserInterface) UpdateInput(keys map[driver.Key]struct{}, runes []rune, touches []Touch, gamepads []Gamepad) {
u.input.update(keys, runes, touches, gamepads)
func (u *UserInterface) UpdateInput(keys map[driver.Key]struct{}, runes []rune, touches []Touch) {
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 {
u.renderRequester.RequestRenderIfNeeded()
}

View File

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