Line data Source code
1 : package menuframe
2 :
3 : import (
4 : "image"
5 :
6 : "github.com/hajimehoshi/ebiten/v2"
7 : text "github.com/hajimehoshi/ebiten/v2/text/v2"
8 :
9 : "github.com/kijimaD/ruins/internal/resources"
10 : "github.com/kijimaD/ruins/internal/widgets/theme"
11 : "github.com/kijimaD/ruins/internal/widgets/uicore"
12 : w "github.com/kijimaD/ruins/internal/world"
13 : )
14 :
15 : // PanelInner はパネル矩形の内側、すなわち標準の内側余白ぶん縮めた矩形を返す。パネルに収める
16 : // 中身の寸法・配置は必ずこれを通す。ImagePanel の画像配置も、呼び出し側の画像サイズ決定も
17 : // 同じ式にして、余白の値が変わっても両者がずれないようにする。
18 : func PanelInner(rect image.Rectangle) image.Rectangle {
19 1 : return image.Rect(rect.Min.X+theme.MenuPad, rect.Min.Y+theme.MenuPad, rect.Max.X-theme.MenuPad, rect.Max.Y-theme.MenuPad)
20 1 : }
21 :
22 : // ImagePanelArea は ImagePanel が画像を置く矩形を返す。パネルの内側から下端のフッタ行を除いた範囲。
23 : func ImagePanelArea(rect image.Rectangle) image.Rectangle {
24 1 : area := PanelInner(rect)
25 1 : area.Max.Y -= theme.MenuTabRowH
26 1 : return area
27 1 : }
28 :
29 : // ImagePanel は1枚の画像を PanelBG のパネルへ収めて返す。格子や図のように、リストの行に馴染ま
30 : // ない内容を他メニューと同じパネル意匠で見せる画面が使う。footer は TabScreen と同じ下端の行に置く。
31 : func ImagePanel(res resources.UIResources, rect image.Rectangle, img *ebiten.Image, footer string) uicore.Widget {
32 1 : bg := uicore.NewBox(theme.PanelBackground, theme.PanelHighlight, theme.CornerRadius)
33 1 : bg.Layout(rect)
34 1 : area := ImagePanelArea(rect)
35 1 : gfx := uicore.NewGraphic(img)
36 1 : gfx.Layout(area)
37 1 : foot := footerRow(footer, "", res)
38 1 : foot.Layout(image.Rect(area.Min.X, area.Max.Y, area.Max.X, area.Max.Y+theme.MenuTabRowH))
39 1 : group := uicore.NewGroup(bg, gfx, foot)
40 1 : group.Layout(rect)
41 1 : return group
42 1 : }
43 :
44 : // noteRowH は補助フェイスを載せる1行の高さ。操作ヒント・説明・版の注記・入力欄のエラーに使う。
45 : // 役割が同じ行はどの画面でも同じ高さにして、画面をまたいだ見た目のずれを防ぐ
46 : const noteRowH = 16
47 :
48 : // panelBackground はパネルの背景を敷く。
49 : func panelBackground(c *uicore.Container, _ resources.UIResources) *uicore.Container {
50 1 : return c.SetStyle(uicore.BoxStyle{Fill: theme.PanelBackground, Border: theme.PanelHighlight, BorderWidth: 1, Radius: theme.CornerRadius})
51 1 : }
52 :
53 : // PanelBox はパネルテクスチャを敷いた縦積みの箱を返す。行高・余白・背景は標準の既定に従う。
54 : // TabScreen・PanelScreen に収まらない独自配置の画面が、意匠だけを部品へ合わせるのに使う。
55 : // 置き場所は呼び出し側が Layout で決める。
56 : func PanelBox(res resources.UIResources, content ...uicore.Drawable) uicore.Widget {
57 1 : return panelBackground(uicore.Panel(uicore.BoxStyle{}, theme.MenuPanelRowH, uicore.Placeable(content)...), res).SetPadding(theme.MenuPad)
58 1 : }
59 :
60 : // footerRow はフッタ行を組む。左にヘルプ、右端にページ表示を並べる。
61 : // ヘルプ列を0幅で伸ばして余りを埋め、ページ表示を右へ寄せる。末尾に余白列を置いて右端を空ける。
62 : // pager が空ならページ表示は出ない。
63 : func footerRow(footer, pager string, res resources.UIResources) *uicore.Container {
64 1 : face := res.Text.SmallFace
65 1 : help := uicore.NewText(footer, face, theme.TextSecondary)
66 1 : help.VCenter = true
67 1 : pg := uicore.NewText(pager, face, theme.TextPrimary)
68 1 : pg.Align = uicore.AlignRight
69 1 : pg.VCenter = true
70 1 : gap := uicore.NewText("", face, theme.TextSecondary)
71 1 : return uicore.Row([]int{0, theme.MenuPagerW, theme.Space3}, help, pg, gap)
72 1 : }
73 :
74 : // PanelScreen は見出し・内容行・フッタを1枚のパネルへ縦に並べ、上端を固定して配置して返す。
75 : // 背景はパネルテクスチャを敷く。内容は RenderList が返す行をそのまま渡す。ページ表示は
76 : // フッタ行の右端に置く。上端固定により、項目数が違ってもタイトル・先頭項目が常に同じ位置に来る。
77 : // 選択メニュー choice や設定メニューなど、項目数相応に伸びるパネル画面で使う。
78 : func PanelScreen(world w.World, res resources.UIResources, title string, content []uicore.Drawable, footer, pager string) uicore.Widget {
79 1 : return panelScreen(world, res, title, content, footer, pager, theme.MenuPanelRowH)
80 1 : }
81 :
82 : // PanelScreenDense は PanelScreen の密行版。行間を空けるコマンドメニューと違い、
83 : // キー一覧のような表を詰まった行高で項目数相応の小さなパネルに収める。
84 : // 項目が多くてもログ領域に被らずに収まる。
85 : func PanelScreenDense(world w.World, res resources.UIResources, title string, content []uicore.Drawable, footer, pager string) uicore.Widget {
86 1 : return panelScreen(world, res, title, content, footer, pager, theme.MenuTabRowH)
87 1 : }
88 :
89 : // panelScreen はパネル画面の実体。行高だけを密度の変種として受け取る。
90 : func panelScreen(world w.World, res resources.UIResources, title string, content []uicore.Drawable, footer, pager string, rowH int) uicore.Widget {
91 1 : face := res.Text.BodyFace
92 1 : items := make([]uicore.FlexItem, 0, len(content)+3)
93 1 : if title != "" {
94 1 : items = append(items, uicore.FlexItem{W: uicore.NewText(title, face, theme.TextPrimary), Height: rowH})
95 1 : }
96 1 : for _, c := range uicore.Placeable(content) {
97 1 : items = append(items, uicore.FlexItem{W: c, Height: rowH})
98 1 : }
99 1 : if footer != "" || pager != "" {
100 : // フッタは内容から一行空けて置く
101 1 : items = append(items, uicore.FlexItem{Height: rowH})
102 1 : items = append(items, uicore.FlexItem{W: footerRow(footer, pager, res), Height: rowH})
103 1 : }
104 :
105 : // パネルの高さは内容に合わせる。横は画面中央、縦は上端を固定する。項目数が違っても
106 : // パネルの開始位置がそろい、メニュー間でタイトル・先頭項目の位置がずれない
107 1 : panelW := theme.MenuRowWidth + theme.MenuPad*2
108 1 : panelH := len(items)*rowH + theme.MenuPad*2
109 1 : crect := WindowRect(world)
110 1 : x := crect.Min.X + (crect.Dx()-panelW)/2
111 1 : y := crect.Min.Y
112 1 : rect := image.Rect(x, y, x+panelW, y+panelH)
113 1 : inner := image.Rect(rect.Min.X+theme.MenuPad, rect.Min.Y+theme.MenuPad, rect.Max.X-theme.MenuPad, rect.Max.Y-theme.MenuPad)
114 1 : uicore.FlexColumn(inner, items)
115 1 :
116 1 : return groupWithPanelBG(rect, res, items)
117 : }
118 :
119 : // tabBar はタブ帯を組む。ラベルを等幅で横に並べ、選択中のタブは金色の選択バーを敷き主色で、
120 : // 他は補助色で描く。
121 : func tabBar(labels []string, selected int, totalWidth int, face text.Face, selBar *resources.NineSliceTex) *uicore.Container {
122 1 : n := len(labels)
123 1 : widths := make([]int, n)
124 1 : cells := make([]uicore.Widget, n)
125 1 : for i, label := range labels {
126 1 : widths[i] = totalWidth / n
127 1 : clr := theme.TextSecondary
128 1 : if i == selected {
129 1 : clr = theme.TextSelected
130 1 : }
131 1 : t := uicore.NewText(label, face, clr)
132 1 : t.Align = uicore.AlignCenter
133 1 : t.VCenter = true
134 1 : if i == selected {
135 : // 選択タブは金色の選択バーを敷いたセルにする
136 1 : cells[i] = tabSelCell(t, selBar)
137 1 : } else {
138 1 : cells[i] = t
139 1 : }
140 : }
141 1 : return uicore.Row(widths, cells...)
142 : }
143 :
144 : // tabSelCell は子を金色の選択バーの上に置くセルを返す。選択タブの強調に使う。
145 : func tabSelCell(child uicore.Widget, selBar *resources.NineSliceTex) *uicore.Container {
146 1 : cell := uicore.VBox(theme.MenuTabRowH, child)
147 1 : if selBar != nil {
148 1 : cell.SetBackgroundNineSlice(selBar.Image, selBar.BX, selBar.BY)
149 1 : }
150 1 : return cell
151 : }
152 :
153 : // TabScreen は見出し・タブ帯・内容行・フッタを1枚のモーダルに縦へ並べて返す。
154 : // モーダルは中央固定枠 ModalRect いっぱいに広げる。タブ帯と一覧の間に一行の余白を空ける。
155 : // フッタは flex-grow のスペーサで常に下端へ固定し、内容の量によらずヘルプの位置がぶれない。
156 : // 容量の逆算や空行埋めは要らない。ページ表示はフッタ行の右端に置く。
157 : func TabScreen(world w.World, res resources.UIResources, header string, tabLabels []string, tabIndex int, content []uicore.Drawable, footer, pager string) uicore.Widget {
158 1 : face := res.Text.BodyFace
159 1 : rect := ModalRect(world)
160 1 : inner := image.Rect(rect.Min.X+theme.MenuPad, rect.Min.Y+theme.MenuPad, rect.Max.X-theme.MenuPad, rect.Max.Y-theme.MenuPad)
161 1 :
162 1 : var items []uicore.FlexItem
163 1 : if header != "" {
164 1 : h := uicore.NewText(header, face, theme.TextPrimary)
165 1 : h.Align = uicore.AlignCenter
166 1 : items = append(items, uicore.FlexItem{W: h, Height: theme.MenuTabRowH})
167 1 : }
168 1 : if len(tabLabels) > 0 {
169 1 : items = append(items, uicore.FlexItem{W: tabBar(tabLabels, tabIndex, inner.Dx(), face, res.SelectionBar), Height: theme.MenuTabRowH})
170 1 : }
171 : // タブ帯と一覧を離す。詰まりすぎないよう一行空ける
172 1 : if header != "" || len(tabLabels) > 0 {
173 1 : items = append(items, uicore.FlexItem{Height: theme.MenuTabRowH})
174 1 : }
175 1 : for _, c := range uicore.Placeable(content) {
176 1 : items = append(items, uicore.FlexItem{W: c, Height: theme.MenuTabRowH})
177 1 : }
178 1 : if footer != "" || pager != "" {
179 1 : items = append(items, uicore.FlexItem{Grow: true})
180 1 : items = append(items, uicore.FlexItem{W: footerRow(footer, pager, res), Height: theme.MenuTabRowH})
181 1 : }
182 1 : uicore.FlexColumn(inner, items)
183 1 :
184 1 : return groupWithPanelBG(rect, res, items)
185 : }
186 :
187 : // groupWithPanelBG はパネルの背景と、配置済みの flex 行を1つの Group に束ねる。
188 : func groupWithPanelBG(rect image.Rectangle, _ resources.UIResources, items []uicore.FlexItem) uicore.Widget {
189 1 : bg := uicore.NewBox(theme.PanelBackground, theme.PanelHighlight, theme.CornerRadius)
190 1 : bg.Layout(rect)
191 1 : children := make([]uicore.Widget, 0, len(items)+1)
192 1 : children = append(children, bg)
193 1 : for _, it := range items {
194 1 : if it.W != nil {
195 1 : children = append(children, it.W)
196 1 : }
197 : }
198 1 : group := uicore.NewGroup(children...)
199 1 : group.Layout(rect)
200 1 : return group
201 : }
|