affection/src/Affection/Subsystems/AffectionKeyboard.hs

36 lines
1.1 KiB
Haskell
Raw Normal View History

2017-12-12 12:10:55 +00:00
{-# LANGUAGE MultiParamTypeClasses #-}
2017-12-15 16:48:12 +00:00
{-# LANGUAGE FlexibleContexts #-}
2017-12-27 16:33:31 +00:00
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
2017-12-12 12:10:55 +00:00
module Affection.Subsystems.AffectionKeyboard where
import Affection.Types
2017-12-13 14:19:53 +00:00
import Affection.Util
2017-12-13 14:53:51 +00:00
import Affection.MessageBus
2017-12-12 12:10:55 +00:00
import qualified SDL
2018-01-10 15:58:23 +00:00
-- | Helper function that consumes all Keyboard-related 'SDL.EventPayload's
-- and emits appropriate 'KeyboardMessage's
2017-12-15 16:48:12 +00:00
consumeSDLKeyboardEvents
:: forall ak. (Participant ak, Mesg ak ~ KeyboardMessage)
2018-01-10 15:58:23 +00:00
=> ak -- ^ The message system participant
-> [SDL.EventPayload] -- ^ Incoming events
-> Affection [SDL.EventPayload] -- ^ Leftover SDL Events
2017-12-15 16:58:55 +00:00
consumeSDLKeyboardEvents ak = doConsume
2017-12-15 16:48:12 +00:00
where
doConsume [] = return []
doConsume (e:es) = do
ts <- getElapsedTime
case e of
SDL.KeyboardEvent dat -> do
partEmit ak (MsgKeyboardEvent
ts
(SDL.keyboardEventWindow dat)
(SDL.keyboardEventKeyMotion dat)
(SDL.keyboardEventRepeat dat)
(SDL.keyboardEventKeysym dat)
)
doConsume es
_ -> fmap (e :) (doConsume es)