Add genkey.go

This commit is contained in:
Hajime Hoshi 2015-01-07 02:25:26 +09:00
parent 9d12a23172
commit bc26690b14
10 changed files with 489 additions and 39 deletions

View File

@ -19,15 +19,11 @@ import (
)
type Input struct {
states map[ebiten.Key]int
states [256]int
}
func NewInput() *Input {
states := map[ebiten.Key]int{}
for key := ebiten.Key(0); key < ebiten.KeyMax; key++ {
states[key] = 0
}
return &Input{states}
return &Input{}
}
func (i *Input) StateForKey(key ebiten.Key) int {
@ -36,7 +32,7 @@ func (i *Input) StateForKey(key ebiten.Key) int {
func (i *Input) Update() {
for key := range i.states {
if !ebiten.IsKeyPressed(key) {
if !ebiten.IsKeyPressed(ebiten.Key(key)) {
i.states[key] = 0
continue
}

18
generate.go Normal file
View File

@ -0,0 +1,18 @@
// 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.
package ebiten
//go:generate go run genkeys.go
//go:generate gofmt -w .

171
genkeys.go Normal file
View File

@ -0,0 +1,171 @@
// 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
package main
import (
"io/ioutil"
"log"
"os"
"sort"
"strconv"
"strings"
"text/template"
)
var keyCodeToName map[int]string
func init() {
keyCodeToName = map[int]string{
0xBC: "KeyComma",
0xBE: "KeyPeriod",
0x12: "KeyLeftAlt",
0x14: "KeyCapsLock",
0x11: "KeyLeftControl",
0x10: "KeyLeftShift",
0x0D: "KeyEnter",
0x20: "KeySpace",
0x09: "KeyTab",
0x2E: "KeyDelete",
0x23: "KeyEnd",
0x24: "KeyHome",
0x2D: "KeyInsert",
0x22: "KeyPageDown",
0x21: "KeyPageUp",
0x28: "KeyDown",
0x25: "KeyLeft",
0x27: "KeyRight",
0x26: "KeyUp",
0x1B: "KeyEscape",
}
// ASCII: 0 - 9
for c := '0'; c <= '9'; c++ {
keyCodeToName[int(c)] = "Key" + string(c)
}
// ASCII: A - Z
for c := 'A'; c <= 'Z'; c++ {
keyCodeToName[int(c)] = "Key" + string(c)
}
// Function keys
for i := 1; i <= 12; i++ {
keyCodeToName[0x70+i-1] = "KeyF" + strconv.Itoa(i)
}
}
const ebitenKeysTmpl = `{{.License}}
package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/ui"
)
// A Key represents a keyboard key.
type Key int
// Keys
const (
{{range $index, $key := .KeyNames}}{{$key}} = Key(ui.{{$key}})
{{end}}
)
`
const uiKeysTmpl = `{{.License}}
package ui
type Key int
const (
{{range $index, $key := .KeyNames}}{{$key}}{{if eq $index 0}} Key = iota{{end}}
{{end}}
)
`
const uiKeysGlfwTmpl = `{{.License}}
// +build !js
package ui
import (
glfw "github.com/go-gl/glfw3"
)
var glfwKeyCodeToKey = map[glfw.Key]Key{
{{range $index, $key := .KeyNames}}glfw.{{$key}}: {{$key}},
{{end}}
}
`
const uiKeysJSTmpl = `{{.License}}
// +build js
package ui
var keyCodeToKey = map[int]Key{
{{range $code, $name := .Keys}}{{$code}}: {{$name}},
{{end}}
}
`
func main() {
l, err := ioutil.ReadFile("license.txt")
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(l), "\n")
license := "// " + strings.Join(lines[:len(lines)-1], "\n// ")
names := []string{}
codes := []int{}
for code, name := range keyCodeToName {
names = append(names, name)
codes = append(codes, code)
}
sort.Strings(names)
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.
tmpl.Execute(f, map[string]interface{}{
"License": license,
"Keys": keyCodeToName,
"Codes": codes,
"KeyNames": names,
})
}
}

View File

@ -15,8 +15,8 @@
package ui
type input struct {
keyPressed [KeyMax]bool
mouseButtonPressed [MouseButtonMax]bool
keyPressed [256]bool
mouseButtonPressed [256]bool
cursorX int
cursorY int
}

View File

@ -34,8 +34,8 @@ func CursorPosition() (x, y int) {
}
func (i *input) update(window *glfw.Window, scale int) {
for g, u := range glfwKeyCodeToKey {
i.keyPressed[u] = window.GetKey(g) == glfw.Press
for g, e := range glfwKeyCodeToKey {
i.keyPressed[e] = window.GetKey(g) == glfw.Press
}
for b := MouseButtonLeft; b < MouseButtonMax; b++ {
i.mouseButtonPressed[b] = window.GetMouseButton(glfw.MouseButton(b)) == glfw.Press

View File

@ -17,19 +17,72 @@ package ui
type Key int
const (
KeyUp Key = iota
Key0 Key = iota
Key1
Key2
Key3
Key4
Key5
Key6
Key7
Key8
Key9
KeyA
KeyB
KeyC
KeyCapsLock
KeyComma
KeyD
KeyDelete
KeyDown
KeyE
KeyEnd
KeyEnter
KeyEscape
KeyF
KeyF1
KeyF10
KeyF11
KeyF12
KeyF2
KeyF3
KeyF4
KeyF5
KeyF6
KeyF7
KeyF8
KeyF9
KeyG
KeyH
KeyHome
KeyI
KeyInsert
KeyJ
KeyK
KeyL
KeyLeft
KeyLeftAlt
KeyLeftControl
KeyLeftShift
KeyM
KeyN
KeyO
KeyP
KeyPageDown
KeyPageUp
KeyPeriod
KeyQ
KeyR
KeyRight
KeyS
KeySpace
KeyMax
)
type MouseButton int
const (
MouseButtonLeft MouseButton = iota
MouseButtonRight
MouseButtonMiddle
MouseButtonMax
KeyT
KeyTab
KeyU
KeyUp
KeyV
KeyW
KeyX
KeyY
KeyZ
)

View File

@ -21,9 +21,72 @@ import (
)
var glfwKeyCodeToKey = map[glfw.Key]Key{
glfw.KeySpace: KeySpace,
glfw.KeyLeft: KeyLeft,
glfw.KeyRight: KeyRight,
glfw.KeyUp: KeyUp,
glfw.KeyDown: KeyDown,
glfw.Key0: Key0,
glfw.Key1: Key1,
glfw.Key2: Key2,
glfw.Key3: Key3,
glfw.Key4: Key4,
glfw.Key5: Key5,
glfw.Key6: Key6,
glfw.Key7: Key7,
glfw.Key8: Key8,
glfw.Key9: Key9,
glfw.KeyA: KeyA,
glfw.KeyB: KeyB,
glfw.KeyC: KeyC,
glfw.KeyCapsLock: KeyCapsLock,
glfw.KeyComma: KeyComma,
glfw.KeyD: KeyD,
glfw.KeyDelete: KeyDelete,
glfw.KeyDown: KeyDown,
glfw.KeyE: KeyE,
glfw.KeyEnd: KeyEnd,
glfw.KeyEnter: KeyEnter,
glfw.KeyEscape: KeyEscape,
glfw.KeyF: KeyF,
glfw.KeyF1: KeyF1,
glfw.KeyF10: KeyF10,
glfw.KeyF11: KeyF11,
glfw.KeyF12: KeyF12,
glfw.KeyF2: KeyF2,
glfw.KeyF3: KeyF3,
glfw.KeyF4: KeyF4,
glfw.KeyF5: KeyF5,
glfw.KeyF6: KeyF6,
glfw.KeyF7: KeyF7,
glfw.KeyF8: KeyF8,
glfw.KeyF9: KeyF9,
glfw.KeyG: KeyG,
glfw.KeyH: KeyH,
glfw.KeyHome: KeyHome,
glfw.KeyI: KeyI,
glfw.KeyInsert: KeyInsert,
glfw.KeyJ: KeyJ,
glfw.KeyK: KeyK,
glfw.KeyL: KeyL,
glfw.KeyLeft: KeyLeft,
glfw.KeyLeftAlt: KeyLeftAlt,
glfw.KeyLeftControl: KeyLeftControl,
glfw.KeyLeftShift: KeyLeftShift,
glfw.KeyM: KeyM,
glfw.KeyN: KeyN,
glfw.KeyO: KeyO,
glfw.KeyP: KeyP,
glfw.KeyPageDown: KeyPageDown,
glfw.KeyPageUp: KeyPageUp,
glfw.KeyPeriod: KeyPeriod,
glfw.KeyQ: KeyQ,
glfw.KeyR: KeyR,
glfw.KeyRight: KeyRight,
glfw.KeyS: KeyS,
glfw.KeySpace: KeySpace,
glfw.KeyT: KeyT,
glfw.KeyTab: KeyTab,
glfw.KeyU: KeyU,
glfw.KeyUp: KeyUp,
glfw.KeyV: KeyV,
glfw.KeyW: KeyW,
glfw.KeyX: KeyX,
glfw.KeyY: KeyY,
glfw.KeyZ: KeyZ,
}

View File

@ -17,9 +17,72 @@
package ui
var keyCodeToKey = map[int]Key{
32: KeySpace,
37: KeyLeft,
39: KeyRight,
38: KeyUp,
40: KeyDown,
9: KeyTab,
13: KeyEnter,
16: KeyLeftShift,
17: KeyLeftControl,
18: KeyLeftAlt,
20: KeyCapsLock,
27: KeyEscape,
32: KeySpace,
33: KeyPageUp,
34: KeyPageDown,
35: KeyEnd,
36: KeyHome,
37: KeyLeft,
38: KeyUp,
39: KeyRight,
40: KeyDown,
45: KeyInsert,
46: KeyDelete,
48: Key0,
49: Key1,
50: Key2,
51: Key3,
52: Key4,
53: Key5,
54: Key6,
55: Key7,
56: Key8,
57: Key9,
65: KeyA,
66: KeyB,
67: KeyC,
68: KeyD,
69: KeyE,
70: KeyF,
71: KeyG,
72: KeyH,
73: KeyI,
74: KeyJ,
75: KeyK,
76: KeyL,
77: KeyM,
78: KeyN,
79: KeyO,
80: KeyP,
81: KeyQ,
82: KeyR,
83: KeyS,
84: KeyT,
85: KeyU,
86: KeyV,
87: KeyW,
88: KeyX,
89: KeyY,
90: KeyZ,
112: KeyF1,
113: KeyF2,
114: KeyF3,
115: KeyF4,
116: KeyF5,
117: KeyF6,
118: KeyF7,
119: KeyF8,
120: KeyF9,
121: KeyF10,
122: KeyF11,
123: KeyF12,
188: KeyComma,
190: KeyPeriod,
}

View File

@ -0,0 +1,24 @@
// 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.
package ui
type MouseButton int
const (
MouseButtonLeft MouseButton = iota
MouseButtonRight
MouseButtonMiddle
MouseButtonMax
)

76
keys.go
View File

@ -1,4 +1,4 @@
// Copyright 2014 Hajime Hoshi
// 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.
@ -23,10 +23,72 @@ type Key int
// Keys
const (
KeyUp = Key(ui.KeyUp)
KeyDown = Key(ui.KeyDown)
KeyLeft = Key(ui.KeyLeft)
KeyRight = Key(ui.KeyRight)
KeySpace = Key(ui.KeySpace)
KeyMax = Key(ui.KeyMax)
Key0 = Key(ui.Key0)
Key1 = Key(ui.Key1)
Key2 = Key(ui.Key2)
Key3 = Key(ui.Key3)
Key4 = Key(ui.Key4)
Key5 = Key(ui.Key5)
Key6 = Key(ui.Key6)
Key7 = Key(ui.Key7)
Key8 = Key(ui.Key8)
Key9 = Key(ui.Key9)
KeyA = Key(ui.KeyA)
KeyB = Key(ui.KeyB)
KeyC = Key(ui.KeyC)
KeyCapsLock = Key(ui.KeyCapsLock)
KeyComma = Key(ui.KeyComma)
KeyD = Key(ui.KeyD)
KeyDelete = Key(ui.KeyDelete)
KeyDown = Key(ui.KeyDown)
KeyE = Key(ui.KeyE)
KeyEnd = Key(ui.KeyEnd)
KeyEnter = Key(ui.KeyEnter)
KeyEscape = Key(ui.KeyEscape)
KeyF = Key(ui.KeyF)
KeyF1 = Key(ui.KeyF1)
KeyF10 = Key(ui.KeyF10)
KeyF11 = Key(ui.KeyF11)
KeyF12 = Key(ui.KeyF12)
KeyF2 = Key(ui.KeyF2)
KeyF3 = Key(ui.KeyF3)
KeyF4 = Key(ui.KeyF4)
KeyF5 = Key(ui.KeyF5)
KeyF6 = Key(ui.KeyF6)
KeyF7 = Key(ui.KeyF7)
KeyF8 = Key(ui.KeyF8)
KeyF9 = Key(ui.KeyF9)
KeyG = Key(ui.KeyG)
KeyH = Key(ui.KeyH)
KeyHome = Key(ui.KeyHome)
KeyI = Key(ui.KeyI)
KeyInsert = Key(ui.KeyInsert)
KeyJ = Key(ui.KeyJ)
KeyK = Key(ui.KeyK)
KeyL = Key(ui.KeyL)
KeyLeft = Key(ui.KeyLeft)
KeyLeftAlt = Key(ui.KeyLeftAlt)
KeyLeftControl = Key(ui.KeyLeftControl)
KeyLeftShift = Key(ui.KeyLeftShift)
KeyM = Key(ui.KeyM)
KeyN = Key(ui.KeyN)
KeyO = Key(ui.KeyO)
KeyP = Key(ui.KeyP)
KeyPageDown = Key(ui.KeyPageDown)
KeyPageUp = Key(ui.KeyPageUp)
KeyPeriod = Key(ui.KeyPeriod)
KeyQ = Key(ui.KeyQ)
KeyR = Key(ui.KeyR)
KeyRight = Key(ui.KeyRight)
KeyS = Key(ui.KeyS)
KeySpace = Key(ui.KeySpace)
KeyT = Key(ui.KeyT)
KeyTab = Key(ui.KeyTab)
KeyU = Key(ui.KeyU)
KeyUp = Key(ui.KeyUp)
KeyV = Key(ui.KeyV)
KeyW = Key(ui.KeyW)
KeyX = Key(ui.KeyX)
KeyY = Key(ui.KeyY)
KeyZ = Key(ui.KeyZ)
)