From cdd0c3989cb1c6475b075c75ee17f1fbb9e43d4c Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Wed, 14 Dec 2022 22:20:44 +0100 Subject: [PATCH] #15 Basic input handling --- lib/RPGEngine.hs | 12 +++++------ lib/control/Input.hs | 23 ++++++++++++++++++++ lib/control/InputHandling.hs | 41 ++++++++++++++++++++++++++++++++++++ rpg-engine.cabal | 1 + 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 lib/control/Input.hs create mode 100644 lib/control/InputHandling.hs diff --git a/lib/RPGEngine.hs b/lib/RPGEngine.hs index 44047fb..d5ea2e7 100644 --- a/lib/RPGEngine.hs +++ b/lib/RPGEngine.hs @@ -7,6 +7,8 @@ module RPGEngine import Game import Render +import Input + import Graphics.Gloss ( Color(..) , white @@ -23,10 +25,6 @@ winDimensions = (1280, 720) winOffsets :: (Int, Int) winOffsets = (0, 0) --- Game background color -bgColor :: Color -bgColor = white - ---------------------------------------------------------------------- -- This is the gameloop. @@ -34,6 +32,6 @@ bgColor = white playRPGEngine :: String -> Int -> IO() playRPGEngine title fps = do play window bgColor fps initGame render handleInputs step - where window = initWindow title winDimensions winOffsets - step _ g = g -- TODO Do something with step? - handleInputs _ g = g -- TODO Implement inputHanlders + where window = initWindow title winDimensions winOffsets + step _ g = g -- TODO Do something with step? + handleInputs = handleAllInput diff --git a/lib/control/Input.hs b/lib/control/Input.hs new file mode 100644 index 0000000..e13b523 --- /dev/null +++ b/lib/control/Input.hs @@ -0,0 +1,23 @@ +module Input +( +-- Handle all input for RPG-Engine +handleAllInput +) where + +import Game +import State +import InputHandling + +import Graphics.Gloss.Interface.IO.Game + +---------------------------------------------------------------------- + +handleAllInput :: InputHandler Game +handleAllInput = composeInputHandlers [ + handleSpecialKey KeySpace setNextState + ] + +-- Go to the next stage of the Game +setNextState :: Game -> Game +setNextState game = game{ state = newState } + where newState = nextState $ state game diff --git a/lib/control/InputHandling.hs b/lib/control/InputHandling.hs new file mode 100644 index 0000000..86704e4 --- /dev/null +++ b/lib/control/InputHandling.hs @@ -0,0 +1,41 @@ +-- 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 diff --git a/rpg-engine.cabal b/rpg-engine.cabal index 754ec51..28aea68 100644 --- a/rpg-engine.cabal +++ b/rpg-engine.cabal @@ -11,6 +11,7 @@ library gloss >= 1.11 && < 1.14, gloss-juicy >= 0.2.3 exposed-modules: RPGEngine, + Input, InputHandling, Game, State, Render