Modulebrick-2.10Haskell2010
Brick.Types
Basic types used by this library.
- 23 types
- 1 class
- 54 values
- Packagebrick-2.10
- Exports81
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceTypes.hs
The Widget type
1 declarationLocation types and lenses
6 declarationsInstances12Eq, Ord, Read, Show, Generic, Semigroup, …
Eq LocationDefined in brick-2.10 · Brick.Types.CommonOrd LocationDefined in brick-2.10 · Brick.Types.CommonRead LocationDefined in brick-2.10 · Brick.Types.CommonShow LocationDefined in brick-2.10 · Brick.Types.CommonGeneric LocationDefined in brick-2.10 · Brick.Types.CommonSemigroup LocationDefined in brick-2.10 · Brick.Types.CommonMonoid LocationDefined in brick-2.10 · Brick.Types.CommonNFData LocationDefined in brick-2.10 · Brick.Types.CommonTerminalLocation LocationDefined in brick-2.10 · Brick.Types.InternalField1 Location Location Int IntDefined in brick-2.10 · Brick.Types.CommonField2 Location Location Int IntDefined in brick-2.10 · Brick.Types.Commontype Rep Location = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Common"Location"
"Brick.Types.Common"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"Location"
'PrefixI 'True) (S1 ('MetaSel ('Just"loc"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Int, Int))))
The class of types that behave like terminal locations.
Methods
locationColumnL :: Lens' a IntGet the column out of the value
locationColumn :: a -> IntlocationRowL :: Lens' a IntGet the row out of the value
locationRow :: a -> Int
Instances2TerminalLocation
TerminalLocation LocationDefined in brick-2.10 · Brick.Types.InternalTerminalLocation (CursorLocation n)Defined in brick-2.10 · Brick.Types · orphan
A cursor location. These are returned by the rendering process.
Constructors
CursorLocationcursorLocation :: !LocationThe location
cursorLocationName :: !Maybe nThe name of the widget associated with the location
cursorLocationVisible :: !BoolWhether the cursor should actually be visible
Instances6Read, Show, Generic, NFData, TerminalLocation, Rep
Read n => Read (CursorLocation n)Defined in brick-2.10 · Brick.Types.InternalShow n => Show (CursorLocation n)Defined in brick-2.10 · Brick.Types.InternalGeneric (CursorLocation n)Defined in brick-2.10 · Brick.Types.InternalNFData n => NFData (CursorLocation n)Defined in brick-2.10 · Brick.Types.InternalTerminalLocation (CursorLocation n)Defined in brick-2.10 · Brick.Types · orphantype Rep (CursorLocation n) = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"CursorLocation"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"CursorLocation"
'PrefixI 'True) (S1 ('MetaSel ('Just"cursorLocation"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 Location) :*: (S1 ('MetaSel ('Just"cursorLocationName"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe n)) :*: S1 ('MetaSel ('Just"cursorLocationVisible"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool))))
Viewports
11 declarationsDescribes the state of a viewport as it appears as its most recent rendering.
Constructors
VP_vpLeft :: IntThe column offset of left side of the viewport.
_vpTop :: IntThe row offset of the top of the viewport.
_vpSize :: DisplayRegionThe size of the viewport.
_vpContentSize :: DisplayRegionThe size of the contents of the viewport.
Instances5Read, Show, Generic, NFData, Rep
Read ViewportDefined in brick-2.10 · Brick.Types.InternalShow ViewportDefined in brick-2.10 · Brick.Types.InternalGeneric ViewportDefined in brick-2.10 · Brick.Types.InternalNFData ViewportDefined in brick-2.10 · Brick.Types.Internaltype Rep Viewport = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"Viewport"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"VP"
'PrefixI 'True) ((S1 ('MetaSel ('Just"_vpLeft"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int) :*: S1 ('MetaSel ('Just"_vpTop"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)) :*: (S1 ('MetaSel ('Just"_vpSize"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 DisplayRegion) :*: S1 ('MetaSel ('Just"_vpContentSize"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 DisplayRegion))))
The type of viewports that indicates the direction(s) in which a viewport is scrollable.
Constructors
VerticalViewports of this type are scrollable only vertically.
HorizontalViewports of this type are scrollable only horizontally.
BothViewports of this type are scrollable vertically and horizontally.
Instances2Eq, Show
Eq ViewportTypeDefined in brick-2.10 · Brick.Types.InternalShow ViewportTypeDefined in brick-2.10 · Brick.Types.Internal
Instances2Eq, Show
Eq VScrollBarOrientationDefined in brick-2.10 · Brick.Types.InternalShow VScrollBarOrientationDefined in brick-2.10 · Brick.Types.Internal
Instances2Eq, Show
Eq HScrollBarOrientationDefined in brick-2.10 · Brick.Types.InternalShow HScrollBarOrientationDefined in brick-2.10 · Brick.Types.Internal
A vertical scroll bar renderer.
Constructors
VScrollbarRendererrenderVScrollbar :: Widget nHow to render the body of the scroll bar. This should provide a widget that expands in whatever direction(s) this renderer will be used for. So, for example, this widget would need to be one that expands vertically such as
fill. The same goes for the trough widget.renderVScrollbarTrough :: Widget nHow to render the "trough" of the scroll bar (the area to either side of the scroll bar body). This should expand as described in the documentation for the scroll bar field.
renderVScrollbarHandleBefore :: Widget nHow to render the handle that appears at the top or left of the scrollbar. The result will be allowed to be at most one row high.
renderVScrollbarHandleAfter :: Widget nHow to render the handle that appears at the bottom or right of the scrollbar. The result will be allowed to be at most one row high.
scrollbarWidthAllocation :: IntThe number of columns that will be allocated to the scroll bar. This determines how much space the widgets of the scroll bar elements can take up. If they use less than this amount, padding will be applied between the scroll bar and the viewport contents.
A horizontal scroll bar renderer.
Constructors
HScrollbarRendererrenderHScrollbar :: Widget nHow to render the body of the scroll bar. This should provide a widget that expands in whatever direction(s) this renderer will be used for. So, for example, this widget would need to be one that expands horizontally such as
fill. The same goes for the trough widget.renderHScrollbarTrough :: Widget nHow to render the "trough" of the scroll bar (the area to either side of the scroll bar body). This should expand as described in the documentation for the scroll bar field.
renderHScrollbarHandleBefore :: Widget nHow to render the handle that appears at the top or left of the scrollbar. The result will be allowed to be at most one column wide.
renderHScrollbarHandleAfter :: Widget nHow to render the handle that appears at the bottom or right of the scrollbar. The result will be allowed to be at most one column wide.
scrollbarHeightAllocation :: IntThe number of rows that will be allocated to the scroll bar. This determines how much space the widgets of the scroll bar elements can take up. If they use less than this amount, padding will be applied between the scroll bar and the viewport contents.
Clickable elements of a scroll bar.
Constructors
SBHandleBeforeThe handle at the beginning (left/top) of the scroll bar.
SBHandleAfterThe handle at the end (right/bottom) of the scroll bar.
SBBarThe scroll bar itself.
SBTroughBeforeThe trough before the scroll bar.
SBTroughAfterThe trough after the scroll bar.
Instances3Eq, Ord, Show
Eq ClickableScrollbarElementDefined in brick-2.10 · Brick.Types.InternalOrd ClickableScrollbarElementDefined in brick-2.10 · Brick.Types.InternalShow ClickableScrollbarElementDefined in brick-2.10 · Brick.Types.Internal
Event-handling types and functions
4 declarationsThe monad in which event handlers run.
Instances10MonadState, Monad, Functor, Applicative, MonadIO, MonadCatch, …
MonadState s (EventM n s)Defined in brick-2.10 · Brick.Types.EventMMonad (EventM n s)Defined in brick-2.10 · Brick.Types.EventMFunctor (EventM n s)Defined in brick-2.10 · Brick.Types.EventMApplicative (EventM n s)Defined in brick-2.10 · Brick.Types.EventMMonadIO (EventM n s)Defined in brick-2.10 · Brick.Types.EventMMonadCatch (EventM n s)Defined in brick-2.10 · Brick.Types.EventMMonadMask (EventM n s)Defined in brick-2.10 · Brick.Types.EventMMonadThrow (EventM n s)Defined in brick-2.10 · Brick.Types.EventMZoom (EventM n s) (EventM n t) s tDefined in brick-2.10 · Brick.Types.EventMtype Zoomed (EventM n s) = Zoomed (StateT s (StateT (EventState n) IO))Defined in brick-2.10 · Brick.Types.EventM
The type of events.
Constructors
VtyEvent EventThe event was a Vty event.
AppEvent eThe event was an application event.
MouseDown n Button [Modifier] LocationA mouse-down event on the specified region was received. The
nvalue is the resource name of the clicked widget (seeclickable).MouseUp n (Maybe Button) LocationA mouse-up event on the specified region was received. The
nvalue is the resource name of the clicked widget (seeclickable).
Instances3Eq, Ord, Show
(Eq e, Eq n) => Eq (BrickEvent n e)Defined in brick-2.10 · Brick.Types.Internal(Ord e, Ord n) => Ord (BrickEvent n e)Defined in brick-2.10 · Brick.Types.Internal(Show e, Show n) => Show (BrickEvent n e)Defined in brick-2.10 · Brick.Types.Internal
nestEventM Given a state value and an EventM that mutates that state, run the specified action and return both the resulting modified state and the result of the action itself.
nestEventM' Given a state value and an EventM that mutates that state, run the specified action and return resulting modified state.
Rendering infrastructure
2 declarationsThe type of the rendering monad. This monad is used by the library's rendering routines to manage rendering state and communicate rendering parameters to widgets' rendering functions.
Get the current rendering context.
The rendering context
The rendering context. This tells widgets how to render: how much space they have in which to render, which attribute they should use to render, which bordering style should be used, and the attribute map available for rendering.
The rendering context's current drawing attribute.
Rendering results
The type of result returned by a widget's rendering function. The result provides the image, cursor positions, and visibility requests that resulted from the rendering process.
Constructors
Resultimage :: !ImageThe final rendered image for a widget
cursors :: ![CursorLocation n]The list of reported cursor positions for the application to choose from
visibilityRequests :: ![VisibilityRequest]The list of visibility requests made by widgets rendered while rendering this one (used by viewports)
extents :: ![Extent n]borders :: !BorderMap DynBorderPlaces where we may rewrite the edge of the image when placing this widget next to another one.
Instances5Read, Show, Generic, NFData, Rep
Read n => Read (Result n)Defined in brick-2.10 · Brick.Types.InternalShow n => Show (Result n)Defined in brick-2.10 · Brick.Types.InternalGeneric (Result n)Defined in brick-2.10 · Brick.Types.InternalNFData n => NFData (Result n)Defined in brick-2.10 · Brick.Types.Internaltype Rep (Result n) = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"Result"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"Result"
'PrefixI 'True) ((S1 ('MetaSel ('Just"image"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Image) :*: S1 ('MetaSel ('Just"cursors"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [CursorLocation n])) :*: (S1 ('MetaSel ('Just"visibilityRequests"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [VisibilityRequest]) :*: (S1 ('MetaSel ('Just"extents"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [Extent n]) :*: S1 ('MetaSel ('Just"borders"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (BorderMap DynBorder))))))
Given an attribute name, obtain the attribute for the attribute name by consulting the context's attribute map.
An extent of a named area: its size, location, and origin.
Constructors
ExtentextentName :: !nextentUpperLeft :: !LocationextentSize :: !(Int, Int)
Instances5Read, Show, Generic, NFData, Rep
Read n => Read (Extent n)Defined in brick-2.10 · Brick.Types.InternalShow n => Show (Extent n)Defined in brick-2.10 · Brick.Types.InternalGeneric (Extent n)Defined in brick-2.10 · Brick.Types.InternalNFData n => NFData (Extent n)Defined in brick-2.10 · Brick.Types.Internaltype Rep (Extent n) = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"Extent"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"Extent"
'PrefixI 'True) (S1 ('MetaSel ('Just"extentName"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 n) :*: (S1 ('MetaSel ('Just"extentUpperLeft"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 Location) :*: S1 ('MetaSel ('Just"extentSize"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Int, Int)))))
Rendering result lenses
Visibility requests
Constructors
Instances6Eq, Read, Show, Generic, NFData, Rep
Eq VisibilityRequestDefined in brick-2.10 · Brick.Types.InternalRead VisibilityRequestDefined in brick-2.10 · Brick.Types.InternalShow VisibilityRequestDefined in brick-2.10 · Brick.Types.InternalGeneric VisibilityRequestDefined in brick-2.10 · Brick.Types.InternalNFData VisibilityRequestDefined in brick-2.10 · Brick.Types.Internaltype Rep VisibilityRequest = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"VisibilityRequest"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"VR"
'PrefixI 'True) (S1 ('MetaSel ('Just"vrPosition"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Location) :*: S1 ('MetaSel ('Just"vrSize"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 DisplayRegion)))
Making lenses
2 declarationsA template haskell function to build lenses for a record type. This
function differs from the makeLenses function in that
it does not require the record fields to be prefixed with underscores
and it adds an L suffix to lens names to make it clear that they
are lenses.
A more general version of suffixLenses that allows customization of the lens-building rules and allows customization of the suffix.
Dynamic borders
14 declarationsInformation about how to redraw a dynamic border character when it abuts another dynamic border character.
Constructors
DynBorderdbStyle :: BorderStyleThe Chars to use when redrawing the border. Also used to filter connections: only dynamic borders with equal BorderStyles will connect to each other.
dbAttr :: AttrdbSegments :: Edges BorderSegment
Instances6Eq, Read, Show, Generic, NFData, Rep
Eq DynBorderDefined in brick-2.10 · Brick.Types.InternalRead DynBorderDefined in brick-2.10 · Brick.Types.InternalShow DynBorderDefined in brick-2.10 · Brick.Types.InternalGeneric DynBorderDefined in brick-2.10 · Brick.Types.InternalNFData DynBorderDefined in brick-2.10 · Brick.Types.Internaltype Rep DynBorder = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"DynBorder"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"DynBorder"
'PrefixI 'True) (S1 ('MetaSel ('Just"dbStyle"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 BorderStyle) :*: (S1 ('MetaSel ('Just"dbAttr"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Attr) :*: S1 ('MetaSel ('Just"dbSegments"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Edges BorderSegment)))))
A border character has four segments, one extending in each direction (horizontally and vertically) from the center of the character.
Instances7Eq, Ord, Read, Show, Generic, NFData, …
Eq BorderSegmentDefined in brick-2.10 · Brick.Types.InternalOrd BorderSegmentDefined in brick-2.10 · Brick.Types.InternalRead BorderSegmentDefined in brick-2.10 · Brick.Types.InternalShow BorderSegmentDefined in brick-2.10 · Brick.Types.InternalGeneric BorderSegmentDefined in brick-2.10 · Brick.Types.InternalNFData BorderSegmentDefined in brick-2.10 · Brick.Types.Internaltype Rep BorderSegment = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"BorderSegment"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"BorderSegment"
'PrefixI 'True) (S1 ('MetaSel ('Just"bsAccept"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool) :*: (S1 ('MetaSel ('Just"bsOffer"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool) :*: S1 ('MetaSel ('Just"bsDraw"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool))))
Instances10Monad, Functor, Applicative, Eq, Ord, Read, …
Monad EdgesDefined in brick-2.10 · Brick.Types.CommonFunctor EdgesDefined in brick-2.10 · Brick.Types.CommonApplicative EdgesDefined in brick-2.10 · Brick.Types.CommonEq a => Eq (Edges a)Defined in brick-2.10 · Brick.Types.CommonOrd a => Ord (Edges a)Defined in brick-2.10 · Brick.Types.CommonRead a => Read (Edges a)Defined in brick-2.10 · Brick.Types.CommonShow a => Show (Edges a)Defined in brick-2.10 · Brick.Types.CommonGeneric (Edges a)Defined in brick-2.10 · Brick.Types.CommonNFData a => NFData (Edges a)Defined in brick-2.10 · Brick.Types.Commontype Rep (Edges a) = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Common"Edges"
"Brick.Types.Common"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"Edges"
'PrefixI 'True) ((S1 ('MetaSel ('Just"eTop"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Just"eBottom"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :*: (S1 ('MetaSel ('Just"eLeft"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Just"eRight"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a))))
Miscellaneous
2 declarationsWidget size policies. These policies communicate how a widget uses
space when being rendered. These policies influence rendering order
and space allocation in the box layout algorithm for hBox and
vBox.
Constructors
Instances6Eq, Read, Show, Generic, NFData, Rep
Eq DirectionDefined in brick-2.10 · Brick.Types.InternalRead DirectionDefined in brick-2.10 · Brick.Types.InternalShow DirectionDefined in brick-2.10 · Brick.Types.InternalGeneric DirectionDefined in brick-2.10 · Brick.Types.InternalNFData DirectionDefined in brick-2.10 · Brick.Types.Internaltype Rep Direction = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"Direction"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"Up"
'PrefixI 'False) U1 :+: C1 ('MetaCons"Down"
'PrefixI 'False) U1)
Renderer internals (for benchmarking)
1 declarationInstances5Read, Show, Generic, NFData, Rep
(Ord n, Read n) => Read (RenderState n)Defined in brick-2.10 · Brick.Types.InternalShow n => Show (RenderState n)Defined in brick-2.10 · Brick.Types.InternalGeneric (RenderState n)Defined in brick-2.10 · Brick.Types.InternalNFData n => NFData (RenderState n)Defined in brick-2.10 · Brick.Types.Internaltype Rep (RenderState n) = D1 ('MetaDataDefined in brick-2.10 · Brick.Types.Internal"RenderState"
"Brick.Types.Internal"
"brick-2.10-EjbKtbggBIDAB27Nb1O2P0"
'False) (C1 ('MetaCons"RS"
'PrefixI 'True) ((S1 ('MetaSel ('Just"viewportMap"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map n Viewport)) :*: (S1 ('MetaSel ('Just"rsScrollRequests"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [(n, ScrollRequest)]) :*: S1 ('MetaSel ('Just"observedNames"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Set n)))) :*: ((S1 ('MetaSel ('Just"renderCache"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map n ([n], Result n))) :*: S1 ('MetaSel ('Just"clickableNames"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [n])) :*: (S1 ('MetaSel ('Just"requestedVisibleNames_"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Set n)) :*: S1 ('MetaSel ('Just"reportedExtents"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map n (Extent n)))))))
Re-exports for convenience
5 declarationsReturn the state from the internals of the monad.
Gets specific component of the state, using a projection function supplied.
Replace the state inside the monad.
Monadic state transformer.
Maps an old state to a new state inside a state monad. The old state is thrown away.
Main> :t modify ((+1) :: Int -> Int)
modify (...) :: (MonadState Int a) => a ()This says that modify (+1) acts over any
Monad that is a member of the MonadState class,
with an Int state.
When you're in a state monad, this function lets you operate on a part of your state. For instance, if your state was a record containing a position field, after zooming position would become your whole state (and when you modify it, the bigger structure would be modified as well).
(Your State / StateT or RWS / RWST can be anywhere in the stack, but you can't use zoom with arbitrary MonadState because it doesn't provide any methods to change the type of the state. See this issue for details.)
For the sake of the example, let's define some types first:
data Position = Position {
_x, _y :: Int }
data Player = Player {
_position :: Position,
... }
data Game = Game {
_player :: Player,
_obstacles :: [Position],
... }
concat <$> mapM makeLenses [''Position, ''Player, ''Game]
Now, here's an action that moves the player north-east:
moveNE :: State Game ()
moveNE = do
player.position.x += 1
player.position.y += 1
With zoom, you can use player.position to focus just on a part of the state:
moveNE :: State Game ()
moveNE = do
zoom (player.position) $ do
x += 1
y += 1
You can just as well use it for retrieving things out of the state:
getCoords :: State Game (Int, Int)
getCoords = zoom (player.position) ((,) <$> use x <*> use y)
Or more explicitly:
getCoords = zoom (player.position) $ do
x' <- use x
y' <- use y
return (x', y')
When you pass a traversal to zoom, it'll work as a loop. For instance, here we move all obstacles:
moveObstaclesNE :: State Game ()
moveObstaclesNE = do
zoom (obstacles.each) $ do
x += 1
y += 1
If the action returns a result, all results would be combined with <> – the same way they're combined when ^. is passed a traversal. In this example, moveObstaclesNE returns a list of old coordinates of obstacles in addition to moving them:
moveObstaclesNE = do
xys <- zoom (obstacles.each) $ do
-- Get old coordinates.
x' <- use x
y' <- use y
-- Update them.
x .= x' + 1
y .= y' + 1
-- Return a single-element list with old coordinates.
return [(x', y')]
...
Finally, you might need to write your own instances of Zoom if you use newtyped transformers in your monad stack. This can be done as follows:
import Lens.Micro.Mtl.Internal
type instance Zoomed (MyStateT s m) = Zoomed (StateT s m)
instance Monad m => Zoom (MyStateT s m) (MyStateT t m) s t where
zoom l (MyStateT m) = MyStateT (zoom l m)