diff options
Diffstat (limited to 'go/ui/button.go')
| -rw-r--r-- | go/ui/button.go | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/go/ui/button.go b/go/ui/button.go index afd37a31..a755c5c0 100644 --- a/go/ui/button.go +++ b/go/ui/button.go @@ -4,12 +4,13 @@ import ( "image/color" "github.com/hajimehoshi/ebiten/v2" - "github.com/hajimehoshi/ebiten/v2/text" + "github.com/hajimehoshi/ebiten/v2/text/v2" "github.com/hajimehoshi/ebiten/v2/vector" "muhq.space/muhqs-game/go/font" ) + type Button interface { Widget Clickable @@ -21,10 +22,12 @@ type SimpleButton struct { OnClick func(b *SimpleButton) Bg color.Color Fg color.Color + font *text.GoTextFace + lineSpacing float64 } 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 := &SimpleButton{NewWidgetBase(x, y, w, h), label, onClick, Gray, color.White, font.Font24, 1.5} sb.renderImpl = func() *ebiten.Image { return sb.render() } return sb } @@ -34,10 +37,13 @@ func (b *SimpleButton) render() *ebiten.Image { 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) + w, h := text.Measure(b.label, b.font, b.font.Size * b.lineSpacing) + x := float64(b.Width)/2 - w/2 + y := float64(b.Height)/2 - h/2 + op := &text.DrawOptions{} + op.GeoM.Translate(x, y) + op.ColorScale.ScaleWithColor(b.Fg) + text.Draw(img, b.label, font.Font24, op) return img } |
