module Graphics.Blank.Events where
import Data.Aeson (FromJSON(..), Value(..), ToJSON(..))
import Data.Aeson.Types ((.:), (.=), object)
import Control.Applicative((<|>),(<$>),(<*>))
import Control.Concurrent.STM
import Data.Text (Text)
data Event = Event
{ eMetaKey :: Bool
, ePageXY :: Maybe (Float,Float)
, eType :: EventName
, eWhich :: Maybe Int
}
deriving (Show)
instance FromJSON Event where
parseJSON (Object v) = Event <$> ((v .: "eMetaKey") <|> return False)
<*> (Just <$> (v .: "ePageXY") <|> return Nothing)
<*> (v .: "eType")
<*> (Just <$> (v .: "eWhich") <|> return Nothing)
parseJSON _ = fail "no parse of Event"
instance ToJSON Event where
toJSON e = object
$ ((:) ("eMetaKey" .= eMetaKey e))
$ (case ePageXY e of
Nothing -> id
Just (x,y) -> (:) ("ePageXY" .= (x,y)))
$ ((:) ("eType" .= eType e))
$ (case eWhich e of
Nothing -> id
Just w -> (:) ("eWhich" .= w))
$ []
type EventName = Text
type EventQueue = TChan Event