Skip to content

Commit

Permalink
uidriver/js: Use js.Value as key strings
Browse files Browse the repository at this point in the history
hajimehoshi committed Dec 16, 2020
1 parent 442be20 commit 69b1d2e
Showing 3 changed files with 125 additions and 123 deletions.
6 changes: 4 additions & 2 deletions genkeys.go
Original file line number Diff line number Diff line change
@@ -503,11 +503,13 @@ const uidriverJsKeysTmpl = `{{.License}}
package js
import (
"syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
)
var driverKeyToJSKey = map[driver.Key]string{
{{range $name, $code := .DriverKeyNameToJSKey}}driver.Key{{$name}}: {{$code | printf "%q"}},
var driverKeyToJSKey = map[driver.Key]js.Value{
{{range $name, $code := .DriverKeyNameToJSKey}}driver.Key{{$name}}: js.ValueOf({{$code | printf "%q"}}),
{{end}}
}
28 changes: 13 additions & 15 deletions internal/uidriver/js/input_js.go
Original file line number Diff line number Diff line change
@@ -168,7 +168,7 @@ func (i *Input) resetForFrame() {

func (i *Input) IsKeyPressed(key driver.Key) bool {
if i.keyPressed != nil {
if i.keyPressed[driverKeyToJSKey[key]] {
if i.keyPressed[driverKeyToJSKey[key].String()] {
return true
}
}
@@ -210,18 +210,18 @@ func (i *Input) Wheel() (xoff, yoff float64) {
return i.wheelX, i.wheelY
}

func (i *Input) keyDown(code string) {
func (i *Input) keyDown(code js.Value) {
if i.keyPressed == nil {
i.keyPressed = map[string]bool{}
}
i.keyPressed[code] = true
i.keyPressed[code.String()] = true
}

func (i *Input) keyUp(code string) {
func (i *Input) keyUp(code js.Value) {
if i.keyPressed == nil {
i.keyPressed = map[string]bool{}
}
i.keyPressed[code] = false
i.keyPressed[code.String()] = false
}

func (i *Input) keyDownEdge(code int) {
@@ -318,16 +318,15 @@ func (i *Input) Update(e js.Value) {
i.keyDownEdge(code)
return
}
cs := c.String()
if cs == driverKeyToJSKey[driver.KeyUp] ||
cs == driverKeyToJSKey[driver.KeyDown] ||
cs == driverKeyToJSKey[driver.KeyLeft] ||
cs == driverKeyToJSKey[driver.KeyRight] ||
cs == driverKeyToJSKey[driver.KeyBackspace] ||
cs == driverKeyToJSKey[driver.KeyTab] {
if jsutil.Equal(c, driverKeyToJSKey[driver.KeyUp]) ||
jsutil.Equal(c, driverKeyToJSKey[driver.KeyDown]) ||
jsutil.Equal(c, driverKeyToJSKey[driver.KeyLeft]) ||
jsutil.Equal(c, driverKeyToJSKey[driver.KeyRight]) ||
jsutil.Equal(c, driverKeyToJSKey[driver.KeyBackspace]) ||
jsutil.Equal(c, driverKeyToJSKey[driver.KeyTab]) {
e.Call("preventDefault")
}
i.keyDown(cs)
i.keyDown(c)
case jsutil.Equal(t, stringKeypress):
if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) {
i.runeBuffer = append(i.runeBuffer, r)
@@ -339,8 +338,7 @@ func (i *Input) Update(e js.Value) {
i.keyUpEdge(code)
return
}
code := e.Get("code").String()
i.keyUp(code)
i.keyUp(e.Get("code"))
case jsutil.Equal(t, stringMousedown):
button := e.Get("button").Int()
i.mouseDown(button)
214 changes: 108 additions & 106 deletions internal/uidriver/js/keys_js.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 69b1d2e

Please sign in to comment.