diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-02-05 15:55:11 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-02-05 15:55:11 +0000 |
commit | d7a519f1aa04e489630c63318b200e89e164d280 (patch) | |
tree | 56dcd12c831b0ed0b0f00cadc7c0196b650fae30 /src/Game/Utils.hs | |
parent | f8e1b62244f011c971763c1ec60d1ff99184fac7 (diff) | |
download | space-plat-hs-d7a519f1aa04e489630c63318b200e89e164d280.tar.gz space-plat-hs-d7a519f1aa04e489630c63318b200e89e164d280.zip |
Basic gamepad support
Diffstat (limited to 'src/Game/Utils.hs')
-rw-r--r-- | src/Game/Utils.hs | 15 |
1 files changed, 13 insertions, 2 deletions
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 |