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 package main
import ( import (
"bytes"
"fmt" "fmt"
"image/color" "image/color"
"log" "log"
"math" "math"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio" "github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/ebitenutil" "github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts" "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/v2/inpututil" "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" "github.com/hajimehoshi/ebiten/v2/vector"
) )
const (
arcadeFontSize = 8
)
var ( var (
arcadeFont font.Face arcadeFaceSource *text.GoTextFaceSource
) )
func init() { func init() {
tt, err := opentype.Parse(fonts.PressStart2P_ttf) s, err := text.NewGoTextFaceSource(bytes.NewReader(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,
})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
arcadeFaceSource = s
} }
const ( const (
@ -148,7 +138,14 @@ func init() {
x := i*keyWidth + 36 x := i*keyWidth + 36
height := 112 height := 112
vector.DrawFilledRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.White, false) 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"} blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"}
@ -159,7 +156,14 @@ func init() {
x := i*keyWidth + 24 x := i*keyWidth + 24
height := 64 height := 64
vector.DrawFilledRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.Black, false) 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)
} }
} }