forked from hajimehoshi/ebiten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genkeys.go
762 lines (679 loc) · 19.7 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
// 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 (
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/template"
"github.com/go-gl/glfw/v3.3/glfw"
"golang.org/x/mobile/event/key"
)
var (
nameToGLFWKey map[string]glfw.Key
androidKeyToDriverKeyName map[int]string
gbuildKeyToDriverKeyName map[key.Code]string
driverKeyNameToJSKey map[string]string
edgeKeyCodeToName map[int]string
)
func init() {
nameToGLFWKey = 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,
"KPDecimal": glfw.KeyKPDecimal,
"KPDivide": glfw.KeyKPDivide,
"KPMultiply": glfw.KeyKPMultiply,
"KPSubtract": glfw.KeyKPSubtract,
"KPAdd": glfw.KeyKPAdd,
"KPEnter": glfw.KeyKPEnter,
"KPEqual": glfw.KeyKPEqual,
"Last": glfw.KeyLast,
}
// https://developer.android.com/reference/android/view/KeyEvent
androidKeyToDriverKeyName = map[int]string{
55: "Comma",
56: "Period",
57: "LeftAlt",
58: "RightAlt",
115: "CapsLock",
113: "LeftControl",
114: "RightControl",
59: "LeftShift",
60: "RightShift",
66: "Enter",
62: "Space",
61: "Tab",
112: "Delete", // KEYCODE_FORWARD_DEL
123: "End",
122: "Home",
124: "Insert",
93: "PageDown",
92: "PageUp",
20: "Down",
21: "Left",
22: "Right",
19: "Up",
111: "Escape",
67: "Backspace", // KEYCODE_DEL
75: "Apostrophe",
69: "Minus",
76: "Slash",
74: "Semicolon",
70: "Equal",
71: "LeftBracket",
73: "Backslash",
72: "RightBracket",
68: "GraveAccent",
143: "NumLock",
121: "Pause", // KEYCODE_BREAK
120: "PrintScreen", // KEYCODE_SYSRQ
116: "ScrollLock",
82: "Menu",
158: "KPDecimal",
154: "KPDivide",
155: "KPMultiply",
156: "KPSubtract",
157: "KPAdd",
160: "KPEnter",
161: "KPEqual",
117: "LeftSuper", // KEYCODE_META_LEFT
118: "RightSuper", // KEYCODE_META_RIGHT
}
gbuildKeyToDriverKeyName = map[key.Code]string{
key.CodeComma: "Comma",
key.CodeFullStop: "Period",
key.CodeLeftAlt: "LeftAlt",
key.CodeRightAlt: "RightAlt",
key.CodeCapsLock: "CapsLock",
key.CodeLeftControl: "LeftControl",
key.CodeRightControl: "RightControl",
key.CodeLeftShift: "LeftShift",
key.CodeRightShift: "RightShift",
key.CodeReturnEnter: "Enter",
key.CodeSpacebar: "Space",
key.CodeTab: "Tab",
key.CodeDeleteForward: "Delete",
key.CodeEnd: "End",
key.CodeHome: "Home",
key.CodeInsert: "Insert",
key.CodePageDown: "PageDown",
key.CodePageUp: "PageUp",
key.CodeDownArrow: "Down",
key.CodeLeftArrow: "Left",
key.CodeRightArrow: "Right",
key.CodeUpArrow: "Up",
key.CodeEscape: "Escape",
key.CodeDeleteBackspace: "Backspace",
key.CodeApostrophe: "Apostrophe",
key.CodeHyphenMinus: "Minus",
key.CodeSlash: "Slash",
key.CodeSemicolon: "Semicolon",
key.CodeEqualSign: "Equal",
key.CodeLeftSquareBracket: "LeftBracket",
key.CodeBackslash: "Backslash",
key.CodeRightSquareBracket: "RightBracket",
key.CodeGraveAccent: "GraveAccent",
key.CodeKeypadNumLock: "NumLock",
key.CodePause: "Pause",
key.CodeKeypadFullStop: "KPDecimal",
key.CodeKeypadSlash: "KPDivide",
key.CodeKeypadAsterisk: "KPMultiply",
key.CodeKeypadHyphenMinus: "KPSubtract",
key.CodeKeypadPlusSign: "KPAdd",
key.CodeKeypadEnter: "KPEnter",
key.CodeKeypadEqualSign: "KPEqual",
key.CodeLeftGUI: "LeftSuper",
key.CodeRightGUI: "RightSuper",
// Missing keys:
// driver.KeyPrintScreen
// driver.KeyScrollLock
// driver.KeyMenu
}
driverKeyNameToJSKey = 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",
"KPDecimal": "NumpadDecimal",
"KPDivide": "NumpadDivide",
"KPMultiply": "NumpadMultiply",
"KPSubtract": "NumpadSubtract",
"KPAdd": "NumpadAdd",
"KPEnter": "NumpadEnter",
"KPEqual": "NumpadEqual",
"LeftSuper": "MetaLeft",
"RightSuper": "MetaRight",
}
// ASCII: 0 - 9
for c := '0'; c <= '9'; c++ {
nameToGLFWKey[string(c)] = glfw.Key0 + glfw.Key(c) - '0'
androidKeyToDriverKeyName[7+int(c)-'0'] = string(c)
// Gomobile's key code (= USB HID key codes) has successive key codes for 1, 2, ..., 9, 0
// in this order.
if c == '0' {
gbuildKeyToDriverKeyName[key.Code0] = string(c)
} else {
gbuildKeyToDriverKeyName[key.Code1+key.Code(c)-'1'] = string(c)
}
driverKeyNameToJSKey[string(c)] = "Digit" + string(c)
}
// ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ {
nameToGLFWKey[string(c)] = glfw.KeyA + glfw.Key(c) - 'A'
androidKeyToDriverKeyName[29+int(c)-'A'] = string(c)
gbuildKeyToDriverKeyName[key.CodeA+key.Code(c)-'A'] = string(c)
driverKeyNameToJSKey[string(c)] = "Key" + string(c)
}
// Function keys
for i := 1; i <= 12; i++ {
name := "F" + strconv.Itoa(i)
nameToGLFWKey[name] = glfw.KeyF1 + glfw.Key(i) - 1
androidKeyToDriverKeyName[131+i-1] = name
gbuildKeyToDriverKeyName[key.CodeF1+key.Code(i)-1] = name
driverKeyNameToJSKey[name] = name
}
// Numpad
// https://www.w3.org/TR/uievents-code/#key-numpad-section
for c := '0'; c <= '9'; c++ {
name := "KP" + string(c)
nameToGLFWKey[name] = glfw.KeyKP0 + glfw.Key(c) - '0'
androidKeyToDriverKeyName[144+int(c)-'0'] = name
// Gomobile's key code (= USB HID key codes) has successive key codes for 1, 2, ..., 9, 0
// in this order.
if c == '0' {
gbuildKeyToDriverKeyName[key.CodeKeypad0] = name
} else {
gbuildKeyToDriverKeyName[key.CodeKeypad1+key.Code(c)-'1'] = name
}
driverKeyNameToJSKey[name] = "Numpad" + string(c)
}
}
func init() {
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
// TODO: How should we treat modifier keys? Now 'left' modifier keys are available.
edgeKeyCodeToName = map[int]string{
0xbc: "Comma",
0xbe: "Period",
0x12: "LeftAlt",
0x14: "CapsLock",
0x11: "LeftControl",
0x10: "LeftShift",
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",
0xde: "Apostrophe",
0xbd: "Minus",
0xbf: "Slash",
0xba: "Semicolon",
0xbb: "Equal",
0xdb: "LeftBracket",
0xdc: "Backslash",
0xdd: "RightBracket",
0xc0: "GraveAccent",
0x08: "Backspace",
0x90: "NumLock",
0x6e: "KPDecimal",
0x6f: "KPDivide",
0x6a: "KPMultiply",
0x6d: "KPSubtract",
0x6b: "KPAdd",
0x13: "Pause",
0x91: "ScrollLock",
0x5d: "Menu",
0x5b: "LeftSuper",
0x5c: "RightSuper",
// On Edge, this key does not work. PrintScreen works only on keyup event.
// 0x2C: "PrintScreen",
// On Edge, it is impossible to tell KPEnter and Enter / KPEqual and Equal.
// 0x0d: "KPEnter",
// 0x0c: "KPEqual",
}
// ASCII: 0 - 9
for c := '0'; c <= '9'; c++ {
edgeKeyCodeToName[int(c)] = string(c)
}
// ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ {
edgeKeyCodeToName[int(c)] = string(c)
}
// Function keys
for i := 1; i <= 12; i++ {
edgeKeyCodeToName[0x70+i-1] = "F" + strconv.Itoa(i)
}
// Numpad keys
for c := '0'; c <= '9'; c++ {
edgeKeyCodeToName[0x60+int(c-'0')] = "KP" + string(c)
}
}
const ebitenKeysTmpl = `{{.License}}
{{.DoNotEdit}}
package ebiten
import (
"strings"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
)
// A Key represents a keyboard key.
// These keys represent pysical keys of US keyboard.
// For example, KeyQ represents Q key on US keyboards and ' (quote) key on Dvorak keyboards.
type Key int
// Keys.
const (
{{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)
KeySuper Key = Key(driver.KeyReserved3)
KeyMax Key = KeySuper
)
func (k Key) isValid() bool {
switch k {
{{range $name := .EbitenKeyNames}}case Key{{$name}}:
return true
{{end}}
default:
return false
}
}
// 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 {
{{range $name := .EbitenKeyNames}}case Key{{$name}}:
return {{$name | printf "%q"}}
{{end}}}
return ""
}
func keyNameToKeyCode(name string) (Key, bool) {
switch strings.ToLower(name) {
{{range $name := .EbitenKeyNames}}case {{$name | printf "%q" | ToLower}}:
return Key{{$name}}, true
{{end}}}
return 0, false
}
`
const driverKeysTmpl = `{{.License}}
{{.DoNotEdit}}
package driver
import (
"fmt"
)
type Key int
const (
{{range $index, $name := .DriverKeyNames}}Key{{$name}}{{if eq $index 0}} Key = iota{{end}}
{{end}} KeyReserved0
KeyReserved1
KeyReserved2
KeyReserved3
)
func (k Key) String() string {
switch k {
{{range $index, $name := .DriverKeyNames}}case Key{{$name}}:
return {{$name | printf "Key%s" | printf "%q"}}
{{end}}}
panic(fmt.Sprintf("driver: invalid key: %d", k))
}
`
const eventKeysTmpl = `{{.License}}
{{.DoNotEdit}}
package event
import (
"github.com/hajimehoshi/ebiten/v2/internal/driver"
)
type Key = driver.Key
const (
{{range $index, $name := .DriverKeyNames}}Key{{$name}} = driver.Key{{$name}}
{{end}}
)
`
const uidriverGlfwKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
package glfw
import (
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
)
var glfwKeyToDriverKey = map[glfw.Key]driver.Key{
{{range $index, $name := .DriverKeyNames}}glfw.Key{{$name}}: driver.Key{{$name}},
{{end}}
}
var driverKeyToGLFWKey = map[driver.Key]glfw.Key{
{{range $index, $name := .DriverKeyNames}}driver.Key{{$name}}: glfw.Key{{$name}},
{{end}}
}
`
const uidriverJsKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
package js
import (
"syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
)
var driverKeyToJSKey = map[driver.Key]js.Value{
{{range $name, $code := .DriverKeyNameToJSKey}}driver.Key{{$name}}: js.ValueOf({{$code | printf "%q"}}),
{{end}}
}
var edgeKeyCodeToDriverKey = map[int]driver.Key{
{{range $code, $name := .EdgeKeyCodeToName}}{{$code}}: driver.Key{{$name}},
{{end}}
}
`
const glfwKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
package glfw
const (
{{range $name, $key := .NameToGLFWKey}}Key{{$name}} = Key({{$key}})
{{end}}
)
`
const mobileAndroidKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
package ebitenmobileview
import (
"github.com/hajimehoshi/ebiten/v2/internal/driver"
)
var androidKeyToDriverKey = map[int]driver.Key{
{{range $key, $name := .AndroidKeyToDriverKeyName}}{{$key}}: driver.Key{{$name}},
{{end}}
}
`
const mobileGBuildKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
package mobile
import (
"golang.org/x/mobile/event/key"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
)
var gbuildKeyToDriverKey = map[key.Code]driver.Key{
{{range $key, $name := .GBuildKeyToDriverKeyName}}key.{{$key}}: driver.Key{{$name}},
{{end}}
}
`
func digitKey(name string) int {
if len(name) != 1 {
return -1
}
c := name[0]
if c < '0' || '9' < c {
return -1
}
return int(c - '0')
}
func alphabetKey(name string) rune {
if len(name) != 1 {
return -1
}
c := rune(name[0])
if c < 'A' || 'Z' < c {
return -1
}
return c
}
func functionKey(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 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
}
if d1 != -1 {
return false
}
if a1 != -1 {
return false
}
if f0 != -1 && f1 != -1 {
return f0 < f1
}
return k0 < k1
}
}
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.
`
func main() {
// Follow the standard comment rule (https://golang.org/s/generatedcode).
doNotEdit := "// Code generated by genkeys.go using 'go generate'. DO NOT EDIT."
ebitenKeyNames := []string{}
ebitenKeyNamesWithoutMods := []string{}
driverKeyNames := []string{}
for name := range driverKeyNameToJSKey {
driverKeyNames = append(driverKeyNames, name)
if !strings.HasSuffix(name, "Alt") && !strings.HasSuffix(name, "Control") && !strings.HasSuffix(name, "Shift") && !strings.HasSuffix(name, "Super") {
ebitenKeyNames = append(ebitenKeyNames, name)
ebitenKeyNamesWithoutMods = append(ebitenKeyNamesWithoutMods, name)
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
}
if name == "LeftSuper" {
ebitenKeyNames = append(ebitenKeyNames, "Super")
continue
}
}
sort.Slice(ebitenKeyNames, keyNamesLess(ebitenKeyNames))
sort.Slice(ebitenKeyNamesWithoutMods, keyNamesLess(ebitenKeyNamesWithoutMods))
sort.Slice(driverKeyNames, keyNamesLess(driverKeyNames))
// TODO: Add this line for event package (#926).
//
// filepath.Join("event", "keys.go"): eventKeysTmpl,
for path, tmpl := range map[string]string{
filepath.Join("internal", "driver", "keys.go"): driverKeysTmpl,
filepath.Join("internal", "glfw", "keys.go"): glfwKeysTmpl,
filepath.Join("internal", "uidriver", "glfw", "keys.go"): uidriverGlfwKeysTmpl,
filepath.Join("internal", "uidriver", "mobile", "keys.go"): mobileGBuildKeysTmpl,
filepath.Join("internal", "uidriver", "js", "keys_js.go"): uidriverJsKeysTmpl,
filepath.Join("keys.go"): ebitenKeysTmpl,
filepath.Join("mobile", "ebitenmobileview", "keys_android.go"): mobileAndroidKeysTmpl,
} {
f, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
funcs := template.FuncMap{
"ToLower": strings.ToLower,
}
tmpl, err := template.New(path).Funcs(funcs).Parse(tmpl)
if err != nil {
log.Fatal(err)
}
// 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 {
case filepath.Join("internal", "glfw", "keys.go"):
buildTag = "// +build !js"
case filepath.Join("internal", "uidriver", "glfw", "keys.go"):
buildTag = "// +build darwin freebsd linux windows" +
"\n// +build !android" +
"\n// +build !ios"
}
// NOTE: According to godoc, maps are automatically sorted by key.
if err := tmpl.Execute(f, struct {
License string
DoNotEdit string
BuildTag string
DriverKeyNameToJSKey map[string]string
EdgeKeyCodeToName map[int]string
EbitenKeyNames []string
EbitenKeyNamesWithoutMods []string
DriverKeyNames []string
NameToGLFWKey map[string]glfw.Key
AndroidKeyToDriverKeyName map[int]string
GBuildKeyToDriverKeyName map[key.Code]string
}{
License: license,
DoNotEdit: doNotEdit,
BuildTag: buildTag,
DriverKeyNameToJSKey: driverKeyNameToJSKey,
EdgeKeyCodeToName: edgeKeyCodeToName,
EbitenKeyNames: ebitenKeyNames,
EbitenKeyNamesWithoutMods: ebitenKeyNamesWithoutMods,
DriverKeyNames: driverKeyNames,
NameToGLFWKey: nameToGLFWKey,
AndroidKeyToDriverKeyName: androidKeyToDriverKeyName,
GBuildKeyToDriverKeyName: gbuildKeyToDriverKeyName,
}); err != nil {
log.Fatal(err)
}
}
}