2015-01-06 18:25:26 +01:00
|
|
|
// Copyright 2015 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// Note:
|
|
|
|
// * Respect GLFW key names
|
|
|
|
// * https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode
|
2015-01-06 20:27:57 +01:00
|
|
|
// * It is best to replace keyCode with code, but many browsers don't implement it.
|
2015-01-06 18:25:26 +01:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-18 13:11:03 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal"
|
2015-01-06 18:25:26 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
var keyCodeToName map[int]string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
keyCodeToName = map[int]string{
|
2015-01-07 03:00:15 +01:00
|
|
|
0xBC: "Comma",
|
|
|
|
0xBE: "Period",
|
2015-01-07 15:18:40 +01:00
|
|
|
0x12: "Alt",
|
2015-01-07 03:00:15 +01:00
|
|
|
0x14: "CapsLock",
|
2015-01-07 15:18:40 +01:00
|
|
|
0x11: "Control",
|
|
|
|
0x10: "Shift",
|
2015-01-07 03:00:15 +01:00
|
|
|
0x0D: "Enter",
|
|
|
|
0x20: "Space",
|
|
|
|
0x09: "Tab",
|
|
|
|
0x2E: "Delete",
|
|
|
|
0x23: "End",
|
|
|
|
0x24: "Home",
|
|
|
|
0x2D: "Insert",
|
|
|
|
0x22: "PageDown",
|
|
|
|
0x21: "PageUp",
|
|
|
|
0x28: "Down",
|
|
|
|
0x25: "Left",
|
|
|
|
0x27: "Right",
|
|
|
|
0x26: "Up",
|
|
|
|
0x1B: "Escape",
|
2015-01-07 03:22:25 +01:00
|
|
|
// The keys not listed in the Mozilla site:
|
|
|
|
0x08: "Backspace",
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
// ASCII: 0 - 9
|
|
|
|
for c := '0'; c <= '9'; c++ {
|
2015-01-07 03:00:15 +01:00
|
|
|
keyCodeToName[int(c)] = string(c)
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
// ASCII: A - Z
|
|
|
|
for c := 'A'; c <= 'Z'; c++ {
|
2015-01-07 03:00:15 +01:00
|
|
|
keyCodeToName[int(c)] = string(c)
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
// Function keys
|
|
|
|
for i := 1; i <= 12; i++ {
|
2015-01-07 03:00:15 +01:00
|
|
|
keyCodeToName[0x70+i-1] = "F" + strconv.Itoa(i)
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ebitenKeysTmpl = `{{.License}}
|
|
|
|
|
2015-01-07 15:21:52 +01:00
|
|
|
// {{.Notice}}
|
|
|
|
|
2015-01-06 18:25:26 +01:00
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Key represents a keyboard key.
|
|
|
|
type Key int
|
|
|
|
|
|
|
|
// Keys
|
|
|
|
const (
|
2015-01-07 03:00:15 +01:00
|
|
|
{{range $index, $name := .KeyNames}}Key{{$name}} = Key(ui.Key{{$name}})
|
2015-01-06 18:25:26 +01:00
|
|
|
{{end}}
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
|
|
|
const uiKeysTmpl = `{{.License}}
|
|
|
|
|
2015-01-07 15:21:52 +01:00
|
|
|
// {{.Notice}}
|
|
|
|
|
2015-01-06 18:25:26 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
type Key int
|
|
|
|
|
|
|
|
const (
|
2015-01-07 03:00:15 +01:00
|
|
|
{{range $index, $name := .KeyNames}}Key{{$name}}{{if eq $index 0}} Key = iota{{end}}
|
2015-01-06 18:25:26 +01:00
|
|
|
{{end}}
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
|
|
|
const uiKeysGlfwTmpl = `{{.License}}
|
|
|
|
|
2015-01-07 15:21:52 +01:00
|
|
|
// {{.Notice}}
|
|
|
|
|
2015-01-06 18:25:26 +01:00
|
|
|
// +build !js
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2015-04-12 17:49:30 +02:00
|
|
|
glfw "github.com/go-gl/glfw/v3.0/glfw"
|
2015-01-06 18:25:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var glfwKeyCodeToKey = map[glfw.Key]Key{
|
2015-01-07 15:18:40 +01:00
|
|
|
{{range $index, $name := .KeyNamesWithoutMods}}glfw.Key{{$name}}: Key{{$name}},
|
2015-01-06 18:25:26 +01:00
|
|
|
{{end}}
|
2015-01-07 15:18:40 +01:00
|
|
|
glfw.KeyLeftAlt: KeyAlt,
|
|
|
|
glfw.KeyRightAlt: KeyAlt,
|
|
|
|
glfw.KeyLeftControl: KeyControl,
|
|
|
|
glfw.KeyRightControl: KeyControl,
|
|
|
|
glfw.KeyLeftShift: KeyShift,
|
|
|
|
glfw.KeyRightShift: KeyShift,
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const uiKeysJSTmpl = `{{.License}}
|
|
|
|
|
2015-01-07 15:21:52 +01:00
|
|
|
// {{.Notice}}
|
|
|
|
|
2015-01-06 18:25:26 +01:00
|
|
|
// +build js
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
var keyCodeToKey = map[int]Key{
|
2015-01-07 03:00:15 +01:00
|
|
|
{{range $code, $name := .KeyCodeToName}}{{$code}}: Key{{$name}},
|
2015-01-06 18:25:26 +01:00
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-01-07 03:00:15 +01:00
|
|
|
type KeyNames []string
|
|
|
|
|
|
|
|
func (k KeyNames) digit(name string) int {
|
|
|
|
if len(name) != 1 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
c := name[0]
|
|
|
|
if c < '0' || '9' < c {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return int(c - '0')
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k KeyNames) alphabet(name string) rune {
|
|
|
|
if len(name) != 1 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
c := rune(name[0])
|
|
|
|
if c < 'A' || 'Z' < c {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k KeyNames) function(name string) int {
|
|
|
|
if len(name) < 2 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if name[0] != 'F' {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(name[1:])
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k KeyNames) Len() int {
|
|
|
|
return len(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k KeyNames) Less(i, j int) bool {
|
|
|
|
k0, k1 := k[i], k[j]
|
|
|
|
d0, d1 := k.digit(k0), k.digit(k1)
|
|
|
|
a0, a1 := k.alphabet(k0), k.alphabet(k1)
|
|
|
|
f0, f1 := k.function(k0), k.function(k1)
|
|
|
|
if d0 != -1 {
|
|
|
|
if d1 != -1 {
|
|
|
|
return d0 < d1
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a0 != -1 {
|
|
|
|
if d1 != -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if a1 != -1 {
|
|
|
|
return a0 < a1
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:25:26 +01:00
|
|
|
func main() {
|
2015-01-18 13:11:03 +01:00
|
|
|
license, err := internal.LicenseComment()
|
2015-01-06 18:25:26 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-07 15:21:52 +01:00
|
|
|
notice := "DO NOT EDIT: This file is auto-generated by genkeys.go."
|
|
|
|
|
2015-01-06 18:25:26 +01:00
|
|
|
names := []string{}
|
2015-01-07 15:18:40 +01:00
|
|
|
namesWithoutMods := []string{}
|
2015-01-06 18:25:26 +01:00
|
|
|
codes := []int{}
|
|
|
|
for code, name := range keyCodeToName {
|
|
|
|
names = append(names, name)
|
|
|
|
codes = append(codes, code)
|
2015-01-07 15:18:40 +01:00
|
|
|
if name != "Alt" && name != "Control" && name != "Shift" {
|
|
|
|
namesWithoutMods = append(namesWithoutMods, name)
|
|
|
|
}
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
2015-01-07 03:00:15 +01:00
|
|
|
sort.Sort(KeyNames(names))
|
2015-01-07 15:18:40 +01:00
|
|
|
sort.Sort(KeyNames(namesWithoutMods))
|
2015-01-06 18:25:26 +01:00
|
|
|
sort.Ints(codes)
|
|
|
|
|
|
|
|
for path, tmpl := range map[string]string{
|
|
|
|
"keys.go": ebitenKeysTmpl,
|
|
|
|
"internal/ui/keys.go": uiKeysTmpl,
|
|
|
|
"internal/ui/keys_glfw.go": uiKeysGlfwTmpl,
|
|
|
|
"internal/ui/keys_js.go": uiKeysJSTmpl,
|
|
|
|
} {
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
tmpl, err := template.New(path).Parse(tmpl)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
// NOTE: According to godoc, maps are automatically sorted by key.
|
2015-01-18 13:11:03 +01:00
|
|
|
if err := tmpl.Execute(f, map[string]interface{}{
|
2015-01-07 15:18:40 +01:00
|
|
|
"License": license,
|
2015-01-07 15:21:52 +01:00
|
|
|
"Notice": notice,
|
2015-01-07 15:18:40 +01:00
|
|
|
"KeyCodeToName": keyCodeToName,
|
|
|
|
"Codes": codes,
|
|
|
|
"KeyNames": names,
|
|
|
|
"KeyNamesWithoutMods": namesWithoutMods,
|
2015-01-18 13:11:03 +01:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
}
|