examples/piano: use text/v2

Updates #2454
This commit is contained in:
Hajime Hoshi 2023-11-16 02:20:15 +09:00
parent bf338eb76b
commit 006777220c

View File

@ -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)
}
}