forked from hajimehoshi/ebiten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenkeys.go
273 lines (234 loc) · 5.15 KB
/
genkeys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// 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
// * It is best to replace keyCode with code, but many browsers don't implement it.
package main
import (
"github.com/hajimehoshi/ebiten/internal"
"log"
"os"
"sort"
"strconv"
"text/template"
)
var keyCodeToName map[int]string
func init() {
keyCodeToName = map[int]string{
0xBC: "Comma",
0xBE: "Period",
0x12: "Alt",
0x14: "CapsLock",
0x11: "Control",
0x10: "Shift",
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",
// The keys not listed in the Mozilla site:
0x08: "Backspace",
}
// ASCII: 0 - 9
for c := '0'; c <= '9'; c++ {
keyCodeToName[int(c)] = string(c)
}
// ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ {
keyCodeToName[int(c)] = string(c)
}
// Function keys
for i := 1; i <= 12; i++ {
keyCodeToName[0x70+i-1] = "F" + strconv.Itoa(i)
}
}
const ebitenKeysTmpl = `{{.License}}
// {{.Notice}}
package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/ui"
)
// A Key represents a keyboard key.
type Key int
// Keys
const (
{{range $index, $name := .KeyNames}}Key{{$name}} = Key(ui.Key{{$name}})
{{end}}
)
`
const uiKeysTmpl = `{{.License}}
// {{.Notice}}
package ui
type Key int
const (
{{range $index, $name := .KeyNames}}Key{{$name}}{{if eq $index 0}} Key = iota{{end}}
{{end}}
)
`
const uiKeysGlfwTmpl = `{{.License}}
// {{.Notice}}
// +build !js
package ui
import (
glfw "github.com/go-gl/glfw/v3.1/glfw"
)
var glfwKeyCodeToKey = map[glfw.Key]Key{
{{range $index, $name := .KeyNamesWithoutMods}}glfw.Key{{$name}}: Key{{$name}},
{{end}}
glfw.KeyLeftAlt: KeyAlt,
glfw.KeyRightAlt: KeyAlt,
glfw.KeyLeftControl: KeyControl,
glfw.KeyRightControl: KeyControl,
glfw.KeyLeftShift: KeyShift,
glfw.KeyRightShift: KeyShift,
}
`
const uiKeysJSTmpl = `{{.License}}
// {{.Notice}}
// +build js
package ui
var keyCodeToKey = map[int]Key{
{{range $code, $name := .KeyCodeToName}}{{$code}}: Key{{$name}},
{{end}}
}
`
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]
}
func main() {
license, err := internal.LicenseComment()
if err != nil {
log.Fatal(err)
}
notice := "DO NOT EDIT: This file is auto-generated by genkeys.go."
names := []string{}
namesWithoutMods := []string{}
codes := []int{}
for code, name := range keyCodeToName {
names = append(names, name)
codes = append(codes, code)
if name != "Alt" && name != "Control" && name != "Shift" {
namesWithoutMods = append(namesWithoutMods, name)
}
}
sort.Sort(KeyNames(names))
sort.Sort(KeyNames(namesWithoutMods))
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.
if err := tmpl.Execute(f, map[string]interface{}{
"License": license,
"Notice": notice,
"KeyCodeToName": keyCodeToName,
"Codes": codes,
"KeyNames": names,
"KeyNamesWithoutMods": namesWithoutMods,
}); err != nil {
log.Fatal(err)
}
}
}