clone

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

chore/app-model-decomp
puzzletea / app / screen_menu.go
screen_menu.go 3.0K
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, openCreateAction{}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 }80 case key.Matches(keyMsg, rootKeys.Escape):81 return s, nil, backAction{target: mainMenuView}82 }8384 return s, nil, nil85}8687func (s playMenuScreen) View(notice noticeState) string {88 return centerContentWithNotice(s.width, s.height, notice, s.menu.ViewAsPanel("Play"))89}9091type optionsMenuScreen struct {92 width int93 height int94 menu ui.MainMenu95}9697func (s optionsMenuScreen) Resize(width, height int) screenModel {98 s.width = width99 s.height = height100 return s101}102103func (s optionsMenuScreen) Update(msg tea.Msg) (screenModel, tea.Cmd, screenAction) {104 updateMainMenuCursor(msg, &s.menu)105 keyMsg, ok := msg.(tea.KeyPressMsg)106 if !ok {107 return s, nil, nil108 }109110 switch {111 case key.Matches(keyMsg, rootKeys.Enter):112 switch s.menu.SelectedAction() {113 case optionsMenuActionTheme:114 return s, nil, openThemeSelectAction{}115 case optionsMenuActionGuides:116 return s, nil, openHelpSelectAction{}117 }118 case key.Matches(keyMsg, rootKeys.Escape):119 return s, nil, backAction{target: mainMenuView}120 }121122 return s, nil, nil123}124125func (s optionsMenuScreen) View(notice noticeState) string {126 items := s.menu.RenderItems() + "\n\n" + ui.DimItemStyle().Render("- Dami")127 return renderPanelView(s.width, s.height, notice, "Options", items, "↑/↓ navigate • enter select • esc back")128}