{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
module Miso.Subscription.Keyboard
(
Arrows (..)
, arrowsSub
, directionSub
, keyboardSub
, wasdSub
) where
import Control.Monad.IO.Class
import Data.IORef
import Data.Set
import qualified Data.Set as S
import GHCJS.Marshal
import JavaScript.Object
import JavaScript.Object.Internal
import Miso.Effect (Sub)
import Miso.FFI
data Arrows = Arrows {
arrowX :: !Int
, arrowY :: !Int
} deriving (Show, Eq)
toArrows :: ([Int], [Int], [Int], [Int]) -> Set Int -> Arrows
toArrows (up, down, left, right) set' =
Arrows {
arrowX =
case (check left, check right) of
(True, False) -> -1
(False, True) -> 1
(_,_) -> 0
, arrowY =
case (check down, check up) of
(True, False) -> -1
(False, True) -> 1
(_,_) -> 0
}
where
check = any (`S.member` set')
arrowsSub :: (Arrows -> action) -> Sub action
arrowsSub = directionSub ([38], [40], [37], [39])
wasdSub :: (Arrows -> action) -> Sub action
wasdSub = directionSub ([87], [83], [65], [68])
directionSub :: ([Int], [Int], [Int], [Int])
-> (Arrows -> action)
-> Sub action
directionSub dirs = keyboardSub . (. toArrows dirs)
keyboardSub :: (Set Int -> action) -> Sub action
keyboardSub f sink = do
keySetRef <- liftIO (newIORef mempty)
windowAddEventListener "keyup" $ keyUpCallback keySetRef
windowAddEventListener "keydown" $ keyDownCallback keySetRef
windowAddEventListener "blur" $ blurCallback keySetRef
where
keyDownCallback keySetRef = \keyDownEvent -> do
Just key <- fromJSVal =<< getProp "keyCode" (Object keyDownEvent)
newKeys <- liftIO $ atomicModifyIORef' keySetRef $ \keys ->
let !new = S.insert key keys
in (new, new)
liftIO (sink (f newKeys))
keyUpCallback keySetRef = \keyUpEvent -> do
Just key <- fromJSVal =<< getProp "keyCode" (Object keyUpEvent)
newKeys <- liftIO $ atomicModifyIORef' keySetRef $ \keys ->
let !new = S.delete key keys
in (new, new)
liftIO (sink (f newKeys))
blurCallback keySetRef = \_ -> do
newKeys <- liftIO $ atomicModifyIORef' keySetRef $ \_ ->
let !new = S.empty
in (new, new)
liftIO (sink (f newKeys))