83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package game
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
)
|
|
import "code.rocketnine.space/tslocum/kibodo"
|
|
|
|
type game struct {
|
|
w, h int
|
|
|
|
k *kibodo.Keyboard
|
|
|
|
userInput []byte
|
|
|
|
op *ebiten.DrawImageOptions
|
|
}
|
|
|
|
// NewDemoGame returns a new kibodo demo game.
|
|
func NewDemoGame() *game {
|
|
k := kibodo.NewKeyboard()
|
|
k.SetAllowUserHide(true)
|
|
k.SetPassThroughPhysicalInput(true)
|
|
k.SetKeys(kibodo.KeysQWERTY)
|
|
|
|
g := &game{
|
|
k: k,
|
|
op: &ebiten.DrawImageOptions{
|
|
Filter: ebiten.FilterNearest,
|
|
},
|
|
}
|
|
|
|
ch := make(chan *kibodo.Input, 10)
|
|
k.Show(ch)
|
|
|
|
go func() {
|
|
for input := range ch {
|
|
if input.Rune > 0 {
|
|
g.userInput = append(g.userInput, []byte(string(input.Rune))...)
|
|
continue
|
|
}
|
|
g.userInput = append(g.userInput, []byte("<"+input.Key.String()+">")...)
|
|
}
|
|
g.userInput = []byte("(Hidden, click to show)")
|
|
}()
|
|
|
|
return g
|
|
}
|
|
|
|
func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
s := ebiten.DeviceScaleFactor()
|
|
outsideWidth, outsideHeight = int(float64(outsideWidth)*s), int(float64(outsideHeight)*s)
|
|
if g.w == outsideWidth && g.h == outsideHeight {
|
|
return outsideWidth, outsideHeight
|
|
}
|
|
|
|
g.w, g.h = outsideWidth, outsideHeight
|
|
|
|
sizeH := 200
|
|
g.k.SetRect(0, sizeH, g.w, g.h-sizeH)
|
|
|
|
return outsideWidth, outsideHeight
|
|
}
|
|
|
|
func (g *game) Update() error {
|
|
return g.k.Update()
|
|
}
|
|
|
|
func (g *game) Draw(screen *ebiten.Image) {
|
|
g.k.Draw(screen)
|
|
|
|
debugBox := image.NewRGBA(image.Rect(10, 20, 200, 200))
|
|
debugImg := ebiten.NewImageFromImage(debugBox)
|
|
ebitenutil.DebugPrint(debugImg, fmt.Sprintf("FPS %0.0f - TPS %0.0f\n\n%s", ebiten.CurrentFPS(), ebiten.CurrentTPS(), g.userInput))
|
|
g.op.GeoM.Reset()
|
|
g.op.GeoM.Translate(3, 0)
|
|
g.op.GeoM.Scale(2, 2)
|
|
screen.DrawImage(debugImg, g.op)
|
|
}
|