Files
loop/src/Dodge/Event/Input.hs
T

52 lines
1.7 KiB
Haskell

-- | Deals with events that only affect Input.
module Dodge.Event.Input (
handleKeyboardEvent,
handleTextInput,
handleMouseMotionEvent,
handleMouseButtonEvent,
handleMouseWheelEvent,
) where
import qualified Data.Text as T
import Dodge.Data.Input
import Dodge.Data.Config
import LensHelp
import SDL
-- annoyingly, the text input event doesn't register backspace, so deletion has to be
-- dealt with separately
handleTextInput :: T.Text -> Input -> Input
handleTextInput text = textInput %~ (`T.append` text)
{- | Handles keyboard press and release.
-}
handleKeyboardEvent :: KeyboardEventData -> Input -> Input
handleKeyboardEvent kev = case keyboardEventKeyMotion kev of
Released -> pressedKeys . at scode .~ Nothing
Pressed -> pressedKeys . at scode ?~ val
where
val | keyboardEventRepeat kev = LongPress
| otherwise = InitialPress
scode = (keysymScancode . keyboardEventKeysym) kev
handleMouseMotionEvent :: MouseMotionEventData -> Configuration -> Input -> Input
handleMouseMotionEvent mmev cfig inp =
inp & mousePos
.~ V2
(fromIntegral x - 0.5 * _windowX cfig)
(0.5 * _windowY cfig - fromIntegral y)
where
P (V2 x y) = mouseMotionEventPos mmev
handleMouseWheelEvent :: MouseWheelEventData -> Input -> Input
handleMouseWheelEvent mwev = scrollAmount +~ fromIntegral y
where
V2 _ y = mouseWheelEventPos mwev
handleMouseButtonEvent :: MouseButtonEventData -> Input -> Input
handleMouseButtonEvent mbev = case mouseButtonEventMotion mbev of
Released -> mouseButtons . at thebutton .~ Nothing
Pressed -> mouseButtons . at thebutton ?~ False
where
thebutton = mouseButtonEventButton mbev