2024-10-28 11:20:04 +00:00
|
|
|
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)
|
2024-10-28 11:20:04 +00:00
|
|
|
|
|
|
|
import Graphics.Vty
|
|
|
|
|
|
|
|
-- internal imports
|
|
|
|
|
2024-11-03 14:22:28 +00:00
|
|
|
import Client.Communication
|
2024-10-28 11:20:04 +00:00
|
|
|
import Client.Types
|
|
|
|
|
|
|
|
handleEvent
|
|
|
|
:: Event
|
|
|
|
-> Game ()
|
|
|
|
handleEvent (EvKey KEsc _) = do
|
2024-11-03 14:22:28 +00:00
|
|
|
st <- get
|
|
|
|
liftIO $ gracefulExit st "Quitting due to user input"
|
2024-10-28 11:20:04 +00:00
|
|
|
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 ()
|