input: Refactoring

This commit is contained in:
Hajime Hoshi 2019-09-01 18:07:11 +09:00
parent 31a47d05b1
commit e465c59395

View File

@ -351,9 +351,7 @@ const (
) )
` `
type KeyNames []string func digitKey(name string) int {
func (k KeyNames) digit(name string) int {
if len(name) != 1 { if len(name) != 1 {
return -1 return -1
} }
@ -364,7 +362,7 @@ func (k KeyNames) digit(name string) int {
return int(c - '0') return int(c - '0')
} }
func (k KeyNames) alphabet(name string) rune { func alphabetKey(name string) rune {
if len(name) != 1 { if len(name) != 1 {
return -1 return -1
} }
@ -375,7 +373,7 @@ func (k KeyNames) alphabet(name string) rune {
return c return c
} }
func (k KeyNames) function(name string) int { func functionKey(name string) int {
if len(name) < 2 { if len(name) < 2 {
return -1 return -1
} }
@ -389,44 +387,38 @@ func (k KeyNames) function(name string) int {
return i return i
} }
func (k KeyNames) Len() int { func keyNamesLess(k []string) func(i, j int) bool {
return len(k) return func(i, j int) bool {
} k0, k1 := k[i], k[j]
d0, d1 := digitKey(k0), digitKey(k1)
func (k KeyNames) Less(i, j int) bool { a0, a1 := alphabetKey(k0), alphabetKey(k1)
k0, k1 := k[i], k[j] f0, f1 := functionKey(k0), functionKey(k1)
d0, d1 := k.digit(k0), k.digit(k1) if d0 != -1 {
a0, a1 := k.alphabet(k0), k.alphabet(k1) if d1 != -1 {
f0, f1 := k.function(k0), k.function(k1) return d0 < d1
if d0 != -1 { }
if d1 != -1 { return true
return d0 < d1 }
if a0 != -1 {
if d1 != -1 {
return false
}
if a1 != -1 {
return a0 < a1
}
return true
} }
return true
}
if a0 != -1 {
if d1 != -1 { if d1 != -1 {
return false return false
} }
if a1 != -1 { if a1 != -1 {
return a0 < a1 return false
} }
return true if f0 != -1 && f1 != -1 {
return f0 < f1
}
return k0 < k1
} }
if d1 != -1 {
return false
}
if a1 != -1 {
return false
}
if f0 != -1 && f1 != -1 {
return f0 < f1
}
return k0 < k1
}
func (k KeyNames) Swap(i, j int) {
k[i], k[j] = k[j], k[i]
} }
const license = `// Copyright 2013 The Ebiten Authors const license = `// Copyright 2013 The Ebiten Authors
@ -467,8 +459,8 @@ func main() {
namesWithoutMods = append(namesWithoutMods, n) namesWithoutMods = append(namesWithoutMods, n)
} }
sort.Sort(KeyNames(names)) sort.Slice(names, keyNamesLess(names))
sort.Sort(KeyNames(namesWithoutMods)) sort.Slice(namesWithoutMods, keyNamesLess(namesWithoutMods))
sort.Strings(codes) sort.Strings(codes)
for path, tmpl := range map[string]string{ for path, tmpl := range map[string]string{
@ -505,17 +497,28 @@ func main() {
buildTag = "// +build js" buildTag = "// +build js"
} }
// NOTE: According to godoc, maps are automatically sorted by key. // NOTE: According to godoc, maps are automatically sorted by key.
if err := tmpl.Execute(f, map[string]interface{}{ if err := tmpl.Execute(f, struct {
"License": license, License string
"DoNotEdit": doNotEdit, DoNotEdit string
"BuildTag": buildTag, BuildTag string
"NameToJSKeyCodes": nameToJSKeyCodes, NameToJSKeyCodes map[string][]string
"KeyCodeToNameEdge": keyCodeToNameEdge, KeyCodeToNameEdge map[int]string
"Codes": codes, Codes []string
"KeyNames": names, KeyNames []string
"LastKeyName": names[len(names)-1], LastKeyName string
"KeyNamesWithoutMods": namesWithoutMods, KeyNamesWithoutMods []string
"NameToGLFWKeys": nameToGLFWKeys, 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,
}); err != nil { }); err != nil {
log.Fatal(err) log.Fatal(err)
} }