examples/block: use text/v2

This commit is contained in:
Hajime Hoshi 2023-11-15 22:16:55 +09:00
parent acd5207142
commit 9c95b4accc
4 changed files with 44 additions and 70 deletions

View File

@ -15,16 +15,13 @@
package blocks
import (
"bytes"
"image/color"
"log"
"strings"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
const (
@ -32,65 +29,39 @@ const (
)
var (
arcadeFonts map[int]font.Face
arcadeFaceSource *text.GoTextFaceSource
)
func getArcadeFonts(scale int) font.Face {
if arcadeFonts == nil {
tt, err := opentype.Parse(fonts.PressStart2P_ttf)
if err != nil {
log.Fatal(err)
}
arcadeFonts = map[int]font.Face{}
for i := 1; i <= 4; i++ {
const dpi = 72
arcadeFonts[i], err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: float64(arcadeFontBaseSize * i),
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil {
log.Fatal(err)
}
}
func init() {
s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.PressStart2P_ttf))
if err != nil {
log.Fatal(err)
}
return arcadeFonts[scale]
}
func textWidth(str string) int {
maxW := 0
for _, line := range strings.Split(str, "\n") {
a := font.MeasureString(getArcadeFonts(1), line)
w := a.Floor()
if maxW < w {
maxW = w
}
}
return maxW
arcadeFaceSource = s
}
var (
shadowColor = color.NRGBA{0, 0, 0, 0x80}
shadowColor = color.RGBA{0, 0, 0, 0x80}
)
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) {
offsetY := arcadeFontBaseSize * scale
for _, line := range strings.Split(str, "\n") {
y += offsetY
text.Draw(rt, line, getArcadeFonts(scale), x+1, y+1, shadowColor)
text.Draw(rt, line, getArcadeFonts(scale), x, y, clr)
}
}
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, primaryAlign, secondaryAlign text.Align) {
op := &text.DrawOptions{}
op.GeoM.Translate(float64(x)+1, float64(y)+1)
op.ColorScale.ScaleWithColor(shadowColor)
op.LineHeight = arcadeFontBaseSize * float64(scale)
op.PrimaryAlign = primaryAlign
op.SecondaryAlign = secondaryAlign
text.Draw(rt, str, &text.GoTextFace{
Source: arcadeFaceSource,
Size: arcadeFontBaseSize * float64(scale),
}, op)
func drawTextWithShadowCenter(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) {
w := textWidth(str) * scale
x += (width - w) / 2
drawTextWithShadow(rt, str, x, y, scale, clr)
}
func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) {
w := textWidth(str) * scale
x += width - w
drawTextWithShadow(rt, str, x, y, scale, clr)
op.GeoM.Reset()
op.GeoM.Translate(float64(x), float64(y))
op.ColorScale.Reset()
op.ColorScale.ScaleWithColor(clr)
text.Draw(rt, str, &text.GoTextFace{
Source: arcadeFaceSource,
Size: arcadeFontBaseSize * float64(scale),
}, op)
}

View File

@ -21,6 +21,7 @@ import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
type GamepadScene struct {
@ -103,5 +104,5 @@ ROTATE RIGHT: %s
msg = "OK!"
}
str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg)
drawTextWithShadow(screen, str, 16, 16, 1, color.White)
drawTextWithShadow(screen, str, 16, 16, 1, color.White, text.AlignStart, text.AlignStart)
}

View File

@ -28,6 +28,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/colorm"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
@ -85,7 +86,7 @@ func init() {
// Windows: Next
x, y = nextWindowLabelPosition()
drawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor)
drawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor, text.AlignStart, text.AlignStart)
x, y = nextWindowPosition()
drawWindow(imageWindows, x, y, 5*blockWidth, 5*blockHeight)
@ -102,26 +103,26 @@ func init() {
drawTextBox(imageWindows, "LINES", x, y, textBoxWidth())
// Gameover
imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80})
imageGameover.Fill(color.RGBA{0x00, 0x00, 0x00, 0x80})
y = (ScreenHeight - blockHeight) / 2
drawTextWithShadowCenter(imageGameover, "GAME OVER\n\nPRESS SPACE", 0, y, 1, color.White, ScreenWidth)
drawTextWithShadow(imageGameover, "GAME OVER\n\nPRESS SPACE", ScreenWidth/2, y, 1, color.White, text.AlignCenter, text.AlignStart)
}
func drawWindow(r *ebiten.Image, x, y, width, height int) {
vector.DrawFilledRect(r, float32(x), float32(y), float32(width), float32(height), color.RGBA{0, 0, 0, 0xc0}, false)
}
var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff}
var fontColor = color.RGBA{0x40, 0x40, 0xff, 0xff}
func drawTextBox(r *ebiten.Image, label string, x, y, width int) {
drawTextWithShadow(r, label, x, y, 1, fontColor)
y += blockWidth
drawTextWithShadow(r, label, x, y, 1, fontColor, text.AlignStart, text.AlignStart)
y += blockHeight
drawWindow(r, x, y, width, 2*blockHeight)
}
func drawTextBoxContent(r *ebiten.Image, content string, x, y, width int) {
y += blockWidth
drawTextWithShadowRight(r, content, x, y+blockHeight*3/4, 1, color.White, width-blockWidth/2)
y += blockHeight
drawTextWithShadow(r, content, x+width-2*blockHeight/4, y+2*blockHeight/2, 1, color.White, text.AlignEnd, text.AlignCenter)
}
type GameScene struct {

View File

@ -23,6 +23,7 @@ import (
"github.com/hajimehoshi/ebiten/v2"
rblocks "github.com/hajimehoshi/ebiten/v2/examples/resources/images/blocks"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
var imageBackground *ebiten.Image
@ -88,9 +89,9 @@ func (s *TitleScene) Draw(r *ebiten.Image) {
drawLogo(r, "BLOCKS")
message := "PRESS SPACE TO START"
x := 0
x := ScreenWidth / 2
y := ScreenHeight - 48
drawTextWithShadowCenter(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}, ScreenWidth)
drawTextWithShadow(r, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff}, text.AlignCenter, text.AlignStart)
}
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) {
@ -109,7 +110,7 @@ func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) {
func drawLogo(r *ebiten.Image, str string) {
const scale = 4
x := 0
x := ScreenWidth / 2
y := 32
drawTextWithShadowCenter(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff}, ScreenWidth)
drawTextWithShadow(r, str, x, y, scale, color.RGBA{0x00, 0x00, 0x80, 0xff}, text.AlignCenter, text.AlignStart)
}