ibis/src/Events.hs

52 lines
1.4 KiB
Haskell

module Events where
import qualified SDL
import qualified Data.Text as T
import Linear
import Control.Concurrent.MVar
-- internal impomrts
import Util
import Types
eventHandler :: MVar Bool -> MVar State -> SDL.Event -> IO ()
eventHandler run state (SDL.Event _ payload) =
handlePayload run state payload
handlePayload :: MVar Bool -> MVar State -> SDL.EventPayload -> IO ()
handlePayload _ _ (SDL.WindowResizedEvent (SDL.WindowResizedEventData _ dim)) =
fitViewport (800 / 600) dim
handlePayload run _ (SDL.WindowClosedEvent _) = do
_ <- swapMVar run False
return ()
handlePayload run state (SDL.KeyboardEvent (SDL.KeyboardEventData _ SDL.Pressed False sym))
| SDL.keysymKeycode sym == SDL.KeycodeEscape = do
_ <- swapMVar run False
return ()
| SDL.keysymKeycode sym == SDL.KeycodeF12 || SDL.keysymKeycode sym == SDL.KeycodeF =
modifyMVar_ state $ \st -> do
let st2 = st
{ stPresentationWindow =
if length (stWindows st) > 1 && stPresentationWindow st == 0
then 1
else 0
}
mapM_ (\(ident, win) -> do
let title = "Ibis - " `T.append` stFilename st2
if ident == stPresentationWindow st2
then SDL.windowTitle win SDL.$= title
else SDL.windowTitle win SDL.$= title `T.append` " - Notes"
)
(stWindows st2)
return st2
-- catch all other events
handlePayload _ _ _ = return ()