From 76d05eb2de39a69e93c2c342073cde2627c9f50c Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 1 May 2021 22:07:47 -0400 Subject: [PATCH] custom tick event --- Ekitai.hs | 15 +++------------ Render.hs | 40 +++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Ekitai.hs b/Ekitai.hs index 9e416e1..af47e02 100644 --- a/Ekitai.hs +++ b/Ekitai.hs @@ -2,15 +2,11 @@ import System.Environment import System.IO import qualified Data.Vector as V -import qualified Brick as B import Parse import Sim import Render --- ui :: B.Widget () --- ui = B.str "hello" <+> B.str "World" - hGetLines :: Handle -> IO [String] hGetLines h = do line <- hGetLine h @@ -21,18 +17,13 @@ hGetLines h = do return (line:lines) main = do + -- handle file stuff argv <- getArgs (opts, fname) <- ekitaiOpts argv handle <- openFile fname ReadMode contents <- hGetLines handle hClose handle - -- putStrLn $ show $ stringToSim contents - initialState <- buildInitialState $ stringToSim contents - endState <- B.defaultMain ekitaiApp initialState - -- print endState + -- start brick + ekitaiMain $ stringToSim contents return 0 --- main :: IO () --- main = do - -- return 0 - diff --git a/Render.hs b/Render.hs index 036944b..bc4a15a 100644 --- a/Render.hs +++ b/Render.hs @@ -1,10 +1,15 @@ -module Render ( buildInitialState, ekitaiApp ) where +module Render ( ekitaiMain ) where import Brick.AttrMap import Brick.Main import Brick.Types import Brick.Widgets.Core -import Graphics.Vty.Input.Events +import Brick.BChan (newBChan, writeBChan) +import Graphics.Vty + +import Control.Monad (forever, void) +import Control.Monad.IO.Class (liftIO) +import Control.Concurrent (threadDelay, forkIO) import qualified Data.Vector as V import Sim @@ -15,7 +20,10 @@ data EkitaiState = EkitaiState { ekitaiStateSim :: Simulation } deriving (Show) -ekitaiApp :: App EkitaiState e ResourceName +-- custom event +data Tick = Tick + +ekitaiApp :: App EkitaiState Tick ResourceName ekitaiApp = App { appDraw = drawEkitai , appChooseCursor = showFirstCursor @@ -24,6 +32,18 @@ ekitaiApp = App , appAttrMap = const $ attrMap mempty [] } +ekitaiMain sim = do + chan <- newBChan 10 + -- tick game + forkIO $ forever $ do + writeBChan chan Tick + threadDelay 50000 + let buildVty = Graphics.Vty.mkVty Graphics.Vty.defaultConfig + initialVty <- buildVty + initialState <- buildInitialState sim + endState <- customMain initialVty buildVty (Just chan) ekitaiApp initialState + return 0 + buildInitialState :: Simulation -> IO EkitaiState buildInitialState sim = pure EkitaiState @@ -31,16 +51,10 @@ buildInitialState sim = } drawEkitai :: EkitaiState -> [Widget ResourceName] --- drawEkitai state = [ vBox $ drawSim $ ekitaiStateSim state ] drawEkitai state = [ vBox [str $ simToString $ ekitaiStateSim state] ] -handleEkitaiEvent :: EkitaiState -> BrickEvent n e -> EventM n (Next EkitaiState) -handleEkitaiEvent s e = - case e of - VtyEvent vtye -> - case vtye of - EvKey (KChar 'q') [] -> halt s - EvKey (KChar 's') [] -> continue s { ekitaiStateSim = physStep $ ekitaiStateSim s } - _ -> continue s - _ -> continue s +handleEkitaiEvent :: EkitaiState -> BrickEvent n Tick -> EventM n (Next EkitaiState) +handleEkitaiEvent s (VtyEvent (EvKey (KChar 'q') [])) = halt s +handleEkitaiEvent s (AppEvent Tick) = continue s { ekitaiStateSim = physStep $ ekitaiStateSim s } +handleEkitaiEvent s _ = continue s -- 2.20.1