Line data Source code
1 : package menuframe
2 :
3 : import (
4 : "image"
5 : "image/color"
6 :
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 : // 名前入力のような1項目の入力画面の寸法。画面はこれらを知らない
16 : const (
17 : formWidth = 320 // 入力欄と見出しの幅
18 : formTitleH = 36 // 見出し行の高さ
19 : formInputH = 44 // 入力欄の高さ
20 : formRowGap = theme.Space6 // 行と行の間隔
21 : )
22 :
23 : // FormScreen は1項目の入力画面を組む。見出し・入力欄・エラー・操作ヒントを画面中央の
24 : // 縦並びに置く。errText が空ならエラー行は高さを取らない。
25 : // 幅・行高・間隔は部品が持ち、画面は文言と入力欄の中身だけを渡す。
26 : func FormScreen(world w.World, res resources.UIResources, title string, body uicore.Widget, errText, hint string) uicore.Widget {
27 1 : sd := world.Resources.ScreenDimensions
28 1 :
29 1 : items := []uicore.FlexItem{
30 1 : {W: newCenteredText(title, res.Text.TitleFontFace, theme.TextPrimary), Height: formTitleH},
31 1 : {Height: formRowGap},
32 1 : {W: InputBox(res, body), Height: formInputH},
33 1 : {Height: formRowGap},
34 1 : }
35 1 : if errText != "" {
36 1 : items = append(items,
37 1 : uicore.FlexItem{W: newCenteredText(errText, res.Text.SmallFace, theme.StatusDanger), Height: noteRowH},
38 1 : uicore.FlexItem{Height: formRowGap},
39 1 : )
40 1 : }
41 1 : items = append(items, uicore.FlexItem{W: newCenteredText(hint, res.Text.SmallFace, theme.TextAccent), Height: noteRowH})
42 1 :
43 : // 縦並びの総高を出し、画面の中央へ置く
44 1 : total := 0
45 1 : for _, it := range items {
46 1 : total += it.Height
47 1 : }
48 1 : rect := image.Rect(
49 1 : (sd.Width-formWidth)/2,
50 1 : (sd.Height-total)/2,
51 1 : (sd.Width-formWidth)/2+formWidth,
52 1 : (sd.Height-total)/2+total,
53 1 : )
54 1 : uicore.FlexColumn(rect, items)
55 1 :
56 1 : widgets := make([]uicore.Widget, 0, len(items))
57 1 : for _, it := range items {
58 1 : if it.W != nil {
59 1 : widgets = append(widgets, it.W)
60 1 : }
61 : }
62 1 : root := uicore.NewGroup(widgets...)
63 1 : root.Layout(image.Rect(0, 0, sd.Width, sd.Height))
64 1 : return root
65 : }
66 :
67 : // newCenteredText は中央寄せの1行を作る。入力画面の各行はすべて中央へそろえる
68 : func newCenteredText(s string, face text.Face, c color.Color) *uicore.Text {
69 1 : t := uicore.NewText(s, face, c)
70 1 : t.Align = uicore.AlignCenter
71 1 : return t
72 1 : }
|