This repository has been archived on 2023-06-24. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
2022FuncProg-project3-RPGEn.../lib/control/InputHandling.hs

41 lines
1.3 KiB
Haskell

-- Allows to create a massive inputHandler that can handle anything
-- after you specify what you want it to do.
module InputHandling
( InputHandler(..),
-- Compose multiple InputHandlers into one InputHandler that handles
-- all of them.
composeInputHandlers,
handle,
handleSpecialKey
) where
import Graphics.Gloss.Interface.IO.Game
----------------------------- Constants ------------------------------
type InputHandler a = Event -> (a -> a)
----------------------------------------------------------------------
composeInputHandlers :: [InputHandler a] -> InputHandler a
composeInputHandlers [] ev a = a
composeInputHandlers (ih:ihs) ev a = composeInputHandlers ihs ev (ih ev a)
handle :: Event -> (a -> a) -> Event -> (a -> a)
handle (EventKey key _ _ _) = handleKey key
-- handle (EventMotion _) = undefined
-- handle (EventResize _) = undefined
handle _ = (\_ -> (\_ -> id))
handleKey :: Key -> (a -> a) -> Event -> (a -> a)
handleKey (SpecialKey key) = handleSpecialKey key
handleKey (Char _ ) = (\_ -> (\_ -> id))
handleKey (MouseButton _ ) = (\_ -> (\_ -> id))
handleSpecialKey :: SpecialKey -> (a -> a) -> Event -> (a -> a)
handleSpecialKey sk1 f (EventKey (SpecialKey sk2) Down _ _)
| sk1 == sk2 = f
| otherwise = id
handleSpecialKey _ _ _ = id