Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Feb 1, 2023
1 parent 6e50ba3 commit f7bec52
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 34 deletions.
21 changes: 9 additions & 12 deletions src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ import Swarm.Version (NewReleaseFailure (..))
import System.Clock
import System.FilePath (splitDirectories)
import Witch (into)
import Language.LSP.Types.Lens (HasXdata(xdata))

tutorialsDirname :: FilePath
tutorialsDirname = "Tutorials"
Expand All @@ -135,7 +134,7 @@ handleEvent = \case
-- quitGame function would have already halted the app).
NoMenu -> const halt
MainMenu l -> handleMainMenuEvent l
NewGameMenu l c -> case c of
NewGameMenu l -> case s ^. uiState . uiLaunchConfig of
Nothing -> handleNewGameMenuEvent l
Just launchOptions -> handleLaunchOptionsEvent launchOptions
MessagesMenu -> handleMainMessagesEvent
Expand Down Expand Up @@ -164,7 +163,7 @@ handleMainMenuEvent menu = \case
NewGame -> do
cheat <- use $ uiState . uiCheatMode
ss <- use $ gameState . scenarios
uiState . uiMenu .= NewGameMenu (NE.fromList [mkScenarioList cheat ss]) Nothing
uiState . uiMenu .= NewGameMenu (NE.fromList [mkScenarioList cheat ss])
Tutorial -> do
-- Set up the menu stack as if the user had chosen "New Game > Tutorials"
cheat <- use $ uiState . uiCheatMode
Expand All @@ -176,7 +175,7 @@ handleMainMenuEvent menu = \case
(mkScenarioList cheat ss)
tutorialMenu = mkScenarioList cheat tutorialCollection
menuStack = NE.fromList [tutorialMenu, topMenu]
uiState . uiMenu .= NewGameMenu menuStack Nothing
uiState . uiMenu .= NewGameMenu menuStack

-- Extract the first tutorial challenge and run it
let firstTutorial = case scOrder tutorialCollection of
Expand Down Expand Up @@ -212,7 +211,7 @@ getTutorials sc = case M.lookup tutorialsDirname (scMap sc) of
-- is the only place this function should be called.
advanceMenu :: Menu -> Menu
advanceMenu m = case m of
NewGameMenu (z :| zs) x -> NewGameMenu (BL.listMoveDown z :| zs) x
NewGameMenu (z :| zs) -> NewGameMenu (BL.listMoveDown z :| zs)
_ -> m

handleMainAchievementsEvent ::
Expand Down Expand Up @@ -245,11 +244,9 @@ prepareGameStart ::
NonEmpty (BL.List Name ScenarioItem) ->
EventM Name AppState ()
prepareGameStart siPair stack = do
uiState . uiMenu .= NewGameMenu stack launchOptions
uiState . uiMenu .= NewGameMenu stack
return ()
where
launchOptions = Just $ LaunchOptions Nothing myForm ring

