clone

fifteen puzzle games for the terminal, with daily puzzles, weekly ladders and xp, built on bubble tea

feat/app-decomposition
puzzletea / app / screen_menu.go
screen_menu.go 3.1K
1package app23import (4 "github.com/FelineStateMachine/puzzletea/ui"56 "charm.land/bubbles/v2/key"7 tea "charm.land/bubbletea/v2"8)910type mainMenuScreen struct {11 width int12 height int13 menu ui.MainMenu14}1516func (s mainMenuScreen) Resize(width, height int) screenModel {17 s.width = width18 s.height = height19 return s20}2122func (s mainMenuScreen) Update(msg tea.Msg) (screenModel, tea.Cmd, screenAction) {23 updateMainMenuCursor(msg, &s.menu)24 keyMsg, ok := msg.(tea.KeyPressMsg)25 if !ok || !key.Matches(keyMsg, rootKeys.Enter) {26 return s, nil, nil27 }2829 switch s.menu.SelectedAction() {30 case mainMenuActionPlay:31 return s, nil, openPlayMenuAction{}32 case mainMenuActionExport:33 return s, nil, openExportAction{}34 case mainMenuActionStats:35 return s, nil, openStatsAction{}36 case mainMenuActionOptions:37 return s, nil, openOptionsMenuAction{}38 case mainMenuActionQuit:39 return s, nil, quitAction{}40 default:41 return s, nil, nil42 }43}4445func (s mainMenuScreen) View(notice noticeState) string {46 return centerContentWithNotice(s.width, s.height, notice, s.menu.View())47}4849type playMenuScreen struct {50 width int51 height int52 menu ui.MainMenu53}5455func (s playMenuScreen) Resize(width, height int) screenModel {56 s.width = width57 s.height = height58 return s59}6061func (s playMenuScreen) Update(msg tea.Msg) (screenModel, tea.Cmd, screenAction) {62 updateMainMenuCursor(msg, &s.menu)63 keyMsg, ok := msg.(tea.KeyPressMsg)64 if !ok {65 return s, nil, nil66 }6768 switch {69 case key.Matches(keyMsg, rootKeys.Enter):70 switch s.menu.SelectedAction() {71 case playMenuActionCreate:72 return s, nil, openGameSelectAction{}73 case playMenuActionContinue:74 return s, nil, openContinueAction{}75 case playMenuActionDaily:76 return s, nil, openDailyAction{}77 case playMenuActionWeekly:78 return s, nil, openWeeklyAction{}79 case playMenuActionSeeded:80 return s, nil, openSeedInputAction{}81 }82 case key.Matches(keyMsg, rootKeys.Escape):83 return s, nil, backAction{target: mainMenuView}84 }8586 return s, nil, nil87}8889func (s playMenuScreen) View(notice noticeState) string {90 return centerContentWithNotice(s.width, s.height, notice, s.menu.ViewAsPanel("Play"))91}9293type optionsMenuScreen struct {94 width int95 height int96 menu ui.MainMenu97}9899func (s optionsMenuScreen) Resize(width, height int) screenModel {100 s.width = width101 s.height = height102 return s103}104105func (s optionsMenuScreen) Update(msg tea.Msg) (screenModel, tea.Cmd, screenAction) {106 updateMainMenuCursor(msg, &s.menu)107 keyMsg, ok := msg.(tea.KeyPressMsg)108 if !ok {109 return s, nil, nil110 }111112 switch {113 case key.Matches(keyMsg, rootKeys.Enter):114 switch s.menu.SelectedAction() {115 case optionsMenuActionTheme:116 return s, nil, openThemeSelectAction{}117 case optionsMenuActionGuides:118 return s, nil, openHelpSelectAction{}119 }120 case key.Matches(keyMsg, rootKeys.Escape):121 return s, nil, backAction{target: mainMenuView}122 }123124 return s, nil, nil125}126127func (s optionsMenuScreen) View(notice noticeState) string {128 items := s.menu.RenderItems() + "\n\n" + ui.DimItemStyle().Render("- Dami")129 return renderPanelView(s.width, s.height, notice, "Options", items, "↑/↓ navigate • enter select • esc back")130}