2017-11-27 22:30:11 +00:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE AllowAmbiguousTypes #-}
|
|
|
|
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
|
|
|
|
|
2017-11-29 07:29:44 +00:00
|
|
|
import Control.Monad.State.Class (gets)
|
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-11-29 07:29:44 +00:00
|
|
|
data AffectionWindow m = AffectionWindow
|
|
|
|
{ windowChannel :: Channel m
|
2017-11-27 04:27:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 07:29:44 +00:00
|
|
|
instance (Message m) => Participant (AffectionWindow m) m where
|
2017-11-27 04:27:34 +00:00
|
|
|
partChannel = windowChannel
|
|
|
|
|
2017-11-27 22:30:11 +00:00
|
|
|
partInit = do
|
2017-11-29 07:29:44 +00:00
|
|
|
chan <- gets messageChannel :: Affection sd m (Channel m)
|
|
|
|
nchan <- liftIO $ dupChannel $ chan
|
2017-11-27 22:30:11 +00:00
|
|
|
return $ AffectionWindow
|
|
|
|
{ windowChannel = nchan
|
|
|
|
}
|
|
|
|
|
2017-11-27 04:27:34 +00:00
|
|
|
partListen p = do
|
|
|
|
let chan = partChannel p
|
|
|
|
mmsg <- tryPeekChannel chan
|
|
|
|
return mmsg
|
|
|
|
|
2017-11-29 07:29:44 +00:00
|
|
|
partEmit p message = do
|
2017-11-27 04:27:34 +00:00
|
|
|
let chan = partChannel p
|
2017-11-29 07:29:44 +00:00
|
|
|
writeChannel chan message
|
2017-11-27 04:27:34 +00:00
|
|
|
|
2017-11-29 07:29:44 +00:00
|
|
|
instance Subsystem (AffectionWindow SystemMessage) SystemMessage where
|
2017-11-27 04:27:34 +00:00
|
|
|
consumeEvents aw evs = doConsume evs
|
|
|
|
where
|
|
|
|
doConsume (e:es) = case SDL.eventPayload e of
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowShownEvent (SDL.WindowShownEventData window) -> do
|
|
|
|
partEmit aw (MsgWindowShown (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
|
|
|
|
partEmit aw (MsgWindowHidden (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-11-27 04:27:34 +00:00
|
|
|
partEmit aw (MsgWindowExposed (SDL.eventTimestamp e) window)
|
|
|
|
doConsume es
|
2017-11-27 22:30:11 +00:00
|
|
|
SDL.WindowMovedEvent (SDL.WindowMovedEventData window (SDL.P newPos)) -> do
|
|
|
|
partEmit aw (MsgWindowMoved (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
|
|
|
|
partEmit aw (MsgWindowResized (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
|
|
|
|
partEmit aw (MsgWindowSizeChanged (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
|
|
|
|
partEmit aw (MsgWindowMinimized (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
|
|
|
|
partEmit aw (MsgWindowMaximized (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
|
|
|
|
partEmit aw (MsgWindowRestored (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
|
|
|
|
partEmit aw (MsgWindowGainedMouseFocus (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
|
|
|
|
partEmit aw (MsgWindowLostMouseFocus (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
|
|
|
|
partEmit aw (MsgWindowGainedKeyboardFocus (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
|
|
|
|
partEmit aw (MsgWindowLostKeyboardFocus (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
|
|
|
|
partEmit aw (MsgWindowClosed (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)
|