From aaba06efd87df63efac68b5e9feb5102bbc1ee28 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 17 Oct 2023 18:52:19 +0100 Subject: Cleaner, more readable code --- src/Game/Controller.hs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'src/Game/Controller.hs') diff --git a/src/Game/Controller.hs b/src/Game/Controller.hs index 4d0ba81..0914766 100644 --- a/src/Game/Controller.hs +++ b/src/Game/Controller.hs @@ -24,26 +24,28 @@ data Controls = Controls deriving (Show) processControllerEvents :: Controls -> T.Toaster -> [SDL.EventPayload] -> IO (Controls, T.Toaster) -processControllerEvents controls toaster (SDL.ControllerDeviceEvent (SDL.ControllerDeviceEventData SDL.ControllerDeviceAdded joyIndex) : t) = do - if isNothing controls.joyId - then do - joyName <- peekCAString =<< SDL.Raw.gameControllerNameForIndex (fromIntegral joyIndex) - let toaster' = T.add toaster $ "Connected " ++ show joyName - gc <- SDL.Raw.gameControllerOpen (fromIntegral joyIndex) - joy <- SDL.Raw.gameControllerGetJoystick gc - joyId <- SDL.Raw.joystickInstanceID joy - processControllerEvents controls {joyId = Just joyId, gc = Just gc} toaster' t - else processControllerEvents controls toaster t -processControllerEvents controls toaster (SDL.ControllerDeviceEvent (SDL.ControllerDeviceEventData SDL.ControllerDeviceRemoved joyId) : t) = do - (c, toaster') <- - if Just joyId == controls.joyId - then do - forM_ (controls.gc) SDL.Raw.gameControllerClose - pure (controls {joyId = Nothing, gc = Nothing}, T.add toaster "Gamepad disconnected") - else pure (controls, toaster) - processControllerEvents c toaster' t -processControllerEvents controls toaster (_ : t) = processControllerEvents controls toaster t -processControllerEvents controls toaster [] = pure (controls, toaster) +processControllerEvents controls toaster ev = + case ev of + (SDL.ControllerDeviceEvent (SDL.ControllerDeviceEventData SDL.ControllerDeviceAdded joyIndex) : t) -> + if isNothing controls.joyId + then do + joyName <- peekCAString =<< SDL.Raw.gameControllerNameForIndex (fromIntegral joyIndex) + let toaster' = T.add toaster $ "Connected " ++ show joyName + gc <- SDL.Raw.gameControllerOpen (fromIntegral joyIndex) + joy <- SDL.Raw.gameControllerGetJoystick gc + joyId <- SDL.Raw.joystickInstanceID joy + processControllerEvents controls {joyId = Just joyId, gc = Just gc} toaster' t + else processControllerEvents controls toaster t + (SDL.ControllerDeviceEvent (SDL.ControllerDeviceEventData SDL.ControllerDeviceRemoved joyId) : t) -> do + (c, toaster') <- + if Just joyId == controls.joyId + then do + forM_ controls.gc SDL.Raw.gameControllerClose + pure (controls {joyId = Nothing, gc = Nothing}, T.add toaster "Gamepad disconnected") + else pure (controls, toaster) + processControllerEvents c toaster' t + (_ : t) -> processControllerEvents controls toaster t + [] -> pure (controls, toaster) init :: Controls init = Controls False False False False False False False Nothing Nothing -- cgit v1.2.3