LCOV - code coverage report
Current view: top level - widgets/overlay - detail.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 67 67
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package overlay
       2              : 
       3              : import (
       4              :         "image"
       5              : 
       6              :         "github.com/kijimaD/ruins/internal/inputmapper"
       7              :         "github.com/kijimaD/ruins/internal/keybind"
       8              :         "github.com/kijimaD/ruins/internal/widgets/entityspec"
       9              :         "github.com/kijimaD/ruins/internal/widgets/theme"
      10              :         "github.com/kijimaD/ruins/internal/widgets/uicore"
      11              :         w "github.com/kijimaD/ruins/internal/world"
      12              :         "github.com/kijimaD/ruins/internal/world/query"
      13              :         "github.com/mlange-42/ark/ecs"
      14              : )
      15              : 
      16              : var _ ScreenRenderer = (*Detail)(nil)
      17              : 
      18              : // DetailContent は詳細モーダルに出す1件分の内容。名前・説明・性能行・用途をそのまま持つ。
      19              : // 実体から組むなら EntityDetailContent を使い、独自の行を出すなら Rows を直接与える。
      20              : // 行の型は entityspec.SpecRow を使う。Uses は性質・使い道を「用途」見出しでまとめた行で、不要なら空でよい
      21              : type DetailContent struct {
      22              :         Name string
      23              :         Desc string
      24              :         Rows []entityspec.SpecRow
      25              :         Uses []entityspec.SpecRow
      26              : }
      27              : 
      28              : // EntityDetailContent は実体から名前・説明・性能行を組んだ詳細内容を返す。
      29              : // 在庫や持ち物のように対象の実体さえあれば表示を導ける詳細で使う。
      30              : // 死んだ実体には空を返し、ゼロ実体への Get で落ちるのを防ぐ
      31              : func EntityDetailContent(world w.World, e ecs.Entity) DetailContent {
      32            1 :         if !world.ECS.Alive(e) {
      33            1 :                 return DetailContent{}
      34            1 :         }
      35            1 :         desc := ""
      36            1 :         if world.Components.Description.Has(e) {
      37            1 :                 desc = query.T(world, world.Components.Description.Get(e).Description)
      38            1 :         }
      39            1 :         return DetailContent{
      40            1 :                 Name: query.GetEntityName(e, world),
      41            1 :                 Desc: desc,
      42            1 :                 Rows: entityspec.SpecRows(world, e),
      43            1 :                 Uses: entityspec.UseHints(world, e),
      44            1 :         }
      45              : }
      46              : 
      47              : // Detail は詳細モーダルの表示状態・ページ送り入力・ウィンドウ組み立てをまとめて担う。
      48              : // provide が返す内容を描き、1件なら中央に1枚、複数なら横並びで並べる。呼び出し側は
      49              : // 「何を出すか」を返す provide を渡すだけでよく、入力・ページ数・描画といった内部には触れない。
      50              : // x で開き、左右でページを繰り、Esc・Enter で閉じる
      51              : type Detail struct {
      52              :         active  bool
      53              :         page    int
      54              :         provide func(world w.World) ([]DetailContent, bool)
      55              : }
      56              : 
      57              : // NewDetail は1件の詳細内容を返す provide を受け取り Detail を作る。中央に1枚で描く。
      58              : // provide は対象が無ければ ok=false を返す
      59              : func NewDetail(provide func(world w.World) (DetailContent, bool)) Detail {
      60            1 :         return Detail{provide: func(world w.World) ([]DetailContent, bool) {
      61            1 :                 c, ok := provide(world)
      62            1 :                 if !ok {
      63            1 :                         return nil, false
      64            1 :                 }
      65            1 :                 return []DetailContent{c}, true
      66              :         }}
      67              : }
      68              : 
      69              : // NewComparison は複数の詳細内容を返す provide を受け取り Detail を作る。返った内容を横並びで描く
      70              : func NewComparison(provide func(world w.World) ([]DetailContent, bool)) Detail {
      71            1 :         return Detail{provide: provide}
      72            1 : }
      73              : 
      74              : // NewEntityDetail は選択中の実体をそのまま詳細に出す Detail を作る。
      75              : // 対象の解決だけ渡せば、名前・説明・性能は実体から組む。対象が無いか死んでいれば開かない
      76              : func NewEntityDetail(provide func() (ecs.Entity, bool)) Detail {
      77            1 :         return NewDetail(func(world w.World) (DetailContent, bool) {
      78            1 :                 e, ok := provide()
      79            1 :                 if !ok || !world.ECS.Alive(e) {
      80            1 :                         return DetailContent{}, false
      81            1 :                 }
      82            1 :                 return EntityDetailContent(world, e), true
      83              :         })
      84              : }
      85              : 
      86              : // Active は詳細モーダルを表示中かを返す
      87            1 : func (d *Detail) Active() bool { return d.active }
      88              : 
      89              : // Open は詳細モーダルを先頭ページで開く。出す内容が無ければ開かない。
      90              : // アイテムが無いメニューで開くと、中身の無いモーダルが入力を奪って抜けられなくなるのを防ぐ。
      91              : func (d *Detail) Open(world w.World) {
      92            1 :         if _, ok := d.provide(world); !ok {
      93            1 :                 return
      94            1 :         }
      95            1 :         d.active = true
      96            1 :         d.page = 0
      97              : }
      98              : 
      99              : // HandleInput は表示中のキー入力を処理する。ページ数は provide の内容から自身で算出する。
     100              : // 表示中でなければ何もしない。
     101              : // error は Layer の契約に合わせた戻り値で、詳細モーダルでは常に nil
     102              : func (d *Detail) HandleInput(world w.World) error {
     103            1 :         if !d.active {
     104            1 :                 return nil
     105            1 :         }
     106              :         // メニューと同じ入力供給源から読む。再生ドライバが差した Action もここに届くので、
     107              :         // overlay 表示中も本番フローのまま駆動できる
     108            1 :         action, ok := keybind.ReadInput(world, keybind.MenuCommon)
     109            1 :         if !ok {
     110            1 :                 return nil
     111            1 :         }
     112            1 :         total := 1
     113            1 :         if contents, ok := d.provide(world); ok {
     114              :                 // 複数枚は行数の最も多い枚に合わせてページ数を決める
     115            1 :                 for _, c := range contents {
     116            1 :                         if p := detailPageCount(len(c.Rows)); p > total {
     117            1 :                                 total = p
     118            1 :                         }
     119              :                 }
     120              :         }
     121            1 :         switch action {
     122              :         case inputmapper.ActionMenuCancel, inputmapper.ActionMenuSelect:
     123            1 :                 d.active = false
     124              :         case inputmapper.ActionMenuTabPrev, inputmapper.ActionMenuLeft:
     125            1 :                 if d.page > 0 {
     126            1 :                         d.page--
     127            1 :                 }
     128              :         case inputmapper.ActionMenuTabNext, inputmapper.ActionMenuRight:
     129            1 :                 if d.page < total-1 {
     130            1 :                         d.page++
     131            1 :                 }
     132            1 :         default:
     133              :                 // 詳細モーダルは閉じるとページ送りしか扱わない
     134              :         }
     135            1 :         return nil
     136              : }
     137              : 
     138              : // RenderOverlay は現在の内容から詳細モーダルを uicore のツリーへ組み、rect 内に配置して返す。
     139              : // 対象が無ければ nil を返す。Screen はこれを本体の上へ重ねて描く。
     140              : func (d *Detail) RenderOverlay(world w.World, rect image.Rectangle) uicore.Drawable {
     141            1 :         contents, ok := d.provide(world)
     142            1 :         if !ok || len(contents) == 0 {
     143            1 :                 return nil
     144            1 :         }
     145            1 :         res := world.Resources.UIResources
     146            1 :         if len(contents) == 1 {
     147            1 :                 return buildPanelUI(res, rect, contents[0], d.page)
     148            1 :         }
     149              :         // 複数枚は狭い WindowRect でなく画面幅の大半へ横並びで描く。上下は rect と揃える
     150            1 :         sw := world.Resources.ScreenDimensions.Width
     151            1 :         wide := image.Rect(theme.MenuModalMarginX, rect.Min.Y, sw-theme.MenuModalMarginX, rect.Max.Y)
     152            1 :         return buildPanelsUI(res, wide, contents, d.page)
     153              : }
        

Generated by: LCOV version 2.0-1