Line data Source code
1 : package states
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/hajimehoshi/ebiten/v2"
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/menuloop"
11 : "github.com/kijimaD/ruins/internal/messagedata"
12 : "github.com/kijimaD/ruins/internal/resources"
13 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
14 : "github.com/kijimaD/ruins/internal/widgets/styled"
15 : "github.com/kijimaD/ruins/internal/widgets/uicore"
16 : w "github.com/kijimaD/ruins/internal/world"
17 : )
18 :
19 : // Choice は選択メニューの1項目。Run が選択時の実行で、戻り値のステート遷移で
20 : // 画面を閉じる、別画面へ進む、などを表す。Header が真の行はカーソルが止まらない見出し
21 : type Choice struct {
22 : Label string
23 : // Value はラベルに添える値。右寄せ列で描く。空なら列を作らない
24 : Value string
25 : Run func(world w.World) (es.Transition[w.World], error)
26 : Header bool
27 : // Indent は menuframe.Row.Indent へ渡す字下げの段数
28 : Indent int
29 : // Disabled が真の項目は淡色で出し、カーソルは止まらず選べない。状態で使えない項目を消さずに見せる
30 : Disabled bool
31 : }
32 :
33 : // ChoiceProps は選択メニューの表示スナップショット
34 : type ChoiceProps struct {
35 : Title string
36 : Choices []Choice
37 : }
38 :
39 : // ChoiceMenuState は本文と選択肢だけの単純メニュー。ダンジョンメニュー・デバッグメニュー・
40 : // 交流メニュー・言語選択のように「選んで実行する」画面を1つの実装で担う。
41 : // 選択肢は provide で毎フレーム解決するため、静的な画面は固定の選択肢を返せばよい
42 : type ChoiceMenuState struct {
43 : es.BaseState[w.World]
44 : provide func(world w.World) (title string, choices []Choice)
45 : screen *menuloop.Screen[ChoiceProps]
46 : }
47 :
48 : var _ es.State[w.World] = &ChoiceMenuState{}
49 :
50 : // NewChoiceMenu は選択肢を返す provide を受け取り選択メニューを作る
51 : func NewChoiceMenu(provide func(world w.World) (string, []Choice)) *ChoiceMenuState {
52 1 : return &ChoiceMenuState{provide: provide}
53 1 : }
54 :
55 : // OnStart はステートが開始される際に呼ばれる
56 : func (st *ChoiceMenuState) OnStart(_ w.World) error {
57 1 : st.screen = menuloop.NewScreen[ChoiceProps](st)
58 1 : return nil
59 1 : }
60 :
61 : // Update はゲームステートの更新処理を行う
62 : func (st *ChoiceMenuState) Update(world w.World) (es.Transition[w.World], error) {
63 1 : return st.screen.Update(world)
64 1 : }
65 :
66 : // Draw はゲームステートの描画処理を行う
67 : func (st *ChoiceMenuState) Draw(_ w.World, screen *ebiten.Image) error {
68 1 : st.screen.Draw(screen)
69 1 : return nil
70 1 : }
71 :
72 : // DoAction は Action を実行する。選択で現在行の Run を呼び、その遷移を返す
73 : func (st *ChoiceMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
74 1 : switch action {
75 : case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
76 1 : return es.Transition[w.World]{Type: es.TransPop}, nil
77 : case inputmapper.ActionMenuSelect:
78 1 : choices := st.screen.Props().Choices
79 1 : i := st.screen.Selection().ItemIndex
80 1 : if i < 0 || i >= len(choices) || choices[i].Header || choices[i].Disabled || choices[i].Run == nil {
81 0 : return es.Transition[w.World]{Type: es.TransNone}, nil
82 0 : }
83 1 : return choices[i].Run(world)
84 : default:
85 0 : return es.Transition[w.World]{}, fmt.Errorf("choiceMenu: unsupported action: %s", action)
86 : }
87 : }
88 :
89 : // Fetch は現在の選択肢を表示スナップショットへ射影する
90 : func (st *ChoiceMenuState) Fetch(world w.World) (ChoiceProps, error) {
91 1 : title, choices := st.provide(world)
92 1 : return ChoiceProps{Title: title, Choices: choices}, nil
93 1 : }
94 :
95 : // Menu は単一タブの選択リストとして構成を返す。見出し行はカーソルを飛ばし、多い画面はページ送りする
96 : func (st *ChoiceMenuState) Menu(props ChoiceProps) menuloop.MenuConfig {
97 1 : skips := make([]bool, len(props.Choices))
98 1 : for i, c := range props.Choices {
99 1 : skips[i] = c.Header || c.Disabled
100 1 : }
101 1 : return menuloop.MenuConfig{Key: "choice", TabCount: 1, ItemCounts: []int{len(props.Choices)}, ItemsPerPage: menuloop.ItemsPerPageAuto, Skips: [][]bool{skips}}
102 : }
103 :
104 : // ViewUI は選択肢の一覧を中央パネルに uicore のツリーで組んで返す。
105 : // Screen はこれを EbitenCanvas で本体として描く。
106 : func (st *ChoiceMenuState) ViewUI(world w.World, props ChoiceProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
107 : // いずれかが Value を持つなら右寄せ列を足し、全行を2セルで揃える
108 1 : hasValue := false
109 1 : for _, c := range props.Choices {
110 1 : if c.Value != "" {
111 0 : hasValue = true
112 0 : break
113 : }
114 : }
115 1 : cols := styled.Cols(styled.Name())
116 1 : if hasValue {
117 0 : cols = styled.Cols(styled.Name(), styled.Num())
118 0 : }
119 1 : rows := make([]menuframe.Row, len(props.Choices))
120 1 : for i, c := range props.Choices {
121 1 : cells := styled.TextCells(c.Label)
122 1 : if hasValue {
123 0 : cells = styled.TextCells(c.Label, c.Value)
124 0 : }
125 1 : rows[i] = menuframe.Row{Cells: cells, Header: c.Header, Indent: c.Indent, Disabled: c.Disabled}
126 : }
127 1 : perPage := menuframe.ListCapacity(world, false, true)
128 1 : // ページ表示はフッタ行の右端に出す。1ページのメニューは内容を上端から並べる。
129 1 : list, pager := menuframe.RenderList(cursor.ItemIndex, rows, cols, menuframe.ListOpts{ItemsPerPage: perPage}, res)
130 1 : return menuframe.PanelScreen(world, res, props.Title, list, keybind.HelpHint(world), pager)
131 : }
132 :
133 : // pushChoice は指定ファクトリの state を push する Choice.Run を返す。選択メニューの共通部品
134 : func pushChoice(factory es.StateFactory[w.World]) func(w.World) (es.Transition[w.World], error) {
135 1 : return func(_ w.World) (es.Transition[w.World], error) {
136 0 : return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{factory}}, nil
137 0 : }
138 : }
139 :
140 : // popAfter は fn を実行して閉じる Choice.Run を返す
141 : func popAfter(fn func(w.World) error) func(w.World) (es.Transition[w.World], error) {
142 1 : return func(world w.World) (es.Transition[w.World], error) {
143 0 : if err := fn(world); err != nil {
144 0 : return es.Transition[w.World]{}, err
145 0 : }
146 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
147 : }
148 : }
149 :
150 : // stayAfter は fn を実行してメニューに留まる Choice.Run を返す。敵やPropの連続スポーンに使う
151 : func stayAfter(fn func(w.World) error) func(w.World) (es.Transition[w.World], error) {
152 1 : return func(world w.World) (es.Transition[w.World], error) {
153 0 : return es.Transition[w.World]{Type: es.TransNone}, fn(world)
154 0 : }
155 : }
156 :
157 : // pushMessage は指定メッセージ画面を push する Choice.Run を返す
158 : func pushMessage(md *messagedata.MessageData) func(w.World) (es.Transition[w.World], error) {
159 1 : return pushChoice(func() (es.State[w.World], error) { return NewMessageState(md) })
160 : }
|