2017-11-27 22:30:11 +00:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2017-12-13 04:05:18 +00:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2017-11-27 22:30:11 +00:00
|
|
|
module Affection.Subsystems.AffectionWindow where
|
2017-11-27 04:27:34 +00:00
|
|
|
|
2017-12-13 03:37:16 +00:00
|
|
|
import Affection.Types
|
2017-12-13 14:19:53 +00:00
|
|
|
import Affection.Util
|
2017-11-27 04:27:34 +00:00
|
|
|
import Affection.MessageBus
|
|
|
|
import Affection.Subsystems.Class
|
2017-11-27 22:30:11 +00:00
|
|
|
|
2017-12-13 03:37:16 +00:00
|
|
|
import Control.Concurrent.STM as STM
|
2017-11-27 22:30:11 +00:00
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2017-11-27 04:27:34 +00:00
|
|
|
|
|
|
|
import qualified SDL
|
|
|
|
|
2017-12-13 03:37:16 +00:00
|
|
|
data AffectionWindow sd = AffectionWindow
|
2017-12-13 04:05:18 +00:00
|
|
|
{ windowSubscribers :: forall sd. TVar [(UUID, WindowMessage -> Affection sd ())]
|
2017-11-27 04:27:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 04:05:18 +00:00
|
|
|
instance Participant (AffectionWindow sd) WindowMessage where
|
2017-12-13 03:37:16 +00:00
|
|
|
partSubscribe p funct = do
|
|
|
|
uuid <- genUUID
|
|
|
|
liftIO $ atomically $ modifyTVar' (windowSubscribers p) ((uuid, funct) :)
|
|
|
|
return uuid
|
|
|
|
|
|
|
|
partUnSubscribe p uuid =
|
|
|
|
liftIO $ atomically $ modifyTVar' (windowSubscribers p)
|
|
|
|
(filter (\(u, _) -> u /= uuid))
|
|
|
|
|
|
|
|
partSubscribers p = do
|
|
|
|
subTups <- liftIO $ readTVarIO $ windowSubscribers p
|
|
|
|
return $ map snd subTups
|
2017-11-27 04:27:34 +00:00
|
|
|
|
2017-12-13 04:05:18 +00:00
|
|
|
instance SDLSubsystem (AffectionWindow sd) WindowMessage where
|
2017-12-13 14:19:53 +00:00
|
|
|
consumeSDLEvents aw eps = doConsume eps
|
2017-11-27 04:27:34 +00:00
|
|
|
where
|
2017-12-13 14:19:53 +00:00
|
|
|
doConsume (e:es) = do
|
|
|
|
ts <- getElapsedTime
|
|
|
|
case e of
|
|
|
|
SDL.WindowShownEvent (SDL.WindowShownEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowShow ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowHiddenEvent (SDL.WindowHiddenEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowHide ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowExposedEvent (SDL.WindowExposedEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowExpose ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowMovedEvent (SDL.WindowMovedEventData window (SDL.P newPos)) -> do
|
|
|
|
partEmit aw (MsgWindowMove ts window newPos)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowResizedEvent (SDL.WindowResizedEventData window newSize) -> do
|
|
|
|
partEmit aw (MsgWindowResize ts window newSize)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowSizeChangedEvent (SDL.WindowSizeChangedEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowSizeChange ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowMinimizedEvent (SDL.WindowMinimizedEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowMinimize ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowMaximizedEvent (SDL.WindowMaximizedEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowMaximize ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowRestoredEvent (SDL.WindowRestoredEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowRestore ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowGainedMouseFocusEvent (SDL.WindowGainedMouseFocusEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowGainMouseFocus ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowLostMouseFocusEvent (SDL.WindowLostMouseFocusEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowLoseMouseFocus ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowGainedKeyboardFocusEvent (SDL.WindowGainedKeyboardFocusEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowGainKeyboardFocus ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowLostKeyboardFocusEvent (SDL.WindowLostKeyboardFocusEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowLoseKeyboardFocus ts window)
|
|
|
|
doConsume es
|
|
|
|
SDL.WindowClosedEvent (SDL.WindowClosedEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowClose ts window)
|
|
|
|
doConsume es
|
|
|
|
_ -> fmap (e :) (doConsume es)
|