diff options
Diffstat (limited to 'go/ui/textBox.go')
| -rw-r--r-- | go/ui/textBox.go | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/go/ui/textBox.go b/go/ui/textBox.go index 4fc2b533..f43e42b3 100644 --- a/go/ui/textBox.go +++ b/go/ui/textBox.go @@ -7,7 +7,7 @@ import ( "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/inpututil" - "github.com/hajimehoshi/ebiten/v2/text" + "github.com/hajimehoshi/ebiten/v2/text/v2" "muhq.space/muhqs-game/go/font" "muhq.space/muhqs-game/go/game" ) @@ -26,6 +26,8 @@ type TextBox struct { text string fg color.Color bg color.Color + font *text.GoTextFace + lineSpacing float64 XMargin int YMargin int center bool @@ -37,6 +39,8 @@ func NewFixedTextBox(x, y int, width, height int, t string) *TextBox { text: t, fg: TEXTBOX_DEFAULT_FOREGROUND, bg: TEXTBOX_DEFAULT_BACKGROUND, + font: font.Font24, + lineSpacing: 1.5, XMargin: TEXTBOX_DEFAULT_MARGIN, YMargin: TEXTBOX_DEFAULT_MARGIN, } @@ -100,26 +104,30 @@ func NewPermInfo(x, y int, p game.Permanent) *TextBox { } func (tb *TextBox) render() *ebiten.Image { - var x, y int - b := text.BoundString(font.Font24, tb.text) + var x, y float64 + w, h := text.Measure(tb.text, tb.font, tb.font.Size * tb.lineSpacing) if tb.Width == -1 || tb.Height == -1 { - tb.Width = b.Dx() + 2*tb.XMargin - tb.Height = b.Dy() + 2*tb.YMargin - x = tb.XMargin + b.Min.X - y = tb.YMargin - b.Min.Y + tb.Width = int(w) + 2*tb.XMargin + tb.Height = int(h) + 2*tb.YMargin + x = float64(tb.XMargin) + y = float64(tb.YMargin) } else { if !tb.center { - x = tb.XMargin + b.Min.X + x = float64(tb.XMargin) } else { - x = tb.Width/2 - centerTextXOffset(b) + x = (float64(tb.Width) - w)/2 } - y = tb.Height/2 - centerTextYOffset(b) + y = (float64(tb.Height) - h)/2 } img := ebiten.NewImage(tb.Width, tb.Height) img.Fill(tb.bg) - text.Draw(img, tb.text, font.Font24, x, y, tb.fg) + op := &text.DrawOptions{} + op.GeoM.Translate(x, y) + op.ColorScale.ScaleWithColor(tb.fg) + op.LineSpacing = tb.font.Size * tb.lineSpacing + text.Draw(img, tb.text, tb.font, op) return img } @@ -161,9 +169,9 @@ func (w *PocList) setText() { w.text = "No cards" } - b := text.BoundString(font.Font24, w.text) - w.X = w.centerX - b.Dx()/2 - w.XMargin - w.Y = w.bottomY - b.Dy() - w.YMargin + width, height := text.Measure(w.text, w.font, w.font.Size * w.lineSpacing) + w.X = w.centerX - int(width/2) - w.XMargin + w.Y = w.bottomY - int(height) - w.YMargin } func (w *PocList) FindObjectAt(_x, _y int) interface{} { @@ -178,8 +186,8 @@ func (w *PocList) FindObjectAt(_x, _y int) interface{} { y := _y - w.Y - b := text.BoundString(font.Font24, w.text) - i := y / (b.Dy() / w.poc.Size()) + _, height := text.Measure(w.text, w.font, w.font.Size * w.lineSpacing) + i := y / (int(height) / w.poc.Size()) if i >= len(cards) { return nil |
