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-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-12 12:12:47 +00:00
|
|
|
consumeSDLEvents aw evs = doConsume evs
|
2017-11-27 04:27:34 +00:00
|
|
|
where
|
2017-12-12 12:12:47 +00:00
|
|
|
doConsume [] = return []
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume (e:es) = case SDL.eventPayload e of
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowShownEvent (SDL.WindowShownEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowShow (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowHiddenEvent (SDL.WindowHiddenEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowHide (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowExposedEvent (SDL.WindowExposedEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowExpose (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowMovedEvent (SDL.WindowMovedEventData window (SDL.P newPos)) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowMove (SDL.eventTimestamp e) window newPos)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowResizedEvent (SDL.WindowResizedEventData window newSize) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowResize (SDL.eventTimestamp e) window newSize)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowSizeChangedEvent (SDL.WindowSizeChangedEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowSizeChange (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowMinimizedEvent (SDL.WindowMinimizedEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowMinimize (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowMaximizedEvent (SDL.WindowMaximizedEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowMaximize (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowRestoredEvent (SDL.WindowRestoredEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowRestore (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowGainedMouseFocusEvent (SDL.WindowGainedMouseFocusEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowGainMouseFocus (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowLostMouseFocusEvent (SDL.WindowLostMouseFocusEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowLoseMouseFocus (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowGainedKeyboardFocusEvent (SDL.WindowGainedKeyboardFocusEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowGainKeyboardFocus (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowLostKeyboardFocusEvent (SDL.WindowLostKeyboardFocusEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowLoseKeyboardFocus (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowClosedEvent (SDL.WindowClosedEventData window) -> do
|
2017-12-12 12:12:47 +00:00
|
|
|
partEmit aw (MsgWindowClose (SDL.eventTimestamp e) window)
|
2017-11-27 04:27:34 +00:00
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
_ -> fmap (e :) (doConsume es)
|