pituicat/src/Affection/Subsystems/AffectionWindow.hs

81 lines
3.4 KiB
Haskell
Raw Normal View History

2017-11-27 22:30:11 +00:00
{-# LANGUAGE MultiParamTypeClasses #-}
module Affection.Subsystems.AffectionWindow where
2017-11-27 04:27:34 +00:00
import Affection.MessageBus
import Affection.Subsystems.Class
2017-11-27 22:30:11 +00:00
import Affection.Types
import Control.Monad.IO.Class (liftIO)
2017-11-27 04:27:34 +00:00
import qualified SDL
2017-12-12 12:12:47 +00:00
data AffectionWindow = AffectionWindow
{ windowInChannel :: Channel WindowMessage
, windowOutChannel :: Channel WindowMessage
2017-11-27 04:27:34 +00:00
}
2017-12-12 12:12:47 +00:00
instance Participant AffectionWindow WindowMessage where
partChannel = windowOutChannel
2017-11-27 04:27:34 +00:00
2017-12-12 12:12:47 +00:00
partInit ichan = do
ochan <- liftIO $ newBroadcastChannel
2017-11-27 22:30:11 +00:00
return $ AffectionWindow
2017-12-12 12:12:47 +00:00
{ windowOutChannel = ochan
, windowInChannel = ichan
2017-11-27 22:30:11 +00:00
}
2017-12-12 12:12:47 +00:00
partListen p =
liftIO $ tryPeekChannel (windowInChannel p)
2017-11-27 04:27:34 +00:00
2017-12-12 12:12:47 +00:00
partEmit p message =
liftIO $ writeChannel (windowOutChannel p) message
2017-11-27 04:27:34 +00:00
2017-12-12 12:12:47 +00:00
instance SDLSubsystem AffectionWindow WindowMessage where
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)