-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.hs
executable file
·43 lines (38 loc) · 1.83 KB
/
Event.hs
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
{-# LANGUAGE Arrows #-}
module Type.Event (Direction(..), movement, quitPressed) where
import Control.Applicative (Alternative ((<|>)))
import Data.List (foldl')
import FRP.Yampa (SF, arr)
import FRP.Yampa.Basic (constant)
import FRP.Yampa.Event (Event (..), maybeToEvent)
import SDL hiding (Event)
import qualified SDL
import SDL.Event (EventPayload (..))
data Direction = UP
| DOWN
| LEFT
| RIGHT
movement :: SF [SDL.Event] (Event Direction)
movement =
arr (maybeToEvent . foldr (<|>) Nothing . fmap dispatch)
where dispatch event = case eventPayload event of
KeyboardEvent keyboardEvent
| keyboardEventKeyMotion keyboardEvent == Pressed
-> case keysymKeycode (keyboardEventKeysym keyboardEvent) of
KeycodeW -> Just UP
KeycodeS -> Just DOWN
KeycodeA -> Just LEFT
KeycodeD -> Just RIGHT
_ -> Nothing
_ -> Nothing
quitPressed :: [SDL.Event] -> Bool
quitPressed =
or . fmap dispatch
where dispatch event = case eventPayload event of
KeyboardEvent keyboardEvent
| keyboardEventKeyMotion keyboardEvent == Pressed
-> case keysymKeycode (keyboardEventKeysym keyboardEvent) of
KeycodeQ -> True
_ -> False
WindowClosedEvent _ -> True
_ -> False