Push universe into events

This commit is contained in:
2021-11-28 12:29:57 +00:00
parent 98e0ea971a
commit 0723def26a
2 changed files with 35 additions and 37 deletions
+29 -33
View File
@@ -8,8 +8,7 @@ We cannot handle multiple keys held down at once here,
(eg left mouse button + right mouse button) (eg left mouse button + right mouse button)
because these are separate events. because these are separate events.
Instead we store the events in a set, and deal with the combinations in Instead we store the events in a set, and deal with the combinations in
"Dodge.Update". "Dodge.Update". -}
-}
module Dodge.Event module Dodge.Event
( handleEvent ( handleEvent
) where ) where
@@ -35,13 +34,13 @@ import qualified Data.Set as S
import SDL import SDL
handleEvent :: Event -> Universe -> Maybe Universe --handleEvent :: Event -> Universe -> Maybe Universe
handleEvent e uv = case handleEvent' e (_uvWorld uv) of --handleEvent e uv = case handleEvent' e (_uvWorld uv) of
Nothing -> Nothing -- Nothing -> Nothing
Just w -> Just $ uv & uvWorld .~ w -- Just w -> Just $ uv & uvWorld .~ w
handleEvent' :: Event -> World -> Maybe World handleEvent :: Event -> Universe -> Maybe Universe
handleEvent' e = case eventPayload e of handleEvent e = case eventPayload e of
KeyboardEvent kev -> handleKeyboardEvent kev KeyboardEvent kev -> handleKeyboardEvent kev
MouseMotionEvent mmev -> handleMouseMotionEvent mmev MouseMotionEvent mmev -> handleMouseMotionEvent mmev
MouseButtonEvent mbev -> handleMouseButtonEvent mbev MouseButtonEvent mbev -> handleMouseButtonEvent mbev
@@ -50,40 +49,39 @@ handleEvent' e = case eventPayload e of
WindowMovedEvent mev -> handleWindowMoveEvent mev WindowMovedEvent mev -> handleWindowMoveEvent mev
_ -> Just _ -> Just
handleMouseMotionEvent :: MouseMotionEventData -> World -> Maybe World handleMouseMotionEvent :: MouseMotionEventData -> Universe -> Maybe Universe
handleMouseMotionEvent mmev w = Just $ w & mousePos .~ V2 handleMouseMotionEvent mmev u = Just $ u & uvWorld . mousePos .~ V2
(fromIntegral x - 0.5*getWindowX w) (fromIntegral x - 0.5*getWindowX w)
(0.5*getWindowY w - fromIntegral y) (0.5*getWindowY w - fromIntegral y)
where where
w = _uvWorld u
P (V2 x y) = mouseMotionEventPos mmev P (V2 x y) = mouseMotionEventPos mmev
handleMouseButtonEvent :: MouseButtonEventData -> World -> Maybe World handleMouseButtonEvent :: MouseButtonEventData -> Universe -> Maybe Universe
handleMouseButtonEvent mbev w = case mouseButtonEventMotion mbev of handleMouseButtonEvent mbev u = case mouseButtonEventMotion mbev of
Released -> Just $ over mouseButtons (S.delete but) w Released -> Just $ u & uvWorld . mouseButtons %~ S.delete but
Pressed -> handlePressedMouseButton but $ over mouseButtons (S.insert but) w Pressed -> uvWorld
(handlePressedMouseButton but)
(u & uvWorld . mouseButtons %~ S.insert but)
where where
but = mouseButtonEventButton mbev but = mouseButtonEventButton mbev
{- | Sets window position in config. -} {- | Sets window position in config. -}
handleWindowMoveEvent :: WindowMovedEventData -> World -> Maybe World handleWindowMoveEvent :: WindowMovedEventData -> Universe -> Maybe Universe
handleWindowMoveEvent mev w = Just $ w handleWindowMoveEvent mev u = Just $ u
& config . windowPosX .~ fromIntegral x & uvWorld . config . windowPosX .~ fromIntegral x
& config . windowPosY .~ fromIntegral y & uvWorld . config . windowPosY .~ fromIntegral y
where where
P (V2 x y) = windowMovedEventPosition mev P (V2 x y) = windowMovedEventPosition mev
{- | Resets the world window size, and resizes the fbo that gets the light map drawn into it. -} {- | Resets the world window size, and resizes the fbo that gets the light map drawn into it. -}
handleResizeEvent :: WindowSizeChangedEventData -> World -> Maybe World handleResizeEvent :: WindowSizeChangedEventData -> Universe -> Maybe Universe
handleResizeEvent sev w = Just handleResizeEvent sev u = Just $ u
. set (config . windowX) (fromIntegral x) & uvWorld . config . windowX .~ fromIntegral x
. set (config . windowY) (fromIntegral y) & uvWorld . config . windowY .~ fromIntegral y
$ over sideEffects (sideEffectUpdatePreload divRes x y) & uvWorld . sideEffects %~ sideEffectUpdatePreload divRes x y
w
where where
-- up :: (World -> IO World) -> World -> IO World w = _uvWorld u
-- up f w' = do
-- pdata <- pdataResizeUpdate (x `div` divRes) (y `div` divRes) x y (_preloadData w')
-- f $ w' & preloadData .~ pdata
x = fromIntegral x' x = fromIntegral x'
y = fromIntegral y' y = fromIntegral y'
V2 x' y' = windowSizeChangedEventSize sev V2 x' y' = windowSizeChangedEventSize sev
@@ -95,10 +93,10 @@ handlePressedMouseButton but w
= Just $ w & clickMousePos .~ _mousePos w = Just $ w & clickMousePos .~ _mousePos w
| otherwise = Just w | otherwise = Just w
handleMouseWheelEvent :: MouseWheelEventData -> World -> Maybe World handleMouseWheelEvent :: MouseWheelEventData -> Universe -> Maybe Universe
handleMouseWheelEvent mwev w = case _menuLayers w of handleMouseWheelEvent mwev w = case _menuLayers (_uvWorld w) of
[] -> case mouseWheelEventPos mwev of [] -> case mouseWheelEventPos mwev of
V2 _ y -> Just $ wheelEvent (fromIntegral y) w V2 _ y -> Just $ w & uvWorld %~ wheelEvent (fromIntegral y)
_ -> Just w _ -> Just w
wheelEvent :: Float -> World -> World wheelEvent :: Float -> World -> World
@@ -138,5 +136,3 @@ moveYourAmmoParam i w = case yourItem w ^? wpAmmo . aoType. amPjParams . ix para
_ -> w _ -> w
where where
paramid = _amParamSel $ _aoType $ _wpAmmo $ yourItem w paramid = _amParamSel $ _aoType $ _wpAmmo $ yourItem w
+6 -4
View File
@@ -23,10 +23,12 @@ On release, remove scancode from the 'Set' of pressed keys.
On press, adds the scancode, and perhaps applies a direct effect: On press, adds the scancode, and perhaps applies a direct effect:
see 'handlePressedKeyInGame'. see 'handlePressedKeyInGame'.
-} -}
handleKeyboardEvent :: KeyboardEventData -> World -> Maybe World handleKeyboardEvent :: KeyboardEventData -> Universe -> Maybe Universe
handleKeyboardEvent kev w = case keyboardEventKeyMotion kev of handleKeyboardEvent kev u = case keyboardEventKeyMotion kev of
Released -> Just $ w & keys %~ S.delete kcode Released -> Just $ u & uvWorld . keys %~ S.delete kcode
Pressed -> handlePressedKey (keyboardEventRepeat kev) kcode (w & keys %~ S.insert kcode) Pressed -> uvWorld
(handlePressedKey (keyboardEventRepeat kev) kcode)
(u & uvWorld . keys %~ S.insert kcode)
where where
kcode = (keysymScancode . keyboardEventKeysym) kev kcode = (keysymScancode . keyboardEventKeysym) kev