Line data Source code
1 : package menuframe
2 :
3 : import (
4 : "image/color"
5 :
6 : text "github.com/hajimehoshi/ebiten/v2/text/v2"
7 :
8 : "github.com/kijimaD/ruins/internal/resources"
9 : "github.com/kijimaD/ruins/internal/widgets/pagination"
10 : "github.com/kijimaD/ruins/internal/widgets/styled"
11 : "github.com/kijimaD/ruins/internal/widgets/theme"
12 : "github.com/kijimaD/ruins/internal/widgets/uicore"
13 : )
14 :
15 : // Row は一覧の1行。Cells は各列のセルで、アイコンと文字列が混ざってよい。
16 : // Header が真なら見出し行でカーソルが止まらない。
17 : // Indent は字下げの段数。0 は段なし。見出しを0、配下のエントリを1以上にするとまとまりが見える。
18 : // 詳細パネルの entityspec.SpecRow.Indent と同じ概念で、行全体を Indent*Space4 だけ右へ寄せる。
19 : type Row struct {
20 : Cells []styled.Cell
21 : Header bool
22 : Indent int
23 : // Disabled が真の行は淡色で描く。選べないことを見せるための無効表示で、カーソルは呼び出し側が飛ばす
24 : Disabled bool
25 : }
26 :
27 : // ListOpts は一覧描画の方針。HeaderRow は表の先頭に置く列見出し、選択やページ送りの対象には
28 : // 含めない、EmptyText は行が無いときに表の下へ出す説明。ページ表示はフッタ右端に出すので
29 : // 一覧側では扱わない。行の高さと間隔は並べる先の TabScreen・PanelScreen が決める。
30 : type ListOpts struct {
31 : HeaderRow []string
32 : EmptyText string
33 : // ItemsPerPage は1ページの行数の上書き。0 なら全行を1ページに収める。
34 : // 見出しやタブ帯を持つ画面は ListCapacity で自分の容量を求めて渡す。
35 : ItemsPerPage int
36 : }
37 :
38 : // RenderList は行データから見出し・選択強調・空行埋めを備えた行ウィジェット列と、
39 : // フッタ右端へ出すページ表示文字列を返す。ページ表示は複数ページのときだけ非空になる。
40 : // 呼び出し側は行を TabScreen/PanelScreen へ並べ、ページ表示をそのフッタへ渡す。
41 : // 1ページの件数は呼び出し側が opts.ItemsPerPage に解決して渡す。0 なら全行を1ページに収める。
42 : // itemIndex に負値を渡すとどの行も選択されず、カーソルを持たない表になる。
43 : // 列幅は CSS Grid のトラックと同じ規則で解決する。Fit の列は全行の内容の実測から、
44 : // Grow の列は余り幅から決まり、表全体で列が揃う。呼び出し側は幅の数値を書かない。
45 : func RenderList(itemIndex int, rows []Row, cols []styled.Col, opts ListOpts, res resources.UIResources) ([]uicore.Drawable, string) {
46 1 : face := res.Text.BodyFace
47 1 : colWidths := resolveColWidths(cols, opts.HeaderRow, rows, face)
48 1 : aligns := styled.Aligns(cols)
49 1 : perPage := opts.ItemsPerPage
50 1 : if perPage <= 0 {
51 1 : perPage = max(len(rows), 1)
52 1 : }
53 1 : pg := pagination.New(itemIndex, len(rows), perPage)
54 1 :
55 1 : var items []uicore.Drawable
56 1 : if opts.HeaderRow != nil {
57 : // 列見出しは表の先頭に置く見出しで、字下げはしない
58 1 : items = append(items, headerRow(opts.HeaderRow, colWidths, face, 0))
59 1 : }
60 1 : visible := pagination.VisibleEntries(rows, pg)
61 1 : for _, entry := range visible {
62 1 : if entry.Item.Header {
63 1 : items = append(items, headerRow(cellTexts(entry.Item.Cells), colWidths, face, entry.Item.Indent))
64 1 : continue
65 : }
66 1 : items = append(items, dataRow(entry.Item.Cells, colWidths, aligns, pg.IsSelectedInPage(entry.Index), entry.Item.Disabled, face, res, entry.Item.Indent))
67 : }
68 : // 複数ページの画面は各ページを1ページ件数ぶんの空行で埋め、ページを繰っても高さを一定にする
69 1 : if len(rows) > perPage {
70 1 : for i := len(visible); i < perPage; i++ {
71 1 : items = append(items, blankRow(colWidths, face))
72 1 : }
73 : }
74 : // 行が無いときの空表示を一覧側で持つ。各メニューが同じ後処理を書かずに済む
75 1 : if len(rows) == 0 && opts.EmptyText != "" {
76 1 : items = append(items, uicore.NewText(opts.EmptyText, face, theme.TextSecondary))
77 1 : }
78 1 : return items, pg.GetPageText()
79 : }
80 :
81 : // resolveColWidths は列のトラック幅を解決する。Fit の列は見出しと全行の内容の実測の最大値に
82 : // 列間の間隔を足した幅、Icon の列は正方の固定幅にする。Grow の列は 0 のままにし、
83 : // 行の flex が余り幅を割り当てる。
84 : func resolveColWidths(cols []styled.Col, headerRow []string, rows []Row, face text.Face) []int {
85 1 : widths := make([]int, len(cols))
86 1 : for i, c := range cols {
87 1 : switch c.Mode {
88 : case styled.ColIcon:
89 1 : widths[i] = theme.MenuIconW
90 : case styled.ColFit:
91 : // 見出しと全行のこの列の中身を並べ、いちばん広いものに列間の間隔を足す
92 1 : contents := make([]int, 0, len(rows)+1)
93 1 : if i < len(headerRow) {
94 1 : contents = append(contents, uicore.MeasureTextWidth(headerRow[i], face))
95 1 : }
96 1 : for _, r := range rows {
97 1 : if i >= len(r.Cells) {
98 1 : continue
99 : }
100 1 : if cell := r.Cells[i]; cell.Icon != nil {
101 : // アイコンセルは画像の実幅で測る。正方の一覧アイコンも
102 : // 横長のキーキャップも同じ規則で測れる
103 1 : contents = append(contents, cell.Icon.Bounds().Dx())
104 1 : } else {
105 1 : contents = append(contents, uicore.MeasureTextWidth(cell.Text, face))
106 1 : }
107 : }
108 1 : widths[i] = uicore.FitWidth(contents, theme.Space3, 0)
109 1 : case styled.ColGrow:
110 : // 0 のままにする。行の flex が余り幅を割り当てる
111 : }
112 : }
113 1 : return widths
114 : }
115 :
116 : // cellTexts は見出し行のセルから文字列を取り出す。見出しはアイコンを持たない前提で Text を並べる。
117 : func cellTexts(cells []styled.Cell) []string {
118 1 : texts := make([]string, len(cells))
119 1 : for i, c := range cells {
120 1 : texts[i] = c.Text
121 1 : }
122 1 : return texts
123 : }
124 :
125 : // toAlign は styled のそろえを uicore のそろえへ写す。
126 : func toAlign(a styled.TextAlign) uicore.Align {
127 1 : if a == styled.AlignRight {
128 1 : return uicore.AlignRight
129 1 : }
130 1 : return uicore.AlignLeft
131 : }
132 :
133 : // indentedRow は行を指定段数だけ字下げして組む。段数0ならそのまま、1以上なら先頭に
134 : // Indent*Space4 幅の空トラックを足して行全体を右へ寄せる。詳細パネルの字下げと同じ規則。
135 : func indentedRow(indent int, colWidths []int, cells []uicore.Widget) *uicore.Container {
136 1 : if indent <= 0 {
137 1 : return uicore.Row(colWidths, cells...)
138 1 : }
139 1 : widths := append([]int{indent * theme.Space4}, colWidths...)
140 1 : widgets := append([]uicore.Widget{uicore.NewGroup()}, cells...)
141 1 : return uicore.Row(widths, widgets...)
142 : }
143 :
144 : // headerRow は見出し行を組む。カーソルは止まらず、補助色で描く。
145 : func headerRow(texts []string, colWidths []int, face text.Face, indent int) *uicore.Container {
146 1 : cells := make([]uicore.Widget, len(texts))
147 1 : for i, s := range texts {
148 1 : t := uicore.NewText(s, face, theme.TextSecondary)
149 1 : t.VCenter = true
150 1 : cells[i] = t
151 1 : }
152 1 : return indentedRow(indent, colWidths, cells)
153 : }
154 :
155 : // dataRow はデータ行を組む。選択中なら金色の選択バーを敷き文字色を選択色にする。無効行は淡色で描く。
156 : // アイコンセルは画像で描く。
157 : func dataRow(cells []styled.Cell, colWidths []int, aligns []styled.TextAlign, selected bool, disabled bool, face text.Face, res resources.UIResources, indent int) *uicore.Container {
158 : // 非選択は暗く、選択は明るくして、カーソル位置を際立たせる。無効行はさらに淡くして選べないことを示す
159 1 : var textColor color.Color = theme.TextSecondary
160 1 : switch {
161 : case disabled:
162 1 : textColor = theme.TextDisabled
163 : case selected:
164 1 : textColor = theme.TextSelected
165 : }
166 1 : cellWidgets := make([]uicore.Widget, len(cells))
167 1 : for i, c := range cells {
168 1 : if c.Icon != nil {
169 1 : cellWidgets[i] = uicore.NewGraphic(c.Icon)
170 1 : continue
171 : }
172 1 : t := uicore.NewText(c.Text, face, textColor)
173 1 : t.VCenter = true
174 1 : if i < len(aligns) {
175 1 : t.Align = toAlign(aligns[i])
176 1 : }
177 1 : cellWidgets[i] = t
178 : }
179 1 : return rowChrome(indentedRow(indent, colWidths, cellWidgets), res, selected)
180 : }
181 :
182 : // SelectionRow は中身を持たない1行ぶんの意匠を返す。選択中の強調と下端の区切り線だけを持ち、
183 : // 中身は呼び出し側が別に重ねる。行の内容を列で表せない一覧が、意匠だけを共有するのに使う。
184 : func SelectionRow(res resources.UIResources, selected bool) uicore.Widget {
185 1 : return rowChrome(uicore.Row(nil), res, selected)
186 1 : }
187 :
188 : // rowChrome は一覧の1行に共通の意匠を着せる。選択中は金色のバーを敷き、下端に区切り線を引く。
189 : // この2つが一覧の行の見た目を決めるので、定義をここ1箇所に置く。
190 : func rowChrome(row *uicore.Container, res resources.UIResources, selected bool) *uicore.Container {
191 1 : if selected && res.SelectionBar != nil {
192 1 : row.SetBackgroundNineSlice(res.SelectionBar.Image, res.SelectionBar.BX, res.SelectionBar.BY)
193 1 : }
194 : // RowDivider は非乗算済みの値なので NRGBA として色を掛ける
195 1 : if res.GradientLine != nil {
196 1 : row.SetBottomLine(res.GradientLine, color.NRGBA(theme.RowDivider))
197 1 : }
198 1 : return row
199 : }
200 :
201 : // blankRow は高さを揃えるための空行を組む。高さは行高が確保するので文字は持たせず、
202 : // フォント測定と描画を省く。
203 : func blankRow(colWidths []int, face text.Face) *uicore.Container {
204 1 : cells := make([]uicore.Widget, len(colWidths))
205 1 : for i := range cells {
206 1 : cells[i] = uicore.NewText("", face, theme.TextPrimary)
207 1 : }
208 1 : return uicore.Row(colWidths, cells...)
209 : }
|