uidriver/mobile: Implement InputChars for gomobile-build

This change also fixes InputChars to return only printable Unicode
chars on Android.

Updates #237
This commit is contained in:
Hajime Hoshi 2020-02-23 00:26:48 +09:00
parent de52eb75e4
commit 3af869732c
2 changed files with 20 additions and 8 deletions

View File

@ -21,6 +21,7 @@ import (
"fmt"
"runtime/debug"
"sync"
"unicode"
"golang.org/x/mobile/app"
"golang.org/x/mobile/event/key"
@ -151,6 +152,7 @@ func (u *UserInterface) appMain(a app.App) {
for e := range a.Events() {
var updateInput bool
var runes []rune
switch e := a.Filter(e).(type) {
case lifecycle.Event:
@ -204,14 +206,20 @@ func (u *UserInterface) appMain(a app.App) {
updateInput = true
case key.Event:
k, ok := gbuildKeyToDriverKey[e.Code]
if !ok {
continue
if ok {
switch e.Direction {
case key.DirPress, key.DirNone:
keys[k] = struct{}{}
case key.DirRelease:
delete(keys, k)
}
}
switch e.Direction {
case key.DirPress, key.DirNone:
keys[k] = struct{}{}
case key.DirRelease:
delete(keys, k)
if e.Rune != -1 && unicode.IsPrint(e.Rune) {
runes = []rune{e.Rune}
}
}
updateInput = true
}
@ -221,7 +229,7 @@ func (u *UserInterface) appMain(a app.App) {
for _, t := range touches {
ts = append(ts, t)
}
u.input.update(keys, nil, ts)
u.input.update(keys, runes, ts)
}
}
}

View File

@ -14,6 +14,10 @@
package ebitenmobileview
import (
"unicode"
)
func UpdateTouchesOnAndroid(action int, id int, x, y int) {
switch action {
case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE
@ -37,8 +41,8 @@ func OnKeyDownOnAndroid(keyCode int, unicodeChar int) {
return
}
keys[key] = struct{}{}
if unicodeChar != 0 {
runes = []rune{rune(unicodeChar)}
if r := rune(unicodeChar); r != 0 && unicode.IsPrint(r) {
runes = []rune{r}
}
updateInput()
}