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