Move towards inputs in terminal

This commit is contained in:
2022-06-01 14:34:16 +01:00
parent b54864bbda
commit 2cedc1b968
13 changed files with 90 additions and 64 deletions
+15 -9
View File
@@ -1,7 +1,7 @@
{- | Deals with keyboard events. -}
module Dodge.Event.Keyboard
( handleKeyboardEvent
, handleTextInputEvent
, handleTextInput
) where
import qualified Data.IntMap.Strict as IM
import Dodge.Base
@@ -19,15 +19,21 @@ import LensHelp
import Data.Maybe
import qualified Data.Set as S
import SDL
import Data.Text (unpack)
--import Data.Text (unpack)
import qualified Data.Text as T
handleTextInputEvent :: TextInputEventData -> Universe -> IO (Maybe Universe)
handleTextInputEvent = handleTextInput . unpack . textInputEventText
handleTextInput :: T.Text -> Universe -> IO (Maybe Universe)
handleTextInput text = return . Just . -- case mState of
-- InputScreen {} ->
(menuLayers . ix 0 . scInput %~ updateText)
-- _ -> id
where
updateText s = case T.unpack text of
"\b" -> undefined
_ -> s `T.append` text
handleTextInput :: String -> Universe -> IO (Maybe Universe)
handleTextInput text w
| null (_menuLayers w) = return $ Just w
| otherwise = handleTextInMenu (head $ _menuLayers w) text w
-- | null (_menuLayers w) = return $ Just w
-- | otherwise = handleTextInMenu (head $ _menuLayers w) text w
{- | Handles keyboard press and release.
On release, remove scancode from the 'Set' of pressed keys.
@@ -78,7 +84,7 @@ toggleInspectInv he = case he of
gotoTerminal :: Universe -> Universe
gotoTerminal w = case _menuLayers w of
(InputScreen {} : _) -> w
_ -> w & menuLayers .:~ InputScreen [] "Enter command"
_ -> w & menuLayers .:~ InputScreen T.empty "Enter command"
spaceAction :: World -> World
spaceAction w = case _hudElement $ _hud w of
+18 -15
View File
@@ -1,6 +1,6 @@
module Dodge.Event.Menu
( handlePressedKeyInMenu
, handleTextInMenu
-- , handleTextInMenu
) where
import Dodge.Data
import Dodge.Debug.Terminal
@@ -9,17 +9,20 @@ import Dodge.Menu.PushPop
import Data.Maybe
--import Control.Monad
import Control.Lens
--import Control.Lens
import SDL
--import qualified Debug.Trace
import qualified Data.Text as T
handleTextInMenu :: ScreenLayer -> [Char] -> Universe -> IO (Maybe Universe)
handleTextInMenu mState text = return . Just . case mState of
InputScreen {} -> menuLayers . ix 0 . scInput %~ updateText
_ -> id
where
updateText "" = ">"
updateText s = s ++ text
--handleTextInMenu :: ScreenLayer -> T.Text -> Universe -> IO (Maybe Universe)
--handleTextInMenu mState text = return . Just . -- case mState of
---- InputScreen {} ->
-- (menuLayers . ix 0 . scInput %~ updateText)
---- _ -> id
-- where
-- updateText s = case T.unpack text of
-- "\b" -> undefined
-- _ -> s `T.append` text
handlePressedKeyInMenu :: ScreenLayer -> Scancode -> Universe -> IO (Maybe Universe)
handlePressedKeyInMenu mState scode = case mState of
@@ -30,15 +33,15 @@ handlePressedKeyInMenu mState scode = case mState of
WaitScreen {} -> return . Just
InputScreen s help -> case scode of
ScancodeEscape -> popScreen
ScancodeReturn -> popScreen . applyTerminalString (tail s)
ScancodeTab -> autoCompleteTerminal s help
ScancodeBackspace -> return . Just . (menuLayers . ix 0 . scInput %~ doBackspace)
ScancodeReturn -> popScreen . applyTerminalString (T.unpack s)
ScancodeTab -> autoCompleteTerminal (T.unpack s) help
-- ScancodeBackspace -> return . Just . (menuLayers . ix 0 . scInput %~ doBackspace)
-- text input handled by handleText
_ -> return . Just
where
doBackspace [_] = ['>']
doBackspace (x:xs) = init (x:xs)
doBackspace _ = ['>']
-- doBackspace [_] = ['>']
-- doBackspace (x:xs) = init (x:xs)
-- doBackspace _ = ['>']
optionListToEffects
:: (Universe -> IO (Maybe Universe))