-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scenario launch configuration dialog
Towards #358
- Loading branch information
Showing
10 changed files
with
268 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Swarm.Game.Scenario.Launch.Controller where | ||
|
||
import Brick hiding (Direction, Location) | ||
import Brick.Focus | ||
import Brick.Widgets.Edit (handleEditorEvent) | ||
import Brick.Widgets.FileBrowser | ||
import Control.Lens | ||
import Control.Monad.Except | ||
import Graphics.Vty qualified as V | ||
import Swarm.Game.Scenario.Launch.Model | ||
import Swarm.Game.ScenarioInfo | ||
import Swarm.TUI.Controller.Util | ||
import Swarm.TUI.Model | ||
import Swarm.TUI.Model.Name | ||
import Swarm.TUI.Model.StateUpdate | ||
import Swarm.TUI.Model.UI | ||
|
||
handleLaunchOptionsEvent :: | ||
ScenarioInfoPair -> | ||
BrickEvent Name AppEvent -> | ||
EventM Name AppState () | ||
handleLaunchOptionsEvent siPair = \case | ||
CharKey '\t' -> do | ||
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 | ||
StartGameButton -> do | ||
closeModal | ||
startGame siPair Nothing | ||
_ -> return () | ||
Key V.KEsc -> closeModal | ||
CharKey 'q' -> closeModal | ||
ControlChar 'q' -> closeModal | ||
ev -> do | ||
fr <- use $ uiState . uiLaunchConfig . scenarioConfigFocusRing | ||
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 () | ||
where | ||
closeModal = uiState . uiLaunchConfig . isDisplayedFor .= Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Swarm.Game.Scenario.Launch.Model where | ||
|
||
import Brick.Focus qualified as Focus | ||
import Brick.Widgets.Edit | ||
import Brick.Widgets.FileBrowser qualified as FB | ||
import Control.Lens (makeLenses) | ||
import Data.Text (Text) | ||
import Swarm.Game.ScenarioInfo | ||
import Swarm.TUI.Model.Name | ||
import Swarm.Util (listEnums) | ||
|
||
newtype SeedSelection = SeedSelection | ||
{ _seedVal :: Int | ||
} | ||
|
||
makeLenses ''SeedSelection | ||
|
||
-- | UI elements to configure scenario launch options | ||
data LaunchOptions = LaunchOptions | ||
{ _fileBrowser :: Maybe (FB.FileBrowser Name) | ||
, _seedValueEditor :: Editor Text Name | ||
, _scenarioConfigFocusRing :: Focus.FocusRing Name | ||
, _isDisplayedFor :: Maybe ScenarioInfoPair | ||
} | ||
|
||
makeLenses ''LaunchOptions | ||
|
||
initConfigPanel :: LaunchOptions | ||
initConfigPanel = | ||
LaunchOptions Nothing myForm ring Nothing | ||
where | ||
myForm = | ||
editorText | ||
(ScenarioConfigControl $ ScenarioConfigPanelControl SeedSelector) | ||
(Just 1) | ||
"0" | ||
ring = Focus.focusRing $ map (ScenarioConfigControl . ScenarioConfigPanelControl) listEnums |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Swarm.Game.Scenario.Launch.View where | ||
|
||
import Brick | ||
import Brick.Focus | ||
import Brick.Forms qualified as BF | ||
import Brick.Widgets.Border | ||
import Brick.Widgets.Center (centerLayer, hCenter) | ||
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.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 | ||
where | ||
ui = | ||
hCenter $ | ||
vLimit 15 $ | ||
hLimit 50 $ | ||
borderWithLabel (txt "Choose a file") $ | ||
FB.renderFileBrowser True b | ||
help = | ||
padTop (Pad 1) $ | ||
vBox | ||
[ case FB.fileBrowserException b of | ||
Nothing -> emptyWidget | ||
Just e -> | ||
hCenter $ | ||
withDefAttr BF.invalidFormInputAttr $ | ||
txt $ | ||
T.pack $ | ||
E.displayException e | ||
, hCenter $ txt "Up/Down: select" | ||
, hCenter $ txt "/: search, Ctrl-C or Esc: cancel search" | ||
, hCenter $ txt "Enter: change directory or select file" | ||
, hCenter $ txt "Esc: quit" | ||
] | ||
|
||
drawLaunchConfigPanel :: LaunchOptions -> [Widget Name] | ||
drawLaunchConfigPanel (LaunchOptions maybeFileBrowser seedEditor ring _isDisplayedFor) = case maybeFileBrowser of | ||
Nothing -> [panelWidget] | ||
Just fb -> [drawFileBrowser fb, panelWidget] | ||
where | ||
seedEditorHasFocus = case focusGetCurrent ring of | ||
Just (ScenarioConfigControl (ScenarioConfigPanelControl SeedSelector)) -> True | ||
_ -> False | ||
|
||
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" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.