Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Proper Keyboard Support #60

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adding Controller Awareness and Keyboard Movement
This patch adds the input handling required for proper keyboard movement, including proper handling for instant left/right switching, should play fairly well.

Modifications to Game.hs are to follow this
ProfessorDey authored Mar 11, 2018
commit 67a1b3b7086759c28ce4eb03e34067353525aca7
40 changes: 32 additions & 8 deletions src/Input.hs
Original file line number Diff line number Diff line change
@@ -60,12 +60,17 @@ import Graphics.UI.Extra.SDL
import Constants

-- * Game controller

data ControllerType = KEY
| MOUSE
| WIIMOTE
| KINECT
-- | Controller info at any given point.
data Controller = Controller
{ controllerPos :: (Double, Double)
, controllerClick :: Bool
, controllerPause :: Bool
, controllerType :: ControlType
, controllerVel :: Double
}

-- | Controller info at any given point, plus a pointer
@@ -105,7 +110,7 @@ initializeInputDevices = do

nr <- newIORef defaultInfo
return $ ControllerRef (nr, dev')
where defaultInfo = Controller (0,0) False False
where defaultInfo = Controller (0,0) False False MOUSE 0

-- | Sense from the controller, providing its current
-- state. This should return a new Controller state
@@ -195,6 +200,8 @@ senseWiimote wmdev controller = do
-- Update state
return (controller { controllerPos = (finX, finY) -- pos'
, controllerClick = isClick
, controllerType = WIIMOTE
, controllerVel = 0
})
#endif

@@ -223,15 +230,29 @@ sdlGetController info =
handleEvent :: Controller -> SDL.Event -> Controller
handleEvent c e =
case e of
MouseMotion x y _ _ -> c { controllerPos = (fromIntegral x, fromIntegral y)}
MouseMotion x y _ _ -> c { controllerPos = (fromIntegral x, fromIntegral y), controllerType = MOUSE, controllerVel = 0 }
-- Click
MouseButtonDown _ _ ButtonLeft -> c { controllerClick = True }
MouseButtonUp _ _ ButtonLeft -> c { controllerClick = False}
KeyDown Keysym { symKey = SDLK_UP } -> c { controllerClick = True }
-- Unclick
MouseButtonUp _ _ ButtonLeft -> c { controllerClick = False }
KeyUp Keysym { symKey = SDLK_UP } -> c { controllerClick = False }
-- Slide Left
KeyDown Keysym { symKey = SDLK_LEFT } -> c { controllerVel = -4, controllerType = KEY }
-- Slide Right
KeyDown Keysym { symKey = SDLK_RIGHT } -> c { controllerVel = 4, controllerType = KEY }
-- Stop Sliding
KeyDown Keysym { symKey = SDLK_DOWN } -> c { controllerVel = 0 }
KeyUp Keysym { symKey = SDLK_LEFT } -> case c of
Controller { controllerVel = -4 } -> c { controllerVel = 0 }
_ -> c
KeyUp Keysym { symKey = SDLK_RIGHT } -> case c of
Controller { controllerVel = 4 } -> c { controllerVel = 0 }
_ -> c
-- Pause
KeyUp Keysym { symKey = SDLK_p } -> c { controllerPause = not (controllerPause c) }
KeyDown Keysym { symKey = SDLK_SPACE } -> c { controllerClick = True }
KeyUp Keysym { symKey = SDLK_SPACE } -> c { controllerClick = False }
_ -> c


-- Kinect

#ifdef kinect
@@ -244,7 +265,10 @@ kinectGetController :: KinectPosRef -> Controller -> IO Controller
kinectGetController kinectPosRef c = do
kinectPos <- readIORef kinectPosRef
c' <- sdlGetController c
let c'' = maybe c' (\p -> c' { controllerPos = p }) kinectPos
let c'' = maybe c' (\p -> c' { controllerPos = p
, controllerType = KINECT
, controllerVel = 0 })
kinectPos
return c''

-- TODO Use these instead of hard-coded values