glfw: Start implementing glfw package (Key)

This commit is contained in:
Hajime Hoshi 2018-12-29 19:12:11 +09:00
parent 873128cc7e
commit 1fd445b0e4
4 changed files with 236 additions and 4 deletions

View File

@ -28,14 +28,63 @@ import (
"strconv"
"strings"
"text/template"
"github.com/go-gl/glfw/v3.2/glfw"
)
var (
nameToGLFWKeys map[string]glfw.Key
nameToJSKeyCodes map[string][]string
keyCodeToNameEdge map[int]string
)
func init() {
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,
}
nameToJSKeyCodes = map[string][]string{
"Comma": {"Comma"},
"Period": {"Period"},
@ -75,21 +124,36 @@ func init() {
}
// ASCII: 0 - 9
for c := '0'; c <= '9'; c++ {
nameToGLFWKeys[string(c)] = glfw.Key0 + glfw.Key(c) - '0'
nameToJSKeyCodes[string(c)] = []string{"Digit" + string(c)}
}
// ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ {
nameToGLFWKeys[string(c)] = glfw.KeyA + glfw.Key(c) - 'A'
nameToJSKeyCodes[string(c)] = []string{"Key" + string(c)}
}
// Function keys
for i := 1; i <= 12; i++ {
nameToJSKeyCodes["F"+strconv.Itoa(i)] = []string{"F" + strconv.Itoa(i)}
name := "F" + strconv.Itoa(i)
nameToGLFWKeys[name] = glfw.KeyF1 + glfw.Key(i) - 1
nameToJSKeyCodes[name] = []string{name}
}
// Numpad
// https://www.w3.org/TR/uievents-code/#key-numpad-section
for c := '0'; c <= '9'; c++ {
nameToJSKeyCodes["KP"+string(c)] = []string{"Numpad" + string(c)}
name := "KP" + string(c)
nameToGLFWKeys[name] = glfw.KeyKP0 + glfw.Key(c) - '0'
nameToJSKeyCodes[name] = []string{"Numpad" + string(c)}
}
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
nameToJSKeyCodes["KPDecimal"] = []string{"NumpadDecimal"}
nameToJSKeyCodes["KPDivide"] = []string{"NumpadDivide"}
nameToJSKeyCodes["KPMultiply"] = []string{"NumpadMultiply"}
@ -232,7 +296,7 @@ const inputKeysGlfwTmpl = `{{.License}}
package input
import (
glfw "github.com/go-gl/glfw/v3.2/glfw"
"github.com/hajimehoshi/ebiten/internal/glfw"
)
var glfwKeyCodeToKey = map[glfw.Key]Key{
@ -268,6 +332,20 @@ var keyCodeToKeyEdge = map[int]Key{
}
`
const glfwKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
package glfw
const (
{{range $name, $key := .NameToGLFWKeys}}Key{{$name}} = Key({{$key}})
{{end}}
)
`
type KeyNames []string
func (k KeyNames) digit(name string) int {
@ -393,6 +471,7 @@ func main() {
"internal/input/keys.go": inputKeysTmpl,
"internal/input/keys_glfw.go": inputKeysGlfwTmpl,
"internal/input/keys_js.go": inputKeysJSTmpl,
"internal/glfw/keys.go": glfwKeysTmpl,
} {
f, err := os.Create(path)
if err != nil {
@ -431,6 +510,7 @@ func main() {
"KeyNames": names,
"LastKeyName": names[len(names)-1],
"KeyNamesWithoutMods": namesWithoutMods,
"NameToGLFWKeys": nameToGLFWKeys,
}); err != nil {
log.Fatal(err)
}

23
internal/glfw/glfw.go Normal file
View File

@ -0,0 +1,23 @@
// Copyright 2018 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.
package glfw
import (
"github.com/go-gl/glfw/v3.2/glfw"
)
type (
Key = glfw.Key
)

129
internal/glfw/keys.go Normal file
View File

@ -0,0 +1,129 @@
// 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.
// Code generated by genkeys.go using 'go generate'. DO NOT EDIT.
package glfw
const (
Key0 = Key(48)
Key1 = Key(49)
Key2 = Key(50)
Key3 = Key(51)
Key4 = Key(52)
Key5 = Key(53)
Key6 = Key(54)
Key7 = Key(55)
Key8 = Key(56)
Key9 = Key(57)
KeyA = Key(65)
KeyApostrophe = Key(39)
KeyB = Key(66)
KeyBackslash = Key(92)
KeyBackspace = Key(259)
KeyC = Key(67)
KeyCapsLock = Key(280)
KeyComma = Key(44)
KeyD = Key(68)
KeyDelete = Key(261)
KeyDown = Key(264)
KeyE = Key(69)
KeyEnd = Key(269)
KeyEnter = Key(257)
KeyEqual = Key(61)
KeyEscape = Key(256)
KeyF = Key(70)
KeyF1 = Key(290)
KeyF10 = Key(299)
KeyF11 = Key(300)
KeyF12 = Key(301)
KeyF2 = Key(291)
KeyF3 = Key(292)
KeyF4 = Key(293)
KeyF5 = Key(294)
KeyF6 = Key(295)
KeyF7 = Key(296)
KeyF8 = Key(297)
KeyF9 = Key(298)
KeyG = Key(71)
KeyGraveAccent = Key(96)
KeyH = Key(72)
KeyHome = Key(268)
KeyI = Key(73)
KeyInsert = Key(260)
KeyJ = Key(74)
KeyK = Key(75)
KeyKP0 = Key(320)
KeyKP1 = Key(321)
KeyKP2 = Key(322)
KeyKP3 = Key(323)
KeyKP4 = Key(324)
KeyKP5 = Key(325)
KeyKP6 = Key(326)
KeyKP7 = Key(327)
KeyKP8 = Key(328)
KeyKP9 = Key(329)
KeyKPAdd = Key(334)
KeyKPDecimal = Key(330)
KeyKPDivide = Key(331)
KeyKPEnter = Key(335)
KeyKPEqual = Key(336)
KeyKPMultiply = Key(332)
KeyKPSubtract = Key(333)
KeyL = Key(76)
KeyLast = Key(348)
KeyLeft = Key(263)
KeyLeftAlt = Key(342)
KeyLeftBracket = Key(91)
KeyLeftControl = Key(341)
KeyLeftShift = Key(340)
KeyLeftSuper = Key(343)
KeyM = Key(77)
KeyMenu = Key(348)
KeyMinus = Key(45)
KeyN = Key(78)
KeyNumLock = Key(282)
KeyO = Key(79)
KeyP = Key(80)
KeyPageDown = Key(267)
KeyPageUp = Key(266)
KeyPause = Key(284)
KeyPeriod = Key(46)
KeyPrintScreen = Key(283)
KeyQ = Key(81)
KeyR = Key(82)
KeyRight = Key(262)
KeyRightAlt = Key(346)
KeyRightBracket = Key(93)
KeyRightControl = Key(345)
KeyRightShift = Key(344)
KeyRightSuper = Key(347)
KeyS = Key(83)
KeyScrollLock = Key(281)
KeySemicolon = Key(59)
KeySlash = Key(47)
KeySpace = Key(32)
KeyT = Key(84)
KeyTab = Key(258)
KeyU = Key(85)
KeyUnknown = Key(-1)
KeyUp = Key(265)
KeyV = Key(86)
KeyW = Key(87)
KeyWorld1 = Key(161)
KeyWorld2 = Key(162)
KeyX = Key(88)
KeyY = Key(89)
KeyZ = Key(90)
)

View File

@ -22,7 +22,7 @@
package input
import (
glfw "github.com/go-gl/glfw/v3.2/glfw"
"github.com/hajimehoshi/ebiten/internal/glfw"
)
var glfwKeyCodeToKey = map[glfw.Key]Key{