Skip to content

Commit

Permalink
mobile/ebitenmobileview: make runes a parameter. (hajimehoshi#2683)
Browse files Browse the repository at this point in the history
This prevents duplicate input characters due to concurrent touch events.

Closes hajimehoshi#2682
  • Loading branch information
divVerent authored Jun 24, 2023
1 parent 1789f50 commit ede787c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
3 changes: 1 addition & 2 deletions mobile/ebitenmobileview/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ type position struct {

var (
keys = map[ui.Key]struct{}{}
runes []rune
touches = map[ui.TouchID]position{}
)

var (
touchSlice []ui.TouchForInput
)

func updateInput() {
func updateInput(runes []rune) {
touchSlice = touchSlice[:0]
for id, position := range touches {
touchSlice = append(touchSlice, ui.TouchForInput{
Expand Down
9 changes: 5 additions & 4 deletions mobile/ebitenmobileview/input_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ func UpdateTouchesOnAndroid(action int, id int, x, y int) {
switch action {
case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE
touches[ui.TouchID(id)] = position{x, y}
updateInput()
updateInput(nil)
case 0x01, 0x06: // ACTION_UP, ACTION_POINTER_UP
delete(touches, ui.TouchID(id))
updateInput()
updateInput(nil)
}
}

Expand All @@ -142,10 +142,11 @@ func OnKeyDownOnAndroid(keyCode int, unicodeChar int, source int, deviceID int)
case source&sourceKeyboard == sourceKeyboard:
if key, ok := androidKeyToUIKey[keyCode]; ok {
keys[key] = struct{}{}
var runes []rune
if r := rune(unicodeChar); r != 0 && unicode.IsPrint(r) {
runes = []rune{r}
}
updateInput()
updateInput(runes)
}
}
}
Expand All @@ -162,7 +163,7 @@ func OnKeyUpOnAndroid(keyCode int, source int, deviceID int) {
case source&sourceKeyboard == sourceKeyboard:
if key, ok := androidKeyToUIKey[keyCode]; ok {
delete(keys, key)
updateInput()
updateInput(nil)
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions mobile/ebitenmobileview/input_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary:
id := getIDFromPtr(ptr)
touches[ui.TouchID(id)] = position{x, y}
runes = nil
updateInput()
updateInput(nil)
case C.UITouchPhaseEnded, C.UITouchPhaseCancelled:
id := getIDFromPtr(ptr)
delete(ptrToID, ptr)
delete(touches, ui.TouchID(id))
runes = nil
updateInput()
updateInput(nil)
default:
panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase))
}
Expand All @@ -68,7 +66,7 @@ func UpdatePressesOnIOS(phase int, keyCode int, keyString string) {
if key, ok := iosKeyToUIKey[keyCode]; ok {
keys[key] = struct{}{}
}
runes = nil
var runes []rune
if phase == C.UITouchPhaseBegan {
for _, r := range keyString {
if !unicode.IsPrint(r) {
Expand All @@ -77,13 +75,12 @@ func UpdatePressesOnIOS(phase int, keyCode int, keyString string) {
runes = append(runes, r)
}
}
updateInput()
updateInput(runes)
case C.UITouchPhaseEnded, C.UITouchPhaseCancelled:
if key, ok := iosKeyToUIKey[keyCode]; ok {
delete(keys, key)
}
runes = nil
updateInput()
updateInput(nil)
default:
panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase))
}
Expand Down

0 comments on commit ede787c

Please sign in to comment.