Skip to content

Commit

Permalink
WIP UI controls
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Feb 19, 2023
1 parent f9acda0 commit 68fe08f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 37 deletions.
37 changes: 23 additions & 14 deletions src/Swarm/Game/Scenario/Launch/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,39 @@ import Swarm.TUI.Model.Name
import Swarm.TUI.Model.StateUpdate
import Swarm.TUI.Model.UI

handleFBEvent ::
BrickEvent Name AppEvent ->
EventM Name AppState ()
handleFBEvent = \case
Key V.KEsc -> closeModal
CharKey 'q' -> closeModal
ControlChar 'q' -> closeModal
VtyEvent e ->
Brick.zoom (uiState . uiLaunchConfig . fileBrowser . fbWidget) (handleFileBrowserEvent e)
_ -> return ()
where
closeModal = uiState . uiLaunchConfig . fileBrowser . fbIsDisplayed .= False

handleLaunchOptionsEvent ::
ScenarioInfoPair ->
BrickEvent Name AppEvent ->
EventM Name AppState ()
handleLaunchOptionsEvent siPair = \case
CharKey '\t' -> do
Key V.KBackTab ->
uiState . uiLaunchConfig . scenarioConfigFocusRing %= focusPrev
Key V.KUp ->
uiState . uiLaunchConfig . scenarioConfigFocusRing %= focusPrev
CharKey '\t' ->
uiState . uiLaunchConfig . scenarioConfigFocusRing %= focusNext
Key V.KDown ->
uiState . uiLaunchConfig . scenarioConfigFocusRing %= focusNext
Key V.KEnter -> do
fr <- use $ uiState . uiLaunchConfig . scenarioConfigFocusRing
case focusGetCurrent fr of
Just (ScenarioConfigControl (ScenarioConfigPanelControl item)) -> case item of
SeedSelector -> return ()
ScriptSelector -> do
fb <-
liftIO $
newFileBrowser
selectNonDirectories
(ScenarioConfigControl $ ScenarioConfigPanelControl ScriptSelector)
Nothing
uiState . uiLaunchConfig . fileBrowser .= Just fb
ScriptSelector ->
uiState . uiLaunchConfig . fileBrowser . fbIsDisplayed .= True
StartGameButton -> do
closeModal
startGame siPair Nothing
Expand All @@ -47,11 +60,7 @@ handleLaunchOptionsEvent siPair = \case
case focusGetCurrent fr of
Just (ScenarioConfigControl (ScenarioConfigPanelControl item)) -> case item of
SeedSelector -> Brick.zoom (uiState . uiLaunchConfig . seedValueEditor) (handleEditorEvent ev)
ScriptSelector -> case ev of
VtyEvent e ->
Brick.zoom (uiState . uiLaunchConfig . fileBrowser . _Just) (handleFileBrowserEvent e)
_ -> return ()
StartGameButton -> return ()
_ -> return ()
_ -> return ()
where
closeModal = uiState . uiLaunchConfig . isDisplayedFor .= Nothing
22 changes: 18 additions & 4 deletions src/Swarm/Game/Scenario/Launch/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,33 @@ newtype SeedSelection = SeedSelection

makeLenses ''SeedSelection

data FileBrowserControl = FileBrowserControl
{ _fbWidget :: FB.FileBrowser Name
, _fbIsDisplayed :: Bool
}

makeLenses ''FileBrowserControl

-- | UI elements to configure scenario launch options
data LaunchOptions = LaunchOptions
{ _fileBrowser :: Maybe (FB.FileBrowser Name)
{ _fileBrowser :: FileBrowserControl
, _seedValueEditor :: Editor Text Name
, _scenarioConfigFocusRing :: Focus.FocusRing Name
, _isDisplayedFor :: Maybe ScenarioInfoPair
}

makeLenses ''LaunchOptions

initConfigPanel :: LaunchOptions
initConfigPanel =
LaunchOptions Nothing myForm ring Nothing
initConfigPanel :: IO LaunchOptions
initConfigPanel = do
fb <-
FB.newFileBrowser
FB.selectNonDirectories
-- (const False)
(ScenarioConfigControl $ ScenarioConfigPanelControl ScriptSelector)
Nothing
let configuredFB = FB.setFileBrowserEntryFilter (Just $ FB.fileExtensionMatch "sw") fb
return $ LaunchOptions (FileBrowserControl configuredFB False) myForm ring Nothing
where
myForm =
editorText
Expand Down
43 changes: 30 additions & 13 deletions src/Swarm/Game/Scenario/Launch/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ import Brick.Widgets.Edit
import Brick.Widgets.Edit qualified as E
import Brick.Widgets.FileBrowser qualified as FB
import Control.Exception qualified as E
import Data.Maybe (listToMaybe)
import Data.Text qualified as T
import Swarm.Game.Scenario.Launch.Model
import Swarm.TUI.Attr
import Swarm.TUI.Model.Name

drawFileBrowser :: FB.FileBrowser Name -> Widget Name
drawFileBrowser b =
centerLayer $ ui <=> help
centerLayer $ hLimit 50 $ ui <=> help
where
ui =
hCenter $
vLimit 15 $
hLimit 50 $
borderWithLabel (txt "Choose a file") $
FB.renderFileBrowser True b
vLimit 15 $
borderWithLabel (txt "Choose a file") $
FB.renderFileBrowser True b
help =
padTop (Pad 1) $
vBox
Expand All @@ -44,22 +43,40 @@ drawFileBrowser b =
]

