brick/programs/LayerDemo.hs

72 lines
2.3 KiB
Haskell
Raw Normal View History

2015-07-04 01:49:33 +03:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Lens.Micro ((^.), (&), (%~))
import Lens.Micro.TH (makeLenses)
2015-07-04 01:49:33 +03:00
import Control.Monad (void)
import Data.Default
2015-07-10 23:20:05 +03:00
import qualified Graphics.Vty as V
2015-07-04 01:49:33 +03:00
2015-07-10 23:20:05 +03:00
import qualified Brick.Types as T
import Brick.Types (rowL, columnL, Widget)
2015-07-10 23:20:05 +03:00
import qualified Brick.Main as M
import qualified Brick.Widgets.Border as B
import qualified Brick.Widgets.Center as C
import Brick.Widgets.Core
( translateBy
, str
2015-07-10 23:20:05 +03:00
)
2015-07-04 01:49:33 +03:00
data St =
2015-07-10 23:20:05 +03:00
St { _topLayerLocation :: T.Location
, _bottomLayerLocation :: T.Location
2015-07-04 01:49:33 +03:00
}
makeLenses ''St
drawUi :: St -> [Widget ()]
2015-07-04 01:49:33 +03:00
drawUi st =
[ C.centerLayer $
B.border $ str "This layer is centered but other\nlayers are visible underneath it."
, topLayer st
2015-07-04 01:49:33 +03:00
, bottomLayer st
]
topLayer :: St -> Widget ()
2015-07-04 01:49:33 +03:00
topLayer st =
translateBy (st^.topLayerLocation) $
B.border $ str "Top layer\n(Arrow keys move)"
2015-07-04 01:49:33 +03:00
bottomLayer :: St -> Widget ()
2015-07-04 01:49:33 +03:00
bottomLayer st =
translateBy (st^.bottomLayerLocation) $
B.border $ str "Bottom layer\n(Ctrl-arrow keys move)"
2015-07-04 01:49:33 +03:00
appEvent :: St -> V.Event -> T.EventM () (T.Next St)
appEvent st (V.EvKey V.KDown []) = M.continue $ st & topLayerLocation.rowL %~ (+ 1)
appEvent st (V.EvKey V.KUp []) = M.continue $ st & topLayerLocation.rowL %~ (subtract 1)
appEvent st (V.EvKey V.KRight []) = M.continue $ st & topLayerLocation.columnL %~ (+ 1)
appEvent st (V.EvKey V.KLeft []) = M.continue $ st & topLayerLocation.columnL %~ (subtract 1)
appEvent st (V.EvKey V.KDown [V.MCtrl]) = M.continue $ st & bottomLayerLocation.rowL %~ (+ 1)
appEvent st (V.EvKey V.KUp [V.MCtrl]) = M.continue $ st & bottomLayerLocation.rowL %~ (subtract 1)
appEvent st (V.EvKey V.KRight [V.MCtrl]) = M.continue $ st & bottomLayerLocation.columnL %~ (+ 1)
appEvent st (V.EvKey V.KLeft [V.MCtrl]) = M.continue $ st & bottomLayerLocation.columnL %~ (subtract 1)
2015-07-10 23:20:05 +03:00
appEvent st (V.EvKey V.KEsc []) = M.halt st
appEvent st _ = M.continue st
2015-07-04 01:49:33 +03:00
app :: M.App St V.Event ()
2015-07-04 01:49:33 +03:00
app =
2015-07-10 23:20:05 +03:00
M.App { M.appDraw = drawUi
, M.appStartEvent = return
, M.appHandleEvent = appEvent
, M.appAttrMap = const def
, M.appLiftVtyEvent = id
2015-07-10 23:20:05 +03:00
, M.appChooseCursor = M.neverShowCursor
}
2015-07-04 01:49:33 +03:00
main :: IO ()
2015-07-10 23:20:05 +03:00
main = void $ M.defaultMain app $ St (T.Location (0, 0)) (T.Location (0, 0))