From 7b3aa61462dc5a1135e01beedcbc98efe47ffb13 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sat, 15 Apr 2023 22:59:15 +0100 Subject: "Toaster" notifications To provide feedback to the user when a controller is plugged/unplugged. --- src/Game/Controller.hs | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'src/Game/Controller.hs') diff --git a/src/Game/Controller.hs b/src/Game/Controller.hs index d47963e..4d0ba81 100644 --- a/src/Game/Controller.hs +++ b/src/Game/Controller.hs @@ -4,6 +4,7 @@ import Control.Monad import Data.Int (Int32) import Data.Maybe (fromMaybe, isNothing) import Foreign.C (peekCAString) +import qualified Game.Toaster as T import qualified SDL import qualified SDL.Input.GameController as SDL import qualified SDL.Raw @@ -22,28 +23,27 @@ data Controls = Controls } deriving (Show) -processControllerEvents :: Controls -> [SDL.EventPayload] -> IO Controls -processControllerEvents controls (SDL.ControllerDeviceEvent (SDL.ControllerDeviceEventData SDL.ControllerDeviceAdded joyIndex) : t) = do +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) - putStrLn $ "Connected gamepad: " ++ show joyName + 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} t - else processControllerEvents controls t -processControllerEvents controls (SDL.ControllerDeviceEvent (SDL.ControllerDeviceEventData SDL.ControllerDeviceRemoved joyId) : t) = do - c <- + 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 - putStrLn "Disconnected gamepad" - pure controls {joyId = Nothing, gc = Nothing} - else pure controls - processControllerEvents c t -processControllerEvents controls (_ : t) = processControllerEvents controls t -processControllerEvents controls [] = pure controls + 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) init :: Controls init = Controls False False False False False False False Nothing Nothing @@ -78,12 +78,15 @@ updateKeyboard controls events = menu = fromMaybe False $ isPressed SDL.KeycodeReturn events } -update :: Controls -> [SDL.EventPayload] -> IO Controls -update controls events = do - updated <- processControllerEvents controls events - pure $ case updated.joyId of - Just _ -> updateGamepad updated events - _ -> updateKeyboard updated events +update :: Controls -> T.Toaster -> [SDL.EventPayload] -> IO (Controls, T.Toaster) +update controls toaster events = do + (updated, toaster') <- processControllerEvents controls toaster events + pure + ( case updated.joyId of + Just _ -> updateGamepad updated events + _ -> updateKeyboard updated events, + toaster' + ) isPressed :: SDL.Keycode -> [SDL.EventPayload] -> Maybe Bool isPressed code events -- cgit v1.2.3