wizard-wipeout/src-client/Client/Events.hs

47 lines
908 B
Haskell
Raw Normal View History

module Client.Events where
import Control.Concurrent.STM (atomically, writeTQueue, flushTQueue)
import Control.Monad.IO.Class (liftIO)
2024-11-03 14:22:28 +00:00
import Control.Monad.RWS (asks, get)
import Graphics.Vty
-- internal imports
2024-11-03 14:22:28 +00:00
import Client.Communication
import Client.Types
handleEvent
:: Event
-> Game ()
handleEvent (EvKey KEsc _) = do
2024-11-03 14:22:28 +00:00
st <- get
2024-11-07 16:47:11 +00:00
curLevel <- asks rcLogLevel
liftIO $ gracefulExit curLevel st "Quitting due to user input"
handleEvent _ =
pure ()
handleEvents
:: Game ()
handleEvents = do
eventQueue <- asks rcEventQueue
evs <- liftIO $ atomically $ flushTQueue eventQueue
mapM_
handleEvent
evs
pumpEvents
:: Vty
-> Game ()
pumpEvents vty = do
eventQueue <- asks rcEventQueue
mev <- liftIO $ nextEventNonblocking vty
case mev of
Just ev -> do
liftIO $ atomically $ writeTQueue eventQueue ev
pumpEvents vty
Nothing ->
return ()