Line data Source code
1 : package states
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/hajimehoshi/ebiten/v2"
7 : gc "github.com/kijimaD/ruins/internal/components"
8 : es "github.com/kijimaD/ruins/internal/engine/states"
9 : "github.com/kijimaD/ruins/internal/inputmapper"
10 : "github.com/kijimaD/ruins/internal/keybind"
11 : "github.com/kijimaD/ruins/internal/menuloop"
12 : "github.com/kijimaD/ruins/internal/resources"
13 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
14 : "github.com/kijimaD/ruins/internal/widgets/uicore"
15 : w "github.com/kijimaD/ruins/internal/world"
16 : "github.com/kijimaD/ruins/internal/world/query"
17 : "github.com/mlange-42/ark/ecs"
18 : )
19 :
20 : const cubeInfoMenuKey = "cube_info"
21 :
22 : // CubeInfoState は移動拠点キューブの情報をタブで見る読み取り専用画面。
23 : // いまは基本タブだけで、収納の総重量と燃料残量を出す。タブは後で増やす。
24 : type CubeInfoState struct {
25 : es.BaseState[w.World]
26 :
27 : cube ecs.Entity
28 : screen *menuloop.Screen[CubeInfoProps]
29 : }
30 :
31 : // CubeInfoProps は情報画面の表示 props
32 : type CubeInfoProps struct {
33 : Tabs []statsTab
34 : }
35 :
36 : var _ es.State[w.World] = &CubeInfoState{}
37 :
38 : // NewCubeInfoState はキューブの情報画面を作る
39 : func NewCubeInfoState(cube ecs.Entity) (es.State[w.World], error) {
40 1 : return &CubeInfoState{cube: cube}, nil
41 1 : }
42 :
43 : // OnStart は Screen を組む。overlay は持たない読み取り専用画面
44 : func (st *CubeInfoState) OnStart(_ w.World) error {
45 0 : st.screen = menuloop.NewScreen[CubeInfoProps](st)
46 0 : return nil
47 0 : }
48 :
49 : // Update はステートの更新を Screen へ委譲する
50 : func (st *CubeInfoState) Update(world w.World) (es.Transition[w.World], error) {
51 0 : return st.screen.Update(world)
52 0 : }
53 :
54 : // Draw はステートの描画を Screen へ委譲する
55 : func (st *CubeInfoState) Draw(_ w.World, screen *ebiten.Image) error {
56 0 : st.screen.Draw(screen)
57 0 : return nil
58 0 : }
59 :
60 : // DoAction は読み取り専用なので閉じる操作だけ扱う
61 : func (st *CubeInfoState) DoAction(_ w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
62 0 : switch action {
63 : case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
64 0 : return es.Transition[w.World]{Type: es.TransPop}, nil
65 : case inputmapper.ActionMenuSelect:
66 0 : return es.Transition[w.World]{Type: es.TransNone}, nil
67 : default:
68 0 : return es.Transition[w.World]{}, fmt.Errorf("cubeInfo: unsupported action: %s", action)
69 : }
70 : }
71 :
72 : // Fetch は表示するタブを組む。いまは基本タブだけ
73 : func (st *CubeInfoState) Fetch(world w.World) (CubeInfoProps, error) {
74 1 : return CubeInfoProps{Tabs: []statsTab{
75 1 : {Label: query.T(world, "Basic"), Items: cubeInfoItems(world, st.cube)},
76 1 : }}, nil
77 1 : }
78 :
79 : // Menu はタブ構成を返す。見出し行が無いのでスキップは不要
80 : func (st *CubeInfoState) Menu(props CubeInfoProps) menuloop.MenuConfig {
81 0 : itemCounts := make([]int, len(props.Tabs))
82 0 : for i, tab := range props.Tabs {
83 0 : itemCounts[i] = len(tab.Items)
84 0 : }
85 0 : return menuloop.MenuConfig{Key: cubeInfoMenuKey, TabCount: len(props.Tabs), ItemCounts: itemCounts}
86 : }
87 :
88 : // ViewUI はタブ帯つきの情報表を組む
89 : func (st *CubeInfoState) ViewUI(world w.World, props CubeInfoProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
90 0 : labels := make([]string, len(props.Tabs))
91 0 : for i, tab := range props.Tabs {
92 0 : labels[i] = tab.Label
93 0 : }
94 0 : tabIndex := cursor.TabIndex
95 0 : if tabIndex >= len(props.Tabs) {
96 0 : tabIndex = 0
97 0 : }
98 0 : content, pager := buildStatsTableUI(world, props.Tabs[tabIndex].Items, cursor.ItemIndex, res)
99 0 : return menuframe.TabScreen(world, res, query.T(world, "Cube info"), labels, tabIndex, content, keybind.HelpHint(world), pager)
100 : }
101 :
102 : // cubeInfoItems はキューブの基本情報を表の行に組む。展開サイズ、収納の総重量、燃料残量。
103 : // 値は表示時に都度算出する。死んだキューブには空を返す
104 : func cubeInfoItems(world w.World, cube ecs.Entity) []statusItemData {
105 1 : if !world.ECS.Alive(cube) {
106 0 : return nil
107 0 : }
108 1 : r := query.CubeDeployRange(world, cube)
109 1 : weight := query.CubeWeight(world, cube)
110 1 : return []statusItemData{
111 1 : {Label: query.T(world, "Deploy size"), Value: fmt.Sprintf("%dx%d", 2*r.X+1, 2*r.Y+1)},
112 1 : {Label: query.T(world, "Total weight"), Value: weight.KgString()},
113 1 : {Label: query.T(world, "Fuel"), Value: fuelTankText(*world.Components.FuelTank.Get(cube))},
114 1 : }
115 : }
116 :
117 : // fuelTankText は燃料タンクを現在値と上限で整形する。
118 : func fuelTankText(tank gc.FuelTank) string {
119 1 : return fmt.Sprintf("%s / %d", tank.Current.String(), int(tank.Max))
120 1 : }
|