Line data Source code
1 : package messagewindow
2 :
3 : import (
4 : "image"
5 : "image/color"
6 :
7 : text "github.com/hajimehoshi/ebiten/v2/text/v2"
8 : "github.com/kijimaD/ruins/internal/messagedata"
9 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
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 : "github.com/kijimaD/ruins/internal/world/query"
14 : )
15 :
16 : const (
17 : titleBarHeight = 25 // タイトルバーの高さ
18 : choiceRowH = 26 // 選択肢1行の高さ。本文 + 上下パディング + 区切り線
19 : choiceTopPad = 0 // 選択肢一覧の上パディング
20 : choicePadL = theme.Space3 // 選択肢の左パディング
21 : choiceLabelGap = theme.Space2 // ラベルと追加ラベルの間隔
22 : pageRowH = 24 // ページ表示の高さ
23 : choiceMinWidth = 120 // 選択肢の塊の最小幅
24 : // messageAreaHeight は本文に確保する高さ。行数で伸縮させず、窓の大きさを一定に保つ
25 : messageAreaHeight = 150
26 : )
27 :
28 : // chromeHeight は選択肢の塊を除いた窓の取り分を返す。タイトルバー・本文欄・上下の余白の合計で、
29 : // 選択肢が何件でも変わらない。窓の高さの算出と、1ページに入る件数の逆算が同じ値を使う。
30 : func (win *Window) chromeHeight() int {
31 1 : h := theme.Space6 + theme.Space5 // 本文上の余白と、選択肢の塊の下の余白
32 1 : if win.content.SpeakerName != "" {
33 1 : h += titleBarHeight
34 1 : }
35 1 : if win.hasMessage() {
36 1 : h += messageAreaHeight + theme.Space5 // 本文欄と、その下の間隔
37 1 : }
38 1 : return h
39 : }
40 :
41 : // requiredHeight は内容を収めるのに要る窓の高さを返す。実際に描くときと同じ寸法で積み上げるので、
42 : // 計算した高さと描いた中身がずれない。窓の高さはこれと設定の最小高の大きいほうになる。
43 : func (win *Window) requiredHeight() int {
44 : // ページ分割が決まっていれば実際に描く件数を、決まる前は全件を見込む。
45 : // 選択肢が無くても Enter プロンプトが1行ぶん要る
46 1 : rows := len(win.content.Choices)
47 1 : if items, _ := getVisibleItems(win.choiceConfig, win.choiceState); len(items) > 0 {
48 1 : rows = len(items)
49 1 : }
50 1 : h := win.chromeHeight() + choiceTopPad + max(rows, 1)*choiceRowH
51 1 : if win.hasChoices && pageIndicatorText(win.choiceConfig, win.choiceState) != "" {
52 0 : h += pageRowH
53 0 : }
54 1 : return h
55 : }
56 :
57 : // buildTree は現在の状態からウィンドウ全体を uicore のツリーへ組み、画面上に配置して返す。
58 : // 枠・タイトルバー・メッセージ・選択肢またはEnterプロンプトを絶対座標で重ねる
59 : func (win *Window) buildTree() uicore.Widget {
60 1 : res := win.world.Resources.UIResources
61 1 : sd := win.world.Resources.ScreenDimensions
62 1 :
63 1 : size := win.calculateWindowSize()
64 1 : x, y := win.calculateWindowPosition(size)
65 1 : rect := image.Rect(x, y, x+size.Width, y+size.Height)
66 1 :
67 1 : var children []uicore.Widget
68 1 :
69 1 : frame := uicore.NewBox(theme.WindowBackground, theme.WindowBorder, theme.CornerRadius)
70 1 : frame.Layout(rect)
71 1 : children = append(children, frame)
72 1 :
73 : // タイトルバー
74 1 : contentTop := rect.Min.Y
75 1 : if win.content.SpeakerName != "" {
76 1 : bar := uicore.NewNineSlice(res.TitleBar.Image, res.TitleBar.BX, res.TitleBar.BY)
77 1 : bar.Layout(image.Rect(rect.Min.X, rect.Min.Y, rect.Max.X, rect.Min.Y+titleBarHeight))
78 1 : children = append(children, bar)
79 1 : name := uicore.NewText(win.content.SpeakerName, res.Text.SmallFace, theme.TextPrimary)
80 1 : name.Align = uicore.AlignCenter
81 1 : name.VCenter = true
82 1 : name.Layout(image.Rect(rect.Min.X, rect.Min.Y, rect.Max.X, rect.Min.Y+titleBarHeight))
83 1 : children = append(children, name)
84 1 : contentTop = rect.Min.Y + titleBarHeight
85 1 : }
86 :
87 1 : padL := rect.Min.X + theme.Space6
88 1 : padR := rect.Max.X - theme.Space6
89 1 :
90 : // メッセージ本文
91 1 : if win.hasMessage() {
92 1 : children = append(children, win.segmentedLineWidgets(padL, padR, contentTop+theme.Space6, res.Text.BodyFace)...)
93 1 : }
94 :
95 : // 選択肢またはEnterプロンプトを下端寄りに置く。一覧は自然幅の塊を中央へ寄せる
96 1 : cx := (rect.Min.X + rect.Max.X) / 2
97 1 : if win.hasChoices {
98 1 : items, _ := getVisibleItems(win.choiceConfig, win.choiceState)
99 1 : blockH := choiceTopPad + len(items)*choiceRowH
100 1 : if pageIndicatorText(win.choiceConfig, win.choiceState) != "" {
101 0 : blockH += pageRowH
102 0 : }
103 1 : blockTop := rect.Max.Y - theme.Space5 - blockH
104 1 : cw := choiceBlockWidth(win.choiceConfig, win.choiceState, win.world)
105 1 : choiceRect := image.Rect(cx-cw/2, blockTop, cx-cw/2+cw, rect.Max.Y-theme.Space5)
106 1 : children = append(children, renderChoiceList(win.choiceConfig, win.choiceState, win.world, choiceRect))
107 : } else {
108 1 : enter := query.T(win.world, "Enter")
109 1 : lw, th := uicore.MeasureText(enter, res.Text.BodyFace)
110 1 : // 選択肢の塊と同じ規則で幅を決め、文字幅ぴったりでボタンが痩せないようにする
111 1 : cw := uicore.FitWidth([]int{lw}, choicePadL*2, choiceMinWidth)
112 1 : bx := cx - cw/2
113 1 : py := rect.Max.Y - theme.Space5 - choiceRowH
114 1 : hl := uicore.NewNineSlice(res.SelectionBar.Image, res.SelectionBar.BX, res.SelectionBar.BY)
115 1 : hl.Layout(image.Rect(bx, py, bx+cw, py+choiceRowH))
116 1 : children = append(children, hl)
117 1 : prompt := uicore.NewText(enter, res.Text.BodyFace, theme.TextPrimary)
118 1 : prompt.Align = uicore.AlignCenter
119 1 : prompt.Layout(image.Rect(bx, py+(choiceRowH-th)/2, bx+cw, py+choiceRowH))
120 1 : children = append(children, prompt)
121 1 : }
122 :
123 1 : root := uicore.NewGroup(children...)
124 1 : root.Layout(image.Rect(0, 0, sd.Width, sd.Height))
125 1 : return root
126 : }
127 :
128 : // segmentedLineWidgets は TextSegmentLines を色付きの1行ずつへ組む。
129 : // 各行は左端から測定幅ぶん右へセグメントを連ね、空行は行高ぶん送る。背景色付きは帯を敷く
130 : func (win *Window) segmentedLineWidgets(padL, padR, top int, face text.Face) []uicore.Widget {
131 : // 行送りはフェイスの自然な行高から取る。固定値だと本文の行間が字面に対して間延びする
132 1 : lineH := uicore.LineHeight(face)
133 1 : var out []uicore.Widget
134 1 : yy := top
135 1 : for _, line := range win.content.TextSegmentLines {
136 1 : if lineIsBlank(line) {
137 1 : yy += lineH
138 1 : continue
139 : }
140 1 : xx := padL
141 1 : for _, seg := range line {
142 1 : if seg.Text == "" {
143 1 : continue
144 : }
145 1 : wpx, _ := uicore.MeasureText(seg.Text, face)
146 1 : if seg.BackgroundColor != nil {
147 1 : bg := uicore.Panel(uicore.BoxStyle{Fill: *seg.BackgroundColor}, lineH)
148 1 : bg.Layout(image.Rect(xx, yy, xx+wpx, yy+lineH))
149 1 : out = append(out, bg)
150 1 : }
151 1 : var col color.Color = win.config.textStyle.Color
152 1 : if seg.Color != nil {
153 1 : col = *seg.Color
154 1 : }
155 1 : t := uicore.NewText(seg.Text, face, col)
156 1 : t.Layout(image.Rect(xx, yy, padR, yy+lineH))
157 1 : out = append(out, t)
158 1 : xx += wpx
159 : }
160 1 : yy += lineH
161 : }
162 1 : return out
163 : }
164 :
165 : // lineIsBlank は行の全セグメントが空白文字だけかを返す
166 : func lineIsBlank(line []messagedata.TextSegment) bool {
167 1 : for _, seg := range line {
168 1 : for _, r := range seg.Text {
169 1 : if r != ' ' && r != '\t' && r != '\n' && r != '\r' {
170 1 : return false
171 1 : }
172 : }
173 : }
174 1 : return true
175 : }
176 :
177 : // renderChoiceList は選択肢一覧を rect 内へ組む。上端にページ表示、以降を1行ずつ下へ並べる。
178 : // 各行はウィンドウ中央に文字を寄せ、選択中は背景を強調する。窓とテスト双方から使う
179 : func renderChoiceList(config tabMenuConfig, state viewState, world w.World, rect image.Rectangle) uicore.Widget {
180 1 : res := world.Resources.UIResources
181 1 : face := res.Text.BodyFace
182 1 :
183 1 : var children []uicore.Widget
184 1 : yy := rect.Min.Y
185 1 : if pageText := pageIndicatorText(config, state); pageText != "" {
186 1 : pi := uicore.NewText(pageText, res.Text.SmallFace, theme.TextPrimary)
187 1 : pi.Align = uicore.AlignCenter
188 1 : pi.Layout(image.Rect(rect.Min.X, yy, rect.Max.X, yy+pageRowH))
189 1 : children = append(children, pi)
190 1 : yy += pageRowH
191 1 : }
192 1 : yy += choiceTopPad
193 1 :
194 1 : items, indices := getVisibleItems(config, state)
195 1 : for i, it := range items {
196 1 : focused := indices[i] == state.ItemIndex
197 1 : _, textH := uicore.MeasureText(it.Label, face)
198 1 : off := (choiceRowH - textH) / 2
199 1 :
200 : // 選択の強調と下端の区切り線は一覧の行と同じ意匠を使う
201 1 : chrome := menuframe.SelectionRow(res, focused)
202 1 : chrome.Layout(image.Rect(rect.Min.X, yy, rect.Max.X, yy+choiceRowH))
203 1 : children = append(children, chrome)
204 1 :
205 1 : col := theme.TextSecondary
206 1 : if focused {
207 1 : col = theme.TextSelected
208 1 : }
209 : // ラベルは左寄せ。追加ラベルはラベルの右に間隔を空けて連ねる
210 1 : x := rect.Min.X + choicePadL
211 1 : lbl := uicore.NewText(it.Label, face, col)
212 1 : lbl.Layout(image.Rect(x, yy+off, rect.Max.X, yy+choiceRowH))
213 1 : children = append(children, lbl)
214 1 : lw, _ := uicore.MeasureText(it.Label, face)
215 1 : x += lw
216 1 : for _, a := range it.AdditionalLabels {
217 1 : x += choiceLabelGap
218 1 : at := uicore.NewText(a, face, col)
219 1 : at.Layout(image.Rect(x, yy+off, rect.Max.X, yy+choiceRowH))
220 1 : children = append(children, at)
221 1 : aw, _ := uicore.MeasureText(a, face)
222 1 : x += aw
223 1 : }
224 :
225 1 : yy += choiceRowH
226 : }
227 :
228 1 : root := uicore.NewGroup(children...)
229 1 : root.Layout(rect)
230 1 : return root
231 : }
232 :
233 : // choiceLabelsWidth は1項目のラベルと追加ラベルを連ねた送り幅を返す。
234 : // ラベルの右へ間隔を空けて追加ラベルを並べる、という描画のしかたと同じ足し方をする。
235 : func choiceLabelsWidth(it item, face text.Face) int {
236 1 : w := uicore.MeasureTextWidth(it.Label, face)
237 1 : for _, a := range it.AdditionalLabels {
238 1 : w += choiceLabelGap + uicore.MeasureTextWidth(a, face)
239 1 : }
240 1 : return w
241 : }
242 :
243 : // choiceBlockWidth は選択肢一覧の自然幅を返す。窓では中央寄せ、テストでは左寄せの塊の幅に使う。
244 : // 各項目のラベルと追加ラベルの合計に左右パディングを足し、最大を採る
245 : func choiceBlockWidth(config tabMenuConfig, state viewState, world w.World) int {
246 1 : res := world.Resources.UIResources
247 1 : face := res.Text.BodyFace
248 1 : items, _ := getVisibleItems(config, state)
249 1 : contents := make([]int, len(items))
250 1 : for i, it := range items {
251 1 : contents[i] = choiceLabelsWidth(it, face)
252 1 : }
253 1 : return uicore.FitWidth(contents, choicePadL*2, choiceMinWidth)
254 : }
|