formFields =
[ (padRight (Pad 2) (txt "Seed") <+>)
@@= Forms.editShowableField seedVal (ScenarioConfigControl $ ScenarioConfigPanelControl SeedSelector)
Expand All @@ -272,7 +269,7 @@ handleNewGameMenuEvent scenarioStack@(curMenu :| rest) = \case
Just (SISingle siPair) -> startGame siPair Nothing
Just (SICollection _ c) -> do
cheat <- use $ uiState . uiCheatMode
uiState . uiMenu .= NewGameMenu (NE.cons (mkScenarioList cheat c) scenarioStack) Nothing
uiState . uiMenu .= NewGameMenu (NE.cons (mkScenarioList cheat c) scenarioStack)
CharKey 'o' -> case snd <$> BL.listSelectedElement curMenu of
Just (SISingle siPair) -> prepareGameStart siPair scenarioStack
_ -> continueWithoutRedraw
Expand All @@ -281,7 +278,7 @@ handleNewGameMenuEvent scenarioStack@(curMenu :| rest) = \case
ControlChar 'q' -> halt
VtyEvent ev -> do
menu' <- nestEventM' curMenu (handleListEvent ev)
uiState . uiMenu .= NewGameMenu (menu' :| rest) maybeLaunchOptions
uiState . uiMenu .= NewGameMenu (menu' :| rest)
_ -> continueWithoutRedraw

exitNewGameMenu :: NonEmpty (BL.List Name ScenarioItem) -> EventM Name AppState ()
Expand All @@ -290,7 +287,7 @@ exitNewGameMenu stk = do
. uiMenu
.= case snd (NE.uncons stk) of
Nothing -> MainMenu (mainMenu NewGame)
Just stk' -> NewGameMenu stk' Nothing
Just stk' -> NewGameMenu stk'

pressAnyKey :: Menu -> BrickEvent Name AppEvent -> EventM Name AppState ()
pressAnyKey m (VtyEvent (V.EvKey _ _)) = uiState . uiMenu .= m
Expand Down Expand Up @@ -513,7 +510,7 @@ saveScenarioInfoOnQuit = do
-- currentScenarioPath or it might be different.
uim <- preuse $ uiState . uiMenu
let curPath = case uim of
Just (NewGameMenu (z :| _) _) ->
Just (NewGameMenu (z :| _)) ->
case BL.listSelectedElement z of
Just (_, SISingle (_, sInfo)) -> Just $ _scenarioPath sInfo
_ -> Nothing
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/TUI/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ data AppOpts = AppOpts
-- is the last among its siblings.
nextScenario :: Menu -> Maybe ScenarioInfoPair
nextScenario = \case
NewGameMenu (curMenu :| _) _ ->
NewGameMenu (curMenu :| _) ->
let nextMenuList = BL.listMoveDown curMenu
isLastScenario = BL.listSelected curMenu == Just (length (BL.listElements curMenu) - 1)
in if isLastScenario
Expand Down
3 changes: 1 addition & 2 deletions src/Swarm/TUI/Model/Menu.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Data.Map qualified as M
import Data.Text (Text)
import Data.Vector qualified as V
import Swarm.Game.Entity as E
import Swarm.Game.Scenario.Launch
import Swarm.Game.ScenarioInfo (
ScenarioCollection,
ScenarioInfo (..),
Expand Down Expand Up @@ -102,7 +101,7 @@ mkScenarioList cheat = flip (BL.list ScenarioList) 1 . V.fromList . filterTest .
mkNewGameMenu :: Bool -> ScenarioCollection -> FilePath -> Maybe Menu
mkNewGameMenu cheat sc path = do
theList <- NE.fromList <$> go (Just sc) (splitPath path) []
return $ NewGameMenu theList Nothing
return $ NewGameMenu theList
where
go ::
Maybe ScenarioCollection ->
Expand Down
14 changes: 0 additions & 14 deletions src/Swarm/TUI/Model/Name.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ data Button
| NextButton
deriving (Eq, Ord, Show, Read, Bounded, Enum)

data GoalWidget
= ObjectivesList
| GoalSummary
deriving (Eq, Ord, Show, Read, Bounded, Enum)

-- | Clickable buttons in modal dialogs.
data Button
= CancelButton
| KeepPlayingButton
| StartOverButton
| QuitButton
| NextButton
deriving (Eq, Ord, Show, Read, Bounded, Enum)

-- | 'Name' represents names to uniquely identify various components
-- of the UI, such as forms, panels, caches, extents, lists, and buttons.
data Name
Expand Down
6 changes: 4 additions & 2 deletions src/Swarm/TUI/Model/UI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Swarm.TUI.Model.UI (
uiPlaying,
uiCheatMode,
uiFocusRing,
uiLaunchConfig,
uiWorldCursor,
uiREPL,
uiInventory,
Expand Down Expand Up @@ -62,6 +63,7 @@ import Swarm.Game.ScenarioInfo (
ScenarioInfoPair,
)
import Swarm.Game.World qualified as W
import Swarm.Game.Scenario.Launch
import Swarm.TUI.Attr (swarmAttrMap)
import Swarm.TUI.Inventory.Sorting
import Swarm.TUI.Model.Achievement.Attainment
Expand All @@ -85,7 +87,7 @@ data UIState = UIState
, _uiPlaying :: Bool
, _uiCheatMode :: Bool
, _uiFocusRing :: FocusRing Name
, _uiLaunchConfig :: Maybe LaunchConfig
, _uiLaunchConfig :: Maybe LaunchOptions
, _uiWorldCursor :: Maybe W.Coords
, _uiREPL :: REPLState
, _uiInventory :: Maybe (Int, BL.List Name InventoryListEntry)
Expand Down Expand Up @@ -139,7 +141,7 @@ uiPlaying :: Lens' UIState Bool
uiCheatMode :: Lens' UIState Bool

-- | Configuration modal when launching a scenario
uiLaunchConfig :: Lens' UIState (Maybe LaunchConfig)
uiLaunchConfig :: Lens' UIState (Maybe LaunchOptions)

-- | The focus ring is the set of UI panels we can cycle among using
-- the Tab key.
Expand Down
4 changes: 3 additions & 1 deletion src/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ drawUI s
-- quit the app instead. But just in case, we display the main menu anyway.
NoMenu -> [drawMainMenuUI s (mainMenu NewGame)]
MainMenu l -> [drawMainMenuUI s l]
NewGameMenu stk scenarioCfg -> drawNewGameMenuUI stk scenarioCfg
NewGameMenu stk -> do
let launchOptions = s ^. uiState . uiLaunchConfig
drawNewGameMenuUI stk launchOptions
AchievementsMenu l -> [drawAchievementsMenuUI s l]
MessagesMenu -> [drawMainMessages s]
AboutMenu -> [drawAboutMenuUI (s ^. uiState . appData . at "about")]
Expand Down
4 changes: 2 additions & 2 deletions src/Swarm/TUI/View/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ maxModalWindowWidth = 500
-- | Get the name of the current New Game menu.
curMenuName :: AppState -> Maybe Text
curMenuName s = case s ^. uiState . uiMenu of
NewGameMenu (_ :| (parentMenu : _)) _ ->
NewGameMenu (_ :| (parentMenu : _)) ->
Just (parentMenu ^. BL.listSelectedElementL . to scenarioItemName)
NewGameMenu _ _ -> Just "Scenarios"
NewGameMenu _ -> Just "Scenarios"
_ -> Nothing

quitMsg :: Menu -> Text
Expand Down

0 comments on commit f7bec52

Please sign in to comment.