Allow for events to do io

This commit is contained in:
2021-11-29 01:02:15 +00:00
parent 8832a73d86
commit 3a605b8156
13 changed files with 127 additions and 142 deletions
+8 -8
View File
@@ -22,21 +22,21 @@ On release, remove scancode from the 'Set' of pressed keys.
On press, adds the scancode, and perhaps applies a direct effect:
see 'handlePressedKeyInGame'.
-}
handleKeyboardEvent :: KeyboardEventData -> Universe -> Maybe Universe
handleKeyboardEvent :: KeyboardEventData -> Universe -> IO (Maybe Universe)
handleKeyboardEvent kev u = case keyboardEventKeyMotion kev of
Released -> Just $ u & uvWorld . keys %~ S.delete kcode
Released -> return . Just $ u & uvWorld . keys %~ S.delete kcode
Pressed -> handlePressedKey (keyboardEventRepeat kev) kcode
(u & uvWorld . keys %~ S.insert kcode)
where
kcode = (keysymScancode . keyboardEventKeysym) kev
handlePressedKey :: Bool -> Scancode -> Universe -> Maybe Universe
handlePressedKey True _ w = Just w
handlePressedKey _ ScancodeF5 w = Just $ doQuicksave w
handlePressedKey _ ScancodeF9 w = Just $ loadSaveSlot QuicksaveSlot w
handlePressedKey _ ScancodeSemicolon w = Just $ gotoTerminal w
handlePressedKey :: Bool -> Scancode -> Universe -> IO (Maybe Universe)
handlePressedKey True _ w = return $ Just w
handlePressedKey _ ScancodeF5 w = return . Just $ doQuicksave w
handlePressedKey _ ScancodeF9 w = return . Just $ loadSaveSlot QuicksaveSlot w
handlePressedKey _ ScancodeSemicolon w = return . Just $ gotoTerminal w
handlePressedKey _ scode w
| null (_menuLayers w) = uvWorld (handlePressedKeyInGame scode) w
| null (_menuLayers w) = return $ uvWorld (handlePressedKeyInGame scode) w
| otherwise = handlePressedKeyInMenu (head $ _menuLayers w) scode w
handlePressedKeyInGame :: Scancode -> World -> Maybe World
+13 -12
View File
@@ -7,30 +7,31 @@ import Dodge.Debug.Terminal
import Dodge.Menu
import Control.Monad
import Control.Lens
--import Control.Lens
import SDL
handlePressedKeyInMenu :: ScreenLayer -> Scancode -> Universe -> Maybe Universe
handlePressedKeyInMenu :: ScreenLayer -> Scancode -> Universe -> IO (Maybe Universe)
handlePressedKeyInMenu mState scode = case mState of
OptionScreen { _scOptions = mos, _scDefaultEff = defeff}
-> optionListToEffects mos defeff scode
DisplayScreen {} -> popScreen
ColumnsScreen {} -> popScreen
WaitScreen {} -> Just
DisplayScreen {} -> return . popScreen
ColumnsScreen {} -> return . popScreen
WaitScreen {} -> return . Just
InputScreen s -> case scode of
ScancodeEscape -> popScreen
ScancodeReturn -> popScreen . applyTerminalString s
ScancodeEscape -> return . popScreen
ScancodeReturn -> return . popScreen . applyTerminalString s
ScancodeBackspace
-> popScreen >=> pushScreen (InputScreen $ dropLast s)
_ -> popScreen >=> pushScreen (InputScreen $ s ++ [scodeToChar scode])
-> return . (popScreen >=> pushScreen (InputScreen $ dropLast s))
_ -> return . (popScreen >=> pushScreen (InputScreen $ s ++ [scodeToChar scode]))
where
dropLast (x:xs) = init (x:xs)
dropLast _ = []
optionListToEffects :: [MenuOption] -> (Universe -> Maybe Universe) -> Scancode
-> Universe -> Maybe Universe
optionListToEffects :: [MenuOption] -> (Universe -> IO (Maybe Universe)) -> Scancode
-> Universe -> IO (Maybe Universe)
optionListToEffects mos defaulteff sc w = case lookup sc listEffects of
Nothing -> defaulteff w
Just eff -> eff w
Just eff -> return $ eff w
where
listEffects = concatMap menuOptionToEffects mos