Browse Source

Stretch keys to fill keyboard

main
Trevor Slocum 1 year ago
parent
commit
5e1a355e90
  1. 10
      demos/kibodo/game/game.go
  2. 1
      demos/kibodo/main.go
  3. 4
      go.mod
  4. 8
      go.sum
  5. 34
      keyboard.go

10
demos/kibodo/game/game.go

@ -42,6 +42,16 @@ func NewDemoGame() *game {
g.userInput = append(g.userInput, []byte(string(input.Rune))...)
continue
}
if input.Key == ebiten.KeyBackspace {
s := string(g.userInput)
if len(s) > 0 {
g.userInput = []byte(s[:len(s)-1])
}
continue
} else if input.Key == ebiten.KeyEnter {
g.userInput = nil
continue
}
g.userInput = append(g.userInput, []byte("<"+input.Key.String()+">")...)
}
g.userInput = []byte("(Hidden, click to show)")

1
demos/kibodo/main.go

@ -10,6 +10,7 @@ import (
func main() {
ebiten.SetWindowTitle("キーボード")
ebiten.SetWindowResizable(true)
ebiten.SetFPSMode(ebiten.FPSModeVsyncOffMinimum)
ebiten.SetMaxTPS(60)
g := game.NewDemoGame()

4
go.mod

@ -2,13 +2,13 @@ module code.rocketnine.space/tslocum/kibodo
go 1.17
require github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.12.0.20210828073710-0e5dca9453a5
require github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.13
require (
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 // indirect
golang.org/x/mobile v0.0.0-20210902104108-5d9a33257ab5 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55 // indirect
)

8
go.sum

@ -2,8 +2,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be h1:vEIVIuBApEBQTEJt19GfhoU+zFSV+sNTa9E9FdnRYfk=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/hajimehoshi/bitmapfont/v2 v2.1.3/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.12.0.20210828073710-0e5dca9453a5 h1:fUSKz2wvklV02UTmBXXDlNKc6molRGUu5O8b80AvEa4=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.12.0.20210828073710-0e5dca9453a5/go.mod h1:B4Cje+Kb1ZjztrKFPaiMWOGXO3Vp8u+zIBdxkZqkyD4=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.13 h1:p3gKkH346TwW/MFPETJEk767hPWegGf1YGXMvBm4afA=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.13/go.mod h1:44O6eBPGyRv8YctRbfzaqUH2sek5UdXh0aLWOP02ELI=
github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
github.com/hajimehoshi/go-mp3 v0.3.2/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
@ -26,8 +26,8 @@ golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jp
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 h1:3In5TnfvnuXTF/uflgpYxSCEGP2NdYT37KsPh3VjZYU=
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554/go.mod h1:jFTmtFYCV0MFtXBU+J5V/+5AUeVS0ON/0WkE/KSrl6E=
golang.org/x/mobile v0.0.0-20210902104108-5d9a33257ab5 h1:peBP2oZO/xVnGMaWMCyFEI0WENsGj71wx5K12mRELHQ=
golang.org/x/mobile v0.0.0-20210902104108-5d9a33257ab5/go.mod h1:c4YKU3ZylDmvbw+H/PSvm42vhdWbuxCzbonauEAP9B8=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=

34
keyboard.go

@ -27,6 +27,8 @@ type Keyboard struct {
background *ebiten.Image
op *ebiten.DrawImageOptions
backgroundColor color.Color
}
// NewKeyboard returns a new Keyboard.
@ -36,7 +38,8 @@ func NewKeyboard() *Keyboard {
op: &ebiten.DrawImageOptions{
Filter: ebiten.FilterNearest,
},
background: ebiten.NewImage(1, 1),
background: ebiten.NewImage(1, 1),
backgroundColor: color.Black,
}
return k
}
@ -64,12 +67,26 @@ func (k *Keyboard) SetKeys(keys [][]*Key) {
}
func (k *Keyboard) updateKeyRects() {
maxCells := 0
for _, rowKeys := range k.keys {
if len(rowKeys) > maxCells {
maxCells = len(rowKeys)
}
}
cellPaddingW := 10
cellPaddingH := 10
cellH := (k.h - (cellPaddingH * (len(k.keys) - 1))) / len(k.keys)
for row, rowKeys := range k.keys {
cellW := (k.w - (cellPaddingW * (len(rowKeys) - 1))) / len(rowKeys)
for i, key := range rowKeys {
key.x = 50 * i
key.y = 50 * row
key.w = 40
key.h = 40
key.x = (cellW + cellPaddingW) * i
key.y = (cellH + cellPaddingH) * row
key.w = cellW
key.h = cellH
}
}
}
@ -141,7 +158,7 @@ func (k *Keyboard) drawBackground() {
debugBox := image.NewRGBA(image.Rect(0, 0, k.w, k.h))
buffer := ebiten.NewImageFromImage(debugBox)
buffer.Fill(color.White)
buffer.Fill(k.backgroundColor)
for _, rowKeys := range k.keys {
for _, key := range rowKeys {
@ -150,9 +167,10 @@ func (k *Keyboard) drawBackground() {
img := ebiten.NewImageFromImage(image.NewRGBA(image.Rect(0, 0, key.w, key.h)))
img.Fill(color.RGBA{150, 150, 150, 255})
k.op.GeoM.Reset()
k.op.GeoM.Scale(2, 2)
k.op.GeoM.Scale(3, 3)
k.op.GeoM.Translate(float64(0), float64(key.h)/4)
if len(key.LowerLabel) <= 1 {
k.op.GeoM.Translate(float64(12), 0)
k.op.GeoM.Translate(float64(key.w/4), 0)
}
img.DrawImage(labelImg, k.op)

Loading…
Cancel
Save