Line data Source code
1 : package menuloop
2 :
3 : import (
4 : "github.com/hajimehoshi/ebiten/v2"
5 : text "github.com/hajimehoshi/ebiten/v2/text/v2"
6 : "github.com/hajimehoshi/ebiten/v2/vector"
7 : es "github.com/kijimaD/ruins/internal/engine/states"
8 : "github.com/kijimaD/ruins/internal/inputmapper"
9 : "github.com/kijimaD/ruins/internal/keybind"
10 : "github.com/kijimaD/ruins/internal/resources"
11 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
12 : "github.com/kijimaD/ruins/internal/widgets/styled"
13 : "github.com/kijimaD/ruins/internal/widgets/theme"
14 : "github.com/kijimaD/ruins/internal/widgets/uicore"
15 : w "github.com/kijimaD/ruins/internal/world"
16 : "github.com/kijimaD/ruins/internal/world/query"
17 : )
18 :
19 : // KeyHelpState は現在の文脈のキー束縛一覧を表示するステート。? でどの画面からも開ける。
20 : // 表示は開いた画面の束縛表から導出するので、キーを変えれば一覧が追随する。
21 : // メニュー画面では Screen の入力ゲートが ActionOpenKeyHelp を吸ってこれを push し、
22 : // メニュー外の画面は自分の DoAction から push する
23 : type KeyHelpState struct {
24 : es.BaseState[w.World]
25 : table []keybind.Binding
26 : body uicore.Drawable
27 : }
28 :
29 : var _ es.State[w.World] = &KeyHelpState{}
30 :
31 : // NewKeyHelpState は合成済みの表のキー一覧を表示するヘルプのファクトリを返す
32 : func NewKeyHelpState(table []keybind.Binding) es.StateFactory[w.World] {
33 1 : return func() (es.State[w.World], error) {
34 1 : return &KeyHelpState{table: table}, nil
35 1 : }
36 : }
37 :
38 : // keyHelpBindings はヘルプ画面自身の束縛表。Esc か開いたときと同じ ? で閉じる
39 : var keyHelpBindings = []keybind.Binding{
40 : {Key: ebiten.KeyEscape, Action: inputmapper.ActionCloseMenu, Label: "Back"},
41 : {Key: ebiten.KeySlash, Shift: keybind.ShiftRequired, Action: inputmapper.ActionCloseMenu},
42 : }
43 :
44 : // OnStart は一覧の UI を組む。束縛表は state の寿命の間変わらないので1度だけ組めばよい。
45 : // キーは1粒ずつ描き、箱を持たないグリフには白い箱を敷いて、全キーを白背景に黒グリフの
46 : // キーキャップで統一する。枠・行高・余白・行の意匠は menuframe の部品の既定に従う
47 : func (st *KeyHelpState) OnStart(world w.World) error {
48 1 : res := world.Resources.UIResources
49 1 : entries := keybind.HintEntries(world, st.table)
50 1 : // 開いた画面の表に Esc の表示行が無ければ、ヘルプ自身の閉じ方を末尾に足す。
51 : // メニューは共通表の Back 行が既にあり、ダンジョンのような表には無いので補う
52 1 : if !hasEscapeLabel(st.table) {
53 1 : entries = append(entries, keybind.HintEntries(world, keyHelpBindings)...)
54 1 : }
55 :
56 1 : rows := make([]menuframe.Row, len(entries))
57 1 : for i, e := range entries {
58 1 : rows[i] = menuframe.Row{Cells: []styled.Cell{
59 1 : styled.IconCell(renderKeycaps(e.Tokens, res)),
60 1 : styled.TextCell(e.Label),
61 1 : }}
62 : }
63 : // カーソルは持たないので負のインデックスを渡す
64 1 : list, pager := menuframe.RenderList(-1, rows, styled.Cols(styled.Fit(), styled.Desc()), menuframe.ListOpts{}, res)
65 1 : st.body = menuframe.PanelScreenDense(world, res, query.T(world, "Key bindings"), list, "", pager)
66 1 : return nil
67 : }
68 :
69 : func hasEscapeLabel(table []keybind.Binding) bool {
70 1 : for _, b := range table {
71 1 : if b.Key == ebiten.KeyEscape && b.Label != "" {
72 1 : return true
73 1 : }
74 : }
75 1 : return false
76 : }
77 :
78 : // renderKeycaps はキーの粒の並びを1枚の画像へ描く。全トークンへ一律に角丸の箱を敷き、
79 : // 中の表記を黒で描いてキーキャップに見せる。中身は全てアイコンフォントのグリフなので、
80 : // face 1つで描ける。
81 : // widget の入れ子で組むと preferred 幅の計算で潰れるため、画像にして寸法を確定させる
82 : func renderKeycaps(tokens []string, res resources.UIResources) *ebiten.Image {
83 : // 箱の高さは一覧アイコンの正方と同じにして、行内の見た目の粒を揃える
84 1 : const height = theme.MenuIconW
85 1 : // 箱内の左右余白とチップ間の間隔は最小間隔で統一する
86 1 : const chipPad = theme.Space2
87 1 : // 角丸の半径。キーキャップの箱の意匠
88 1 : const radius = 4
89 1 :
90 1 : type keycap struct {
91 1 : text string
92 1 : face text.Face
93 1 : w int
94 1 : h int
95 1 : }
96 1 : caps := make([]keycap, 0, len(tokens))
97 1 : total := 0
98 1 : face := res.Text.KeycapFace
99 1 : for i, tok := range tokens {
100 1 : w, h := uicore.MeasureText(tok, face)
101 1 : cw := w + chipPad*2
102 1 : caps = append(caps, keycap{text: tok, face: face, w: cw, h: h})
103 1 : total += cw
104 1 : if i > 0 {
105 1 : total += theme.Space2
106 1 : }
107 : }
108 1 : if total <= 0 {
109 1 : total = 1
110 1 : }
111 :
112 1 : img := ebiten.NewImage(total, height)
113 1 : x := 0
114 1 : for i, c := range caps {
115 1 : if i > 0 {
116 1 : x += theme.Space2
117 1 : }
118 1 : fillRoundedRect(img, float32(x), 0, float32(c.w), height, radius)
119 1 : op := &text.DrawOptions{}
120 1 : op.GeoM.Translate(float64(x+chipPad), float64(height-c.h)/2)
121 1 : op.ColorScale.ScaleWithColor(theme.ScreenBackground)
122 1 : text.Draw(img, c.text, c.face, op)
123 1 : x += c.w
124 : }
125 1 : return img
126 : }
127 :
128 : // fillRoundedRect は角丸の塗り矩形を描く。キーキャップの箱に使う
129 : func fillRoundedRect(dst *ebiten.Image, x, y, w, h, r float32) {
130 1 : var p vector.Path
131 1 : p.MoveTo(x+r, y)
132 1 : p.LineTo(x+w-r, y)
133 1 : p.ArcTo(x+w, y, x+w, y+r, r)
134 1 : p.LineTo(x+w, y+h-r)
135 1 : p.ArcTo(x+w, y+h, x+w-r, y+h, r)
136 1 : p.LineTo(x+r, y+h)
137 1 : p.ArcTo(x, y+h, x, y+h-r, r)
138 1 : p.LineTo(x, y+r)
139 1 : p.ArcTo(x, y, x+r, y, r)
140 1 : p.Close()
141 1 : op := &vector.DrawPathOptions{AntiAlias: true}
142 1 : op.ColorScale.ScaleWithColor(theme.TextPrimary)
143 1 : vector.FillPath(dst, &p, nil, op)
144 1 : }
145 :
146 : // Update は閉じる入力だけを読む。ヘルプ表示中は時間を進めない
147 : func (st *KeyHelpState) Update(world w.World) (es.Transition[w.World], error) {
148 1 : if action, ok := keybind.ReadInput(world, keyHelpBindings); ok && action == inputmapper.ActionCloseMenu {
149 1 : return es.Transition[w.World]{Type: es.TransPop}, nil
150 1 : }
151 1 : return st.ConsumeTransition(), nil
152 : }
153 :
154 : // Draw は保持中の一覧を描く
155 : func (st *KeyHelpState) Draw(_ w.World, screen *ebiten.Image) error {
156 1 : st.body.Draw(uicore.NewEbitenCanvas(screen))
157 1 : return nil
158 1 : }
|