Last active
November 7, 2017 09:10
-
-
Save andrevdm/e4fadb36bc1e8b13fe17359d8855e7e1 to your computer and use it in GitHub Desktop.
Brick: Simple example (simplified from https://samtay.github.io/articles/brick.html)
This file contains hidden or 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
| {-# LANGUAGE NoImplicitPrelude #-} | |
| {-# LANGUAGE OverloadedStrings #-} | |
| module Main where | |
| import Protolude | |
| import Brick ((<+>)) | |
| import qualified Brick as B | |
| import qualified Brick.Widgets.Center as BC | |
| import qualified Brick.BChan as BCh | |
| import qualified Brick.Widgets.Border as BB | |
| import qualified Brick.Widgets.Border.Style as BBS | |
| import Control.Monad (forever, void) | |
| import Control.Concurrent (threadDelay, forkIO) | |
| import qualified Graphics.Vty as V | |
| -- Based on: https://samtay.github.io/articles/brick.html | |
| main :: IO () | |
| main = do | |
| chan <- BCh.newBChan 10 | |
| void . forkIO $ forever $ do | |
| BCh.writeBChan chan Tick | |
| threadDelay 100000 -- decides how fast your game moves | |
| let g = Game 0 | |
| void $ B.customMain (V.mkVty V.defaultConfig) (Just chan) app g | |
| type Name = () | |
| data Tick = Tick | |
| data Game = Game { gmScore :: Int | |
| } | |
| app :: B.App Game Tick Name | |
| app = B.App { B.appDraw = drawUI | |
| , B.appChooseCursor = B.neverShowCursor | |
| , B.appHandleEvent = handleEvent | |
| , B.appStartEvent = pure | |
| , B.appAttrMap = const theMap | |
| } | |
| handleEvent :: Game -> B.BrickEvent Name Tick -> B.EventM Name (B.Next Game) | |
| handleEvent (Game score) (B.AppEvent Tick) = B.continue $ Game (score + 1) | |
| handleEvent g (B.VtyEvent (V.EvKey V.KEsc [])) = B.halt g | |
| handleEvent g _ = B.continue g | |
| -- Drawing | |
| drawUI :: Game -> [B.Widget Name] | |
| drawUI g = | |
| [ BC.center $ B.padRight (B.Pad 2) (drawStats g) <+> drawGrid g ] | |
| drawStats :: Game -> B.Widget Name | |
| drawStats g = B.hLimit 20 $ B.withBorderStyle BBS.unicodeRounded | |
| $ BB.borderWithLabel (B.str "Score") | |
| $ BC.hCenter | |
| $ B.padAll 1 | |
| $ B.str $ show . gmScore $ g | |
| drawGrid :: Game -> B.Widget Name | |
| drawGrid g = B.hLimit 10 $ B.vBox [ B.str $ show . gmScore $ g | |
| , B.str $ show . gmScore $ g | |
| ] | |
| theMap :: B.AttrMap | |
| theMap = B.attrMap V.defAttr [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment