module Graphics.Blank.Events
(
Event(..)
, NamedEvent(..)
, EventName(..)
, EventQueue
, writeEventQueue
, readEventQueue
, tryReadEventQueue
, newEventQueue
) where
import Data.Aeson (FromJSON(..))
import qualified Data.Map as Map
import Data.Map (Map)
import Data.Char
import Control.Monad
import Control.Concurrent.STM
data Event = Event
{ jsCode :: Int
, jsMouse :: Maybe (Int,Int)
}
deriving (Show)
data NamedEvent = NamedEvent EventName Event
deriving (Show)
instance FromJSON NamedEvent where
parseJSON o = do
(str,code,x,y) <- parseJSON o
case Map.lookup str namedEventDB of
Just n -> return $ NamedEvent n (Event code (Just (x,y)))
Nothing -> do (str',code',(),()) <- parseJSON o
case Map.lookup str' namedEventDB of
Just n -> return $ NamedEvent n (Event code' Nothing)
Nothing -> fail "bad parse"
namedEventDB :: Map String EventName
namedEventDB = Map.fromList
[ (map toLower (show n),n)
| n <- [minBound..maxBound]
]
data EventName
= KeyPress
| KeyDown
| KeyUp
| MouseDown
| MouseEnter
| MouseMove
| MouseOut
| MouseOver
| MouseUp
deriving (Eq, Ord, Show, Enum, Bounded)
type EventQueue = TChan Event
writeEventQueue :: EventQueue -> Event -> IO ()
writeEventQueue q e = atomically $ writeTChan q e
readEventQueue :: EventQueue -> IO Event
readEventQueue q = atomically $ readTChan q
tryReadEventQueue :: EventQueue -> IO (Maybe Event)
tryReadEventQueue q = atomically $ do
b <- isEmptyTChan q
if b then return Nothing
else liftM Just (readTChan q)
newEventQueue :: IO EventQueue
newEventQueue = atomically newTChan