Line data Source code
1 : package hud
2 :
3 : import (
4 : "image"
5 : "image/color"
6 :
7 : "github.com/hajimehoshi/ebiten/v2"
8 : "github.com/hajimehoshi/ebiten/v2/text/v2"
9 : theme "github.com/kijimaD/ruins/internal/widgets/theme"
10 : "github.com/kijimaD/ruins/internal/widgets/uicore"
11 : w "github.com/kijimaD/ruins/internal/world"
12 : )
13 :
14 : // MacroMap は HUD 右上のマクロ地図エリア。N キーで開く地形俯瞰と同じ内容を縮小して常時表示する。
15 : // キューブが同じステージにいる間は、地図の下へ名前と燃料ゲージを並べる。
16 : type MacroMap struct {
17 : face text.Face // 地形セルの記号とキューブに使うフォント
18 : labelFace text.Face // 地図の下のキューブ名に使うフォント
19 : gaugeFill *ebiten.Image // 燃料ゲージの塗りテクスチャ
20 : chrome Chrome
21 : enabled bool
22 : }
23 :
24 : // NewMacroMap は新しい HUD マクロ地図を作成する。
25 : func NewMacroMap(face, labelFace text.Face, gaugeFill *ebiten.Image, chrome Chrome) *MacroMap {
26 1 : return &MacroMap{face: face, labelFace: labelFace, gaugeFill: gaugeFill, chrome: chrome, enabled: true}
27 1 : }
28 :
29 : // Update はマクロ地図を更新する。描画データは毎フレーム抽出されるので保持状態は持たない。
30 0 : func (m *MacroMap) Update(_ w.World) {}
31 :
32 : // Draw はプレイヤー近傍の地形俯瞰をパネルへ描く。オーバーワールド外は地図を出さない。
33 : func (m *MacroMap) Draw(cv uicore.Canvas, data MacroMapData) {
34 1 : if !m.enabled {
35 1 : return
36 1 : }
37 : // オーバーワールド外は帯が無いので、地図パネルごと出さない
38 1 : if !data.HasBand || len(data.View.Cells) == 0 || len(data.View.Cells[0]) == 0 {
39 1 : return
40 1 : }
41 1 : width, height := data.Config.Width, data.Config.Height
42 1 : if width <= 0 || height <= 0 {
43 0 : return
44 0 : }
45 : // 右上へ固定サイズのパネルを置く。先頭0幅列で右寄せ、末尾 Grow で上寄せする
46 1 : panel := ¯oPanelWidget{chrome: m.chrome, face: m.face, data: data}
47 1 : row := uicore.Row([]int{0, width}, uicore.NewGroup(), panel)
48 1 : items := []uicore.FlexItem{{W: row, Height: height}}
49 1 : if data.Cube.Visible {
50 1 : items = append(items, m.cubeStatusItems(data.Cube, width)...)
51 1 : }
52 1 : items = append(items, uicore.FlexItem{Grow: true})
53 1 : inner := image.Rect(0, theme.Space4, data.Screen.Width-theme.Space4, data.Screen.Height)
54 1 : uicore.FlexColumn(inner, items)
55 1 : drawFlexItems(cv, items)
56 : }
57 :
58 : // cubeStatusItems は地図の下へ積む、キューブの名前と地図と同じ幅の燃料ゲージを返す。
59 : func (m *MacroMap) cubeStatusItems(cube CubeStatusData, width int) []uicore.FlexItem {
60 1 : _, labelH := uicore.MeasureText(cube.Name, m.labelFace)
61 1 : label := uicore.Row([]int{0, width},
62 1 : uicore.NewGroup(),
63 1 : &uicore.Text{Value: cube.Name, Face: m.labelFace, Color: theme.TextPrimary, OutlineColor: theme.HUDTextOutline},
64 1 : )
65 1 : gauge := uicore.Row([]int{0, width}, uicore.NewGroup(), fuelGauge(m.gaugeFill, cube.FuelRatio))
66 1 : return []uicore.FlexItem{
67 1 : {Height: theme.Space2},
68 1 : {W: label, Height: labelH},
69 1 : {Height: gaugeSpacing},
70 1 : {W: gauge, Height: gaugeHeight},
71 1 : }
72 : }
73 :
74 : // macroPanelWidget は右上のマクロ地図パネル。与えられた矩形に背景枠を敷き、帯全体を中央寄せで描く。
75 : // 地図のセル座標計算は地図の本質なのでここに残し、パネルの画面内配置だけ FlexColumn/Row へ委ねる。
76 : type macroPanelWidget struct {
77 : rect image.Rectangle
78 : chrome Chrome
79 : face text.Face
80 : data MacroMapData
81 : }
82 :
83 : // Layout は uicore.Widget を満たす。
84 1 : func (m *macroPanelWidget) Layout(r image.Rectangle) { m.rect = r }
85 :
86 : // Draw は uicore.Widget を満たす。背景枠と、帯全体を収める正方セルの地図を描く。
87 : func (m *macroPanelWidget) Draw(cv uicore.Canvas) {
88 : // 背景枠はメニュー枠と同じ共通 chrome に揃える
89 1 : m.chrome.Panel(cv, m.rect)
90 1 :
91 1 : rows := len(m.data.View.Cells)
92 1 : cols := len(m.data.View.Cells[0])
93 1 : // 帯全体がパネルに収まるようセル辺を決める。縦横で小さいほうに合わせて正方セルにする
94 1 : cellPx := m.rect.Dx() / cols
95 1 : if h := m.rect.Dy() / rows; h < cellPx {
96 0 : cellPx = h
97 0 : }
98 1 : if cellPx < 1 {
99 0 : cellPx = 1
100 0 : }
101 : // 地図をパネル内で中央寄せする余白
102 1 : offX := m.rect.Min.X + (m.rect.Dx()-cellPx*cols)/2
103 1 : offY := m.rect.Min.Y + (m.rect.Dy()-cellPx*rows)/2
104 1 :
105 : // 格子・道・キューブ・現在地を DrawMapGrid で描く。ミニマップは記号表示に閾値を持つ
106 1 : DrawMapGrid(cv, m.data.View, MapGridStyle{
107 1 : OriginX: offX,
108 1 : OriginY: offY,
109 1 : CellPx: cellPx,
110 1 : MinGlyphPx: m.data.Config.MinGlyphPx,
111 1 : GlyphFace: m.face,
112 1 : PlayerFacing: m.data.PlayerFacing,
113 1 : })
114 : }
115 :
116 : // Children は uicore.Widget を満たす。子は持たない。
117 0 : func (m *macroPanelWidget) Children() []uicore.Widget { return nil }
118 :
119 : // drawCenteredGlyph はセルの中央に1文字を描く。字形の外接矩形を測って四辺の余白を揃えるので、
120 : // フォントの行メトリクスでなく実際の字形がセル中央へ来る。小さいセルでも記号が上下へ偏らない。
121 : func drawCenteredGlyph(cv uicore.Canvas, s string, face text.Face, x, y, cell int, col color.Color) {
122 1 : tw, th := uicore.MeasureText(s, face)
123 1 : cv.DrawText(image.Pt(x+(cell-tw)/2, y+(cell-th)/2), s, face, col)
124 1 : }
|