drawLaunchConfigPanel :: LaunchOptions -> [Widget Name]
drawLaunchConfigPanel (LaunchOptions maybeFileBrowser seedEditor ring _isDisplayedFor) = case maybeFileBrowser of
Nothing -> [panelWidget]
Just fb -> [drawFileBrowser fb, panelWidget]
drawLaunchConfigPanel (LaunchOptions (FileBrowserControl fb isFbDisplayed) seedEditor ring _isDisplayedFor) =
if isFbDisplayed
then [drawFileBrowser fb, panelWidget]
else [panelWidget]
where
seedEditorHasFocus = case focusGetCurrent ring of
Just (ScenarioConfigControl (ScenarioConfigPanelControl SeedSelector)) -> True
_ -> False

highlightIfFocused x =
if focusGetCurrent ring == Just (ScenarioConfigControl (ScenarioConfigPanelControl x))
then withAttr highlightAttr
else id

mkButton name label = highlightIfFocused name $ str label

panelWidget =
centerLayer $
borderWithLabel (str "Configure scenario") $
hLimit 50 $
vBox
[ padAll 1 $ txt "Hello there!"
, overrideAttr E.editFocusedAttr customEditFocusedAttr $
renderEditor (txt . mconcat) seedEditorHasFocus seedEditor
, hCenter $ str "Select script"
, hCenter $ str "Launch"
, hBox
[ mkButton SeedSelector "Seed: "
, hLimit 10 $
overrideAttr E.editFocusedAttr customEditFocusedAttr $
renderEditor (txt . mconcat) seedEditorHasFocus seedEditor
]
, hBox
[ mkButton ScriptSelector "Script: "
, str $
maybe "<none>" FB.fileInfoSanitizedFilename $
listToMaybe $
FB.fileBrowserSelection fb
]
, hCenter $ mkButton StartGameButton ">> Launch with these settings <<"
]
1 change: 0 additions & 1 deletion src/Swarm/TUI/Attr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,3 @@ yellowAttr = attrName "yellow"
cyanAttr = attrName "cyan"
lightCyanAttr = attrName "lightCyan"
magentaAttr = attrName "magenta"

9 changes: 6 additions & 3 deletions src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ handleEvent = \case
-- quitGame function would have already halted the app).
NoMenu -> const halt
MainMenu l -> handleMainMenuEvent l
NewGameMenu l -> case s ^. uiState . uiLaunchConfig . isDisplayedFor of
Nothing -> handleNewGameMenuEvent l
Just siPair -> handleLaunchOptionsEvent siPair
NewGameMenu l ->
if s ^. uiState . uiLaunchConfig . fileBrowser . fbIsDisplayed
then handleFBEvent
else case s ^. uiState . uiLaunchConfig . isDisplayedFor of
Nothing -> handleNewGameMenuEvent l
Just siPair -> handleLaunchOptionsEvent siPair
MessagesMenu -> handleMainMessagesEvent
AchievementsMenu l -> handleMainAchievementsEvent l
AboutMenu -> pressAnyKey (MainMenu (mainMenu About))
Expand Down
5 changes: 3 additions & 2 deletions src/Swarm/TUI/Model/UI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ import Data.Map (Map)
import Data.Map qualified as M
import Data.Text (Text)
import Data.Text qualified as T
import Swarm.Game.Scenario.Launch.Model
import Swarm.Game.Achievement.Attainment
import Swarm.Game.Achievement.Definitions
import Swarm.Game.Achievement.Persistence
import Swarm.Game.Scenario.Launch.Model
import Swarm.Game.ScenarioInfo (
ScenarioInfoPair,
)
Expand Down Expand Up @@ -281,12 +281,13 @@ initUIState showMainMenu cheatMode = do
let history = maybe [] (map REPLEntry . T.lines) historyT
startTime <- liftIO $ getTime Monotonic
(warnings, achievements) <- liftIO loadAchievementsInfo
launchConfigPanel <- liftIO initConfigPanel
let out =
UIState
{ _uiMenu = if showMainMenu then MainMenu (mainMenu NewGame) else NoMenu
, _uiPlaying = not showMainMenu
, _uiCheatMode = cheatMode
, _uiLaunchConfig = initConfigPanel
, _uiLaunchConfig = launchConfigPanel
, _uiFocusRing = initFocusRing
, _uiWorldCursor = Nothing
, _uiREPL = initREPLState $ newREPLHistory history
Expand Down

0 comments on commit 68fe08f

Please sign in to comment.