Skip to content

Commit

Permalink
more preparation for World Editor PR (#902)
Browse files Browse the repository at this point in the history
Towards #873.

* Removes the `Eq` instance for `ModalType` --- I plan to add another member to that enum that contains data without an `Eq` instance.
* Decompose `toggleModal` function
  • Loading branch information
kostmo committed Dec 14, 2022
1 parent 9c06024 commit f751764
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
22 changes: 15 additions & 7 deletions src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ handleMainEvent ev = do
safeAutoUnpause
uiState . uiModal .= Nothing
-- message modal is not autopaused, so update notifications when leaving it
when (m ^. modalType == MessagesModal) $ do
gameState . lastSeenMessageTime .= s ^. gameState . ticks
case m ^. modalType of
MessagesModal -> do
gameState . lastSeenMessageTime .= s ^. gameState . ticks
_ -> return ()
FKey 1 -> toggleModal HelpModal
FKey 2 -> toggleModal RobotsModal
FKey 3 | not (null (s ^. gameState . availableRecipes . notificationsContent)) -> do
Expand Down Expand Up @@ -351,11 +353,14 @@ toggleModal :: ModalType -> EventM Name AppState ()
toggleModal mt = do
modal <- use $ uiState . uiModal
case modal of
Nothing -> do
newModal <- gets $ flip generateModal mt
ensurePause
uiState . uiModal ?= newModal
Nothing -> openModal mt
Just _ -> uiState . uiModal .= Nothing >> safeAutoUnpause

openModal :: ModalType -> EventM Name AppState ()
openModal mt = do
newModal <- gets $ flip generateModal mt
ensurePause
uiState . uiModal ?= newModal
where
-- Set the game to AutoPause if needed
ensurePause = do
Expand All @@ -365,7 +370,10 @@ toggleModal mt = do

-- | The running modals do not autopause the game.
isRunningModal :: ModalType -> Bool
isRunningModal mt = mt `elem` [RobotsModal, MessagesModal]
isRunningModal = \case
RobotsModal -> True
MessagesModal -> True
_ -> False

handleModalEvent :: V.Event -> EventM Name AppState ()
handleModalEvent = \case
Expand Down
11 changes: 8 additions & 3 deletions src/Swarm/TUI/Model/Menus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ data ModalType
| KeepPlayingModal
| DescriptionModal Entity
| GoalModal [Text]
deriving (Eq, Show)

data ButtonSelection = CancelButton | KeepPlayingButton | StartOverButton Seed ScenarioInfoPair | QuitButton | NextButton ScenarioInfoPair
deriving (Show)

data ButtonSelection
= CancelButton
| KeepPlayingButton
| StartOverButton Seed ScenarioInfoPair
| QuitButton
| NextButton ScenarioInfoPair

data Modal = Modal
{ _modalType :: ModalType
Expand Down

0 comments on commit f751764

Please sign in to comment.