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 GitHub
parent 1789f509e1
commit ede787c228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 14 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

@ -49,14 +49,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}
runes = nil updateInput(nil)
updateInput()
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))
runes = nil updateInput(nil)
updateInput()
default: default:
panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase)) panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase))
} }
@ -68,7 +66,7 @@ func UpdatePressesOnIOS(phase int, keyCode int, keyString string) {
if key, ok := iosKeyToUIKey[keyCode]; ok { if key, ok := iosKeyToUIKey[keyCode]; ok {
keys[key] = struct{}{} keys[key] = struct{}{}
} }
runes = nil var runes []rune
if phase == C.UITouchPhaseBegan { if phase == C.UITouchPhaseBegan {
for _, r := range keyString { for _, r := range keyString {
if !unicode.IsPrint(r) { if !unicode.IsPrint(r) {
@ -77,13 +75,12 @@ func UpdatePressesOnIOS(phase int, keyCode int, keyString string) {
runes = append(runes, r) runes = append(runes, r)
} }
} }
updateInput() updateInput(runes)
case C.UITouchPhaseEnded, C.UITouchPhaseCancelled: case C.UITouchPhaseEnded, C.UITouchPhaseCancelled:
if key, ok := iosKeyToUIKey[keyCode]; ok { if key, ok := iosKeyToUIKey[keyCode]; ok {
delete(keys, key) delete(keys, key)
} }
runes = nil updateInput(nil)
updateInput()
default: default:
panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase)) panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase))
} }