{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE AllowAmbiguousTypes #-} module Affection.Subsystems.AffectionWindow where import Affection.MessageBus import Affection.Subsystems.Class import Affection.Types import Control.Monad.State.Class (get) import Control.Monad.IO.Class (liftIO) import qualified SDL data AffectionWindow msg = AffectionWindow { windowChannel :: Channel msg } instance Participant (AffectionWindow (SystemMessage m)) (SystemMessage m) where partChannel = windowChannel partInit = do ad <- get nchan <- liftIO $ dupChannel $ messageChannel ad return $ AffectionWindow { windowChannel = nchan } partListen p = do let chan = partChannel p mmsg <- tryPeekChannel chan return mmsg partEmit p m = do let chan = partChannel p writeChannel chan m instance Subsystem (AffectionWindow (SystemMessage m)) (SystemMessage m) where consumeEvents aw evs = doConsume evs where doConsume (e:es) = case SDL.eventPayload e of SDL.WindowShownEvent (SDL.WindowShownEventData window) -> do partEmit aw (MsgWindowShown (SDL.eventTimestamp e) window) doConsume es SDL.WindowHiddenEvent (SDL.WindowHiddenEventData window) -> do partEmit aw (MsgWindowHidden (SDL.eventTimestamp e) window) doConsume es SDL.WindowExposedEvent (SDL.WindowExposedEventData window) -> do partEmit aw (MsgWindowExposed (SDL.eventTimestamp e) window) doConsume es SDL.WindowMovedEvent (SDL.WindowMovedEventData window (SDL.P newPos)) -> do partEmit aw (MsgWindowMoved (SDL.eventTimestamp e) window newPos) doConsume es SDL.WindowResizedEvent (SDL.WindowResizedEventData window newSize) -> do partEmit aw (MsgWindowResized (SDL.eventTimestamp e) window newSize) doConsume es SDL.WindowSizeChangedEvent (SDL.WindowSizeChangedEventData window) -> do partEmit aw (MsgWindowSizeChanged (SDL.eventTimestamp e) window) doConsume es SDL.WindowMinimizedEvent (SDL.WindowMinimizedEventData window) -> do partEmit aw (MsgWindowMinimized (SDL.eventTimestamp e) window) doConsume es SDL.WindowMaximizedEvent (SDL.WindowMaximizedEventData window) -> do partEmit aw (MsgWindowMaximized (SDL.eventTimestamp e) window) doConsume es SDL.WindowRestoredEvent (SDL.WindowRestoredEventData window) -> do partEmit aw (MsgWindowRestored (SDL.eventTimestamp e) window) doConsume es SDL.WindowGainedMouseFocusEvent (SDL.WindowGainedMouseFocusEventData window) -> do partEmit aw (MsgWindowGainedMouseFocus (SDL.eventTimestamp e) window) doConsume es SDL.WindowLostMouseFocusEvent (SDL.WindowLostMouseFocusEventData window) -> do partEmit aw (MsgWindowLostMouseFocus (SDL.eventTimestamp e) window) doConsume es SDL.WindowGainedKeyboardFocusEvent (SDL.WindowGainedKeyboardFocusEventData window) -> do partEmit aw (MsgWindowGainedKeyboardFocus (SDL.eventTimestamp e) window) doConsume es SDL.WindowLostKeyboardFocusEvent (SDL.WindowLostKeyboardFocusEventData window) -> do partEmit aw (MsgWindowLostKeyboardFocus (SDL.eventTimestamp e) window) doConsume es SDL.WindowClosedEvent (SDL.WindowClosedEventData window) -> do partEmit aw (MsgWindowClosed (SDL.eventTimestamp e) window) doConsume es _ -> fmap (e :) (doConsume es)