Allow for ingame terminal input

This commit is contained in:
2022-06-01 15:42:30 +01:00
parent 2cedc1b968
commit 61722e55e8
6 changed files with 45 additions and 36 deletions
+28 -12
View File
@@ -22,18 +22,15 @@ import SDL
--import Data.Text (unpack)
import qualified Data.Text as T
-- annoyingly, the text input event doesn't register backspace, so this has to be
-- dealt with elsewhere
handleTextInput :: T.Text -> Universe -> IO (Maybe Universe)
handleTextInput text = return . Just . -- case mState of
-- InputScreen {} ->
(menuLayers . ix 0 . scInput %~ updateText)
-- _ -> id
handleTextInput text = return . Just . (menuLayers . ix 0 . scInput %~ updateText)
. (uvWorld . hud . hudElement . subInventory . termParams . termInput . _Just . _1 %~ updateText)
where
updateText s = case T.unpack text of
"\b" -> undefined
_ -> s `T.append` text
-- | null (_menuLayers w) = return $ Just w
-- | otherwise = handleTextInMenu (head $ _menuLayers w) text w
updateText s = case T.unpack text of
";" -> s
_ -> s `T.append` T.toUpper text
{- | Handles keyboard press and release.
On release, remove scancode from the 'Set' of pressed keys.
@@ -54,12 +51,14 @@ handlePressedKey _ scode w = case scode of
ScancodeF5 -> return . Just $ doQuicksave w
ScancodeF9 -> return . Just $ loadSaveSlot QuicksaveSlot w
ScancodeSemicolon -> return . Just $ gotoTerminal w
_ | null (_menuLayers w) -> return $ uvWorld (handlePressedKeyInGame scode) w
_ | null (_menuLayers w) -> case w ^? uvWorld . hud . hudElement . subInventory . termParams . termInput . _Just of
Just {} -> return $ uvWorld (handlePressedKeyTerminal scode) w
_ -> return $ uvWorld (handlePressedKeyInGame scode) w
_ -> handlePressedKeyInMenu (head $ _menuLayers w) scode w
handlePressedKeyInGame :: Scancode -> World -> Maybe World
handlePressedKeyInGame scode w = case scode of
ScancodeEscape -> Nothing
ScancodeEscape -> Just $ pauseGame $ escapeMap w
ScancodeSpace -> Just $ spaceAction w
ScancodeP -> Just $ pauseGame $ escapeMap w
ScancodeF -> Just $ youDropItem w
@@ -71,6 +70,23 @@ handlePressedKeyInGame scode w = case scode of
ScancodeI -> Just $ w & hud . hudElement %~ toggleInspectInv
_ -> Just w
handlePressedKeyTerminal :: Scancode -> World -> Maybe World
handlePressedKeyTerminal scode w = case scode of
ScancodeEscape -> Just $ w & hud . hudElement . subInventory .~ NoSubInventory
ScancodeReturn -> Just $ w & doTerminalEffect
ScancodeBackspace -> Just $ w
& hud . hudElement . subInventory . termParams . termInput . _Just . _1 %~ doBackspace
_ -> Just w
where
doBackspace t = case T.unsnoc t of
Nothing -> t
Just (t',_) -> t'
doTerminalEffect w' = fromMaybe w' $ do
f <- w' ^? hud . hudElement . subInventory . termParams . termInput . _Just . _2
s <- w' ^? hud . hudElement . subInventory . termParams . termInput . _Just . _1
return $ f (T.unpack s) w'
toggleTweakInv :: HUDElement -> HUDElement
toggleTweakInv he = case he of
DisplayInventory TweakInventory -> DisplayInventory NoSubInventory