diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-10-17 18:52:19 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-10-17 18:52:19 +0100 |
commit | aaba06efd87df63efac68b5e9feb5102bbc1ee28 (patch) | |
tree | f2d3db9c0fad4263d93de073584e9b883401765b /src | |
parent | 8c1c6a328fa5578449a49ef85cf6d433da6704ca (diff) | |
download | space-plat-hs-aaba06efd87df63efac68b5e9feb5102bbc1ee28.tar.gz space-plat-hs-aaba06efd87df63efac68b5e9feb5102bbc1ee28.zip |
Cleaner, more readable code
Diffstat (limited to 'src')
-rw-r--r-- | src/Game/Controller.hs | 42 |
1 files changed, 22 insertions, 20 deletions
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 |