input: Refactoring

This commit is contained in:
Hajime Hoshi 2019-09-01 21:12:29 +09:00
parent e465c59395
commit 23002c7597

View File

@ -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)
}