-
Notifications
You must be signed in to change notification settings - Fork 1
/
Input.hs
72 lines (53 loc) · 2.01 KB
/
Input.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-# LANGUAGE OverloadedStrings #-}
module Input (
newKeyActor
) where
import HGamer3D
import Control.Monad
import qualified Data.Text as T
import Control.Monad.Trans.Reader
import Control.Monad.Trans.State
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Data
import Actor
-- KEY INPUT ACTOR
-- ---------------
-- single keypresses are directly send to screenA
-- polling checks in intervals on status of pressed keys, to be sent to screenA as a list
type KiaR = Actor
type KiaS = (Var [KeyEvent], [T.Text])
handleKey :: Actor -> Var [KeyEvent] -> KeyEvent -> IO ()
handleKey switchA kevts evt = do
case evt of
KeyDownEvent (KeyData _ _ k) -> sendMsg switchA (SingleKey k)
_ -> return ()
updateVar kevts (\l -> (l ++ [evt], ()))
newKeyActor :: HG3D -> Actor -> IO Actor
newKeyActor hg3d switchA = do
keyevts <- makeVar []
ieh <- newE hg3d [ctInputEventHandler #: DefaultEventHandler, ctKeyEvent #: NoKeyEvent]
registerCallback hg3d ieh ctKeyEvent (handleKey switchA keyevts)
actor <- newActor
runActor actor keyInputActorF switchA (keyevts, [])
return actor
keyInputActorF :: Actor -> Message -> ReaderStateIO KiaR KiaS ()
keyInputActorF keyA msg = do
switchA <- lift ask
(keyevts, keysdown) <- get
case msg of
PollKeys -> do
keys <- liftIO $ updateVar keyevts (\l -> ([], l))
keysdown' <- foldM (\kd k -> do
case k of
KeyDownEvent (KeyData _ _ k) -> do
let kd' = if not (k `elem` kd) then (k : kd) else kd
return kd'
KeyUpEvent (KeyData _ _ k) -> do
let kd' = filter (\k' -> k' /= k) kd
return kd'
) keysdown (reverse keys)
if length keysdown' > 0
then liftIO $ sendMsg switchA (KeysPressed keysdown')
else return ()
put (keyevts, keysdown') >> return ()