diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2024-12-28 11:34:53 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 15:57:13 +0200 |
| commit | ba4f24e71e4970c3095971fe6c5df7f829cab3e5 (patch) | |
| tree | b305956002f828dab115f2617fb039b5b8f5d0d7 /go/ui/button.go | |
| parent | bfab1b8f10ce006dd4822ac3e82125ec226597f5 (diff) | |
| download | muhqs-game-ba4f24e71e4970c3095971fe6c5df7f829cab3e5.tar.gz muhqs-game-ba4f24e71e4970c3095971fe6c5df7f829cab3e5.zip | |
migrate to ebiten/v2/text/v2
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 } |
