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

View File

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

View File

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

View File

@ -26,7 +26,7 @@ import (
"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.Key1: driver.Key1,
glfw.Key2: driver.Key2,
@ -132,7 +132,7 @@ var glfwKeyCodeToKey = map[glfw.Key]driver.Key{
glfw.KeyUp: driver.KeyUp,
}
var keyCodeToGLFWKey = map[driver.Key]glfw.Key{
var driverKeyToGLFWKey = map[driver.Key]glfw.Key{
driver.Key0: glfw.Key0,
driver.Key1: glfw.Key1,
driver.Key2: glfw.Key2,

View File

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

View File

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

View File

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