{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ViewPatterns #-}
module Swarm.TUI.Model (
AppEvent (..),
FocusablePanel (..),
Name (..),
WebCommand (..),
WebInvocationState (..),
RejectionReason (..),
ModalType (..),
ScenarioOutcome (..),
Button (..),
ButtonAction (..),
modalType,
modalDialog,
MainMenuEntry (..),
mainMenu,
_NewGameMenu,
mkScenarioList,
InventoryListEntry (..),
_Separator,
_InventoryEntry,
_EquippedEntry,
populateInventoryList,
infoScroll,
modalScroll,
replScroll,
logEvent,
SwarmKeyDispatcher,
KeyEventHandlingState (KeyEventHandlingState),
SwarmKeyDispatchers (..),
keyConfig,
keyDispatchers,
AppState (AppState),
uiState,
playState,
keyEventHandling,
runtimeState,
animationMgr,
ScenarioState (ScenarioState),
gameState,
uiGameplay,
PlayState (..),
scenarioState,
progression,
ProgressionState (..),
scenarios,
attainedAchievements,
uiPopups,
uiPopupAnimationState,
scenarioSequence,
AnimationState (..),
_AnimActive,
_AnimScheduled,
_AnimInactive,
AppOpts (..),
defaultAppOpts,
ColorMode (..),
focusedItem,
focusedEntity,
animTraversal,
) where
import Brick (EventM, ViewportScroll, viewportScroll)
import Brick.Animation (Animation, AnimationManager)
import Brick.Keybindings as BK
import Brick.Widgets.List qualified as BL
import Control.Lens hiding (from, (<.>))
import Control.Monad ((>=>))
import Control.Monad.State (MonadState)
import Data.List (findIndex)
import Data.Map (Map)
import Data.Maybe (fromMaybe)
import Data.Set (Set)
import Data.Text (Text)
import Data.Vector qualified as V
import GitHash (GitInfo)
import Graphics.Vty (ColorMode (..))
import Network.Wai.Handler.Warp (Port)
import Swarm.Game.Achievement.Attainment
import Swarm.Game.Achievement.Definitions
import Swarm.Game.Entity as E
import Swarm.Game.Ingredients
import Swarm.Game.Popup
import Swarm.Game.Robot
import Swarm.Game.Scenario.Status
import Swarm.Game.ScenarioInfo (ScenarioCollection)
import Swarm.Game.State
import Swarm.Game.State.Runtime
import Swarm.Game.State.Substate
import Swarm.Game.Tick (TickNumber (..))
import Swarm.Game.World.Gen (Seed)
import Swarm.Log
import Swarm.TUI.Inventory.Sorting
import Swarm.TUI.Model.DebugOption (DebugOption)
import Swarm.TUI.Model.Event (SwarmEvent)
import Swarm.TUI.Model.Menu
import Swarm.TUI.Model.Name
import Swarm.TUI.Model.UI
import Swarm.TUI.Model.UI.Gameplay
import Swarm.TUI.Model.WebCommand (RejectionReason (..), WebCommand (..), WebInvocationState (..))
import Swarm.Util.Lens (makeLensesNoSigs)
import Text.Fuzzy qualified as Fuzzy
data AppEvent
= Frame
| Web WebCommand
| (EventM Name AppState ())
| UpstreamVersion (Either (Severity, Text) String)
infoScroll :: ViewportScroll Name
infoScroll :: ViewportScroll Name
infoScroll = Name -> ViewportScroll Name
forall n. n -> ViewportScroll n
viewportScroll Name
InfoViewport
modalScroll :: ViewportScroll Name
modalScroll :: ViewportScroll Name
modalScroll = Name -> ViewportScroll Name
forall n. n -> ViewportScroll n
viewportScroll Name
ModalViewport
replScroll :: ViewportScroll Name
replScroll :: ViewportScroll Name
replScroll = Name -> ViewportScroll Name
forall n. n -> ViewportScroll n
viewportScroll Name
REPLViewport
logEvent :: LogSource -> Severity -> Text -> Text -> Notifications LogEntry -> Notifications LogEntry
logEvent :: LogSource
-> Severity
-> Text
-> Text
-> Notifications LogEntry
-> Notifications LogEntry
logEvent LogSource
src Severity
sev Text
who Text
msg Notifications LogEntry
el =
Notifications LogEntry
el
Notifications LogEntry
-> (Notifications LogEntry -> Notifications LogEntry)
-> Notifications LogEntry
forall a b. a -> (a -> b) -> b
& (Int -> Identity Int)
-> Notifications LogEntry -> Identity (Notifications LogEntry)
forall a (f :: * -> *).
Functor f =>
(Int -> f Int) -> Notifications a -> f (Notifications a)
notificationsCount ((Int -> Identity Int)
-> Notifications LogEntry -> Identity (Notifications LogEntry))
-> (Int -> Int) -> Notifications LogEntry -> Notifications LogEntry
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Int -> Int
forall a. Enum a => a -> a
succ
Notifications LogEntry
-> (Notifications LogEntry -> Notifications LogEntry)
-> Notifications LogEntry
forall a b. a -> (a -> b) -> b
& ([LogEntry] -> Identity [LogEntry])
-> Notifications LogEntry -> Identity (Notifications LogEntry)
forall a1 a2 (f :: * -> *).
Functor f =>
([a1] -> f [a2]) -> Notifications a1 -> f (Notifications a2)
notificationsContent (([LogEntry] -> Identity [LogEntry])
-> Notifications LogEntry -> Identity (Notifications LogEntry))
-> ([LogEntry] -> [LogEntry])
-> Notifications LogEntry
-> Notifications LogEntry
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (LogEntry
l LogEntry -> [LogEntry] -> [LogEntry]
forall a. a -> [a] -> [a]
:)
where
l :: LogEntry
l = TickNumber -> LogSource -> Severity -> Text -> Text -> LogEntry
LogEntry (Int64 -> TickNumber
TickNumber Int64
0) LogSource
src Severity
sev Text
who Text
msg
data ScenarioState = ScenarioState
{ ScenarioState -> GameState
_gameState :: GameState
, ScenarioState -> UIGameplay
_uiGameplay :: UIGameplay
}
data AnimationState
= AnimActive (Animation AppState Name)
| AnimScheduled
| AnimInactive
data ProgressionState = ProgressionState
{ ProgressionState -> ScenarioCollection ScenarioInfo
_scenarios :: ScenarioCollection ScenarioInfo
, ProgressionState -> Map CategorizedAchievement Attainment
_attainedAchievements :: Map CategorizedAchievement Attainment
, :: PopupState
, :: AnimationState
, ProgressionState -> [ScenarioWith ScenarioPath]
_scenarioSequence :: [ScenarioWith ScenarioPath]
}
data PlayState = PlayState
{ PlayState -> ScenarioState
_scenarioState :: ScenarioState
, PlayState -> ProgressionState
_progression :: ProgressionState
}
data AppState = AppState
{ AppState -> PlayState
_playState :: PlayState
, AppState -> UIState
_uiState :: UIState
, AppState -> KeyEventHandlingState
_keyEventHandling :: KeyEventHandlingState
, AppState -> RuntimeState
_runtimeState :: RuntimeState
, AppState -> AnimationManager AppState AppEvent Name
_animationMgr :: AnimationManager AppState AppEvent Name
}
type SwarmKeyDispatcher = KeyDispatcher SwarmEvent (EventM Name AppState)
data SwarmKeyDispatchers = SwarmKeyDispatchers
{ SwarmKeyDispatchers -> SwarmKeyDispatcher
mainGameDispatcher :: SwarmKeyDispatcher
, SwarmKeyDispatchers -> SwarmKeyDispatcher
replDispatcher :: SwarmKeyDispatcher
, SwarmKeyDispatchers -> SwarmKeyDispatcher
worldDispatcher :: SwarmKeyDispatcher
, SwarmKeyDispatchers -> SwarmKeyDispatcher
robotDispatcher :: SwarmKeyDispatcher
}
data KeyEventHandlingState = KeyEventHandlingState
{ KeyEventHandlingState -> KeyConfig SwarmEvent
_keyConfig :: KeyConfig SwarmEvent
, KeyEventHandlingState -> SwarmKeyDispatchers
_keyDispatchers :: SwarmKeyDispatchers
}
populateInventoryList :: (MonadState UIInventory m) => Maybe Robot -> m ()
populateInventoryList :: forall (m :: * -> *).
MonadState UIInventory m =>
Maybe Robot -> m ()
populateInventoryList Maybe Robot
Nothing = (Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Identity
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> UIInventory -> Identity UIInventory
Lens'
UIInventory
(Maybe (Int, GenericList Name Vector InventoryListEntry))
uiInventoryList ((Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Identity
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> UIInventory -> Identity UIInventory)
-> Maybe (Int, GenericList Name Vector InventoryListEntry) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= Maybe (Int, GenericList Name Vector InventoryListEntry)
forall a. Maybe a
Nothing
populateInventoryList (Just Robot
r) = do
Maybe (GenericList Name Vector InventoryListEntry)
mList <- Getting
(First (GenericList Name Vector InventoryListEntry))
UIInventory
(GenericList Name Vector InventoryListEntry)
-> m (Maybe (GenericList Name Vector InventoryListEntry))
forall s (m :: * -> *) a.
MonadState s m =>
Getting (First a) s a -> m (Maybe a)
preuse (Getting
(First (GenericList Name Vector InventoryListEntry))
UIInventory
(GenericList Name Vector InventoryListEntry)
-> m (Maybe (GenericList Name Vector InventoryListEntry)))
-> Getting
(First (GenericList Name Vector InventoryListEntry))
UIInventory
(GenericList Name Vector InventoryListEntry)
-> m (Maybe (GenericList Name Vector InventoryListEntry))
forall a b. (a -> b) -> a -> b
$ (Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> UIInventory
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIInventory
Lens'
UIInventory
(Maybe (Int, GenericList Name Vector InventoryListEntry))
uiInventoryList ((Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> UIInventory
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIInventory)
-> ((GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> Getting
(First (GenericList Name Vector InventoryListEntry))
UIInventory
(GenericList Name Vector InventoryListEntry)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Int, GenericList Name Vector InventoryListEntry))
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry))
forall a b (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p (Maybe a) (f (Maybe b))
_Just (((Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Int, GenericList Name Vector InventoryListEntry))
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> ((GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Int, GenericList Name Vector InventoryListEntry))
-> (GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Int, GenericList Name Vector InventoryListEntry)
forall s t a b. Field2 s t a b => Lens s t a b
Lens
(Int, GenericList Name Vector InventoryListEntry)
(Int, GenericList Name Vector InventoryListEntry)
(GenericList Name Vector InventoryListEntry)
(GenericList Name Vector InventoryListEntry)
_2
Bool
showZero <- Getting Bool UIInventory Bool -> m Bool
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting Bool UIInventory Bool
Lens' UIInventory Bool
uiShowZero
InventorySortOptions
sortOptions <- Getting InventorySortOptions UIInventory InventorySortOptions
-> m InventorySortOptions
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting InventorySortOptions UIInventory InventorySortOptions
Lens' UIInventory InventorySortOptions
uiInventorySort
Maybe Text
search <- Getting (Maybe Text) UIInventory (Maybe Text) -> m (Maybe Text)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting (Maybe Text) UIInventory (Maybe Text)
Lens' UIInventory (Maybe Text)
uiInventorySearch
let mkInvEntry :: (Int, Entity) -> InventoryListEntry
mkInvEntry (Int
n, Entity
e) = Int -> Entity -> InventoryListEntry
InventoryEntry Int
n Entity
e
mkInstEntry :: (a, Entity) -> InventoryListEntry
mkInstEntry (a
_, Entity
e) = Entity -> InventoryListEntry
EquippedEntry Entity
e
itemList :: Bool
-> ((Int, Entity) -> InventoryListEntry)
-> Text
-> Inventory
-> [InventoryListEntry]
itemList Bool
isInventoryDisplay (Int, Entity) -> InventoryListEntry
mk Text
label =
(\case [] -> []; [InventoryListEntry]
xs -> Text -> InventoryListEntry
Separator Text
label InventoryListEntry -> [InventoryListEntry] -> [InventoryListEntry]
forall a. a -> [a] -> [a]
: [InventoryListEntry]
xs)
([InventoryListEntry] -> [InventoryListEntry])
-> (Inventory -> [InventoryListEntry])
-> Inventory
-> [InventoryListEntry]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Entity) -> InventoryListEntry)
-> [(Int, Entity)] -> [InventoryListEntry]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Entity) -> InventoryListEntry
mk
([(Int, Entity)] -> [InventoryListEntry])
-> (Inventory -> [(Int, Entity)])
-> Inventory
-> [InventoryListEntry]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InventorySortOptions -> [(Int, Entity)] -> [(Int, Entity)]
forall a.
Ord a =>
InventorySortOptions -> [(a, Entity)] -> [(a, Entity)]
sortInventory InventorySortOptions
sortOptions
([(Int, Entity)] -> [(Int, Entity)])
-> (Inventory -> [(Int, Entity)]) -> Inventory -> [(Int, Entity)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Entity) -> Bool) -> [(Int, Entity)] -> [(Int, Entity)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool -> Bool
(&&) (Bool -> Bool -> Bool)
-> ((Int, Entity) -> Bool) -> (Int, Entity) -> Bool -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Entity) -> Bool
matchesSearch ((Int, Entity) -> Bool -> Bool)
-> ((Int, Entity) -> Bool) -> (Int, Entity) -> Bool
forall a b.
((Int, Entity) -> a -> b)
-> ((Int, Entity) -> a) -> (Int, Entity) -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Int, Entity) -> Bool
forall {a}. (Ord a, Num a) => (a, Entity) -> Bool
shouldDisplay)
([(Int, Entity)] -> [(Int, Entity)])
-> (Inventory -> [(Int, Entity)]) -> Inventory -> [(Int, Entity)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inventory -> [(Int, Entity)]
elems
where
shouldDisplay :: (a, Entity) -> Bool
shouldDisplay (a
n, Entity
e) =
a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0
Bool -> Bool -> Bool
|| Bool
isInventoryDisplay
Bool -> Bool -> Bool
&& Bool
showZero
Bool -> Bool -> Bool
&& Bool -> Bool
not ((Robot
r Robot -> Getting Inventory Robot Inventory -> Inventory
forall s a. s -> Getting a s a -> a
^. Getting Inventory Robot Inventory
Lens' Robot Inventory
equippedDevices) Inventory -> Entity -> Bool
`E.contains` Entity
e)
matchesSearch :: (Count, Entity) -> Bool
matchesSearch :: (Int, Entity) -> Bool
matchesSearch (Int
_, Entity
e) = (Text -> Bool)
-> (Text -> Text -> Bool) -> Maybe Text -> Text -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Bool -> Text -> Bool
forall a b. a -> b -> a
const Bool
True) Text -> Text -> Bool
forall s. TextualMonoid s => s -> s -> Bool
Fuzzy.test Maybe Text
search (Entity
e Entity -> Getting Text Entity Text -> Text
forall s a. s -> Getting a s a -> a
^. Getting Text Entity Text
Lens' Entity Text
E.entityName)
items :: [InventoryListEntry]
items =
(Robot
r Robot
-> Getting [InventoryListEntry] Robot [InventoryListEntry]
-> [InventoryListEntry]
forall s a. s -> Getting a s a -> a
^. (Inventory -> Const [InventoryListEntry] Inventory)
-> Robot -> Const [InventoryListEntry] Robot
Lens' Robot Inventory
robotInventory ((Inventory -> Const [InventoryListEntry] Inventory)
-> Robot -> Const [InventoryListEntry] Robot)
-> (([InventoryListEntry]
-> Const [InventoryListEntry] [InventoryListEntry])
-> Inventory -> Const [InventoryListEntry] Inventory)
-> Getting [InventoryListEntry] Robot [InventoryListEntry]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inventory -> [InventoryListEntry])
-> ([InventoryListEntry]
-> Const [InventoryListEntry] [InventoryListEntry])
-> Inventory
-> Const [InventoryListEntry] Inventory
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to (Bool
-> ((Int, Entity) -> InventoryListEntry)
-> Text
-> Inventory
-> [InventoryListEntry]
itemList Bool
True (Int, Entity) -> InventoryListEntry
mkInvEntry Text
"Compendium"))
[InventoryListEntry]
-> [InventoryListEntry] -> [InventoryListEntry]
forall a. [a] -> [a] -> [a]
++ (Robot
r Robot
-> Getting [InventoryListEntry] Robot [InventoryListEntry]
-> [InventoryListEntry]
forall s a. s -> Getting a s a -> a
^. (Inventory -> Const [InventoryListEntry] Inventory)
-> Robot -> Const [InventoryListEntry] Robot
Lens' Robot Inventory
equippedDevices ((Inventory -> Const [InventoryListEntry] Inventory)
-> Robot -> Const [InventoryListEntry] Robot)
-> (([InventoryListEntry]
-> Const [InventoryListEntry] [InventoryListEntry])
-> Inventory -> Const [InventoryListEntry] Inventory)
-> Getting [InventoryListEntry] Robot [InventoryListEntry]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inventory -> [InventoryListEntry])
-> ([InventoryListEntry]
-> Const [InventoryListEntry] [InventoryListEntry])
-> Inventory
-> Const [InventoryListEntry] Inventory
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to (Bool
-> ((Int, Entity) -> InventoryListEntry)
-> Text
-> Inventory
-> [InventoryListEntry]
itemList Bool
False (Int, Entity) -> InventoryListEntry
forall {a}. (a, Entity) -> InventoryListEntry
mkInstEntry Text
"Equipped devices"))
sel :: Maybe (Int, InventoryListEntry)
sel = Maybe (GenericList Name Vector InventoryListEntry)
mList Maybe (GenericList Name Vector InventoryListEntry)
-> (GenericList Name Vector InventoryListEntry
-> Maybe (Int, InventoryListEntry))
-> Maybe (Int, InventoryListEntry)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= GenericList Name Vector InventoryListEntry
-> Maybe (Int, InventoryListEntry)
forall (t :: * -> *) e n.
(Splittable t, Traversable t, Semigroup (t e)) =>
GenericList n t e -> Maybe (Int, e)
BL.listSelectedElement
idx :: Int
idx = case Maybe (Int, InventoryListEntry)
sel of
Maybe (Int, InventoryListEntry)
Nothing -> Int
1
Just (Int
selIdx, InventoryEntry Int
_ Entity
e) ->
Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
selIdx ((InventoryListEntry -> Bool) -> [InventoryListEntry] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex ((Maybe Entity -> Maybe Entity -> Bool
forall a. Eq a => a -> a -> Bool
== Entity -> Maybe Entity
forall a. a -> Maybe a
Just Entity
e) (Maybe Entity -> Bool)
-> (InventoryListEntry -> Maybe Entity)
-> InventoryListEntry
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting (First Entity) InventoryListEntry Entity
-> InventoryListEntry -> Maybe Entity
forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (((Int, Entity) -> Const (First Entity) (Int, Entity))
-> InventoryListEntry -> Const (First Entity) InventoryListEntry
Prism' InventoryListEntry (Int, Entity)
_InventoryEntry (((Int, Entity) -> Const (First Entity) (Int, Entity))
-> InventoryListEntry -> Const (First Entity) InventoryListEntry)
-> ((Entity -> Const (First Entity) Entity)
-> (Int, Entity) -> Const (First Entity) (Int, Entity))
-> Getting (First Entity) InventoryListEntry Entity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Entity -> Const (First Entity) Entity)
-> (Int, Entity) -> Const (First Entity) (Int, Entity)
forall s t a b. Field2 s t a b => Lens s t a b
Lens (Int, Entity) (Int, Entity) Entity Entity
_2)) [InventoryListEntry]
items)
Just (Int
selIdx, EquippedEntry Entity
e) ->
Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
selIdx ((InventoryListEntry -> Bool) -> [InventoryListEntry] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex ((Maybe Entity -> Maybe Entity -> Bool
forall a. Eq a => a -> a -> Bool
== Entity -> Maybe Entity
forall a. a -> Maybe a
Just Entity
e) (Maybe Entity -> Bool)
-> (InventoryListEntry -> Maybe Entity)
-> InventoryListEntry
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting (First Entity) InventoryListEntry Entity
-> InventoryListEntry -> Maybe Entity
forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview Getting (First Entity) InventoryListEntry Entity
Prism' InventoryListEntry Entity
_EquippedEntry) [InventoryListEntry]
items)
Just (Int
selIdx, InventoryListEntry
_) -> Int
selIdx
lst :: GenericList Name Vector InventoryListEntry
lst = Int
-> GenericList Name Vector InventoryListEntry
-> GenericList Name Vector InventoryListEntry
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
BL.listMoveTo Int
idx (GenericList Name Vector InventoryListEntry
-> GenericList Name Vector InventoryListEntry)
-> GenericList Name Vector InventoryListEntry
-> GenericList Name Vector InventoryListEntry
forall a b. (a -> b) -> a -> b
$ Name
-> Vector InventoryListEntry
-> Int
-> GenericList Name Vector InventoryListEntry
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
BL.list Name
InventoryList ([InventoryListEntry] -> Vector InventoryListEntry
forall a. [a] -> Vector a
V.fromList [InventoryListEntry]
items) Int
1
(Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Identity
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> UIInventory -> Identity UIInventory
Lens'
UIInventory
(Maybe (Int, GenericList Name Vector InventoryListEntry))
uiInventoryList ((Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Identity
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> UIInventory -> Identity UIInventory)
-> Maybe (Int, GenericList Name Vector InventoryListEntry) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= (Int, GenericList Name Vector InventoryListEntry)
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
forall a. a -> Maybe a
Just (Robot
r Robot -> Getting Int Robot Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int Robot Int
Getter Robot Int
inventoryHash, GenericList Name Vector InventoryListEntry
lst)
data AppOpts = AppOpts
{ AppOpts -> Maybe Int
userSeed :: Maybe Seed
, AppOpts -> Maybe FilePath
userScenario :: Maybe FilePath
, AppOpts -> Maybe FilePath
scriptToRun :: Maybe FilePath
, AppOpts -> Bool
pausedAtStart :: Bool
, AppOpts -> Bool
autoPlay :: Bool
, AppOpts -> Bool
autoShowObjectives :: Bool
, AppOpts -> Int
speed :: Int
, AppOpts -> Set DebugOption
debugOptions :: Set DebugOption
, AppOpts -> Maybe ColorMode
colorMode :: Maybe ColorMode
, AppOpts -> Maybe Int
userWebPort :: Maybe Port
, AppOpts -> Maybe GitInfo
repoGitInfo :: Maybe GitInfo
}
defaultAppOpts :: AppOpts
defaultAppOpts :: AppOpts
defaultAppOpts =
AppOpts
{ userSeed :: Maybe Int
userSeed = Maybe Int
forall a. Maybe a
Nothing
, userScenario :: Maybe FilePath
userScenario = Maybe FilePath
forall a. Maybe a
Nothing
, scriptToRun :: Maybe FilePath
scriptToRun = Maybe FilePath
forall a. Maybe a
Nothing
, pausedAtStart :: Bool
pausedAtStart = Bool
False
, autoShowObjectives :: Bool
autoShowObjectives = Bool
True
, autoPlay :: Bool
autoPlay = Bool
False
, speed :: Int
speed = Int
defaultInitLgTicksPerSecond
, debugOptions :: Set DebugOption
debugOptions = Set DebugOption
forall a. Monoid a => a
mempty
, colorMode :: Maybe ColorMode
colorMode = Maybe ColorMode
forall a. Maybe a
Nothing
, userWebPort :: Maybe Int
userWebPort = Maybe Int
forall a. Maybe a
Nothing
, repoGitInfo :: Maybe GitInfo
repoGitInfo = Maybe GitInfo
forall a. Maybe a
Nothing
}
makeLensesNoSigs ''ScenarioState
gameState :: Lens' ScenarioState GameState
uiGameplay :: Lens' ScenarioState UIGameplay
makeLensesNoSigs ''PlayState
scenarioState :: Lens' PlayState ScenarioState
progression :: Lens' PlayState ProgressionState
attainedAchievements :: Lens' ProgressionState (Map CategorizedAchievement Attainment)
scenarios :: Lens' ProgressionState (ScenarioCollection ScenarioInfo)
uiPopups :: Lens' ProgressionState PopupState
uiPopupAnimationState :: Lens' ProgressionState AnimationState
scenarioSequence :: Lens' ProgressionState [ScenarioWith ScenarioPath]
makeLensesNoSigs ''KeyEventHandlingState
keyConfig :: Lens' KeyEventHandlingState (KeyConfig SwarmEvent)
keyDispatchers :: Lens' KeyEventHandlingState SwarmKeyDispatchers
makeLensesNoSigs ''AppState
playState :: Lens' AppState PlayState
uiState :: Lens' AppState UIState
keyEventHandling :: Lens' AppState KeyEventHandlingState
runtimeState :: Lens' AppState RuntimeState
animationMgr :: Lens' AppState (AnimationManager AppState AppEvent Name)
makePrisms ''AnimationState
focusedItem :: ScenarioState -> Maybe InventoryListEntry
focusedItem :: ScenarioState -> Maybe InventoryListEntry
focusedItem ScenarioState
s = do
GenericList Name Vector InventoryListEntry
list <- ScenarioState
s ScenarioState
-> Getting
(First (GenericList Name Vector InventoryListEntry))
ScenarioState
(GenericList Name Vector InventoryListEntry)
-> Maybe (GenericList Name Vector InventoryListEntry)
forall s a. s -> Getting (First a) s a -> Maybe a
^? (UIGameplay
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIGameplay)
-> ScenarioState
-> Const
(First (GenericList Name Vector InventoryListEntry)) ScenarioState
Lens' ScenarioState UIGameplay
uiGameplay ((UIGameplay
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIGameplay)
-> ScenarioState
-> Const
(First (GenericList Name Vector InventoryListEntry)) ScenarioState)
-> ((GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> UIGameplay
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIGameplay)
-> Getting
(First (GenericList Name Vector InventoryListEntry))
ScenarioState
(GenericList Name Vector InventoryListEntry)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UIInventory
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIInventory)
-> UIGameplay
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIGameplay
Lens' UIGameplay UIInventory
uiInventory ((UIInventory
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIInventory)
-> UIGameplay
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIGameplay)
-> Getting
(First (GenericList Name Vector InventoryListEntry))
UIInventory
(GenericList Name Vector InventoryListEntry)
-> (GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> UIGameplay
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIGameplay
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> UIInventory
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIInventory
Lens'
UIInventory
(Maybe (Int, GenericList Name Vector InventoryListEntry))
uiInventoryList ((Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> UIInventory
-> Const
(First (GenericList Name Vector InventoryListEntry)) UIInventory)
-> ((GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> Getting
(First (GenericList Name Vector InventoryListEntry))
UIInventory
(GenericList Name Vector InventoryListEntry)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Int, GenericList Name Vector InventoryListEntry))
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry))
forall a b (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p (Maybe a) (f (Maybe b))
_Just (((Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Int, GenericList Name Vector InventoryListEntry))
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry)))
-> ((GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Int, GenericList Name Vector InventoryListEntry))
-> (GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> Maybe (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Maybe (Int, GenericList Name Vector InventoryListEntry))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector InventoryListEntry
-> Const
(First (GenericList Name Vector InventoryListEntry))
(GenericList Name Vector InventoryListEntry))
-> (Int, GenericList Name Vector InventoryListEntry)
-> Const
(First (GenericList Name Vector InventoryListEntry))
(Int, GenericList Name Vector InventoryListEntry)
forall s t a b. Field2 s t a b => Lens s t a b
Lens
(Int, GenericList Name Vector InventoryListEntry)
(Int, GenericList Name Vector InventoryListEntry)
(GenericList Name Vector InventoryListEntry)
(GenericList Name Vector InventoryListEntry)
_2
(Int
_, InventoryListEntry
entry) <- GenericList Name Vector InventoryListEntry
-> Maybe (Int, InventoryListEntry)
forall (t :: * -> *) e n.
(Splittable t, Traversable t, Semigroup (t e)) =>
GenericList n t e -> Maybe (Int, e)
BL.listSelectedElement GenericList Name Vector InventoryListEntry
list
InventoryListEntry -> Maybe InventoryListEntry
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return InventoryListEntry
entry
focusedEntity :: ScenarioState -> Maybe Entity
focusedEntity :: ScenarioState -> Maybe Entity
focusedEntity =
ScenarioState -> Maybe InventoryListEntry
focusedItem (ScenarioState -> Maybe InventoryListEntry)
-> (InventoryListEntry -> Maybe Entity)
-> ScenarioState
-> Maybe Entity
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \case
Separator Text
_ -> Maybe Entity
forall a. Maybe a
Nothing
InventoryEntry Int
_ Entity
e -> Entity -> Maybe Entity
forall a. a -> Maybe a
Just Entity
e
EquippedEntry Entity
e -> Entity -> Maybe Entity
forall a. a -> Maybe a
Just Entity
e
animTraversal :: Traversal' AnimationState (Maybe (Animation AppState Name))
animTraversal :: Traversal' AnimationState (Maybe (Animation AppState Name))
animTraversal = ((Maybe (Animation AppState Name)
-> f (Maybe (Animation AppState Name)))
-> AnimationState -> f AnimationState)
-> (Maybe (Animation AppState Name)
-> f (Maybe (Animation AppState Name)))
-> AnimationState
-> f AnimationState
forall a (f :: * -> *) b s t.
((a -> f b) -> s -> f t) -> (a -> f b) -> s -> f t
traversal (Maybe (Animation AppState Name)
-> f (Maybe (Animation AppState Name)))
-> AnimationState -> f AnimationState
Traversal' AnimationState (Maybe (Animation AppState Name))
go
where
go :: Applicative f => (Maybe (Animation AppState Name) -> f (Maybe (Animation AppState Name))) -> AnimationState -> f AnimationState
go :: Traversal' AnimationState (Maybe (Animation AppState Name))
go Maybe (Animation AppState Name)
-> f (Maybe (Animation AppState Name))
focus = \case
AnimationState
AnimInactive -> AnimationState
-> (Animation AppState Name -> AnimationState)
-> Maybe (Animation AppState Name)
-> AnimationState
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AnimationState
AnimInactive Animation AppState Name -> AnimationState
AnimActive (Maybe (Animation AppState Name) -> AnimationState)
-> f (Maybe (Animation AppState Name)) -> f AnimationState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Animation AppState Name)
-> f (Maybe (Animation AppState Name))
focus Maybe (Animation AppState Name)
forall a. Maybe a
Nothing
AnimationState
AnimScheduled -> AnimationState
-> (Animation AppState Name -> AnimationState)
-> Maybe (Animation AppState Name)
-> AnimationState
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AnimationState
AnimInactive Animation AppState Name -> AnimationState
AnimActive (Maybe (Animation AppState Name) -> AnimationState)
-> f (Maybe (Animation AppState Name)) -> f AnimationState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Animation AppState Name)
-> f (Maybe (Animation AppState Name))
focus Maybe (Animation AppState Name)
forall a. Maybe a
Nothing
AnimActive Animation AppState Name
x -> AnimationState
-> (Animation AppState Name -> AnimationState)
-> Maybe (Animation AppState Name)
-> AnimationState
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AnimationState
AnimInactive Animation AppState Name -> AnimationState
AnimActive (Maybe (Animation AppState Name) -> AnimationState)
-> f (Maybe (Animation AppState Name)) -> f AnimationState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Animation AppState Name)
-> f (Maybe (Animation AppState Name))
focus (Animation AppState Name -> Maybe (Animation AppState Name)
forall a. a -> Maybe a
Just Animation AppState Name
x)