From: Daniel Liu Date: Sun, 2 May 2021 02:07:47 +0000 (-0400) Subject: custom tick event X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=76d05eb2de39a69e93c2c342073cde2627c9f50c;p=ekitaihs.git custom tick event --- 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