Skip to content

Commit

Permalink
address some review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed May 6, 2023
1 parent 31da4d3 commit b255f1b
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 40 deletions.
24 changes: 10 additions & 14 deletions src/Swarm/Game/Scenario/Cell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ data PCell e = Cell
-- and optionally an entity and robot.
type Cell = PCell Entity

mkPCellJson :: ToJSON b => (a -> b) -> PCell a -> Value
mkPCellJson modifier x = toJSON $
catMaybes
[ Just . toJSON . getTerrainWord $ cellTerrain x
, toJSON . modifier <$> cellEntity x
, listToMaybe []
]

instance ToJSON Cell where
toJSON x =
toJSON $
catMaybes
[ Just $ toJSON $ getTerrainWord $ cellTerrain x
, toJSON . (^. entityName) <$> cellEntity x
, listToMaybe []
]
toJSON = mkPCellJson $ view entityName

-- | Parse a tuple such as @[grass, rock, base]@ into a 'Cell'. The
-- entity and robot, if present, are immediately looked up and
Expand Down Expand Up @@ -83,10 +85,4 @@ type CellPaintDisplay = PCell EntityFacade

-- Note: This instance is used only for the purpose of WorldPalette
instance ToJSON CellPaintDisplay where
toJSON x =
toJSON $
catMaybes
[ Just $ toJSON $ getTerrainWord $ cellTerrain x
, toJSON <$> cellEntity x
, listToMaybe []
]
toJSON = mkPCellJson id
13 changes: 11 additions & 2 deletions src/Swarm/Game/Scenario/EntityFacade.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{-# LANGUAGE DerivingVia #-}

-- | Stand-in type for an "Entity" for purposes
-- that do not require carrying around the entire state
-- of an Entity.
--
-- Useful for simplified serialization, debugging,
-- and equality checking, particularly for the World Editor.
module Swarm.Game.Scenario.EntityFacade where

import Control.Lens hiding (from, (.=), (<.>))
Expand All @@ -10,15 +16,18 @@ import Swarm.Game.Entity qualified as E

type EntityName = Text

-- | This datatype is a lightweight stand-in for the
-- full-fledged "Entity" type without the baggage of all
-- of its other fields.
data EntityFacade = EntityFacade EntityName Display
deriving (Eq)

-- Note: This instance is used only for the purpose of WorldPalette
instance ToJSON EntityFacade where
toJSON (EntityFacade eName _display) = toJSON eName

mkPaint :: E.Entity -> EntityFacade
mkPaint e =
mkFacade :: E.Entity -> EntityFacade
mkFacade e =
EntityFacade
(e ^. E.entityName)
(e ^. E.entityDisplay)
2 changes: 1 addition & 1 deletion src/Swarm/Game/Scenario/WorldDescription.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ cellToTerrainEntityNamePair (Cell terrain maybeEntity _) = (terrain, maybeEntity

toCellPaintDisplay :: Cell -> CellPaintDisplay
toCellPaintDisplay (Cell terrain maybeEntity r) =
Cell terrain (mkPaint <$> maybeEntity) r
Cell terrain (mkFacade <$> maybeEntity) r

type WorldDescriptionPaint = PWorldDescription EntityFacade

Expand Down
3 changes: 1 addition & 2 deletions src/Swarm/Game/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ import Swarm.Language.Syntax (Const, SrcLoc (..), Syntax' (..), allConst)
import Swarm.Language.Typed (Typed (Typed))
import Swarm.Language.Types
import Swarm.Language.Value (Value)
import Swarm.TUI.Editor.Model (BoundsRectangle)
import Swarm.Util (uniq, (<+=), (<<.=), (?))
import System.Clock qualified as Clock
import System.Random (StdGen, mkStdGen, randomRIO)
Expand Down Expand Up @@ -737,7 +736,7 @@ unfocus = (\g -> g {_focusedRobotID = -1000}) . modifyViewCenter id

-- | Given a width and height, compute the region, centered on the
-- 'viewCenter', that should currently be in view.
viewingRegion :: GameState -> (Int32, Int32) -> BoundsRectangle
viewingRegion :: GameState -> (Int32, Int32) -> W.BoundsRectangle
viewingRegion g (w, h) = (W.Coords (rmin, cmin), W.Coords (rmax, cmax))
where
Location cx cy = g ^. viewCenter
Expand Down
8 changes: 7 additions & 1 deletion src/Swarm/Game/World.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Swarm.Game.World (
Coords (..),
locToCoords,
coordsToLoc,

BoundsRectangle,

-- * Worlds
WorldFun (..),
worldFunFromArray,
Expand Down Expand Up @@ -87,6 +88,11 @@ locToCoords (Location x y) = Coords (-y, x)
coordsToLoc :: Coords -> Location
coordsToLoc (Coords (r, c)) = Location c (-r)


-- | Represents the top-left and bottom-right coordinates
-- of a bounding rectangle of cells in the world map
type BoundsRectangle = (Coords, Coords)

------------------------------------------------------------
-- World function
------------------------------------------------------------
Expand Down
12 changes: 4 additions & 8 deletions src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,10 @@ handleModalEvent = \case
Brick.zoom (uiState . uiModal . _Just . modalDialog) (handleDialogEvent ev)
modal <- preuse $ uiState . uiModal . _Just . modalType
case modal of
Just TerrainPaletteModal -> do
lw <- use $ uiState . uiWorldEditor . terrainList
newList <- refreshList lw
uiState . uiWorldEditor . terrainList .= newList
Just TerrainPaletteModal ->
refreshList $ uiState . uiWorldEditor . terrainList
Just EntityPaletteModal -> do
lw <- use $ uiState . uiWorldEditor . entityPaintList
newList <- refreshList lw
uiState . uiWorldEditor . entityPaintList .= newList
refreshList $ uiState . uiWorldEditor . entityPaintList
Just GoalModal -> case ev of
V.EvKey (V.KChar '\t') [] -> uiState . uiGoal . focus %= focusNext
_ -> do
Expand All @@ -465,7 +461,7 @@ handleModalEvent = \case
_ -> handleInfoPanelEvent modalScroll (VtyEvent ev)
where
refreshGoalList lw = nestEventM' lw $ handleListEventWithSeparators ev shouldSkipSelection
refreshList w = nestEventM' w $ BL.handleListEvent ev
refreshList z = Brick.zoom z $ BL.handleListEvent ev

getNormalizedCurrentScenarioPath :: (MonadIO m, MonadState AppState m) => m (Maybe FilePath)
getNormalizedCurrentScenarioPath =
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/TUI/Editor/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ handleMiddleClick mouseLoc = do
where
p = case elementPaint of
Facade efd -> efd
Ref r -> mkPaint r
Ref r -> mkFacade r

mouseCoordsM <- Brick.zoom gameState $ mouseLocToWorldCoords mouseLoc
whenJust mouseCoordsM setTerrainPaint
Expand Down
8 changes: 2 additions & 6 deletions src/Swarm/TUI/Editor/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import Swarm.TUI.Model.Name
import Swarm.Util
import System.Clock

-- | Represents the top-left and bottom-right coordinates
-- of a bounding rectangle of cells in the world map
type BoundsRectangle = (W.Coords, W.Coords)

data BoundsSelectionStep
= UpperLeftPending
| -- | Stores the *world coords* of the upper-left click
Expand All @@ -40,13 +36,13 @@ getDisplay (Ref e) = e ^. E.entityDisplay
toFacade :: EntityPaint -> EntityFacade
toFacade = \case
Facade f -> f
Ref e -> mkPaint e
Ref e -> mkFacade e

getEntityName :: EntityFacade -> EntityName
getEntityName (EntityFacade name _) = name

data MapEditingBounds = MapEditingBounds
{ _boundsRect :: Maybe BoundsRectangle
{ _boundsRect :: Maybe W.BoundsRectangle
-- ^ Upper-left and lower-right coordinates
-- of the map to be saved.
, _boundsPersistDisplayUntil :: TimeSpec
Expand Down
8 changes: 4 additions & 4 deletions src/Swarm/TUI/Editor/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import Swarm.TUI.Model

getEntitiesForList :: EntityMap -> V.Vector EntityFacade
getEntitiesForList em =
V.fromList $ map mkPaint entities
V.fromList $ map mkFacade entities
where
entities = M.elems $ entitiesByName em

getEditingBounds :: WorldDescription -> (Bool, BoundsRectangle)
getEditingBounds :: WorldDescription -> (Bool, W.BoundsRectangle)
getEditingBounds myWorld =
(EA.isEmpty a, newBounds)
where
Expand Down Expand Up @@ -86,7 +86,7 @@ isOutsideBottomRightCorner (W.Coords (yBottom, xRight)) (W.Coords (y, x)) =

isOutsideRegion ::
-- | full bounds
BoundsRectangle ->
W.BoundsRectangle ->
-- | current coords
W.Coords ->
Bool
Expand All @@ -95,7 +95,7 @@ isOutsideRegion (tl, br) coord =

getEditedMapRectangle ::
WorldEditor Name ->
Maybe BoundsRectangle ->
Maybe W.BoundsRectangle ->
W.World Int Entity ->
[[CellPaintDisplay]]
getEditedMapRectangle _ Nothing _ = []
Expand Down
5 changes: 4 additions & 1 deletion src/Swarm/TUI/View/CellDisplay.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# LANGUAGE OverloadedStrings #-}

-- |
-- Rendering of cells in the map view
--
-- SPDX-License-Identifier: BSD-3-Clause
module Swarm.TUI.View.CellDisplay where

Expand Down Expand Up @@ -87,7 +89,8 @@ hidingMode g
-- taking into account "static" based on the distance to the robot
-- being @view@ed.
displayLoc :: Bool -> WorldEditor Name -> GameState -> W.Coords -> Display
displayLoc showRobots we g coords = staticDisplay g coords <> displayLocRaw showRobots we g coords
displayLoc showRobots we g coords = staticDisplay g coords
<> displayLocRaw showRobots we g coords

-- | Get the 'Display' for a specific location, by combining the
-- 'Display's for the terrain, entity, and robots at the location.
Expand Down

0 comments on commit b255f1b

Please sign in to comment.