From d7a519f1aa04e489630c63318b200e89e164d280 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 5 Feb 2023 15:55:11 +0000 Subject: Basic gamepad support --- src/Game/Utils.hs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/Game/Utils.hs') diff --git a/src/Game/Utils.hs b/src/Game/Utils.hs index acfab6f..e2b320a 100644 --- a/src/Game/Utils.hs +++ b/src/Game/Utils.hs @@ -1,7 +1,8 @@ -module Game.Utils (rect, isPressed) where +module Game.Utils (rect, isPressed, isPressedGamepad) where import Foreign.C.Types (CInt) import qualified SDL +import qualified SDL.Input.GameController as SDL import SDL.Vect (V2 (..)) rect :: Int -> Int -> Int -> Int -> SDL.Rectangle CInt @@ -9,7 +10,6 @@ rect x y w h = SDL.Rectangle (SDL.P $ V2 (fromIntegral x) (fromIntegral y)) (V2 isPressed :: SDL.Keycode -> [SDL.EventPayload] -> Maybe Bool isPressed code events - -- TODO: gamepad support | any (isEventKey SDL.Pressed code) events = Just True | any (isEventKey SDL.Released code) events = Just False | otherwise = Nothing @@ -17,3 +17,14 @@ isPressed code events isEventKey :: SDL.InputMotion -> SDL.Keycode -> SDL.EventPayload -> Bool isEventKey expected keycode (SDL.KeyboardEvent (SDL.KeyboardEventData _ motion False ksym)) = expected == motion && SDL.keysymKeycode ksym == keycode isEventKey _ _ _ = False + +isPressedGamepad :: SDL.ControllerButton -> [SDL.EventPayload] -> Maybe Bool +isPressedGamepad button events + | any (isEventButton SDL.ControllerButtonPressed button) events = Just True + | any (isEventButton SDL.ControllerButtonReleased button) events = Just False + | otherwise = Nothing + where + isEventButton :: SDL.ControllerButtonState -> SDL.ControllerButton -> SDL.EventPayload -> Bool + -- FIXME: may be don't hardcode it to joystick 0 + isEventButton expected bu (SDL.ControllerButtonEvent (SDL.ControllerButtonEventData 0 b state)) = expected == state && bu == b + isEventButton _ _ _ = False -- cgit v1.2.3