mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
examples/block: use text/v2
This commit is contained in:
parent
acd5207142
commit
9c95b4accc
@ -15,16 +15,13 @@
|
|||||||
package blocks
|
package blocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"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/examples/resources/fonts"
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
||||||
"github.com/hajimehoshi/ebiten/v2/text"
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -32,65 +29,39 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
arcadeFonts map[int]font.Face
|
arcadeFaceSource *text.GoTextFaceSource
|
||||||
)
|
)
|
||||||
|
|
||||||
func getArcadeFonts(scale int) font.Face {
|
func init() {
|
||||||
if arcadeFonts == nil {
|
s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.PressStart2P_ttf))
|
||||||
tt, err := opentype.Parse(fonts.PressStart2P_ttf)
|
if err != nil {
|
||||||
if err != nil {
|
log.Fatal(err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return arcadeFonts[scale]
|
arcadeFaceSource = s
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
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) {
|
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, primaryAlign, secondaryAlign text.Align) {
|
||||||
offsetY := arcadeFontBaseSize * scale
|
op := &text.DrawOptions{}
|
||||||
for _, line := range strings.Split(str, "\n") {
|
op.GeoM.Translate(float64(x)+1, float64(y)+1)
|
||||||
y += offsetY
|
op.ColorScale.ScaleWithColor(shadowColor)
|
||||||
text.Draw(rt, line, getArcadeFonts(scale), x+1, y+1, shadowColor)
|
op.LineHeight = arcadeFontBaseSize * float64(scale)
|
||||||
text.Draw(rt, line, getArcadeFonts(scale), x, y, clr)
|
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) {
|
op.GeoM.Reset()
|
||||||
w := textWidth(str) * scale
|
op.GeoM.Translate(float64(x), float64(y))
|
||||||
x += (width - w) / 2
|
op.ColorScale.Reset()
|
||||||
drawTextWithShadow(rt, str, x, y, scale, clr)
|
op.ColorScale.ScaleWithColor(clr)
|
||||||
}
|
text.Draw(rt, str, &text.GoTextFace{
|
||||||
|
Source: arcadeFaceSource,
|
||||||
func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) {
|
Size: arcadeFontBaseSize * float64(scale),
|
||||||
w := textWidth(str) * scale
|
}, op)
|
||||||
x += width - w
|
|
||||||
drawTextWithShadow(rt, str, x, y, scale, clr)
|
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GamepadScene struct {
|
type GamepadScene struct {
|
||||||
@ -103,5 +104,5 @@ ROTATE RIGHT: %s
|
|||||||
msg = "OK!"
|
msg = "OK!"
|
||||||
}
|
}
|
||||||
str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2/colorm"
|
"github.com/hajimehoshi/ebiten/v2/colorm"
|
||||||
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ func init() {
|
|||||||
|
|
||||||
// Windows: Next
|
// Windows: Next
|
||||||
x, y = nextWindowLabelPosition()
|
x, y = nextWindowLabelPosition()
|
||||||
drawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor)
|
drawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor, text.AlignStart, text.AlignStart)
|
||||||
x, y = nextWindowPosition()
|
x, y = nextWindowPosition()
|
||||||
drawWindow(imageWindows, x, y, 5*blockWidth, 5*blockHeight)
|
drawWindow(imageWindows, x, y, 5*blockWidth, 5*blockHeight)
|
||||||
|
|
||||||
@ -102,26 +103,26 @@ func init() {
|
|||||||
drawTextBox(imageWindows, "LINES", x, y, textBoxWidth())
|
drawTextBox(imageWindows, "LINES", x, y, textBoxWidth())
|
||||||
|
|
||||||
// Gameover
|
// Gameover
|
||||||
imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80})
|
imageGameover.Fill(color.RGBA{0x00, 0x00, 0x00, 0x80})
|
||||||
y = (ScreenHeight - blockHeight) / 2
|
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) {
|
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)
|
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) {
|
func drawTextBox(r *ebiten.Image, label string, x, y, width int) {
|
||||||
drawTextWithShadow(r, label, x, y, 1, fontColor)
|
drawTextWithShadow(r, label, x, y, 1, fontColor, text.AlignStart, text.AlignStart)
|
||||||
y += blockWidth
|
y += blockHeight
|
||||||
drawWindow(r, x, y, width, 2*blockHeight)
|
drawWindow(r, x, y, width, 2*blockHeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawTextBoxContent(r *ebiten.Image, content string, x, y, width int) {
|
func drawTextBoxContent(r *ebiten.Image, content string, x, y, width int) {
|
||||||
y += blockWidth
|
y += blockHeight
|
||||||
drawTextWithShadowRight(r, content, x, y+blockHeight*3/4, 1, color.White, width-blockWidth/2)
|
drawTextWithShadow(r, content, x+width-2*blockHeight/4, y+2*blockHeight/2, 1, color.White, text.AlignEnd, text.AlignCenter)
|
||||||
}
|
}
|
||||||
|
|
||||||
type GameScene struct {
|
type GameScene struct {
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
rblocks "github.com/hajimehoshi/ebiten/v2/examples/resources/images/blocks"
|
rblocks "github.com/hajimehoshi/ebiten/v2/examples/resources/images/blocks"
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var imageBackground *ebiten.Image
|
var imageBackground *ebiten.Image
|
||||||
@ -88,9 +89,9 @@ func (s *TitleScene) Draw(r *ebiten.Image) {
|
|||||||
drawLogo(r, "BLOCKS")
|
drawLogo(r, "BLOCKS")
|
||||||
|
|
||||||
message := "PRESS SPACE TO START"
|
message := "PRESS SPACE TO START"
|
||||||
x := 0
|
x := ScreenWidth / 2
|
||||||
y := ScreenHeight - 48
|
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) {
|
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) {
|
func drawLogo(r *ebiten.Image, str string) {
|
||||||
const scale = 4
|
const scale = 4
|
||||||
x := 0
|
x := ScreenWidth / 2
|
||||||
y := 32
|
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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user