Line data Source code
1 : package states
2 :
3 : import (
4 : "github.com/hajimehoshi/ebiten/v2"
5 : "github.com/kijimaD/ruins/internal/widgets/menuframe"
6 : "github.com/kijimaD/ruins/internal/widgets/styled"
7 : "github.com/kijimaD/ruins/internal/widgets/theme"
8 : w "github.com/kijimaD/ruins/internal/world"
9 : "github.com/kijimaD/ruins/internal/world/query"
10 : "github.com/mlange-42/ark/ecs"
11 : )
12 :
13 : // itemRowData はアイテム一覧の1行の元データ。名前×個数で並べる複数のアイテムメニューで共通に使う。
14 : // 表示の名前・個数・アイコンは itemMenuRow が entity から解決するので、Name・Count は元データの
15 : // 保持にとどまる。Desc は詳細用途で、収納メニューなど使わない画面ではゼロ値のままにする
16 : type itemRowData struct {
17 : Entity ecs.Entity
18 : Name string
19 : Weight string
20 : Count int
21 : Desc string
22 : }
23 :
24 : // itemMenuColumns はアイコン列と伸縮する名前列の共通の先頭2列に、メニュー固有の trailing 列を
25 : // 足した列定義を返す。対象メニュー間でアイコンと名前の見た目を統一する。
26 : // trailing は数値列など画面ごとの追加列を意味的に渡す
27 : func itemMenuColumns(trailing ...styled.Col) []styled.Col {
28 1 : return append([]styled.Col{styled.Icon(), styled.Name()}, trailing...)
29 1 : }
30 :
31 : // menuIcon は entity のスプライトを一覧のアイコン列の大きさで返す。縮小はリソース側が
32 : // 一度だけ済ませるので、描画は等倍になる
33 : func menuIcon(world w.World, e ecs.Entity) *ebiten.Image {
34 1 : return world.Resources.Sprites.Sized(world.Components.SpriteRender.Get(e), theme.MenuIconW)
35 1 : }
36 :
37 : // itemMenuRow はアイテム entity から共通の先頭部 [アイコンセル + 名前×個数セル] を組み、
38 : // メニュー固有の trailing 文字列セルを後続に付けた menuframe.Row を返す。
39 : // 個数は呼び出し側が GroupStacks で確定済みの値を渡す。ここで数え直すと一覧の行ごとに
40 : // 全走査が走るため、束ねた結果を持ち回って表示する
41 : func itemMenuRow(world w.World, e ecs.Entity, count int, trailing ...string) menuframe.Row {
42 1 : name := query.GetEntityName(e, world)
43 1 : icon := menuIcon(world, e)
44 1 : label := query.FormatNameCount(name, count)
45 1 : // 腐敗食は新鮮以外のとき鮮度を名前に添え、状態を見て食べるか捨てるか判断できるようにする
46 1 : if marker := query.FreshnessMarker(world, e); marker != "" {
47 0 : label += " (" + marker + ")"
48 0 : }
49 1 : cells := append([]styled.Cell{styled.IconCell(icon), styled.TextCell(label)}, styled.TextCells(trailing...)...)
50 1 : return menuframe.Row{Cells: cells}
51 : }
52 :
53 : // ================
|