From 006777220c1a42d3ce3d56bf9f9bcaa6c78bf7a9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 16 Nov 2023 02:20:15 +0900 Subject: [PATCH] examples/piano: use text/v2 Updates #2454 --- examples/piano/main.go | 46 +++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/examples/piano/main.go b/examples/piano/main.go index b70d96171..397630333 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -15,45 +15,35 @@ package main import ( + "bytes" "fmt" "image/color" "log" "math" - "golang.org/x/image/font" - "golang.org/x/image/font/opentype" - "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/audio" "github.com/hajimehoshi/ebiten/v2/ebitenutil" "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts" "github.com/hajimehoshi/ebiten/v2/inpututil" - "github.com/hajimehoshi/ebiten/v2/text" + "github.com/hajimehoshi/ebiten/v2/text/v2" "github.com/hajimehoshi/ebiten/v2/vector" ) +const ( + arcadeFontSize = 8 +) + var ( - arcadeFont font.Face + arcadeFaceSource *text.GoTextFaceSource ) func init() { - tt, err := opentype.Parse(fonts.PressStart2P_ttf) - if err != nil { - log.Fatal(err) - } - - const ( - arcadeFontSize = 8 - dpi = 72 - ) - arcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{ - Size: arcadeFontSize, - DPI: dpi, - Hinting: font.HintingFull, - }) + s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.PressStart2P_ttf)) if err != nil { log.Fatal(err) } + arcadeFaceSource = s } const ( @@ -148,7 +138,14 @@ func init() { x := i*keyWidth + 36 height := 112 vector.DrawFilledRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.White, false) - text.Draw(pianoImage, k, arcadeFont, x+8, y+height-8, color.Black) + op := &text.DrawOptions{} + op.GeoM.Translate(float64(x+keyWidth/2), float64(y+height-12)) + op.ColorScale.ScaleWithColor(color.Black) + op.PrimaryAlign = text.AlignCenter + text.Draw(pianoImage, k, &text.GoTextFace{ + Source: arcadeFaceSource, + Size: arcadeFontSize, + }, op) } blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"} @@ -159,7 +156,14 @@ func init() { x := i*keyWidth + 24 height := 64 vector.DrawFilledRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.Black, false) - text.Draw(pianoImage, k, arcadeFont, x+8, y+height-8, color.White) + op := &text.DrawOptions{} + op.GeoM.Translate(float64(x+keyWidth/2), float64(y+height-12)) + op.ColorScale.ScaleWithColor(color.White) + op.PrimaryAlign = text.AlignCenter + text.Draw(pianoImage, k, &text.GoTextFace{ + Source: arcadeFaceSource, + Size: arcadeFontSize, + }, op) } }