From bf304e6ac8b5cbf86cce808e74da2dd821ee26cc Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 14 Apr 2018 04:48:07 +0900 Subject: [PATCH] examples/keyboard: Refactoring: Use ebiten.Key to get the rect --- examples/keyboard/keyboard/gen.go | 89 ++++++++++++++--- examples/keyboard/keyboard/keyrects.go | 126 +++++++++++++------------ examples/keyboard/main.go | 54 +---------- 3 files changed, 145 insertions(+), 124 deletions(-) diff --git a/examples/keyboard/keyboard/gen.go b/examples/keyboard/keyboard/gen.go index a901519d8..c0bdf9c38 100644 --- a/examples/keyboard/keyboard/gen.go +++ b/examples/keyboard/keyboard/gen.go @@ -80,6 +80,68 @@ var keyboardKeys = [][]string{ {"Left", "Down", "Right"}, } +func keyDisplayNameToKey(name string) ebiten.Key { + switch name { + case "Esc": + return ebiten.KeyEscape + case "Tab": + return ebiten.KeyTab + case "Ctrl": + return ebiten.KeyControl + case "Shift": + return ebiten.KeyShift + case "Alt": + return ebiten.KeyAlt + case "Space": + return ebiten.KeySpace + case "Up": + return ebiten.KeyUp + case "Left": + return ebiten.KeyLeft + case "Down": + return ebiten.KeyDown + case "Right": + return ebiten.KeyRight + case "BS": + return ebiten.KeyBackspace + case "Enter": + return ebiten.KeyEnter + case "-": + return ebiten.KeyMinus + case "=": + return ebiten.KeyEqual + case "\\": + return ebiten.KeyBackslash + case "`": + return ebiten.KeyGraveAccent + case "[": + return ebiten.KeyLeftBracket + case "]": + return ebiten.KeyRightBracket + case ";": + return ebiten.KeySemicolon + case "'": + return ebiten.KeyApostrophe + case ",": + return ebiten.KeyComma + case ".": + return ebiten.KeyPeriod + case "/": + return ebiten.KeySlash + } + if len(name) != 1 { + panic("not reached: unknown key " + name) + } + c := name[0] + if '0' <= c && c <= '9' { + return ebiten.Key0 + ebiten.Key(c-'0') + } + if 'A' <= c && c <= 'Z' { + return ebiten.KeyA + ebiten.Key(c-'A') + } + panic("not reached: unknown key " + name) +} + func drawKey(t *ebiten.Image, name string, x, y, width int) { const height = 16 width-- @@ -129,14 +191,14 @@ func drawKey(t *ebiten.Image, name string, x, y, width int) { t.DrawImage(img, op) } -func outputKeyboardImage() (map[string]image.Rectangle, error) { - keyMap := map[string]image.Rectangle{} +func outputKeyboardImage() (map[ebiten.Key]image.Rectangle, error) { + keyMap := map[ebiten.Key]image.Rectangle{} img, _ := ebiten.NewImage(320, 240, ebiten.FilterDefault) x, y := 0, 0 for j, line := range keyboardKeys { x = 0 const height = 18 - for i, key := range line { + for i, keyDisplayName := range line { width := 16 switch j { default: @@ -164,9 +226,10 @@ func outputKeyboardImage() (map[string]image.Rectangle, error) { case 6, 7: width = 16 * 3 } - if key != "" { - drawKey(img, key, x, y, width) - if key != " " { + if keyDisplayName != "" { + drawKey(img, keyDisplayName, x, y, width) + if keyDisplayName != " " { + key := keyDisplayNameToKey(keyDisplayName) keyMap[key] = image.Rect(x, y, x+width, y+height) } } @@ -206,20 +269,22 @@ package keyboard import ( "image" + + "github.com/hajimehoshi/ebiten" ) -var keyboardKeyRects = map[string]image.Rectangle{} +var keyboardKeyRects = map[ebiten.Key]image.Rectangle{} func init() { -{{range $key, $rect := .KeyRectsMap}} keyboardKeyRects[{{printf "%q" $key}}] = image.Rect({{$rect.Min.X}}, {{$rect.Min.Y}}, {{$rect.Max.X}}, {{$rect.Max.Y}}) +{{range $key, $rect := .KeyRectsMap}} keyboardKeyRects[ebiten.Key{{$key}}] = image.Rect({{$rect.Min.X}}, {{$rect.Min.Y}}, {{$rect.Max.X}}, {{$rect.Max.Y}}) {{end}}} -func KeyRect(name string) (image.Rectangle, bool) { - r, ok := keyboardKeyRects[name] +func KeyRect(key ebiten.Key) (image.Rectangle, bool) { + r, ok := keyboardKeyRects[key] return r, ok }` -func outputKeyRectsGo(k map[string]image.Rectangle) error { +func outputKeyRectsGo(k map[ebiten.Key]image.Rectangle) error { license, err := internal.LicenseComment() if err != nil { return err @@ -245,7 +310,7 @@ func outputKeyRectsGo(k map[string]image.Rectangle) error { var regularTermination = errors.New("regular termination") func main() { - var rects map[string]image.Rectangle + var rects map[ebiten.Key]image.Rectangle if err := ebiten.Run(func(_ *ebiten.Image) error { var err error rects, err = outputKeyboardImage() diff --git a/examples/keyboard/keyboard/keyrects.go b/examples/keyboard/keyboard/keyrects.go index fb9904ab7..1d1a3f3f4 100644 --- a/examples/keyboard/keyboard/keyrects.go +++ b/examples/keyboard/keyboard/keyrects.go @@ -18,73 +18,75 @@ package keyboard import ( "image" + + "github.com/hajimehoshi/ebiten" ) -var keyboardKeyRects = map[string]image.Rectangle{} +var keyboardKeyRects = map[ebiten.Key]image.Rectangle{} func init() { - keyboardKeyRects["'"] = image.Rect(208, 36, 224, 54) - keyboardKeyRects[","] = image.Rect(168, 54, 184, 72) - keyboardKeyRects["-"] = image.Rect(192, 0, 208, 18) - keyboardKeyRects["."] = image.Rect(184, 54, 200, 72) - keyboardKeyRects["/"] = image.Rect(200, 54, 216, 72) - keyboardKeyRects["0"] = image.Rect(176, 0, 192, 18) - keyboardKeyRects["1"] = image.Rect(32, 0, 48, 18) - keyboardKeyRects["2"] = image.Rect(48, 0, 64, 18) - keyboardKeyRects["3"] = image.Rect(64, 0, 80, 18) - keyboardKeyRects["4"] = image.Rect(80, 0, 96, 18) - keyboardKeyRects["5"] = image.Rect(96, 0, 112, 18) - keyboardKeyRects["6"] = image.Rect(112, 0, 128, 18) - keyboardKeyRects["7"] = image.Rect(128, 0, 144, 18) - keyboardKeyRects["8"] = image.Rect(144, 0, 160, 18) - keyboardKeyRects["9"] = image.Rect(160, 0, 176, 18) - keyboardKeyRects[";"] = image.Rect(192, 36, 208, 54) - keyboardKeyRects["="] = image.Rect(208, 0, 224, 18) - keyboardKeyRects["A"] = image.Rect(48, 36, 64, 54) - keyboardKeyRects["Alt"] = image.Rect(64, 72, 96, 90) - keyboardKeyRects["B"] = image.Rect(120, 54, 136, 72) - keyboardKeyRects["BS"] = image.Rect(232, 18, 272, 36) - keyboardKeyRects["C"] = image.Rect(88, 54, 104, 72) - keyboardKeyRects["Ctrl"] = image.Rect(0, 36, 48, 54) - keyboardKeyRects["D"] = image.Rect(80, 36, 96, 54) - keyboardKeyRects["Down"] = image.Rect(48, 126, 96, 144) - keyboardKeyRects["E"] = image.Rect(72, 18, 88, 36) - keyboardKeyRects["Enter"] = image.Rect(224, 36, 272, 54) - keyboardKeyRects["Esc"] = image.Rect(0, 0, 32, 18) - keyboardKeyRects["F"] = image.Rect(96, 36, 112, 54) - keyboardKeyRects["G"] = image.Rect(112, 36, 128, 54) - keyboardKeyRects["H"] = image.Rect(128, 36, 144, 54) - keyboardKeyRects["I"] = image.Rect(152, 18, 168, 36) - keyboardKeyRects["J"] = image.Rect(144, 36, 160, 54) - keyboardKeyRects["K"] = image.Rect(160, 36, 176, 54) - keyboardKeyRects["L"] = image.Rect(176, 36, 192, 54) - keyboardKeyRects["Left"] = image.Rect(0, 126, 48, 144) - keyboardKeyRects["M"] = image.Rect(152, 54, 168, 72) - keyboardKeyRects["N"] = image.Rect(136, 54, 152, 72) - keyboardKeyRects["O"] = image.Rect(168, 18, 184, 36) - keyboardKeyRects["P"] = image.Rect(184, 18, 200, 36) - keyboardKeyRects["Q"] = image.Rect(40, 18, 56, 36) - keyboardKeyRects["R"] = image.Rect(88, 18, 104, 36) - keyboardKeyRects["Right"] = image.Rect(96, 126, 144, 144) - keyboardKeyRects["S"] = image.Rect(64, 36, 80, 54) - keyboardKeyRects["Shift"] = image.Rect(0, 54, 56, 72) - keyboardKeyRects["Space"] = image.Rect(96, 72, 176, 90) - keyboardKeyRects["T"] = image.Rect(104, 18, 120, 36) - keyboardKeyRects["Tab"] = image.Rect(0, 18, 40, 36) - keyboardKeyRects["U"] = image.Rect(136, 18, 152, 36) - keyboardKeyRects["Up"] = image.Rect(48, 108, 96, 126) - keyboardKeyRects["V"] = image.Rect(104, 54, 120, 72) - keyboardKeyRects["W"] = image.Rect(56, 18, 72, 36) - keyboardKeyRects["X"] = image.Rect(72, 54, 88, 72) - keyboardKeyRects["Y"] = image.Rect(120, 18, 136, 36) - keyboardKeyRects["Z"] = image.Rect(56, 54, 72, 72) - keyboardKeyRects["["] = image.Rect(200, 18, 216, 36) - keyboardKeyRects["\\"] = image.Rect(224, 0, 240, 18) - keyboardKeyRects["]"] = image.Rect(216, 18, 232, 36) - keyboardKeyRects["`"] = image.Rect(240, 0, 256, 18) + keyboardKeyRects[ebiten.Key0] = image.Rect(176, 0, 192, 18) + keyboardKeyRects[ebiten.Key1] = image.Rect(32, 0, 48, 18) + keyboardKeyRects[ebiten.Key2] = image.Rect(48, 0, 64, 18) + keyboardKeyRects[ebiten.Key3] = image.Rect(64, 0, 80, 18) + keyboardKeyRects[ebiten.Key4] = image.Rect(80, 0, 96, 18) + keyboardKeyRects[ebiten.Key5] = image.Rect(96, 0, 112, 18) + keyboardKeyRects[ebiten.Key6] = image.Rect(112, 0, 128, 18) + keyboardKeyRects[ebiten.Key7] = image.Rect(128, 0, 144, 18) + keyboardKeyRects[ebiten.Key8] = image.Rect(144, 0, 160, 18) + keyboardKeyRects[ebiten.Key9] = image.Rect(160, 0, 176, 18) + keyboardKeyRects[ebiten.KeyA] = image.Rect(48, 36, 64, 54) + keyboardKeyRects[ebiten.KeyB] = image.Rect(120, 54, 136, 72) + keyboardKeyRects[ebiten.KeyC] = image.Rect(88, 54, 104, 72) + keyboardKeyRects[ebiten.KeyD] = image.Rect(80, 36, 96, 54) + keyboardKeyRects[ebiten.KeyE] = image.Rect(72, 18, 88, 36) + keyboardKeyRects[ebiten.KeyF] = image.Rect(96, 36, 112, 54) + keyboardKeyRects[ebiten.KeyG] = image.Rect(112, 36, 128, 54) + keyboardKeyRects[ebiten.KeyH] = image.Rect(128, 36, 144, 54) + keyboardKeyRects[ebiten.KeyI] = image.Rect(152, 18, 168, 36) + keyboardKeyRects[ebiten.KeyJ] = image.Rect(144, 36, 160, 54) + keyboardKeyRects[ebiten.KeyK] = image.Rect(160, 36, 176, 54) + keyboardKeyRects[ebiten.KeyL] = image.Rect(176, 36, 192, 54) + keyboardKeyRects[ebiten.KeyM] = image.Rect(152, 54, 168, 72) + keyboardKeyRects[ebiten.KeyN] = image.Rect(136, 54, 152, 72) + keyboardKeyRects[ebiten.KeyO] = image.Rect(168, 18, 184, 36) + keyboardKeyRects[ebiten.KeyP] = image.Rect(184, 18, 200, 36) + keyboardKeyRects[ebiten.KeyQ] = image.Rect(40, 18, 56, 36) + keyboardKeyRects[ebiten.KeyR] = image.Rect(88, 18, 104, 36) + keyboardKeyRects[ebiten.KeyS] = image.Rect(64, 36, 80, 54) + keyboardKeyRects[ebiten.KeyT] = image.Rect(104, 18, 120, 36) + keyboardKeyRects[ebiten.KeyU] = image.Rect(136, 18, 152, 36) + keyboardKeyRects[ebiten.KeyV] = image.Rect(104, 54, 120, 72) + keyboardKeyRects[ebiten.KeyW] = image.Rect(56, 18, 72, 36) + keyboardKeyRects[ebiten.KeyX] = image.Rect(72, 54, 88, 72) + keyboardKeyRects[ebiten.KeyY] = image.Rect(120, 18, 136, 36) + keyboardKeyRects[ebiten.KeyZ] = image.Rect(56, 54, 72, 72) + keyboardKeyRects[ebiten.KeyAlt] = image.Rect(64, 72, 96, 90) + keyboardKeyRects[ebiten.KeyApostrophe] = image.Rect(208, 36, 224, 54) + keyboardKeyRects[ebiten.KeyBackslash] = image.Rect(224, 0, 240, 18) + keyboardKeyRects[ebiten.KeyBackspace] = image.Rect(232, 18, 272, 36) + keyboardKeyRects[ebiten.KeyComma] = image.Rect(168, 54, 184, 72) + keyboardKeyRects[ebiten.KeyControl] = image.Rect(0, 36, 48, 54) + keyboardKeyRects[ebiten.KeyDown] = image.Rect(48, 126, 96, 144) + keyboardKeyRects[ebiten.KeyEnter] = image.Rect(224, 36, 272, 54) + keyboardKeyRects[ebiten.KeyEqual] = image.Rect(208, 0, 224, 18) + keyboardKeyRects[ebiten.KeyEscape] = image.Rect(0, 0, 32, 18) + keyboardKeyRects[ebiten.KeyGraveAccent] = image.Rect(240, 0, 256, 18) + keyboardKeyRects[ebiten.KeyLeft] = image.Rect(0, 126, 48, 144) + keyboardKeyRects[ebiten.KeyLeftBracket] = image.Rect(200, 18, 216, 36) + keyboardKeyRects[ebiten.KeyMinus] = image.Rect(192, 0, 208, 18) + keyboardKeyRects[ebiten.KeyPeriod] = image.Rect(184, 54, 200, 72) + keyboardKeyRects[ebiten.KeyRight] = image.Rect(96, 126, 144, 144) + keyboardKeyRects[ebiten.KeyRightBracket] = image.Rect(216, 18, 232, 36) + keyboardKeyRects[ebiten.KeySemicolon] = image.Rect(192, 36, 208, 54) + keyboardKeyRects[ebiten.KeyShift] = image.Rect(0, 54, 56, 72) + keyboardKeyRects[ebiten.KeySlash] = image.Rect(200, 54, 216, 72) + keyboardKeyRects[ebiten.KeySpace] = image.Rect(96, 72, 176, 90) + keyboardKeyRects[ebiten.KeyTab] = image.Rect(0, 18, 40, 36) + keyboardKeyRects[ebiten.KeyUp] = image.Rect(48, 108, 96, 126) } -func KeyRect(name string) (image.Rectangle, bool) { - r, ok := keyboardKeyRects[name] +func KeyRect(key ebiten.Key) (image.Rectangle, bool) { + r, ok := keyboardKeyRects[key] return r, ok } diff --git a/examples/keyboard/main.go b/examples/keyboard/main.go index 03514fef4..9a4469cb4 100644 --- a/examples/keyboard/main.go +++ b/examples/keyboard/main.go @@ -21,7 +21,6 @@ import ( "image" _ "image/png" "log" - "strconv" "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/examples/keyboard/keyboard" @@ -44,57 +43,12 @@ func init() { keyboardImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault) } -var keyNames = map[ebiten.Key]string{ - ebiten.KeyBackspace: "BS", - ebiten.KeyComma: ",", - ebiten.KeyEnter: "Enter", - ebiten.KeyEscape: "Esc", - ebiten.KeyPeriod: ".", - ebiten.KeySpace: "Space", - ebiten.KeyTab: "Tab", - ebiten.KeyMinus: "-", - ebiten.KeyEqual: "=", - ebiten.KeyBackslash: "\\", - ebiten.KeyGraveAccent: "`", - ebiten.KeyLeftBracket: "[", - ebiten.KeyRightBracket: "]", - ebiten.KeySemicolon: ";", - ebiten.KeyApostrophe: "'", - ebiten.KeySlash: "/", - - // Arrows - ebiten.KeyDown: "Down", - ebiten.KeyLeft: "Left", - ebiten.KeyRight: "Right", - ebiten.KeyUp: "Up", - - // Mods - ebiten.KeyShift: "Shift", - ebiten.KeyControl: "Ctrl", - ebiten.KeyAlt: "Alt", -} - func update(screen *ebiten.Image) error { // Collect pressed keys' names. - pressed := []string{} - for i := 0; i <= 9; i++ { - if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.Key0) { - pressed = append(pressed, string(i+'0')) - } - } - for c := 'A'; c <= 'Z'; c++ { - if ebiten.IsKeyPressed(ebiten.KeyA + ebiten.Key(c-'A')) { - pressed = append(pressed, string(c)) - } - } - for i := 1; i <= 12; i++ { - if ebiten.IsKeyPressed(ebiten.KeyF1 + ebiten.Key(i-1)) { - pressed = append(pressed, "F"+strconv.Itoa(i)) - } - } - for key, name := range keyNames { - if ebiten.IsKeyPressed(key) { - pressed = append(pressed, name) + pressed := []ebiten.Key{} + for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ { + if ebiten.IsKeyPressed(k) { + pressed = append(pressed, k) } }