Files
ngzz-mc/cmd/client/screens_gl.go

81 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build gl
// 界面屏幕构建:中文标题与按钮(UI还原.md §2、§4 中文回退)。
package main
import (
"image/color"
"os"
"path/filepath"
"mc/internal/assets"
"mc/internal/cjkfont"
"mc/internal/ui"
)
// cjkPaths 系统 CJK 字体候选。
var cjkPaths = []string{
filepath.Join(os.Getenv("WINDIR"), "Fonts", "msyh.ttc"),
filepath.Join(os.Getenv("WINDIR"), "Fonts", "msyh.ttf"),
}
// buildScreens 构建主菜单与暂停菜单(按钮纹理 = 中文文本光栅化)。
func buildScreens(r *ui.Renderer, lang *assets.Lang) (*ui.TitleScreen, *ui.PauseScreen) {
cjk := loadCJK()
text := func(key, fallback string, size float64) (uint32, float32, float32) {
label := fallback
if lang != nil {
label = lang.Get(key)
}
if cjk != nil {
img := cjk.Draw(label, color.RGBA{255, 255, 255, 255})
tex := r.UploadImage(img)
return tex, float32(img.Bounds().Dx()), float32(img.Bounds().Dy())
}
return 0, 0, 0
}
titleTex, titleW, titleH := text("menu.title", "年糕历险记", 48)
if titleW == 0 {
titleW, titleH = 320, 48 // 无 CJK 字体时占位
}
// 主菜单按钮(居中列,UI还原.md §2)
btn := func(key, fallback, action string, y float32, w, h int) ui.Button {
tex, tw, th := text(key, fallback, 24)
x := float32(w)/2 - tw/2
return ui.Button{X: x, Y: y, W: tw, H: th, Tex: tex, Action: action}
}
title := &ui.TitleScreen{}
title.TitleTex = titleTex
title.TitleW = titleW
title.TitleH = titleH
title.Buttons = []ui.Button{
btn("menu.singleplayer", "单人游戏", "play", 300, 1280, 720),
btn("menu.multiplayer", "多人游戏", "multi", 350, 1280, 720),
btn("menu.quit", "退出游戏", "quit", 400, 1280, 720),
}
_ = titleTex
_ = titleH
pause := &ui.PauseScreen{}
pause.TitleTex, pause.TitleW, pause.TitleH = text("menu.game", "游戏菜单", 32)
pause.Buttons = []ui.Button{
btn("gui.resume", "继续游戏", "resume", 300, 1280, 720),
btn("menu.quit", "保存并退出", "quit", 350, 1280, 720),
}
return title, pause
}
// loadCJK 加载系统 CJK 字体(失败返回 nil → 按钮退化不显示文本)。
func loadCJK() *cjkfont.Renderer {
for _, p := range cjkPaths {
if _, err := os.Stat(p); err == nil {
if r, err := cjkfont.Load(p, 24); err == nil {
return r
}
}
}
return nil
}