From 23002c759728ab01f8e76df3df84e9ed3d6459f4 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 1 Sep 2019 21:12:29 +0900 Subject: [PATCH] input: Refactoring --- genkeys.go | 75 ++++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/genkeys.go b/genkeys.go index 187d5709d..7afc37d4b 100644 --- a/genkeys.go +++ b/genkeys.go @@ -249,8 +249,8 @@ type Key int // Keys. const ( -{{range $index, $name := .KeyNames}}Key{{$name}} Key = Key(driver.Key{{$name}}) -{{end}} KeyMax Key = Key{{.LastKeyName}} +{{range $index, $name := .EbitenKeyNames}}Key{{$name}} Key = Key(driver.Key{{$name}}) +{{end}} KeyMax Key = Key{{.LastEbitenKeyName}} ) // String returns a string representing the key. @@ -258,7 +258,7 @@ const ( // If k is an undefined key, String returns an empty string. func (k Key) String() string { switch k { - {{range $name := .KeyNames}}case Key{{$name}}: + {{range $name := .EbitenKeyNames}}case Key{{$name}}: return {{$name | printf "%q"}} {{end}}} return "" @@ -266,7 +266,7 @@ func (k Key) String() string { func keyNameToKey(name string) (Key, bool) { switch strings.ToLower(name) { - {{range $name := .KeyNames}}case {{$name | printf "%q" | ToLower}}: + {{range $name := .EbitenKeyNames}}case {{$name | printf "%q" | ToLower}}: return Key{{$name}}, true {{end}}} return 0, false @@ -282,7 +282,7 @@ package driver type Key int const ( -{{range $index, $name := .KeyNames}}Key{{$name}}{{if eq $index 0}} Key = iota{{end}} +{{range $index, $name := .EbitenKeyNames}}Key{{$name}}{{if eq $index 0}} Key = iota{{end}} {{end}} ) ` @@ -301,7 +301,7 @@ import ( ) var glfwKeyCodeToKey = map[glfw.Key]driver.Key{ -{{range $index, $name := .KeyNamesWithoutMods}}glfw.Key{{$name}}: driver.Key{{$name}}, +{{range $index, $name := .EbitenKeyNamesWithoutMods}}glfw.Key{{$name}}: driver.Key{{$name}}, {{end}} glfw.KeyLeftAlt: driver.KeyAlt, glfw.KeyRightAlt: driver.KeyAlt, @@ -440,28 +440,17 @@ func main() { // Follow the standard comment rule (https://golang.org/s/generatedcode). doNotEdit := "// Code generated by genkeys.go using 'go generate'. DO NOT EDIT." - namesSet := map[string]struct{}{} - namesWithoutModsSet := map[string]struct{}{} - codes := []string{} - for name, cs := range nameToJSKeyCodes { - namesSet[name] = struct{}{} - codes = append(codes, cs...) + ebitenKeyNames := []string{} + ebitenKeyNamesWithoutMods := []string{} + for name := range nameToJSKeyCodes { + ebitenKeyNames = append(ebitenKeyNames, name) if name != "Alt" && name != "Control" && name != "Shift" { - namesWithoutModsSet[name] = struct{}{} + ebitenKeyNamesWithoutMods = append(ebitenKeyNamesWithoutMods, name) } } - names := []string{} - namesWithoutMods := []string{} - for n := range namesSet { - names = append(names, n) - } - for n := range namesWithoutModsSet { - namesWithoutMods = append(namesWithoutMods, n) - } - sort.Slice(names, keyNamesLess(names)) - sort.Slice(namesWithoutMods, keyNamesLess(namesWithoutMods)) - sort.Strings(codes) + sort.Slice(ebitenKeyNames, keyNamesLess(ebitenKeyNames)) + sort.Slice(ebitenKeyNamesWithoutMods, keyNamesLess(ebitenKeyNamesWithoutMods)) for path, tmpl := range map[string]string{ "keys.go": ebitenKeysTmpl, @@ -498,27 +487,25 @@ func main() { } // NOTE: According to godoc, maps are automatically sorted by key. if err := tmpl.Execute(f, struct { - License string - DoNotEdit string - BuildTag string - NameToJSKeyCodes map[string][]string - KeyCodeToNameEdge map[int]string - Codes []string - KeyNames []string - LastKeyName string - KeyNamesWithoutMods []string - NameToGLFWKeys map[string]glfw.Key + License string + DoNotEdit string + BuildTag string + NameToJSKeyCodes map[string][]string + KeyCodeToNameEdge map[int]string + EbitenKeyNames []string + EbitenKeyNamesWithoutMods []string + LastEbitenKeyName string + NameToGLFWKeys map[string]glfw.Key }{ - License: license, - DoNotEdit: doNotEdit, - BuildTag: buildTag, - NameToJSKeyCodes: nameToJSKeyCodes, - KeyCodeToNameEdge: keyCodeToNameEdge, - Codes: codes, - KeyNames: names, - LastKeyName: names[len(names)-1], - KeyNamesWithoutMods: namesWithoutMods, - NameToGLFWKeys: nameToGLFWKeys, + License: license, + DoNotEdit: doNotEdit, + BuildTag: buildTag, + NameToJSKeyCodes: nameToJSKeyCodes, + KeyCodeToNameEdge: keyCodeToNameEdge, + EbitenKeyNames: ebitenKeyNames, + EbitenKeyNamesWithoutMods: ebitenKeyNamesWithoutMods, + LastEbitenKeyName: ebitenKeyNames[len(ebitenKeyNames)-1], + NameToGLFWKeys: nameToGLFWKeys, }); err != nil { log.Fatal(err) }