From d7bac40f11fc18121ca3d26175fb85f187a6f24c Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 28 Dec 2024 11:34:53 +0100 Subject: migrate to ebiten/v2/text/v2 --- go/ui/buffer.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'go/ui/buffer.go') diff --git a/go/ui/buffer.go b/go/ui/buffer.go index 201273b8..e46461bf 100644 --- a/go/ui/buffer.go +++ b/go/ui/buffer.go @@ -4,7 +4,7 @@ import ( "image/color" "github.com/hajimehoshi/ebiten/v2" - "github.com/hajimehoshi/ebiten/v2/text" + "github.com/hajimehoshi/ebiten/v2/text/v2" "muhq.space/muhqs-game/go/font" ) @@ -18,10 +18,13 @@ type Buffer struct { WidgetBase lines []string pos int + font *text.GoTextFace + lineSpacing float64 + xMargin float64 } func NewBuffer(x, y int, width, height int) *Buffer { - b := &Buffer{NewWidgetBase(x, y, width, height), []string{}, 0} + b := &Buffer{NewWidgetBase(x, y, width, height), []string{}, 0, font.Font18, 1.5, 10.} b.renderImpl = func() *ebiten.Image { return b.render() } return b } @@ -29,14 +32,18 @@ func NewBuffer(x, y int, width, height int) *Buffer { func (b *Buffer) render() *ebiten.Image { img := ebiten.NewImage(b.Width, b.Height) img.Fill(BUFFER_BACKGROUND) - y := 0 + var y float64 = -b.font.Size for _, line := range b.lines[b.pos:] { - bounds := text.BoundString(font.Font18, line) - y += bounds.Dy() - if y > b.Height { + _, h := text.Measure(line, b.font, b.font.Size * b.lineSpacing) + y += h + if y > float64(b.Height) { break } - text.Draw(img, line, font.Font18, 0, y, BUFFER_FOREGROUND) + op := &text.DrawOptions{} + op.ColorScale.ScaleWithColor(BUFFER_FOREGROUND) + op.GeoM.Translate(b.xMargin, float64(y)) + op.LineSpacing = b.font.Size * b.lineSpacing + text.Draw(img, line, b.font, op) } return img } -- cgit v1.2.3