input: Refactoring: Rename variables

This commit is contained in:
Hajime Hoshi 2020-02-19 10:42:42 +09:00
parent db2c4dc260
commit 682f454fc2
7 changed files with 70 additions and 70 deletions

View File

@ -34,13 +34,13 @@ import (
) )
var ( var (
nameToGLFWKeys map[string]glfw.Key nameToGLFWKey map[string]glfw.Key
nameToJSKeyCode map[string]string nameToJSKey map[string]string
keyCodeToNameEdge map[int]string edgeKeyCodeToName map[int]string
) )
func init() { func init() {
nameToGLFWKeys = map[string]glfw.Key{ nameToGLFWKey = map[string]glfw.Key{
"Unknown": glfw.KeyUnknown, "Unknown": glfw.KeyUnknown,
"Space": glfw.KeySpace, "Space": glfw.KeySpace,
"Apostrophe": glfw.KeyApostrophe, "Apostrophe": glfw.KeyApostrophe,
@ -86,7 +86,7 @@ func init() {
"Menu": glfw.KeyMenu, "Menu": glfw.KeyMenu,
"Last": glfw.KeyLast, "Last": glfw.KeyLast,
} }
nameToJSKeyCode = map[string]string{ nameToJSKey = map[string]string{
"Comma": "Comma", "Comma": "Comma",
"Period": "Period", "Period": "Period",
"LeftAlt": "AltLeft", "LeftAlt": "AltLeft",
@ -128,48 +128,48 @@ func init() {
} }
// ASCII: 0 - 9 // ASCII: 0 - 9
for c := '0'; c <= '9'; c++ { for c := '0'; c <= '9'; c++ {
nameToGLFWKeys[string(c)] = glfw.Key0 + glfw.Key(c) - '0' nameToGLFWKey[string(c)] = glfw.Key0 + glfw.Key(c) - '0'
nameToJSKeyCode[string(c)] = "Digit" + string(c) nameToJSKey[string(c)] = "Digit" + string(c)
} }
// ASCII: A - Z // ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ { for c := 'A'; c <= 'Z'; c++ {
nameToGLFWKeys[string(c)] = glfw.KeyA + glfw.Key(c) - 'A' nameToGLFWKey[string(c)] = glfw.KeyA + glfw.Key(c) - 'A'
nameToJSKeyCode[string(c)] = "Key" + string(c) nameToJSKey[string(c)] = "Key" + string(c)
} }
// Function keys // Function keys
for i := 1; i <= 12; i++ { for i := 1; i <= 12; i++ {
name := "F" + strconv.Itoa(i) name := "F" + strconv.Itoa(i)
nameToGLFWKeys[name] = glfw.KeyF1 + glfw.Key(i) - 1 nameToGLFWKey[name] = glfw.KeyF1 + glfw.Key(i) - 1
nameToJSKeyCode[name] = name nameToJSKey[name] = name
} }
// Numpad // Numpad
// https://www.w3.org/TR/uievents-code/#key-numpad-section // https://www.w3.org/TR/uievents-code/#key-numpad-section
for c := '0'; c <= '9'; c++ { for c := '0'; c <= '9'; c++ {
name := "KP" + string(c) name := "KP" + string(c)
nameToGLFWKeys[name] = glfw.KeyKP0 + glfw.Key(c) - '0' nameToGLFWKey[name] = glfw.KeyKP0 + glfw.Key(c) - '0'
nameToJSKeyCode[name] = "Numpad" + string(c) nameToJSKey[name] = "Numpad" + string(c)
} }
nameToGLFWKeys["KPDecimal"] = glfw.KeyKPDecimal nameToGLFWKey["KPDecimal"] = glfw.KeyKPDecimal
nameToGLFWKeys["KPDivide"] = glfw.KeyKPDivide nameToGLFWKey["KPDivide"] = glfw.KeyKPDivide
nameToGLFWKeys["KPMultiply"] = glfw.KeyKPMultiply nameToGLFWKey["KPMultiply"] = glfw.KeyKPMultiply
nameToGLFWKeys["KPSubtract"] = glfw.KeyKPSubtract nameToGLFWKey["KPSubtract"] = glfw.KeyKPSubtract
nameToGLFWKeys["KPAdd"] = glfw.KeyKPAdd nameToGLFWKey["KPAdd"] = glfw.KeyKPAdd
nameToGLFWKeys["KPEnter"] = glfw.KeyKPEnter nameToGLFWKey["KPEnter"] = glfw.KeyKPEnter
nameToGLFWKeys["KPEqual"] = glfw.KeyKPEqual nameToGLFWKey["KPEqual"] = glfw.KeyKPEqual
nameToJSKeyCode["KPDecimal"] = "NumpadDecimal" nameToJSKey["KPDecimal"] = "NumpadDecimal"
nameToJSKeyCode["KPDivide"] = "NumpadDivide" nameToJSKey["KPDivide"] = "NumpadDivide"
nameToJSKeyCode["KPMultiply"] = "NumpadMultiply" nameToJSKey["KPMultiply"] = "NumpadMultiply"
nameToJSKeyCode["KPSubtract"] = "NumpadSubtract" nameToJSKey["KPSubtract"] = "NumpadSubtract"
nameToJSKeyCode["KPAdd"] = "NumpadAdd" nameToJSKey["KPAdd"] = "NumpadAdd"
nameToJSKeyCode["KPEnter"] = "NumpadEnter" nameToJSKey["KPEnter"] = "NumpadEnter"
nameToJSKeyCode["KPEqual"] = "NumpadEqual" nameToJSKey["KPEqual"] = "NumpadEqual"
} }
func init() { func init() {
// TODO: How should we treat modifier keys? Now 'left' modifier keys are available. // TODO: How should we treat modifier keys? Now 'left' modifier keys are available.
keyCodeToNameEdge = map[int]string{ edgeKeyCodeToName = map[int]string{
0xbc: "Comma", 0xbc: "Comma",
0xbe: "Period", 0xbe: "Period",
0x12: "LeftAlt", 0x12: "LeftAlt",
@ -219,19 +219,19 @@ func init() {
} }
// ASCII: 0 - 9 // ASCII: 0 - 9
for c := '0'; c <= '9'; c++ { for c := '0'; c <= '9'; c++ {
keyCodeToNameEdge[int(c)] = string(c) edgeKeyCodeToName[int(c)] = string(c)
} }
// ASCII: A - Z // ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ { for c := 'A'; c <= 'Z'; c++ {
keyCodeToNameEdge[int(c)] = string(c) edgeKeyCodeToName[int(c)] = string(c)
} }
// Function keys // Function keys
for i := 1; i <= 12; i++ { for i := 1; i <= 12; i++ {
keyCodeToNameEdge[0x70+i-1] = "F" + strconv.Itoa(i) edgeKeyCodeToName[0x70+i-1] = "F" + strconv.Itoa(i)
} }
// Numpad keys // Numpad keys
for c := '0'; c <= '9'; c++ { for c := '0'; c <= '9'; c++ {
keyCodeToNameEdge[0x60+int(c-'0')] = "KP" + string(c) edgeKeyCodeToName[0x60+int(c-'0')] = "KP" + string(c)
} }
} }
@ -282,7 +282,7 @@ func (k Key) String() string {
return "" return ""
} }
func keyNameToKey(name string) (Key, bool) { func keyNameToKeyCode(name string) (Key, bool) {
switch strings.ToLower(name) { switch strings.ToLower(name) {
{{range $name := .EbitenKeyNames}}case {{$name | printf "%q" | ToLower}}: {{range $name := .EbitenKeyNames}}case {{$name | printf "%q" | ToLower}}:
return Key{{$name}}, true return Key{{$name}}, true
@ -338,12 +338,12 @@ import (
"github.com/hajimehoshi/ebiten/internal/glfw" "github.com/hajimehoshi/ebiten/internal/glfw"
) )
var glfwKeyCodeToKey = map[glfw.Key]driver.Key{ var glfwKeyToDriverKey = map[glfw.Key]driver.Key{
{{range $index, $name := .DriverKeyNames}}glfw.Key{{$name}}: driver.Key{{$name}}, {{range $index, $name := .DriverKeyNames}}glfw.Key{{$name}}: driver.Key{{$name}},
{{end}} {{end}}
} }
var keyCodeToGLFWKey = map[driver.Key]glfw.Key{ var driverKeyToGLFWKey = map[driver.Key]glfw.Key{
{{range $index, $name := .DriverKeyNames}}driver.Key{{$name}}: glfw.Key{{$name}}, {{range $index, $name := .DriverKeyNames}}driver.Key{{$name}}: glfw.Key{{$name}},
{{end}} {{end}}
} }
@ -361,13 +361,13 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
) )
var keyToCode = map[driver.Key]string{ var driverKeyToJSKey = map[driver.Key]string{
{{range $name, $code := .NameToJSKeyCode}}driver.Key{{$name}}: {{$code | printf "%q"}}, {{range $name, $code := .NameToJSKey}}driver.Key{{$name}}: {{$code | printf "%q"}},
{{end}} {{end}}
} }
var keyCodeToKeyEdge = map[int]driver.Key{ var edgeKeyCodeToDriverKey = map[int]driver.Key{
{{range $code, $name := .KeyCodeToNameEdge}}{{$code}}: driver.Key{{$name}}, {{range $code, $name := .EdgeKeyCodeToName}}{{$code}}: driver.Key{{$name}},
{{end}} {{end}}
} }
` `
@ -381,7 +381,7 @@ const glfwKeysTmpl = `{{.License}}
package glfw package glfw
const ( const (
{{range $name, $key := .NameToGLFWKeys}}Key{{$name}} = Key({{$key}}) {{range $name, $key := .NameToGLFWKey}}Key{{$name}} = Key({{$key}})
{{end}} {{end}}
) )
` `
@ -478,7 +478,7 @@ func main() {
ebitenKeyNames := []string{} ebitenKeyNames := []string{}
ebitenKeyNamesWithoutMods := []string{} ebitenKeyNamesWithoutMods := []string{}
driverKeyNames := []string{} driverKeyNames := []string{}
for name := range nameToJSKeyCode { for name := range nameToJSKey {
driverKeyNames = append(driverKeyNames, name) driverKeyNames = append(driverKeyNames, name)
if !strings.HasSuffix(name, "Alt") && !strings.HasSuffix(name, "Control") && !strings.HasSuffix(name, "Shift") { if !strings.HasSuffix(name, "Alt") && !strings.HasSuffix(name, "Control") && !strings.HasSuffix(name, "Shift") {
ebitenKeyNames = append(ebitenKeyNames, name) ebitenKeyNames = append(ebitenKeyNames, name)
@ -542,22 +542,22 @@ func main() {
License string License string
DoNotEdit string DoNotEdit string
BuildTag string BuildTag string
NameToJSKeyCode map[string]string NameToJSKey map[string]string
KeyCodeToNameEdge map[int]string EdgeKeyCodeToName map[int]string
EbitenKeyNames []string EbitenKeyNames []string
EbitenKeyNamesWithoutMods []string EbitenKeyNamesWithoutMods []string
DriverKeyNames []string DriverKeyNames []string
NameToGLFWKeys map[string]glfw.Key NameToGLFWKey map[string]glfw.Key
}{ }{
License: license, License: license,
DoNotEdit: doNotEdit, DoNotEdit: doNotEdit,
BuildTag: buildTag, BuildTag: buildTag,
NameToJSKeyCode: nameToJSKeyCode, NameToJSKey: nameToJSKey,
KeyCodeToNameEdge: keyCodeToNameEdge, EdgeKeyCodeToName: edgeKeyCodeToName,
EbitenKeyNames: ebitenKeyNames, EbitenKeyNames: ebitenKeyNames,
EbitenKeyNamesWithoutMods: ebitenKeyNamesWithoutMods, EbitenKeyNamesWithoutMods: ebitenKeyNamesWithoutMods,
DriverKeyNames: driverKeyNames, DriverKeyNames: driverKeyNames,
NameToGLFWKeys: nameToGLFWKeys, NameToGLFWKey: nameToGLFWKey,
}); err != nil { }); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -112,7 +112,7 @@ func (i *imageDumper) update(screen *Image) error {
i.keyState = map[Key]int{} i.keyState = map[Key]int{}
if keyname := os.Getenv(envScreenshotKey); keyname != "" { if keyname := os.Getenv(envScreenshotKey); keyname != "" {
if key, ok := keyNameToKey(keyname); ok { if key, ok := keyNameToKeyCode(keyname); ok {
i.hasScreenshotKey = true i.hasScreenshotKey = true
i.screenshotKey = key i.screenshotKey = key
} }
@ -120,7 +120,7 @@ func (i *imageDumper) update(screen *Image) error {
if keyname := os.Getenv(envInternalImagesKey); keyname != "" { if keyname := os.Getenv(envInternalImagesKey); keyname != "" {
if isDebug() { if isDebug() {
if key, ok := keyNameToKey(keyname); ok { if key, ok := keyNameToKeyCode(keyname); ok {
i.hasDumpInternalImagesKey = true i.hasDumpInternalImagesKey = true
i.dumpInternalImagesKey = key i.dumpInternalImagesKey = key
} }

View File

@ -248,7 +248,7 @@ func (i *Input) IsKeyPressed(key driver.Key) bool {
if i.keyPressed == nil { if i.keyPressed == nil {
i.keyPressed = map[glfw.Key]bool{} i.keyPressed = map[glfw.Key]bool{}
} }
gk, ok := keyCodeToGLFWKey[key] gk, ok := driverKeyToGLFWKey[key]
if ok && i.keyPressed[gk] { if ok && i.keyPressed[gk] {
r = true r = true
return nil return nil
@ -326,7 +326,7 @@ func (i *Input) update(window *glfw.Window, context driver.UIContext) {
if i.keyPressed == nil { if i.keyPressed == nil {
i.keyPressed = map[glfw.Key]bool{} i.keyPressed = map[glfw.Key]bool{}
} }
for gk := range glfwKeyCodeToKey { for gk := range glfwKeyToDriverKey {
i.keyPressed[gk] = window.GetKey(gk) == glfw.Press i.keyPressed[gk] = window.GetKey(gk) == glfw.Press
} }
if i.mouseButtonPressed == nil { if i.mouseButtonPressed == nil {

View File

@ -26,7 +26,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/glfw" "github.com/hajimehoshi/ebiten/internal/glfw"
) )
var glfwKeyCodeToKey = map[glfw.Key]driver.Key{ var glfwKeyToDriverKey = map[glfw.Key]driver.Key{
glfw.Key0: driver.Key0, glfw.Key0: driver.Key0,
glfw.Key1: driver.Key1, glfw.Key1: driver.Key1,
glfw.Key2: driver.Key2, glfw.Key2: driver.Key2,
@ -132,7 +132,7 @@ var glfwKeyCodeToKey = map[glfw.Key]driver.Key{
glfw.KeyUp: driver.KeyUp, glfw.KeyUp: driver.KeyUp,
} }
var keyCodeToGLFWKey = map[driver.Key]glfw.Key{ var driverKeyToGLFWKey = map[driver.Key]glfw.Key{
driver.Key0: glfw.Key0, driver.Key0: glfw.Key0,
driver.Key1: glfw.Key1, driver.Key1: glfw.Key1,
driver.Key2: glfw.Key2, driver.Key2: glfw.Key2,

View File

@ -154,12 +154,12 @@ func (i *Input) ResetForFrame() {
func (i *Input) IsKeyPressed(key driver.Key) bool { func (i *Input) IsKeyPressed(key driver.Key) bool {
if i.keyPressed != nil { if i.keyPressed != nil {
if i.keyPressed[keyToCode[key]] { if i.keyPressed[driverKeyToJSKey[key]] {
return true return true
} }
} }
if i.keyPressedEdge != nil { if i.keyPressedEdge != nil {
for c, k := range keyCodeToKeyEdge { for c, k := range edgeKeyCodeToDriverKey {
if k != key { if k != key {
continue continue
} }
@ -288,24 +288,24 @@ func (i *Input) Update(e js.Value) {
c := e.Get("code") c := e.Get("code")
if jsutil.Equal(c, js.Undefined()) { if jsutil.Equal(c, js.Undefined()) {
code := e.Get("keyCode").Int() code := e.Get("keyCode").Int()
if keyCodeToKeyEdge[code] == driver.KeyUp || if edgeKeyCodeToDriverKey[code] == driver.KeyUp ||
keyCodeToKeyEdge[code] == driver.KeyDown || edgeKeyCodeToDriverKey[code] == driver.KeyDown ||
keyCodeToKeyEdge[code] == driver.KeyLeft || edgeKeyCodeToDriverKey[code] == driver.KeyLeft ||
keyCodeToKeyEdge[code] == driver.KeyRight || edgeKeyCodeToDriverKey[code] == driver.KeyRight ||
keyCodeToKeyEdge[code] == driver.KeyBackspace || edgeKeyCodeToDriverKey[code] == driver.KeyBackspace ||
keyCodeToKeyEdge[code] == driver.KeyTab { edgeKeyCodeToDriverKey[code] == driver.KeyTab {
e.Call("preventDefault") e.Call("preventDefault")
} }
i.keyDownEdge(code) i.keyDownEdge(code)
return return
} }
cs := c.String() cs := c.String()
if cs == keyToCode[driver.KeyUp] || if cs == driverKeyToJSKey[driver.KeyUp] ||
cs == keyToCode[driver.KeyDown] || cs == driverKeyToJSKey[driver.KeyDown] ||
cs == keyToCode[driver.KeyLeft] || cs == driverKeyToJSKey[driver.KeyLeft] ||
cs == keyToCode[driver.KeyRight] || cs == driverKeyToJSKey[driver.KeyRight] ||
cs == keyToCode[driver.KeyBackspace] || cs == driverKeyToJSKey[driver.KeyBackspace] ||
cs == keyToCode[driver.KeyTab] { cs == driverKeyToJSKey[driver.KeyTab] {
e.Call("preventDefault") e.Call("preventDefault")
} }
i.keyDown(cs) i.keyDown(cs)

View File

@ -22,7 +22,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
) )
var keyToCode = map[driver.Key]string{ var driverKeyToJSKey = map[driver.Key]string{
driver.Key0: "Digit0", driver.Key0: "Digit0",
driver.Key1: "Digit1", driver.Key1: "Digit1",
driver.Key2: "Digit2", driver.Key2: "Digit2",
@ -128,7 +128,7 @@ var keyToCode = map[driver.Key]string{
driver.KeyZ: "KeyZ", driver.KeyZ: "KeyZ",
} }
var keyCodeToKeyEdge = map[int]driver.Key{ var edgeKeyCodeToDriverKey = map[int]driver.Key{
8: driver.KeyBackspace, 8: driver.KeyBackspace,
9: driver.KeyTab, 9: driver.KeyTab,
13: driver.KeyEnter, 13: driver.KeyEnter,

View File

@ -549,7 +549,7 @@ func (k Key) String() string {
return "" return ""
} }
func keyNameToKey(name string) (Key, bool) { func keyNameToKeyCode(name string) (Key, bool) {
switch strings.ToLower(name) { switch strings.ToLower(name) {
case "0": case "0":
return Key0, true return Key0, true