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 (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2018-04-10 18:00:22 +02:00
|
|
|
"strings"
|
2015-01-06 18:25:26 +01:00
|
|
|
"text/template"
|
2018-12-29 11:12:11 +01:00
|
|
|
|
|
|
|
"github.com/go-gl/glfw/v3.2/glfw"
|
2015-01-06 18:25:26 +01:00
|
|
|
)
|
|
|
|
|
2017-04-13 20:02:38 +02:00
|
|
|
var (
|
2018-12-29 11:12:11 +01:00
|
|
|
nameToGLFWKeys map[string]glfw.Key
|
2019-09-01 17:25:39 +02:00
|
|
|
nameToJSKeyCode map[string]string
|
2017-11-09 16:35:46 +01:00
|
|
|
keyCodeToNameEdge map[int]string
|
2017-04-13 20:02:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-12-29 11:12:11 +01:00
|
|
|
nameToGLFWKeys = map[string]glfw.Key{
|
|
|
|
"Unknown": glfw.KeyUnknown,
|
|
|
|
"Space": glfw.KeySpace,
|
|
|
|
"Apostrophe": glfw.KeyApostrophe,
|
|
|
|
"Comma": glfw.KeyComma,
|
|
|
|
"Minus": glfw.KeyMinus,
|
|
|
|
"Period": glfw.KeyPeriod,
|
|
|
|
"Slash": glfw.KeySlash,
|
|
|
|
"Semicolon": glfw.KeySemicolon,
|
|
|
|
"Equal": glfw.KeyEqual,
|
|
|
|
"LeftBracket": glfw.KeyLeftBracket,
|
|
|
|
"Backslash": glfw.KeyBackslash,
|
|
|
|
"RightBracket": glfw.KeyRightBracket,
|
|
|
|
"GraveAccent": glfw.KeyGraveAccent,
|
|
|
|
"World1": glfw.KeyWorld1,
|
|
|
|
"World2": glfw.KeyWorld2,
|
|
|
|
"Escape": glfw.KeyEscape,
|
|
|
|
"Enter": glfw.KeyEnter,
|
|
|
|
"Tab": glfw.KeyTab,
|
|
|
|
"Backspace": glfw.KeyBackspace,
|
|
|
|
"Insert": glfw.KeyInsert,
|
|
|
|
"Delete": glfw.KeyDelete,
|
|
|
|
"Right": glfw.KeyRight,
|
|
|
|
"Left": glfw.KeyLeft,
|
|
|
|
"Down": glfw.KeyDown,
|
|
|
|
"Up": glfw.KeyUp,
|
|
|
|
"PageUp": glfw.KeyPageUp,
|
|
|
|
"PageDown": glfw.KeyPageDown,
|
|
|
|
"Home": glfw.KeyHome,
|
|
|
|
"End": glfw.KeyEnd,
|
|
|
|
"CapsLock": glfw.KeyCapsLock,
|
|
|
|
"ScrollLock": glfw.KeyScrollLock,
|
|
|
|
"NumLock": glfw.KeyNumLock,
|
|
|
|
"PrintScreen": glfw.KeyPrintScreen,
|
|
|
|
"Pause": glfw.KeyPause,
|
|
|
|
"LeftShift": glfw.KeyLeftShift,
|
|
|
|
"LeftControl": glfw.KeyLeftControl,
|
|
|
|
"LeftAlt": glfw.KeyLeftAlt,
|
|
|
|
"LeftSuper": glfw.KeyLeftSuper,
|
|
|
|
"RightShift": glfw.KeyRightShift,
|
|
|
|
"RightControl": glfw.KeyRightControl,
|
|
|
|
"RightAlt": glfw.KeyRightAlt,
|
|
|
|
"RightSuper": glfw.KeyRightSuper,
|
|
|
|
"Menu": glfw.KeyMenu,
|
|
|
|
"Last": glfw.KeyLast,
|
|
|
|
}
|
2019-09-01 17:25:39 +02:00
|
|
|
nameToJSKeyCode = map[string]string{
|
|
|
|
"Comma": "Comma",
|
|
|
|
"Period": "Period",
|
|
|
|
"LeftAlt": "AltLeft",
|
|
|
|
"RightAlt": "AltRight",
|
|
|
|
"CapsLock": "CapsLock",
|
|
|
|
"LeftControl": "ControlLeft",
|
|
|
|
"RightControl": "ControlRight",
|
|
|
|
"LeftShift": "ShiftLeft",
|
|
|
|
"RightShift": "ShiftRight",
|
|
|
|
"Enter": "Enter",
|
|
|
|
"Space": "Space",
|
|
|
|
"Tab": "Tab",
|
|
|
|
"Delete": "Delete",
|
|
|
|
"End": "End",
|
|
|
|
"Home": "Home",
|
|
|
|
"Insert": "Insert",
|
|
|
|
"PageDown": "PageDown",
|
|
|
|
"PageUp": "PageUp",
|
|
|
|
"Down": "ArrowDown",
|
|
|
|
"Left": "ArrowLeft",
|
|
|
|
"Right": "ArrowRight",
|
|
|
|
"Up": "ArrowUp",
|
|
|
|
"Escape": "Escape",
|
|
|
|
"Backspace": "Backspace",
|
|
|
|
"Apostrophe": "Quote",
|
|
|
|
"Minus": "Minus",
|
|
|
|
"Slash": "Slash",
|
|
|
|
"Semicolon": "Semicolon",
|
|
|
|
"Equal": "Equal",
|
|
|
|
"LeftBracket": "BracketLeft",
|
|
|
|
"Backslash": "Backslash",
|
|
|
|
"RightBracket": "BracketRight",
|
|
|
|
"GraveAccent": "Backquote",
|
|
|
|
"NumLock": "NumLock",
|
|
|
|
"Pause": "Pause",
|
|
|
|
"PrintScreen": "PrintScreen",
|
|
|
|
"ScrollLock": "ScrollLock",
|
|
|
|
"Menu": "ContextMenu",
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
|
|
|
// ASCII: 0 - 9
|
|
|
|
for c := '0'; c <= '9'; c++ {
|
2018-12-29 11:12:11 +01:00
|
|
|
nameToGLFWKeys[string(c)] = glfw.Key0 + glfw.Key(c) - '0'
|
2019-09-01 17:25:39 +02:00
|
|
|
nameToJSKeyCode[string(c)] = "Digit" + string(c)
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
|
|
|
// ASCII: A - Z
|
|
|
|
for c := 'A'; c <= 'Z'; c++ {
|
2018-12-29 11:12:11 +01:00
|
|
|
nameToGLFWKeys[string(c)] = glfw.KeyA + glfw.Key(c) - 'A'
|
2019-09-01 17:25:39 +02:00
|
|
|
nameToJSKeyCode[string(c)] = "Key" + string(c)
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
|
|
|
// Function keys
|
|
|
|
for i := 1; i <= 12; i++ {
|
2018-12-29 11:12:11 +01:00
|
|
|
name := "F" + strconv.Itoa(i)
|
|
|
|
nameToGLFWKeys[name] = glfw.KeyF1 + glfw.Key(i) - 1
|
2019-09-01 17:25:39 +02:00
|
|
|
nameToJSKeyCode[name] = name
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
2018-04-12 16:56:50 +02:00
|
|
|
// Numpad
|
|
|
|
// https://www.w3.org/TR/uievents-code/#key-numpad-section
|
|
|
|
for c := '0'; c <= '9'; c++ {
|
2018-12-29 11:12:11 +01:00
|
|
|
name := "KP" + string(c)
|
|
|
|
nameToGLFWKeys[name] = glfw.KeyKP0 + glfw.Key(c) - '0'
|
2019-09-01 17:25:39 +02:00
|
|
|
nameToJSKeyCode[name] = "Numpad" + string(c)
|
2018-04-12 16:56:50 +02:00
|
|
|
}
|
2018-12-29 11:12:11 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-09-01 17:25:39 +02:00
|
|
|
nameToJSKeyCode["KPDecimal"] = "NumpadDecimal"
|
|
|
|
nameToJSKeyCode["KPDivide"] = "NumpadDivide"
|
|
|
|
nameToJSKeyCode["KPMultiply"] = "NumpadMultiply"
|
|
|
|
nameToJSKeyCode["KPSubtract"] = "NumpadSubtract"
|
|
|
|
nameToJSKeyCode["KPAdd"] = "NumpadAdd"
|
|
|
|
nameToJSKeyCode["KPEnter"] = "NumpadEnter"
|
|
|
|
nameToJSKeyCode["KPEqual"] = "NumpadEqual"
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
2015-01-06 18:25:26 +01:00
|
|
|
|
|
|
|
func init() {
|
2019-09-01 15:47:40 +02:00
|
|
|
// TODO: How should we treat modifier keys? Now 'left' modifier keys are available.
|
2017-11-09 16:35:46 +01:00
|
|
|
keyCodeToNameEdge = map[int]string{
|
2017-04-13 20:09:00 +02:00
|
|
|
0xbc: "Comma",
|
|
|
|
0xbe: "Period",
|
2019-09-01 15:47:40 +02:00
|
|
|
0x12: "LeftAlt",
|
2017-04-10 13:38:23 +02:00
|
|
|
0x14: "CapsLock",
|
2019-09-01 15:47:40 +02:00
|
|
|
0x11: "LeftControl",
|
|
|
|
0x10: "LeftShift",
|
2017-04-10 13:38:23 +02: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",
|
2017-04-13 20:09:00 +02:00
|
|
|
0xde: "Apostrophe",
|
|
|
|
0xbd: "Minus",
|
|
|
|
0xbf: "Slash",
|
|
|
|
0xba: "Semicolon",
|
|
|
|
0xbb: "Equal",
|
|
|
|
0xdb: "LeftBracket",
|
|
|
|
0xdc: "Backslash",
|
|
|
|
0xdd: "RightBracket",
|
|
|
|
0xc0: "GraveAccent",
|
2017-04-10 13:38:23 +02:00
|
|
|
0x08: "Backspace",
|
2018-04-13 19:29:18 +02:00
|
|
|
0x90: "NumLock",
|
|
|
|
0x6e: "KPDecimal",
|
|
|
|
0x6f: "KPDivide",
|
|
|
|
0x6a: "KPMultiply",
|
|
|
|
0x6d: "KPSubtract",
|
|
|
|
0x6b: "KPAdd",
|
2018-04-21 18:36:28 +02:00
|
|
|
0x13: "Pause",
|
|
|
|
0x91: "ScrollLock",
|
|
|
|
0x5d: "Menu",
|
|
|
|
|
2018-04-21 18:39:28 +02:00
|
|
|
// On Edge, this key does not work. PrintScreen works only on keyup event.
|
|
|
|
// 0x2C: "PrintScreen",
|
2018-04-13 19:29:18 +02:00
|
|
|
|
2018-04-21 18:39:28 +02:00
|
|
|
// On Edge, it is impossible to tell KPEnter and Enter / KPEqual and Equal.
|
2018-04-13 19:29:18 +02:00
|
|
|
// 0x0d: "KPEnter",
|
|
|
|
// 0x0c: "KPEqual",
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
// ASCII: 0 - 9
|
|
|
|
for c := '0'; c <= '9'; c++ {
|
2017-11-09 16:35:46 +01:00
|
|
|
keyCodeToNameEdge[int(c)] = string(c)
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
// ASCII: A - Z
|
|
|
|
for c := 'A'; c <= 'Z'; c++ {
|
2017-11-09 16:35:46 +01:00
|
|
|
keyCodeToNameEdge[int(c)] = string(c)
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
// Function keys
|
|
|
|
for i := 1; i <= 12; i++ {
|
2017-11-09 16:35:46 +01:00
|
|
|
keyCodeToNameEdge[0x70+i-1] = "F" + strconv.Itoa(i)
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
2018-04-13 19:29:18 +02:00
|
|
|
// Numpad keys
|
|
|
|
for c := '0'; c <= '9'; c++ {
|
|
|
|
keyCodeToNameEdge[0x60+int(c-'0')] = "KP" + string(c)
|
|
|
|
}
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const ebitenKeysTmpl = `{{.License}}
|
|
|
|
|
2018-03-07 18:47:59 +01:00
|
|
|
{{.DoNotEdit}}
|
2015-01-07 15:21:52 +01:00
|
|
|
|
2015-01-06 18:25:26 +01:00
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
2018-04-10 18:00:22 +02:00
|
|
|
"strings"
|
|
|
|
|
2019-03-30 15:28:07 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2015-01-06 18:25:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Key represents a keyboard key.
|
2017-04-10 03:45:04 +02:00
|
|
|
// These keys represent pysical keys of US keyboard.
|
2017-07-23 15:49:04 +02:00
|
|
|
// For example, KeyQ represents Q key on US keyboards and ' (quote) key on Dvorak keyboards.
|
2015-01-06 18:25:26 +01:00
|
|
|
type Key int
|
|
|
|
|
2018-04-13 19:29:18 +02:00
|
|
|
// Keys.
|
2015-01-06 18:25:26 +01:00
|
|
|
const (
|
2019-09-01 15:47:40 +02:00
|
|
|
{{range $index, $name := .EbitenKeyNamesWithoutMods}}Key{{$name}} Key = Key(driver.Key{{$name}})
|
|
|
|
{{end}} KeyAlt Key = Key(driver.KeyReserved0)
|
|
|
|
KeyControl Key = Key(driver.KeyReserved1)
|
|
|
|
KeyShift Key = Key(driver.KeyReserved2)
|
|
|
|
KeyMax Key = KeyShift
|
2015-01-06 18:25:26 +01:00
|
|
|
)
|
2018-04-10 18:00:22 +02:00
|
|
|
|
2019-09-01 15:47:40 +02:00
|
|
|
func (k Key) isValid() bool {
|
|
|
|
switch k {
|
|
|
|
{{range $name := .EbitenKeyNames}}case Key{{$name}}:
|
|
|
|
return true
|
|
|
|
{{end}}
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:20:43 +02:00
|
|
|
// String returns a string representing the key.
|
|
|
|
//
|
|
|
|
// If k is an undefined key, String returns an empty string.
|
|
|
|
func (k Key) String() string {
|
|
|
|
switch k {
|
2019-09-01 14:12:29 +02:00
|
|
|
{{range $name := .EbitenKeyNames}}case Key{{$name}}:
|
2018-04-13 20:20:43 +02:00
|
|
|
return {{$name | printf "%q"}}
|
|
|
|
{{end}}}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-04-10 18:00:22 +02:00
|
|
|
func keyNameToKey(name string) (Key, bool) {
|
|
|
|
switch strings.ToLower(name) {
|
2019-09-01 14:12:29 +02:00
|
|
|
{{range $name := .EbitenKeyNames}}case {{$name | printf "%q" | ToLower}}:
|
2018-04-10 18:00:22 +02:00
|
|
|
return Key{{$name}}, true
|
|
|
|
{{end}}}
|
|
|
|
return 0, false
|
|
|
|
}
|
2015-01-06 18:25:26 +01:00
|
|
|
`
|
|
|
|
|
2019-03-30 15:28:07 +01:00
|
|
|
const driverKeysTmpl = `{{.License}}
|
2015-01-06 18:25:26 +01:00
|
|
|
|
2018-03-07 18:47:59 +01:00
|
|
|
{{.DoNotEdit}}
|
2015-01-07 15:21:52 +01:00
|
|
|
|
2019-03-30 15:28:07 +01:00
|
|
|
package driver
|
2015-01-06 18:25:26 +01:00
|
|
|
|
|
|
|
type Key int
|
|
|
|
|
|
|
|
const (
|
2019-09-01 15:47:40 +02:00
|
|
|
{{range $index, $name := .DriverKeyNames}}Key{{$name}}{{if eq $index 0}} Key = iota{{end}}
|
|
|
|
{{end}} KeyReserved0
|
|
|
|
KeyReserved1
|
|
|
|
KeyReserved2
|
2015-01-06 18:25:26 +01:00
|
|
|
)
|
|
|
|
`
|
|
|
|
|
2019-09-02 17:45:27 +02:00
|
|
|
const eventKeysTmpl = `{{.License}}
|
|
|
|
|
|
|
|
{{.DoNotEdit}}
|
|
|
|
|
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Key int
|
|
|
|
|
|
|
|
const (
|
|
|
|
{{range $index, $name := .DriverKeyNames}}Key{{$name}} Key = Key(driver.Key{{$name}})
|
|
|
|
{{end}}
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
const uidriverGlfwKeysTmpl = `{{.License}}
|
2015-01-06 18:25:26 +01:00
|
|
|
|
2018-03-07 18:47:59 +01:00
|
|
|
{{.DoNotEdit}}
|
2015-01-07 15:21:52 +01:00
|
|
|
|
2016-05-13 17:36:41 +02:00
|
|
|
{{.BuildTag}}
|
2015-01-06 18:25:26 +01:00
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
package glfw
|
2015-01-06 18:25:26 +01:00
|
|
|
|
|
|
|
import (
|
2019-03-30 15:28:07 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2018-12-29 11:12:11 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/glfw"
|
2015-01-06 18:25:26 +01:00
|
|
|
)
|
|
|
|
|
2019-03-30 15:28:07 +01:00
|
|
|
var glfwKeyCodeToKey = map[glfw.Key]driver.Key{
|
2019-09-01 15:47:40 +02:00
|
|
|
{{range $index, $name := .DriverKeyNames}}glfw.Key{{$name}}: driver.Key{{$name}},
|
2015-01-06 18:25:26 +01:00
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
const uidriverJsKeysTmpl = `{{.License}}
|
2015-01-06 18:25:26 +01:00
|
|
|
|
2018-03-07 18:47:59 +01:00
|
|
|
{{.DoNotEdit}}
|
2015-01-07 15:21:52 +01:00
|
|
|
|
2016-05-13 17:36:41 +02:00
|
|
|
{{.BuildTag}}
|
2015-01-06 18:25:26 +01:00
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
package js
|
2015-01-06 18:25:26 +01:00
|
|
|
|
2019-03-30 15:53:27 +01:00
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
|
|
|
)
|
|
|
|
|
2019-09-01 17:25:39 +02:00
|
|
|
var keyToCode = map[driver.Key]string{
|
|
|
|
{{range $name, $code := .NameToJSKeyCode}}driver.Key{{$name}}: {{$code | printf "%q"}},
|
2017-04-13 20:02:38 +02:00
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:53:27 +01:00
|
|
|
var keyCodeToKeyEdge = map[int]driver.Key{
|
2019-03-30 15:28:07 +01:00
|
|
|
{{range $code, $name := .KeyCodeToNameEdge}}{{$code}}: driver.Key{{$name}},
|
2015-01-06 18:25:26 +01:00
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-12-29 11:12:11 +01:00
|
|
|
const glfwKeysTmpl = `{{.License}}
|
|
|
|
|
|
|
|
{{.DoNotEdit}}
|
|
|
|
|
|
|
|
{{.BuildTag}}
|
|
|
|
|
|
|
|
package glfw
|
|
|
|
|
|
|
|
const (
|
|
|
|
{{range $name, $key := .NameToGLFWKeys}}Key{{$name}} = Key({{$key}})
|
|
|
|
{{end}}
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
2019-09-01 11:07:11 +02:00
|
|
|
func digitKey(name string) int {
|
2015-01-07 03:00:15 +01:00
|
|
|
if len(name) != 1 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
c := name[0]
|
|
|
|
if c < '0' || '9' < c {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return int(c - '0')
|
|
|
|
}
|
|
|
|
|
2019-09-01 11:07:11 +02:00
|
|
|
func alphabetKey(name string) rune {
|
2015-01-07 03:00:15 +01:00
|
|
|
if len(name) != 1 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
c := rune(name[0])
|
|
|
|
if c < 'A' || 'Z' < c {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-09-01 11:07:11 +02:00
|
|
|
func functionKey(name string) int {
|
2015-01-07 03:00:15 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-01 11:07:11 +02:00
|
|
|
func keyNamesLess(k []string) func(i, j int) bool {
|
|
|
|
return func(i, j int) bool {
|
|
|
|
k0, k1 := k[i], k[j]
|
|
|
|
d0, d1 := digitKey(k0), digitKey(k1)
|
|
|
|
a0, a1 := alphabetKey(k0), alphabetKey(k1)
|
|
|
|
f0, f1 := functionKey(k0), functionKey(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
|
2015-01-07 03:00:15 +01:00
|
|
|
}
|
|
|
|
if d1 != -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if a1 != -1 {
|
2019-09-01 11:07:11 +02:00
|
|
|
return false
|
2015-01-07 03:00:15 +01:00
|
|
|
}
|
2019-09-01 11:07:11 +02:00
|
|
|
if f0 != -1 && f1 != -1 {
|
|
|
|
return f0 < f1
|
|
|
|
}
|
|
|
|
return k0 < k1
|
2015-01-07 03:00:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 12:21:19 +02:00
|
|
|
const license = `// Copyright 2013 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
`
|
2015-01-06 18:25:26 +01:00
|
|
|
|
2018-07-28 12:21:19 +02:00
|
|
|
func main() {
|
2018-03-07 18:47:59 +01:00
|
|
|
// Follow the standard comment rule (https://golang.org/s/generatedcode).
|
2018-04-20 09:54:33 +02:00
|
|
|
doNotEdit := "// Code generated by genkeys.go using 'go generate'. DO NOT EDIT."
|
2015-01-07 15:21:52 +01:00
|
|
|
|
2019-09-01 14:12:29 +02:00
|
|
|
ebitenKeyNames := []string{}
|
|
|
|
ebitenKeyNamesWithoutMods := []string{}
|
2019-09-01 15:47:40 +02:00
|
|
|
driverKeyNames := []string{}
|
2019-09-01 17:25:39 +02:00
|
|
|
for name := range nameToJSKeyCode {
|
2019-09-01 15:47:40 +02:00
|
|
|
driverKeyNames = append(driverKeyNames, name)
|
|
|
|
if !strings.HasSuffix(name, "Alt") && !strings.HasSuffix(name, "Control") && !strings.HasSuffix(name, "Shift") {
|
|
|
|
ebitenKeyNames = append(ebitenKeyNames, name)
|
2019-09-01 14:12:29 +02:00
|
|
|
ebitenKeyNamesWithoutMods = append(ebitenKeyNamesWithoutMods, name)
|
2019-09-01 15:47:40 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if name == "LeftAlt" {
|
|
|
|
ebitenKeyNames = append(ebitenKeyNames, "Alt")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if name == "LeftControl" {
|
|
|
|
ebitenKeyNames = append(ebitenKeyNames, "Control")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if name == "LeftShift" {
|
|
|
|
ebitenKeyNames = append(ebitenKeyNames, "Shift")
|
|
|
|
continue
|
2015-01-07 15:18:40 +01:00
|
|
|
}
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
2017-04-13 20:02:38 +02:00
|
|
|
|
2019-09-01 14:12:29 +02:00
|
|
|
sort.Slice(ebitenKeyNames, keyNamesLess(ebitenKeyNames))
|
|
|
|
sort.Slice(ebitenKeyNamesWithoutMods, keyNamesLess(ebitenKeyNamesWithoutMods))
|
2019-09-01 15:47:40 +02:00
|
|
|
sort.Slice(driverKeyNames, keyNamesLess(driverKeyNames))
|
2015-01-06 18:25:26 +01:00
|
|
|
|
|
|
|
for path, tmpl := range map[string]string{
|
2019-09-02 17:45:27 +02:00
|
|
|
"event/keys.go": eventKeysTmpl,
|
2019-04-07 11:55:56 +02:00
|
|
|
"internal/driver/keys.go": driverKeysTmpl,
|
|
|
|
"internal/glfw/keys.go": glfwKeysTmpl,
|
|
|
|
"internal/uidriver/glfw/keys.go": uidriverGlfwKeysTmpl,
|
|
|
|
"internal/uidriver/js/keys.go": uidriverJsKeysTmpl,
|
2019-09-02 17:45:27 +02:00
|
|
|
"keys.go": ebitenKeysTmpl,
|
2015-01-06 18:25:26 +01:00
|
|
|
} {
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2018-04-10 18:00:22 +02:00
|
|
|
|
|
|
|
funcs := template.FuncMap{
|
|
|
|
"ToLower": strings.ToLower,
|
|
|
|
}
|
|
|
|
tmpl, err := template.New(path).Funcs(funcs).Parse(tmpl)
|
2015-01-06 18:25:26 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-04-10 18:00:22 +02:00
|
|
|
|
2016-05-13 17:36:41 +02:00
|
|
|
// The build tag can't be included in the templates because of `go vet`.
|
|
|
|
// Pass the build tag and extract this in the template to make `go vet` happy.
|
|
|
|
buildTag := ""
|
|
|
|
switch path {
|
2019-04-07 11:55:56 +02:00
|
|
|
case "internal/uidriver/glfw/keys.go":
|
2017-05-11 12:09:13 +02:00
|
|
|
buildTag = "// +build darwin freebsd linux windows" +
|
2016-06-27 19:51:50 +02:00
|
|
|
"\n// +build !js" +
|
|
|
|
"\n// +build !android" +
|
|
|
|
"\n// +build !ios"
|
2019-04-07 11:55:56 +02:00
|
|
|
case "internal/uidriver/js/keys.go":
|
2016-05-13 17:36:41 +02:00
|
|
|
buildTag = "// +build js"
|
|
|
|
}
|
2015-01-06 18:25:26 +01:00
|
|
|
// NOTE: According to godoc, maps are automatically sorted by key.
|
2019-09-01 11:07:11 +02:00
|
|
|
if err := tmpl.Execute(f, struct {
|
2019-09-01 14:12:29 +02:00
|
|
|
License string
|
|
|
|
DoNotEdit string
|
|
|
|
BuildTag string
|
2019-09-01 17:25:39 +02:00
|
|
|
NameToJSKeyCode map[string]string
|
2019-09-01 14:12:29 +02:00
|
|
|
KeyCodeToNameEdge map[int]string
|
|
|
|
EbitenKeyNames []string
|
|
|
|
EbitenKeyNamesWithoutMods []string
|
2019-09-01 15:47:40 +02:00
|
|
|
DriverKeyNames []string
|
2019-09-01 14:12:29 +02:00
|
|
|
NameToGLFWKeys map[string]glfw.Key
|
2019-09-01 11:07:11 +02:00
|
|
|
}{
|
2019-09-01 14:12:29 +02:00
|
|
|
License: license,
|
|
|
|
DoNotEdit: doNotEdit,
|
|
|
|
BuildTag: buildTag,
|
2019-09-01 17:25:39 +02:00
|
|
|
NameToJSKeyCode: nameToJSKeyCode,
|
2019-09-01 14:12:29 +02:00
|
|
|
KeyCodeToNameEdge: keyCodeToNameEdge,
|
|
|
|
EbitenKeyNames: ebitenKeyNames,
|
|
|
|
EbitenKeyNamesWithoutMods: ebitenKeyNamesWithoutMods,
|
2019-09-01 15:47:40 +02:00
|
|
|
DriverKeyNames: driverKeyNames,
|
2019-09-01 14:12:29 +02:00
|
|
|
NameToGLFWKeys: nameToGLFWKeys,
|
2015-01-18 13:11:03 +01:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-01-06 18:25:26 +01:00
|
|
|
}
|
|
|
|
}
|