LCOV - code coverage report
Current view: top level - states - storage_menu.go Coverage Total Hit
Test: coverage.lcov Lines: 47.6 % 103 49
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package states
       2              : 
       3              : import (
       4              :         "fmt"
       5              : 
       6              :         "github.com/hajimehoshi/ebiten/v2"
       7              :         "github.com/kijimaD/ruins/internal/consts"
       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              :         gs "github.com/kijimaD/ruins/internal/systems"
      14              :         "github.com/kijimaD/ruins/internal/widgets/menuframe"
      15              :         "github.com/kijimaD/ruins/internal/widgets/overlay"
      16              :         "github.com/kijimaD/ruins/internal/widgets/styled"
      17              :         "github.com/kijimaD/ruins/internal/widgets/uicore"
      18              :         w "github.com/kijimaD/ruins/internal/world"
      19              : 
      20              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
      21              :         "github.com/kijimaD/ruins/internal/world/query"
      22              :         "github.com/mlange-42/ark/ecs"
      23              : )
      24              : 
      25              : // tabID は収納メニューのタブ識別子
      26              : type tabID string
      27              : 
      28              : // 収納メニューのタブID。タブ定義・転送処理で参照する。定義型にして任意文字列の混入を防ぐ
      29              : const (
      30              :         tabIDRetrieve tabID = "retrieve"
      31              :         tabIDStore    tabID = "store"
      32              : )
      33              : 
      34              : // StorageMenuState は収納メニューのゲームステート
      35              : type StorageMenuState struct {
      36              :         es.BaseState[w.World]
      37              :         storageEntity ecs.Entity
      38              :         detail        overlay.Detail // 詳細モーダル。overlay として Screen に登録する
      39              :         screen        *menuloop.Screen[StorageProps]
      40              : }
      41              : 
      42              : // State interface ================
      43              : 
      44              : var _ es.State[w.World] = &StorageMenuState{}
      45              : var _ menuloop.KeyBindings = &StorageMenuState{}
      46              : 
      47              : // OnStart はステートが開始される際に呼ばれる
      48              : func (st *StorageMenuState) OnStart(_ w.World) error {
      49            1 :         st.detail = overlay.NewEntityDetail(st.selectedEntity)
      50            1 :         st.screen = menuloop.NewScreen[StorageProps](st, &st.detail)
      51            1 :         return nil
      52            1 : }
      53              : 
      54              : // Update はゲームステートの更新処理を行う
      55              : func (st *StorageMenuState) Update(world w.World) (es.Transition[w.World], error) {
      56              :         // 収納の出し入れで所持品が変わると WeightDirty が立つ。再計算を回して総重量表示を更新する
      57            1 :         if err := runUpdaters(world, &gs.WeightDirtySystem{}); err != nil {
      58            0 :                 return es.Transition[w.World]{}, err
      59            0 :         }
      60            1 :         return st.screen.Update(world)
      61              : }
      62              : 
      63              : // Draw はゲームステートの描画処理を行う
      64              : func (st *StorageMenuState) Draw(_ w.World, screen *ebiten.Image) error {
      65            1 :         st.screen.Draw(screen)
      66            1 :         return nil
      67            1 : }
      68              : 
      69              : // KeyBindings は x の詳細表示を共通入力に足す
      70              : func (st *StorageMenuState) KeyBindings() []keybind.Binding {
      71            1 :         return detailOpenBindings
      72            1 : }
      73              : 
      74              : // DoAction はActionを実行する
      75              : func (st *StorageMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
      76            0 :         switch action {
      77              :         case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
      78            0 :                 return es.Transition[w.World]{Type: es.TransPop}, nil
      79              :         case inputmapper.ActionOpenItemDetail:
      80            0 :                 st.detail.Open(world)
      81              :         case inputmapper.ActionMenuSelect:
      82            0 :                 if err := st.executeTransfer(world); err != nil {
      83            0 :                         return es.Transition[w.World]{}, err
      84            0 :                 }
      85              :         default:
      86            0 :                 return es.Transition[w.World]{}, fmt.Errorf("storageMenu: unsupported action: %s", action)
      87              :         }
      88            0 :         return es.Transition[w.World]{Type: es.TransNone}, nil
      89              : }
      90              : 
      91              : // ================
      92              : // Props
      93              : // ================
      94              : 
      95              : // StorageProps は画面の表示 props。menuloop.Screen の型引数として渡す
      96              : type StorageProps struct {
      97              :         Tabs []storageTabData
      98              : }
      99              : 
     100              : type storageTabData struct {
     101              :         ID    tabID
     102              :         Label string
     103              :         Items []itemRowData
     104              : }
     105              : 
     106              : // Fetch は世界から表示 props を構築する。menuloop.Model の Model 部にあたる。
     107              : // 収納メニューはプレイヤーの操作でしか開かないので、プレイヤー不在は不変条件違反として返す
     108              : func (st *StorageMenuState) Fetch(world w.World) (StorageProps, error) {
     109            1 :         player, err := query.GetPlayerEntity(world)
     110            1 :         if err != nil {
     111            0 :                 return StorageProps{}, err
     112            0 :         }
     113            1 :         return StorageProps{
     114            1 :                 Tabs: []storageTabData{
     115            1 :                         {ID: tabIDRetrieve, Label: query.T(world, "Retrieve"), Items: st.toStorageItemData(world, query.StorageStacks(world, st.storageEntity))},
     116            1 :                         {ID: tabIDStore, Label: query.T(world, "Store"), Items: st.toStorageItemData(world, query.BackpackStacks(world, player))},
     117            1 :                 },
     118            1 :         }, nil
     119              : }
     120              : 
     121              : // Menu は一覧の構成を返す。menuloop.Model の Menu 部にあたる
     122              : func (st *StorageMenuState) Menu(props StorageProps) menuloop.MenuConfig {
     123            1 :         itemCounts := make([]int, len(props.Tabs))
     124            1 :         for i, tab := range props.Tabs {
     125            1 :                 itemCounts[i] = len(tab.Items)
     126            1 :         }
     127            1 :         return menuloop.MenuConfig{Key: "storage", TabCount: len(props.Tabs), ItemCounts: itemCounts, ItemsPerPage: menuloop.ItemsPerPageAuto}
     128              : }
     129              : 
     130              : func (st *StorageMenuState) toStorageItemData(world w.World, stacks []query.Stack) []itemRowData {
     131              :         // 1スタック1行。重量は束の総量、個数は束の大きさを出す
     132            1 :         items := make([]itemRowData, len(stacks))
     133            1 :         for i, stack := range stacks {
     134            1 :                 rep := stack.Rep
     135            1 :                 total := query.GetEntityWeight(world, rep) * consts.Milligram(stack.Count)
     136            1 :                 items[i] = itemRowData{
     137            1 :                         Entity: rep,
     138            1 :                         Name:   query.GetEntityName(rep, world),
     139            1 :                         Weight: total.KgString(),
     140            1 :                         Count:  stack.Count,
     141            1 :                 }
     142              :         }
     143            1 :         return items
     144              : }
     145              : 
     146              : // ================
     147              : // アクション実行
     148              : // ================
     149              : 
     150              : func (st *StorageMenuState) executeTransfer(world w.World) error {
     151            0 :         props := st.screen.Props()
     152            0 :         cursor := st.screen.Selection()
     153            0 :         tabIndex := cursor.TabIndex
     154            0 :         itemIndex := cursor.ItemIndex
     155            0 : 
     156            0 :         if tabIndex >= len(props.Tabs) {
     157            0 :                 return nil
     158            0 :         }
     159            0 :         tab := props.Tabs[tabIndex]
     160            0 :         if len(tab.Items) == 0 || itemIndex >= len(tab.Items) {
     161            0 :                 return nil
     162            0 :         }
     163              : 
     164            0 :         item := tab.Items[itemIndex]
     165            0 : 
     166            0 :         switch tab.ID {
     167              :         case tabIDRetrieve:
     168            0 :                 playerEntity, err := query.GetPlayerEntity(world)
     169            0 :                 if err != nil {
     170            0 :                         return err
     171            0 :                 }
     172            0 :                 if _, err := lifecycle.MoveStackToBackpack(world, item.Entity, playerEntity); err != nil {
     173            0 :                         return err
     174            0 :                 }
     175              :         case tabIDStore:
     176              :                 // 容量判定が束の合計重量を要するため、ここだけ実体列を先に束ねて可否を見てから移す
     177            0 :                 members := query.StackMembers(world, item.Entity)
     178            0 :                 if !query.CanAddStackToStorage(world, st.storageEntity, members) {
     179            0 :                         return nil
     180            0 :                 }
     181            0 :                 if _, err := lifecycle.MoveMembersToStorage(world, members, st.storageEntity); err != nil {
     182            0 :                         return err
     183            0 :                 }
     184              :         }
     185              : 
     186            0 :         return nil
     187              : }
     188              : 
     189              : // ================
     190              : // View
     191              : // ================
     192              : 
     193              : // ViewUI はカテゴリタブとアイコン付きアイテム一覧を組む。
     194              : // 詳細モーダルは ScreenRenderer として Screen が本体の上へ重ねる。
     195              : func (st *StorageMenuState) ViewUI(world w.World, props StorageProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
     196            1 :         labels := make([]string, len(props.Tabs))
     197            1 :         for i, tab := range props.Tabs {
     198            1 :                 labels[i] = tab.Label
     199            1 :         }
     200            1 :         content, pager := st.buildActiveListUI(world, props, cursor.TabIndex, cursor.ItemIndex, cursor.PageSize, res)
     201            1 :         return menuframe.TabScreen(world, res, "", labels, cursor.TabIndex, content, keybind.HelpHint(world), pager)
     202              : }
     203              : 
     204              : // buildActiveListUI は行列とフッタ右端のページ表示を返す。
     205              : func (st *StorageMenuState) buildActiveListUI(world w.World, props StorageProps, tabIndex, itemIndex, perPage int, res resources.UIResources) ([]uicore.Drawable, string) {
     206            1 :         if tabIndex >= len(props.Tabs) {
     207            0 :                 return nil, ""
     208            0 :         }
     209            1 :         currentTab := props.Tabs[tabIndex]
     210            1 :         cols := itemMenuColumns(styled.Num())
     211            1 :         rows := make([]menuframe.Row, len(currentTab.Items))
     212            1 :         for i, it := range currentTab.Items {
     213            1 :                 rows[i] = itemMenuRow(world, it.Entity, it.Count, it.Weight)
     214            1 :         }
     215            1 :         return menuframe.RenderList(itemIndex, rows, cols, menuframe.ListOpts{EmptyText: query.T(world, "No items"), ItemsPerPage: perPage}, res)
     216              : }
     217              : 
     218              : // selectedEntity は現在カーソルが当たっているアイテムのエンティティを返す
     219              : func (st *StorageMenuState) selectedEntity() (ecs.Entity, bool) {
     220            0 :         props := st.screen.Props()
     221            0 :         cursor := st.screen.Selection()
     222            0 :         if cursor.TabIndex >= len(props.Tabs) {
     223            0 :                 return ecs.Entity{}, false
     224            0 :         }
     225            0 :         items := props.Tabs[cursor.TabIndex].Items
     226            0 :         if cursor.ItemIndex >= len(items) {
     227            0 :                 return ecs.Entity{}, false
     228            0 :         }
     229            0 :         return items[cursor.ItemIndex].Entity, true
     230              : }
        

Generated by: LCOV version 2.0-1