LCOV - code coverage report
Current view: top level - states - main_menu.go Coverage Total Hit
Test: coverage.lcov Lines: 80.6 % 67 54
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/kijimaD/ruins/internal/i18n"
       7              :         "github.com/kijimaD/ruins/internal/messagedata"
       8              : 
       9              :         "github.com/hajimehoshi/ebiten/v2"
      10              :         "github.com/kijimaD/ruins/internal/consts"
      11              :         es "github.com/kijimaD/ruins/internal/engine/states"
      12              :         "github.com/kijimaD/ruins/internal/inputmapper"
      13              :         "github.com/kijimaD/ruins/internal/menuloop"
      14              :         "github.com/kijimaD/ruins/internal/resources"
      15              :         "github.com/kijimaD/ruins/internal/widgets/menuframe"
      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              :         "github.com/kijimaD/ruins/internal/world/query"
      20              : )
      21              : 
      22              : // MainMenuState はメインメニューのゲームステート
      23              : type MainMenuState struct {
      24              :         es.BaseState[w.World]
      25              :         screen *menuloop.Screen[MainMenuProps]
      26              :         // loadFailed は、セーブを読めずにメインメニューへ戻ったことを表す。最初の更新で知らせのモーダルを重ねる
      27              :         loadFailed bool
      28              : }
      29              : 
      30              : // NewMainMenuAfterLoadFailure は、セーブを読めなかったことをモーダルで知らせるメインメニューを作る
      31              : func NewMainMenuAfterLoadFailure() (es.State[w.World], error) {
      32            1 :         return &MainMenuState{loadFailed: true}, nil
      33            1 : }
      34              : 
      35              : // State interface ================
      36              : 
      37              : var _ es.State[w.World] = &MainMenuState{}
      38              : 
      39              : // OnStart はステート開始時の処理を行う。world には触れない。
      40              : // 前のゲームの後片付けは新しいゲームを始める側が world.ResetForNewGame で行う
      41              : func (st *MainMenuState) OnStart(_ w.World) error {
      42            1 :         st.screen = menuloop.NewScreen[MainMenuProps](st)
      43            1 :         return nil
      44            1 : }
      45              : 
      46              : // Update はゲームステートの更新処理を行う
      47              : func (st *MainMenuState) Update(world w.World) (es.Transition[w.World], error) {
      48              :         // 知らせの下にメニューの項目を描くため、先に画面を組んでから知らせを積む。入力による遷移があれば先に通す
      49            1 :         tr, err := st.screen.Update(world)
      50            1 :         if err != nil || !st.loadFailed || tr.Type != es.TransNone {
      51            1 :                 return tr, err
      52            1 :         }
      53            1 :         st.loadFailed = false
      54            1 :         return es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{newLoadFailedMessageState}}, nil
      55              : }
      56              : 
      57              : // newLoadFailedMessageState はセーブを読めなかったことを知らせるモーダルを作る
      58              : func newLoadFailedMessageState() (es.State[w.World], error) {
      59            1 :         return &MessageState{build: func(world w.World) *messagedata.MessageData {
      60            1 :                 return messagedata.NewSystemMessage(query.T(world, "Could not load the save data."))
      61            1 :         }}, nil
      62              : }
      63              : 
      64              : // Draw はスクリーンに描画する
      65              : func (st *MainMenuState) Draw(world w.World, screen *ebiten.Image) error {
      66              :         // 背景画像を描画
      67            1 :         bgImage, err := loadBackgroundImage(world, "title1")
      68            1 :         if err != nil {
      69            0 :                 return err
      70            0 :         }
      71            1 :         screen.DrawImage(bgImage, nil)
      72            1 : 
      73            1 :         st.screen.Draw(screen)
      74            1 :         return nil
      75              : }
      76              : 
      77              : // DoAction はActionを実行する
      78              : func (st *MainMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
      79            1 :         switch action {
      80              :         case inputmapper.ActionMenuCancel, inputmapper.ActionCloseMenu:
      81            1 :                 return es.Transition[w.World]{Type: es.TransQuit}, nil
      82              :         case inputmapper.ActionMenuSelect:
      83            1 :                 return st.handleSelection(world)
      84              :         default:
      85            0 :                 return es.Transition[w.World]{}, fmt.Errorf("mainMenu: unsupported action: %s", action)
      86              :         }
      87              : }
      88              : 
      89              : // ================
      90              : // Props
      91              : // ================
      92              : 
      93              : // MainMenuProps はメインメニューのProps
      94              : type MainMenuProps struct {
      95              :         Items []mainMenuItem
      96              : }
      97              : 
      98              : // mainMenuItem はメインメニューの項目。ResetsWorld が真の項目は遷移の前に前のゲームの
      99              : // 全実体を消す。新しいゲームを始める Start が立てる。ロードは save 側が担うので立てない
     100              : type mainMenuItem struct {
     101              :         Label       string
     102              :         Transition  es.Transition[w.World]
     103              :         ResetsWorld bool
     104              : }
     105              : 
     106              : // Fetch は世界から表示 props を構築する。menuloop.Model の Model 部にあたる
     107              : func (st *MainMenuState) Fetch(world w.World) (MainMenuProps, error) {
     108              :         // オープニングはキャラ作成後にゲーム画面へモーダルとして重ねる。開始スタックには積まない
     109            1 :         startFuncs := []es.StateFactory[w.World]{NewCharacterNamingState}
     110            1 : 
     111            2 :         t := func(msgid i18n.Msgid) string { return query.T(world, msgid) }
     112            1 :         items := []mainMenuItem{
     113            1 :                 {Label: t("Start"), Transition: es.Transition[w.World]{Type: es.TransReplace, NewStateFuncs: startFuncs}, ResetsWorld: true},
     114            1 :         }
     115            1 :         if world.Resources.Config.SaveLoadEnabled {
     116            1 :                 items = append(items, mainMenuItem{Label: t("Load"), Transition: es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{NewLoadMenuState}}})
     117            1 :         }
     118            1 :         items = append(items,
     119            1 :                 mainMenuItem{Label: t("Settings"), Transition: es.Transition[w.World]{Type: es.TransPush, NewStateFuncs: []es.StateFactory[w.World]{NewSettingsMenuState}}},
     120            1 :                 mainMenuItem{Label: t("Quit"), Transition: es.Transition[w.World]{Type: es.TransQuit}},
     121            1 :         )
     122            1 :         return MainMenuProps{Items: items}, nil
     123              : }
     124              : 
     125              : // Menu は一覧の構成を返す。menuloop.Model の Menu 部にあたる
     126              : func (st *MainMenuState) Menu(props MainMenuProps) menuloop.MenuConfig {
     127            1 :         return menuloop.MenuConfig{Key: "menu", TabCount: 1, ItemCounts: []int{len(props.Items)}}
     128            1 : }
     129              : 
     130              : func (st *MainMenuState) handleSelection(world w.World) (es.Transition[w.World], error) {
     131            1 :         props := st.screen.Props()
     132            1 :         itemIndex := st.screen.Selection().ItemIndex
     133            1 :         if itemIndex >= len(props.Items) {
     134            0 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
     135            0 :         }
     136            1 :         item := props.Items[itemIndex]
     137            1 :         if item.ResetsWorld {
     138            0 :                 world.ResetForNewGame()
     139            0 :         }
     140            1 :         return item.Transition, nil
     141              : }
     142              : 
     143              : // ================
     144              : // View
     145              : // ================
     146              : 
     147              : // ViewUI はメニューと版の注記をタイトル画面の枠へ載せる。パネル背景は付けず、
     148              : // タイトル背景を透かす。Screen が本体を uicore で描く。
     149              : func (st *MainMenuState) ViewUI(world w.World, props MainMenuProps, cursor menuloop.Selection, res resources.UIResources) uicore.Drawable {
     150            1 :         rows := make([]menuframe.Row, len(props.Items))
     151            1 :         for i, item := range props.Items {
     152            1 :                 rows[i] = menuframe.Row{Cells: styled.TextCells(item.Label)}
     153            1 :         }
     154              : 
     155              :         // 既定値のままの項目は未設定なので出さない
     156            1 :         var notes []string
     157            1 :         if consts.AppVersion != "v0.0.0" {
     158            0 :                 notes = append(notes, consts.AppVersion)
     159            0 :         }
     160            1 :         if consts.AppCommit != "0000000" {
     161            0 :                 notes = append(notes, consts.AppCommit)
     162            0 :         }
     163            1 :         if consts.AppDate != "0000-00-00" {
     164            0 :                 notes = append(notes, consts.AppDate)
     165            0 :         }
     166              : 
     167            1 :         return menuframe.TitleScreen(world, res, cursor.ItemIndex, rows, notes)
     168              : }
        

Generated by: LCOV version 2.0-1