Skip to content

Commit 59da903

Browse files
Returns to the scenario menu after winning a game (#422)
This change slightly improves the navigation by saving the menu position so that the user gets back to the next tutorial or challenge after winning a game.
1 parent 32021df commit 59da903

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/Swarm/TUI/Controller.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,22 @@ handleNewGameMenuEvent scenarioStack@(curMenu :| rest) s = \case
140140
let gs = s ^. gameState
141141
gs' <- liftIO $ playScenario (gs ^. entityMap) scene Nothing Nothing gs
142142

143+
let nextMenu
144+
-- Go back to the scenario list
145+
| null rest = NewGameMenu scenarioStack
146+
-- Advance to the next tutorial or challenge
147+
| otherwise = NewGameMenu (BL.listMoveDown curMenu :| rest)
148+
143149
continue $
144150
s & uiState . uiMenu .~ NoMenu
151+
& uiState . uiPrevMenu .~ nextMenu
145152
& gameState .~ gs'
146153
Just (SICollection _ c) ->
147154
continue $
148155
s & uiState . uiMenu .~ NewGameMenu (NE.cons (mkScenarioList (s ^. uiState . uiCheatMode) c) scenarioStack)
149156
Key V.KEsc -> exitNewGameMenu s scenarioStack
150157
CharKey 'q' -> exitNewGameMenu s scenarioStack
151-
ControlKey 'q' -> exitNewGameMenu s scenarioStack
158+
ControlKey 'q' -> halt s
152159
VtyEvent ev -> do
153160
menu' <- handleListEvent ev curMenu
154161
continue $ s & uiState . uiMenu .~ NewGameMenu (menu' :| rest)
@@ -243,14 +250,14 @@ handleModalEvent s = \case
243250
continue s'
244251

245252
-- | Quit a game. Currently all it does is write out the updated REPL
246-
-- history to a @.swarm_history@ file, and return to the main menu.
253+
-- history to a @.swarm_history@ file, and return to the previous menu.
247254
quitGame :: AppState -> EventM Name (Next AppState)
248255
quitGame s = do
249256
let hist = mapMaybe getREPLEntry $ getLatestREPLHistoryItems maxBound history
250257
liftIO $ (`T.appendFile` T.unlines hist) =<< getSwarmHistoryPath True
251258
let s' =
252259
s & uiState . uiReplHistory %~ restartREPLHistory
253-
& uiState . uiMenu .~ MainMenu (mainMenu NewGame)
260+
& uiState . uiMenu .~ (s ^. uiState . uiPrevMenu)
254261
continue s'
255262
where
256263
history = s ^. uiState . uiReplHistory

src/Swarm/TUI/Model.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module Swarm.TUI.Model (
5757
-- ** UI Model
5858
UIState,
5959
uiMenu,
60+
uiPrevMenu,
6061
uiCheatMode,
6162
uiFocusRing,
6263
uiReplForm,
@@ -359,6 +360,7 @@ makePrisms ''InventoryListEntry
359360
-- see the lenses below.
360361
data UIState = UIState
361362
{ _uiMenu :: Menu
363+
, _uiPrevMenu :: Menu
362364
, _uiCheatMode :: Bool
363365
, _uiFocusRing :: FocusRing Name
364366
, _uiReplForm :: Form Text AppEvent Name
@@ -407,6 +409,9 @@ let exclude = ['_lgTicksPerSecond]
407409
-- | The current menu state.
408410
uiMenu :: Lens' UIState Menu
409411

412+
-- | The previous menu state, to go back after playing a game
413+
uiPrevMenu :: Lens' UIState Menu
414+
410415
-- | Cheat mode, i.e. are we allowed to turn creative mode on and off?
411416
uiCheatMode :: Lens' UIState Bool
412417

@@ -579,6 +584,7 @@ initUIState showMainMenu cheatMode = liftIO $ do
579584
return $
580585
UIState
581586
{ _uiMenu = if showMainMenu then MainMenu (mainMenu NewGame) else NoMenu
587+
, _uiPrevMenu = NoMenu
582588
, _uiCheatMode = cheatMode
583589
, _uiFocusRing = initFocusRing
584590
, _uiReplForm = initReplForm

src/Swarm/TUI/View.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,15 @@ generateModal s mt = Modal mt (dialog (Just title) buttons (maxModalWindowWidth
288288
(title, widget, buttons, requiredWidth) =
289289
case mt of
290290
HelpModal -> (" Help ", helpWidget, Nothing, maxModalWindowWidth)
291-
WinModal -> ("", txt "Congratulations!", Nothing, maxModalWindowWidth)
291+
WinModal ->
292+
let winMsg = "Congratulations!"
293+
continueMsg = "Keep playing"
294+
stopMsg = "Pick the next game"
295+
in ( ""
296+
, padBottom (Pad 1) $ hCenter $ txt winMsg
297+
, Just (0, [(stopMsg, Confirm), (continueMsg, Cancel)])
298+
, length continueMsg + length stopMsg + 32
299+
)
292300
DescriptionModal e -> (descriptionTitle e, descriptionWidget s e, Nothing, 100)
293301
QuitModal ->
294302
let quitMsg = "Are you sure you want to quit this game and return to the menu?"

0 commit comments

Comments
 (0)