mobile/ebitenmobileview: make runes a parameter. (#2683)

This prevents duplicate input characters due to concurrent touch events.

Closes #2682
This commit is contained in:
divVerent 2023-06-24 00:40:29 -04:00 committed by Hajime Hoshi
parent 6566a6d63f
commit 1b9a7439f4
3 changed files with 8 additions and 8 deletions

View File

@ -27,7 +27,6 @@ type position struct {
var ( var (
keys = map[ui.Key]struct{}{} keys = map[ui.Key]struct{}{}
runes []rune
touches = map[ui.TouchID]position{} touches = map[ui.TouchID]position{}
) )
@ -35,7 +34,7 @@ var (
touchSlice []ui.TouchForInput touchSlice []ui.TouchForInput
) )
func updateInput() { func updateInput(runes []rune) {
touchSlice = touchSlice[:0] touchSlice = touchSlice[:0]
for id, position := range touches { for id, position := range touches {
touchSlice = append(touchSlice, ui.TouchForInput{ touchSlice = append(touchSlice, ui.TouchForInput{

View File

@ -123,10 +123,10 @@ func UpdateTouchesOnAndroid(action int, id int, x, y int) {
switch action { switch action {
case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE
touches[ui.TouchID(id)] = position{x, y} touches[ui.TouchID(id)] = position{x, y}
updateInput() updateInput(nil)
case 0x01, 0x06: // ACTION_UP, ACTION_POINTER_UP case 0x01, 0x06: // ACTION_UP, ACTION_POINTER_UP
delete(touches, ui.TouchID(id)) delete(touches, ui.TouchID(id))
updateInput() updateInput(nil)
} }
} }
@ -142,10 +142,11 @@ func OnKeyDownOnAndroid(keyCode int, unicodeChar int, source int, deviceID int)
case source&sourceKeyboard == sourceKeyboard: case source&sourceKeyboard == sourceKeyboard:
if key, ok := androidKeyToUIKey[keyCode]; ok { if key, ok := androidKeyToUIKey[keyCode]; ok {
keys[key] = struct{}{} keys[key] = struct{}{}
var runes []rune
if r := rune(unicodeChar); r != 0 && unicode.IsPrint(r) { if r := rune(unicodeChar); r != 0 && unicode.IsPrint(r) {
runes = []rune{r} runes = []rune{r}
} }
updateInput() updateInput(runes)
} }
} }
} }
@ -162,7 +163,7 @@ func OnKeyUpOnAndroid(keyCode int, source int, deviceID int) {
case source&sourceKeyboard == sourceKeyboard: case source&sourceKeyboard == sourceKeyboard:
if key, ok := androidKeyToUIKey[keyCode]; ok { if key, ok := androidKeyToUIKey[keyCode]; ok {
delete(keys, key) delete(keys, key)
updateInput() updateInput(nil)
} }
} }
} }

View File

@ -48,12 +48,12 @@ func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary: case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary:
id := getIDFromPtr(ptr) id := getIDFromPtr(ptr)
touches[ui.TouchID(id)] = position{x, y} touches[ui.TouchID(id)] = position{x, y}
updateInput() updateInput(nil)
case C.UITouchPhaseEnded, C.UITouchPhaseCancelled: case C.UITouchPhaseEnded, C.UITouchPhaseCancelled:
id := getIDFromPtr(ptr) id := getIDFromPtr(ptr)
delete(ptrToID, ptr) delete(ptrToID, ptr)
delete(touches, ui.TouchID(id)) delete(touches, ui.TouchID(id))
updateInput() updateInput(nil)
default: default:
panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase)) panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase))
} }