1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
{-# LANGUAGE OverloadedStrings #-}
{-|
The following example shows how setting a watch for the WindowSizeChangedEvent
allows us to handle the events as they are generated. Handling them in the
event loop, on the other hand, only allows us to see a final, coalesced, event.
To demonstrate this, run the program, resize the window with your mouse,
and check your console output.
-}
module EventWatch where
import SDL
import Control.Monad (void)
main :: IO ()
main = do
initializeAll
window <- createWindow "resize" WindowConfig {
windowBorder = True
, windowHighDPI = False
, windowInputGrabbed = False
, windowMode = Windowed
, windowGraphicsContext = NoGraphicsContext
, windowPosition = Wherever
, windowResizable = True
, windowInitialSize = V2 800 600
, windowVisible = True
}
_renderer <- createRenderer window (-1) defaultRenderer
void . addEventWatch $ \ev ->
case eventPayload ev of
WindowSizeChangedEvent sizeChangeData ->
putStrLn $ "eventWatch windowSizeChanged: " ++ show sizeChangeData
_ -> return ()
appLoop
appLoop :: IO ()
appLoop = waitEvent >>= go
where
go :: Event -> IO ()
go ev =
case eventPayload ev of
WindowSizeChangedEvent sizeChangeData -> do
putStrLn $ "waitEvent windowSizeChanged: " ++ show sizeChangeData
waitEvent >>= go
KeyboardEvent keyboardEvent
| keyboardEventKeyMotion keyboardEvent == Pressed &&
keysymKeycode (keyboardEventKeysym keyboardEvent) == KeycodeQ
-> return ()
KeyboardEvent keyboardEvent
| keyboardEventKeyMotion keyboardEvent == Pressed
-> print (keyboardEventKeysym keyboardEvent) >> waitEvent >>= go
MouseMotionEvent mouseMotionEvent
-> print mouseMotionEvent >> waitEvent >>= go
MouseButtonEvent mouseButtonEvent
-> print mouseButtonEvent >> waitEvent >>= go
MouseWheelEvent mouseWheelEvent
-> print mouseWheelEvent >> waitEvent >>= go
QuitEvent
-> return ()
_ -> waitEvent >>= go
|