LCOV - code coverage report
Current view: top level - states - component_debug_state.go Coverage Total Hit
Test: coverage.lcov Lines: 88.9 % 45 40
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package states
       2              : 
       3              : import (
       4              :         "cmp"
       5              :         "fmt"
       6              :         "slices"
       7              : 
       8              :         "github.com/hajimehoshi/ebiten/v2"
       9              :         es "github.com/kijimaD/ruins/internal/engine/states"
      10              :         "github.com/kijimaD/ruins/internal/inputmapper"
      11              :         "github.com/kijimaD/ruins/internal/keybind"
      12              :         "github.com/kijimaD/ruins/internal/menuloop"
      13              :         "github.com/kijimaD/ruins/internal/resources"
      14              :         "github.com/kijimaD/ruins/internal/widgets/menuframe"
      15              :         "github.com/kijimaD/ruins/internal/widgets/styled"
      16              :         "github.com/kijimaD/ruins/internal/widgets/uicore"
      17              :         w "github.com/kijimaD/ruins/internal/world"
      18              :         "github.com/mlange-42/ark/ecs"
      19              : )
      20              : 
      21              : // ComponentDebugState はコンポーネント数を一覧表示するデバッグ用ステート
      22              : type ComponentDebugState struct {
      23              :         es.BaseState[w.World]
      24              :         screen *menuloop.Screen[ComponentDebugProps]
      25              : }
      26              : 
      27              : var _ es.State[w.World] = &ComponentDebugState{}
      28              : 
      29              : // OnStart はステートが開始される際に呼ばれる
      30              : func (st *ComponentDebugState) OnStart(_ w.World) error {
      31            1 :         st.screen = menuloop.NewScreen[ComponentDebugProps](st)
      32            1 :         return nil
      33            1 : }
      34              : 
      35              : // Update はゲームステートの更新処理を行う
      36              : func (st *ComponentDebugState) Update(world w.World) (es.Transition[w.World], error) {
      37            1 :         return st.screen.Update(world)
      38            1 : }
      39              : 
      40              : // Draw はゲームステートの描画処理を行う
      41              : func (st *ComponentDebugState) Draw(_ w.World, screen *ebiten.Image) error {
      42            1 :         st.screen.Draw(screen)
      43            1 :         return nil
      44            1 : }
      45              : 
      46              : // DoAction はActionを実行する
      47              : func (st *ComponentDebugState) DoAction(_ w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
      48            0 :         switch action {
      49              :         case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
      50            0 :                 return es.Transition[w.World]{Type: es.TransPop}, nil
      51              :         case inputmapper.ActionMenuSelect:
      52              :                 // 閲覧専用画面。決定では何も起きない
      53            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
      54              :         default:
      55            0 :                 return es.Transition[w.World]{}, fmt.Errorf("unknown action: %s", action)
      56              :         }
      57              : }
      58              : 
      59              : // NewComponentDebugState はコンポーネントデバッグ画面を作成する
      60              : func NewComponentDebugState() (es.State[w.World], error) {
      61            1 :         return &ComponentDebugState{}, nil
      62            1 : }
      63              : 
      64              : // ================
      65              : // Props
      66              : // ================
      67              : 
      68              : // ComponentDebugProps は画面の表示 props。menuloop.Screen の型引数として渡す
      69              : type ComponentDebugProps struct {
      70              :         Items []componentDebugItem
      71              :         Total int
      72              : }
      73              : 
      74              : type componentDebugItem struct {
      75              :         Name  string
      76              :         Count int
      77              : }
      78              : 
      79              : // Fetch は世界から表示 props を構築する。menuloop.Model の Model 部にあたる
      80              : func (st *ComponentDebugState) Fetch(world w.World) (ComponentDebugProps, error) {
      81              :         // Ark に登録された全コンポーネントを走査し、種類ごとの保有エンティティ数を集計する
      82            1 :         ids := ecs.ComponentIDs(world.ECS)
      83            1 :         items := make([]componentDebugItem, 0, len(ids))
      84            1 :         total := 0
      85            1 : 
      86            1 :         for _, id := range ids {
      87            1 :                 info, ok := ecs.ComponentInfo(world.ECS, id)
      88            1 :                 if !ok {
      89            0 :                         continue
      90              :                 }
      91            1 :                 q := ecs.NewUnsafeFilter(world.ECS, id).Query()
      92            1 :                 count := q.Count()
      93            1 :                 // Countは反復を完了しないためワールドロックが残る。明示的に閉じる
      94            1 :                 q.Close()
      95            1 : 
      96            1 :                 items = append(items, componentDebugItem{
      97            1 :                         Name:  info.Type.Name(),
      98            1 :                         Count: count,
      99            1 :                 })
     100            1 :                 total += count
     101              :         }
     102              : 
     103              :         // 数が多い順にソートする
     104            1 :         slices.SortFunc(items, func(a, b componentDebugItem) int {
     105            1 :                 return cmp.Compare(b.Count, a.Count)
     106            1 :         })
     107              : 
     108            1 :         return ComponentDebugProps{Items: items, Total: total}, nil
     109              : }
     110              : 
     111              : // Menu は一覧の構成を返す。menuloop.Model の Menu 部にあたる
     112              : func (st *ComponentDebugState) Menu(props ComponentDebugProps) menuloop.MenuConfig {
     113            1 :         return menuloop.MenuConfig{Key: "compdbg", TabCount: 1, ItemCounts: []int{len(props.Items)}, ItemsPerPage: menuloop.ItemsPerPageAuto}
     114            1 : }
     115              : 
     116              : // ================
     117              : // view
     118              : // ================
     119              : 
     120              : // ViewUI はコンポーネント数の2列表をタブ帯なしのモーダルに組む。
     121              : func (st *ComponentDebugState) ViewUI(world w.World, props ComponentDebugProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
     122            1 :         cols := styled.Cols(styled.Name(), styled.Num())
     123            1 :         rows := make([]menuframe.Row, len(props.Items))
     124            1 :         for i, it := range props.Items {
     125            1 :                 rows[i] = menuframe.Row{Cells: styled.TextCells(it.Name, fmt.Sprintf("%d", it.Count))}
     126            1 :         }
     127            1 :         content, pager := menuframe.RenderList(cursor.ItemIndex, rows, cols, menuframe.ListOpts{ItemsPerPage: cursor.PageSize}, res)
     128            1 :         return menuframe.TabScreen(world, res, fmt.Sprintf("Components total: %d", props.Total), nil, 0, content, keybind.HelpHint(world), pager)
     129              : }
        

Generated by: LCOV version 2.0-1