package ui import ( "image/color" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/text" "github.com/hajimehoshi/ebiten/v2/vector" "muhq.space/muhqs-game/go/font" ) type Button interface { Widget Clickable } type SimpleButton struct { WidgetBase label string OnClick func(b *SimpleButton) Bg color.Color Fg color.Color } func NewSimpleButton(x, y, w, h int, label string, onClick func(b *SimpleButton)) *SimpleButton { sb := &SimpleButton{NewWidgetBase(x, y, w, h), label, onClick, Gray, color.White} sb.renderImpl = func() *ebiten.Image { return sb.render() } return sb } func (b *SimpleButton) render() *ebiten.Image { img := ebiten.NewImage(b.Width, b.Height) vector.DrawFilledRect(img, float32(0), float32(0), float32(b.Width), float32(b.Height), b.Bg, false) bounds := text.BoundString(font.Font24, b.label) x := b.Width/2 - centerTextXOffset(bounds) y := b.Height/2 - centerTextYOffset(bounds) text.Draw(img, b.label, font.Font24, x, y, b.Fg) return img } func (b *SimpleButton) Click(x, y int) { b.OnClick(b) } func (b *SimpleButton) UpdateLabel(label string) { if b.label != label { b.label = label b.ForceRedraw() } }