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/assets/retrieve.go | 7 +++++-- go/font/font.go | 37 +++++++++++++++++-------------------- go/go.mod | 1 + go/go.sum | 4 ++++ go/ui/buffer.go | 21 ++++++++++++++------- go/ui/button.go | 18 ++++++++++++------ go/ui/collection.go | 5 ++--- go/ui/textBox.go | 40 ++++++++++++++++++++++++---------------- go/ui/utils.go | 19 ------------------- 9 files changed, 79 insertions(+), 73 deletions(-) delete mode 100644 go/ui/utils.go diff --git a/go/assets/retrieve.go b/go/assets/retrieve.go index d9b40643..27a06124 100644 --- a/go/assets/retrieve.go +++ b/go/assets/retrieve.go @@ -9,7 +9,7 @@ import ( "strings" "github.com/hajimehoshi/ebiten/v2" - "github.com/hajimehoshi/ebiten/v2/text" + "github.com/hajimehoshi/ebiten/v2/text/v2" "muhq.space/muhqs-game/go/font" ) @@ -94,7 +94,10 @@ func GetGenericSymbol(symbol string) *ebiten.Image { if !found { genericBase := GetSymbol("generic_base") img = ebiten.NewImageFromImage(genericBase) - text.Draw(img, symbol, font.Font24, 5, 27, color.Black) + op := &text.DrawOptions{} + op.GeoM.Translate(5, 7) + op.ColorScale.ScaleWithColor(color.Black) + text.Draw(img, symbol, font.Font24, op) } return img } diff --git a/go/font/font.go b/go/font/font.go index ae75cf8d..e594c39f 100644 --- a/go/font/font.go +++ b/go/font/font.go @@ -1,38 +1,35 @@ package font import ( + "bytes" "log" - "golang.org/x/image/font" "golang.org/x/image/font/gofont/goregular" - "golang.org/x/image/font/opentype" + + "github.com/hajimehoshi/ebiten/v2/text/v2" ) -var Font24 font.Face -var Font18 font.Face -var Font12 font.Face +var fontSource *text.GoTextFaceSource +var Font24 *text.GoTextFace +var Font18 *text.GoTextFace +var Font12 *text.GoTextFace func InitFont() { - tt, err := opentype.Parse(goregular.TTF) + fontSource, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF)) if err != nil { log.Fatal(err) } - opts := &opentype.FaceOptions{Size: 24, DPI: 72} - Font24, err = opentype.NewFace(tt, opts) - if err != nil { - log.Fatal(err) + Font24 = &text.GoTextFace{ + Source: fontSource, + Size: 24, } - - opts = &opentype.FaceOptions{Size: 18, DPI: 72} - Font18, err = opentype.NewFace(tt, opts) - if err != nil { - log.Fatal(err) + Font18 = &text.GoTextFace{ + Source: fontSource, + Size: 18, } - - opts = &opentype.FaceOptions{Size: 12, DPI: 72} - Font12, err = opentype.NewFace(tt, opts) - if err != nil { - log.Fatal(err) + Font12 = &text.GoTextFace{ + Source: fontSource, + Size: 12, } } diff --git a/go/go.mod b/go/go.mod index 2006120c..5f3eb62c 100644 --- a/go/go.mod +++ b/go/go.mod @@ -17,6 +17,7 @@ require ( github.com/ebitengine/gomobile v0.0.0-20241016134836-cc2e38a7c0ee // indirect github.com/ebitengine/hideconsole v1.0.0 // indirect github.com/ebitengine/purego v0.8.1 // indirect + github.com/go-text/typesetting v0.2.1 // indirect github.com/jezek/xgb v1.1.1 // indirect golang.org/x/sync v0.10.0 // indirect golang.org/x/sys v0.28.0 // indirect diff --git a/go/go.sum b/go/go.sum index 7e5d60a1..dd860588 100644 --- a/go/go.sum +++ b/go/go.sum @@ -6,6 +6,10 @@ github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A= github.com/ebitengine/purego v0.8.1 h1:sdRKd6plj7KYW33EH5As6YKfe8m9zbN9JMrOjNVF/BE= github.com/ebitengine/purego v0.8.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= +github.com/go-text/typesetting v0.2.1 h1:x0jMOGyO3d1qFAPI0j4GSsh7M0Q3Ypjzr4+CEVg82V8= +github.com/go-text/typesetting v0.2.1/go.mod h1:mTOxEwasOFpAMBjEQDhdWRckoLLeI/+qrQeBCTGEt6M= +github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066 h1:qCuYC+94v2xrb1PoS4NIDe7DGYtLnU2wWiQe9a1B1c0= +github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o= github.com/hajimehoshi/bitmapfont/v3 v3.2.0 h1:0DISQM/rseKIJhdF29AkhvdzIULqNIIlXAGWit4ez1Q= github.com/hajimehoshi/bitmapfont/v3 v3.2.0/go.mod h1:8gLqGatKVu0pwcNCJguW3Igg9WQqVXF0zg/RvrGQWyg= github.com/hajimehoshi/ebiten/v2 v2.8.6 h1:Dkd/sYI0TYyZRCE7GVxV59XC+WCi2BbGAbIBjXeVC1U= 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 } 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 } diff --git a/go/ui/collection.go b/go/ui/collection.go index 03920f34..58cc4b85 100644 --- a/go/ui/collection.go +++ b/go/ui/collection.go @@ -98,9 +98,8 @@ func (c *Collection) Update() error { } if textInput, ok := activeWidget.(*TextInput); ok { - // var input []rune - // ebiten.AppendInputChars(input) - input := ebiten.InputChars() + var input []rune + input = ebiten.AppendInputChars(input) if len(input) > 0 { textInput.AddInput(input) } 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 diff --git a/go/ui/utils.go b/go/ui/utils.go deleted file mode 100644 index fd15b642..00000000 --- a/go/ui/utils.go +++ /dev/null @@ -1,19 +0,0 @@ -package ui - -import ( - "image" -) - -func centerTextXOffset(b image.Rectangle) int { - return (b.Dx() / 2) - b.Min.X -} - -func centerTextYOffset(b image.Rectangle) int { - return (b.Dy() / 2) + b.Min.Y -} - -func centerText(b image.Rectangle) (x int, y int) { - x = centerTextXOffset(b) - y = centerTextYOffset(b) - return -} -- cgit v1.2.3