aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Controller.hs
blob: b5cd6854820440e96a9c1ea92a7fcdce88bcac4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Game.Controller (Controls (..), init, update) where

import Data.Maybe (fromMaybe, isNothing)
import Data.Vector ((!?))
import Game.Utils (isPressed, isPressedGamepad)
import qualified SDL
import qualified SDL.Input.GameController as SDL
import qualified SDL.Raw
import Prelude hiding (init)

data Controls = Controls
  { up :: Bool,
    down :: Bool,
    left :: Bool,
    right :: Bool,
    a :: Bool,
    b :: Bool,
    menu :: Bool,
    joy :: Maybe SDL.Raw.Joystick
  }
  deriving (Show)

getJoystick :: IO (Maybe SDL.Raw.Joystick)
getJoystick = do
  joys <- SDL.availableJoysticks
  case joys !? 0 of
    Nothing -> pure Nothing
    Just x ->
      Just <$> do
        -- XXX: are we sure this is a gamepad?
        putStrLn ("gamepad: " ++ show x)
        let joyId = SDL.joystickDeviceId x
        g <- SDL.Raw.gameControllerOpen joyId
        SDL.Raw.gameControllerGetJoystick g

init :: IO Controls
init = do
  Controls False False False False False False False <$> getJoystick

updateGamepad :: [SDL.EventPayload] -> Controls -> Controls
updateGamepad events controls
  | isNothing $ controls.joy = controls
  -- XXX: deal with disconnection/reconnection
  | otherwise =
      controls
        { up = fromMaybe controls.up $ isPressedGamepad SDL.ControllerButtonDpadUp events,
          down = fromMaybe controls.down $ isPressedGamepad SDL.ControllerButtonDpadDown events,
          left = fromMaybe controls.left $ isPressedGamepad SDL.ControllerButtonDpadLeft events,
          right = fromMaybe controls.right $ isPressedGamepad SDL.ControllerButtonDpadRight events,
          a = fromMaybe controls.a $ isPressedGamepad SDL.ControllerButtonA events,
          b = fromMaybe controls.b $ isPressedGamepad SDL.ControllerButtonB events,
          menu = fromMaybe controls.menu $ isPressedGamepad SDL.ControllerButtonStart events
        }

updateKeyboard :: [SDL.EventPayload] -> Controls -> Controls
updateKeyboard events controls =
  controls
    { up = fromMaybe controls.up $ isPressed SDL.KeycodeUp events,
      down = fromMaybe controls.down $ isPressed SDL.KeycodeDown events,
      left = fromMaybe controls.left $ isPressed SDL.KeycodeLeft events,
      right = fromMaybe controls.right $ isPressed SDL.KeycodeRight events,
      a = fromMaybe controls.a $ isPressed SDL.KeycodeZ events,
      b = fromMaybe controls.b $ isPressed SDL.KeycodeX events,
      menu = fromMaybe controls.menu $ isPressed SDL.KeycodeReturn events
    }

update :: [SDL.EventPayload] -> Controls -> Controls
update events controls = do
  updateGamepad events $ updateKeyboard events controls