Commit towards merge

This commit is contained in:
2022-03-20 17:18:12 +00:00
parent 6c3e335ded
commit 5e897c6a6c
10 changed files with 301 additions and 98 deletions
+32 -12
View File
@@ -1,6 +1,7 @@
module Dodge.Event.Menu
( handlePressedKeyInMenu
)
, handleTextInMenu
)
where
import Dodge.Data
import Dodge.Debug.Terminal
@@ -11,25 +12,44 @@ import Data.Maybe
import Control.Monad
--import Control.Lens
import SDL
import Control.Lens ((%~))
import qualified Debug.Trace
handleTextInMenu :: ScreenLayer -> [Char] -> Universe -> IO (Maybe Universe)
handleTextInMenu mState text = case mState of
InputScreen s help-> return . (popScreen' >=> pushScreen' (InputScreen new_text help))
where
new_text
-- Text events occur after key events so ignore the first
-- (key that opened the terminal menu)
| s == "" = ">"
| otherwise = s ++ text
_ -> return . Just
handlePressedKeyInMenu :: ScreenLayer -> Scancode -> Universe -> IO (Maybe Universe)
handlePressedKeyInMenu mState scode = case mState of
OptionScreen { _scOptions = mos, _scDefaultEff = defeff}
OptionScreen { _scOptions = mos, _scDefaultEff = defeff}
-> optionListToEffects mos defeff scode
DisplayScreen {} -> popScreen
ColumnsScreen {} -> popScreen
WaitScreen {} -> return . Just
InputScreen s -> case scode of
InputScreen s help -> case scode of
ScancodeEscape -> popScreen
ScancodeReturn -> popScreen . applyTerminalString s
ScancodeBackspace
-> return . (popScreen' >=> pushScreen' (InputScreen $ dropLast s))
_ -> return . (popScreen' >=> pushScreen' (InputScreen $ s ++ [scodeToChar scode]))
where
-- Remove the menu layer (we readd it in applyTerminalString if the commmand fails)
ScancodeReturn -> return. Just . applyTerminalString (tail s) . (menuLayers %~ tail)
ScancodeTab -> autoCompleteTerminal s help
ScancodeBackspace
-> return . (popScreen' >=> pushScreen' (InputScreen (dropLast s) "Enter command"))
-- Ignore (text input now handled by handleText)
_ -> return . (popScreen' >=> pushScreen' (InputScreen s help))
where
dropLast [x] = ['>']
dropLast (x:xs) = init (x:xs)
dropLast _ = []
dropLast _ = ['>']
optionListToEffects :: [MenuOption] -> (Universe -> IO (Maybe Universe)) -> Scancode
optionListToEffects :: [MenuOption] -> (Universe -> IO (Maybe Universe)) -> Scancode
-> Universe -> IO (Maybe Universe)
optionListToEffects mos defaulteff sc = fromMaybe defaulteff .
lookup sc $ concatMap menuOptionToEffects mos
@@ -37,9 +57,9 @@ optionListToEffects mos defaulteff sc = fromMaybe defaulteff .
menuOptionToEffects :: MenuOption -> [(Scancode,Universe -> IO (Maybe Universe))]
menuOptionToEffects Toggle {_moKey = k, _moEff = eff} = [(k,eff)]
menuOptionToEffects InvisibleToggle {_moKey = k, _moEff = eff} = [(k,eff)]
menuOptionToEffects Toggle2
menuOptionToEffects Toggle2
{ _moKey1 = k1
, _moEff1 = eff1
, _moKey2 = k2
, _moEff2 = eff2
, _moEff2 = eff2
} = [(k1,eff1) , (k2,eff2)]