canvas/app/Types/UserData.hs

78 lines
2 KiB
Haskell
Raw Normal View History

2020-02-06 04:10:10 +00:00
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
2020-02-05 06:21:39 +00:00
module Types.UserData where
import Affection
import Control.Concurrent.STM
import qualified SDL
import qualified Data.Map.Strict as M
2020-02-06 04:10:10 +00:00
import Codec.Picture
2020-02-05 06:21:39 +00:00
data UserData = UserData
2020-02-06 04:10:10 +00:00
{ udSubsystems :: Subsystems
, udAssetImages :: M.Map ImgId (Image PixelRGBA8)
2020-02-05 06:21:39 +00:00
}
2020-02-06 04:10:10 +00:00
type ImgId = String
data Subsystems = Subsystems
{ subWindow :: SubWindow
, subMouse :: SubMouse
}
newtype SubWindow = SubWindow (TVar [(UUID, WindowMessage -> Affection UserData ())])
newtype SubMouse = SubMouse (TVar [(UUID, MouseMessage -> Affection UserData ())])
instance Participant SubWindow UserData where
type Mesg SubWindow UserData = WindowMessage
partSubscribers (SubWindow t) = genericSubscribers t
partSubscribe (SubWindow t) = genericSubscribe t
partUnSubscribe (SubWindow t) = genericUnSubscribe t
instance SDLSubsystem SubWindow UserData where
consumeSDLEvents = consumeSDLWindowEvents
instance Participant SubMouse UserData where
type Mesg SubMouse UserData = MouseMessage
partSubscribers (SubMouse t) = genericSubscribers t
partSubscribe (SubMouse t) = genericSubscribe t
partUnSubscribe (SubMouse t) = genericUnSubscribe t
instance SDLSubsystem SubMouse UserData where
consumeSDLEvents = consumeSDLMouseEvents
genericSubscribers
:: TVar [(UUID, msg -> Affection UserData ())]
-> Affection UserData [(msg -> Affection UserData ())]
genericSubscribers t = do
subTups <- liftIO $ readTVarIO t
return $ map snd subTups
genericSubscribe
:: TVar [(UUID, msg -> Affection UserData ())]
-> (msg -> Affection UserData ())
-> Affection UserData UUID
genericSubscribe t funct = do
uu <- genUUID
liftIO $ atomically $ modifyTVar' t ((uu, funct) :)
return uu
genericUnSubscribe
:: TVar [(UUID, msg -> Affection UserData ())]
-> UUID
-> Affection UserData ()
genericUnSubscribe t uu =
liftIO $ atomically $ modifyTVar' t (filter (`filterMsg` uu))
where
filterMsg (u, _) p = u /= p