Unify backspace in regex code

This commit is contained in:
2024-11-26 17:33:08 +00:00
parent dc5c62e8ff
commit 94acc5566c
2 changed files with 56 additions and 53 deletions
+42 -25
View File
@@ -7,11 +7,11 @@ module Dodge.DisplayInventory (
updateCombinePositioning, updateCombinePositioning,
) where ) where
import Data.IntMap.Merge.Strict
import Control.Applicative import Control.Applicative
import Control.Lens import Control.Lens
import Control.Monad import Control.Monad
import Data.Bifunctor import Data.Bifunctor
import Data.IntMap.Merge.Strict
import qualified Data.IntMap.Strict as IM import qualified Data.IntMap.Strict as IM
import Data.Maybe import Data.Maybe
import Data.Monoid import Data.Monoid
@@ -33,12 +33,14 @@ import qualified ListHelp as List
import Picture.Base import Picture.Base
toggleCombineInv :: Universe -> Universe toggleCombineInv :: Universe -> Universe
toggleCombineInv uv = uv & case uv ^? uvWorld . hud . hudElement . subInventory of toggleCombineInv uv =
uv & case uv ^? uvWorld . hud . hudElement . subInventory of
Just CombineInventory{} -> uvWorld . hud . hudElement . subInventory .~ NoSubInventory Just CombineInventory{} -> uvWorld . hud . hudElement . subInventory .~ NoSubInventory
_ -> uvWorld %~ enterCombineInv (uv ^. uvConfig) _ -> uvWorld %~ enterCombineInv (uv ^. uvConfig)
updateCombinePositioning :: Universe -> Universe updateCombinePositioning :: Universe -> Universe
updateCombinePositioning u = u updateCombinePositioning u =
u
& uvWorld . hud . hudElement . subInventory . ciSections & uvWorld . hud . hudElement . subInventory . ciSections
%~ updateCombineSections (_uvWorld u) (_uvConfig u) %~ updateCombineSections (_uvWorld u) (_uvConfig u)
& checkCombineSelectionExists & checkCombineSelectionExists
@@ -52,7 +54,7 @@ updateCombineSections w cfig =
updateSectionsPositioning updateSectionsPositioning
(const 5) (const 5)
(const 0) (const 0)
(fmap (\(x,y,_) -> (x,y)) $ w ^? hud . hudElement . subInventory . ciSelection . _Just) (fmap (\(x, y, _) -> (x, y)) $ w ^? hud . hudElement . subInventory . ciSelection . _Just)
(getAvailableListLines secondColumnParams cfig) (getAvailableListLines secondColumnParams cfig)
[(0, sclose), (-1, sfclose)] [(0, sclose), (-1, sfclose)]
where where
@@ -90,14 +92,15 @@ updateInventoryPositioning u =
%~ updateDisplaySections (_uvWorld u) (_uvConfig u) %~ updateDisplaySections (_uvWorld u) (_uvConfig u)
& checkInventorySelectionExists & checkInventorySelectionExists
-- this is possibly not completely correct
checkInventorySelectionExists :: Universe -> Universe checkInventorySelectionExists :: Universe -> Universe
checkInventorySelectionExists u = case u ^? uvWorld . hud . hudElement . diSections . ix i . ssItems . ix j of checkInventorySelectionExists u =
case u ^? uvWorld . hud . hudElement . diSections . ix i . ssItems . ix j of
Nothing -> u & uvWorld %~ scrollAugNextInSection Nothing -> u & uvWorld %~ scrollAugNextInSection
_ -> u _ -> u
where where
(i,j,_) = fromMaybe (0,-1,mempty) (i, j, _) =
$ u ^? uvWorld . hud . hudElement . diSelection . _Just fromMaybe (1, -1, mempty) $
u ^? uvWorld . hud . hudElement . diSelection . _Just
checkCombineSelectionExists :: Universe -> Universe checkCombineSelectionExists :: Universe -> Universe
checkCombineSelectionExists u = fromMaybe u $ do checkCombineSelectionExists u = fromMaybe u $ do
@@ -107,7 +110,7 @@ checkCombineSelectionExists u = fromMaybe u $ do
<|> Just (0, 0, mempty) <|> Just (0, 0, mempty)
Just $ case u ^? uvWorld . hud . hudElement . subInventory . ciSections . ix i . ssItems . ix j of Just $ case u ^? uvWorld . hud . hudElement . subInventory . ciSections . ix i . ssItems . ix j of
Nothing -> Nothing ->
u & uvWorld . hud . hudElement . subInventory . ciSelection ?~ (0, -1,mempty) u & uvWorld . hud . hudElement . subInventory . ciSelection ?~ (0, -1, mempty)
& uvWorld . hud . hudElement . subInventory . ciSelection & uvWorld . hud . hudElement . subInventory . ciSelection
%~ scrollSelectionSections (-1) sss %~ scrollSelectionSections (-1) sss
_ -> u _ -> u
@@ -135,10 +138,18 @@ updateDisplaySections w cfig =
displayIndents displayIndents
mselpos mselpos
(getAvailableListLines (invDisplayParams w) cfig) (getAvailableListLines (invDisplayParams w) cfig)
[statushead,statusdisplay,invhead,invx,youx, nearbyhead, closex [ statushead
,interfaceshead,interfaces] , statusdisplay
, invhead
, invx
, youx
, nearbyhead
, closex
, interfaceshead
, interfaces
]
where where
mselpos = fmap (\(x,y,_) -> (x,y)) $ w ^? hud . hudElement . diSelection . _Just mselpos = fmap (\(x, y, _) -> (x, y)) $ w ^? hud . hudElement . diSelection . _Just
invfiltcurs = mselpos ^? _Just . _1 == Just (-1) invfiltcurs = mselpos ^? _Just . _1 == Just (-1)
(sfinv, sinv) = (sfinv, sinv) =
filterSectionsPair invfiltcurs plainRegex invitems "INVENTORY" $ filterSectionsPair invfiltcurs plainRegex invitems "INVENTORY" $
@@ -149,18 +160,19 @@ updateDisplaySections w cfig =
filterSectionsPair closefiltcurs plainRegex closeitms "NEARBY ITEMS" $ filterSectionsPair closefiltcurs plainRegex closeitms "NEARBY ITEMS" $
w ^? hud . hudElement . diCloseFilter . _Just w ^? hud . hudElement . diCloseFilter . _Just
(filtclose, closex) = ((2, sfclose), (3, sclose)) (filtclose, closex) = ((2, sfclose), (3, sclose))
nearbyhead = if null sfclose nearbyhead =
then if null sclose then (2,mempty) else makehead "NEARBY ITEMS" 2 if null sfclose
then if null sclose then (2, mempty) else makehead "NEARBY ITEMS" 2
else filtclose else filtclose
youx = (1, youitems) youx = (1, youitems)
interfaceshead = if null btitems then (4,mempty) else makehead "NEARBY INTERFACES" 4 interfaceshead = if null btitems then (4, mempty) else makehead "NEARBY INTERFACES" 4
interfaces = (5,btitems) interfaces = (5, btitems)
btitems = btitems =
IM.fromDistinctAscList . zip [0 ..] $ IM.fromDistinctAscList . zip [0 ..] $
mapMaybe (closeButtonToSelectionItem w) (w ^. hud . closeButtons) mapMaybe (closeButtonToSelectionItem w) (w ^. hud . closeButtons)
makehead str i = (i,IM.singleton 0 $ SelectionInfo [str] 1 False white 0) makehead str i = (i, IM.singleton 0 $ SelectionInfo [str] 1 False white 0)
statushead = (-4,IM.singleton 0 $ SelectionInfo ["STATUS"] 1 False white 0) statushead = (-4, IM.singleton 0 $ SelectionInfo ["STATUS"] 1 False white 0)
statusdisplay = (-3,IM.singleton 0 $ SelectionInfo ["HEALTH: "++show (_crHP cr)] 1 False white 0) statusdisplay = (-3, IM.singleton 0 $ SelectionInfo ["HEALTH: " ++ show (_crHP cr)] 1 False white 0)
invhead = if null sfinv then makehead "INVENTORY" (-1) else filtinv invhead = if null sfinv then makehead "INVENTORY" (-1) else filtinv
youitems = IM.singleton 0 $ SelectionItem [thetext] 1 True invDimColor 2 () youitems = IM.singleton 0 $ SelectionItem [thetext] 1 True invDimColor 2 ()
thetext = displayFreeSlots (crNumFreeSlots cr) thetext = displayFreeSlots (crNumFreeSlots cr)
@@ -312,9 +324,11 @@ updateSection indent mcsel sis availablelines oldoffset =
pos | pos == 0 || length allstrings <= availablelines -> 0 pos | pos == 0 || length allstrings <= availablelines -> 0
pos | pos - 1 < oldoffset -> pos - 1 pos | pos - 1 < oldoffset -> pos - 1
pos | maxcsel == csel -> pos - availablelines + xselsize pos | maxcsel == csel -> pos - availablelines + xselsize
pos | pos + 1 + xselsize - availablelines > oldoffset -> pos
| pos + 1 + xselsize - availablelines > oldoffset ->
pos - availablelines + 1 + xselsize pos - availablelines + 1 + xselsize
_ | length allstrings - oldoffset < availablelines -> _
| length allstrings - oldoffset < availablelines ->
length allstrings - availablelines length allstrings - availablelines
_ -> oldoffset _ -> oldoffset
tweakfirst (x : xs) tweakfirst (x : xs)
@@ -349,15 +363,18 @@ enterCombineInv cfig w =
, _ciSelection = selpos , _ciSelection = selpos
, _ciFilter = Nothing , _ciFilter = Nothing
} }
& hud . hudElement . diInvFilter .~ Nothing & hud . hudElement . diInvFilter
.~ Nothing
where where
cm' = IM.fromDistinctAscList . zip [0 ..] $ combineList w cm' = IM.fromDistinctAscList . zip [0 ..] $ combineList w
cm | null cm' = IM.singleton 0 cm
$ SelectionInfo ["No possible combinations"] 1 False white 0 | null cm' =
IM.singleton 0 $
SelectionInfo ["No possible combinations"] 1 False white 0
| otherwise = cm' | otherwise = cm'
selpos selpos
| null cm' = Nothing | null cm' = Nothing
| otherwise = Just (0, 0,mempty) | otherwise = Just (0, 0, mempty)
filtsection = filtsection =
SelectionSection SelectionSection
{ _ssItems = mempty { _ssItems = mempty
+8 -22
View File
@@ -446,41 +446,27 @@ updateBackspaceRegex :: World -> World
updateBackspaceRegex w = case di ^? subInventory of updateBackspaceRegex w = case di ^? subInventory of
Just NoSubInventory{} Just NoSubInventory{}
| secfocus (-1) 0 -> | secfocus (-1) 0 ->
w & hud . hudElement %~ trybackspace (-1) w & hud . hudElement %~ trybackspace (-1) diInvFilter diInvFilter diSelection
& worldEventFlags . at InventoryChange ?~ () & worldEventFlags . at InventoryChange ?~ ()
Just NoSubInventory{} Just NoSubInventory{}
| secfocus 2 3 -> | secfocus 2 3 ->
w & hud . hudElement %~ trybackspace'' 2 w & hud . hudElement %~ trybackspace 2 diCloseFilter diCloseFilter diSelection
& worldEventFlags . at InventoryChange ?~ () & worldEventFlags . at InventoryChange ?~ ()
Just CombineInventory{} -> Just CombineInventory{} ->
w & hud . hudElement . subInventory %~ trybackspace' (-1) w & hud . hudElement . subInventory %~ trybackspace (-1) ciFilter ciFilter ciSelection
& worldEventFlags . at InventoryChange ?~ () & worldEventFlags . at InventoryChange ?~ ()
_ -> w _ -> w
where where
secfocus a b = fromMaybe False $ do secfocus a b = fromMaybe False $ do
i <- di ^? diSelection . _Just . _1 i <- di ^? diSelection . _Just . _1
return $ i == a || i == b return $ i == a || i == b
trybackspace x he = fromMaybe he $ do trybackspace x filtget filtset selset he = fromMaybe he $ do
str <- he ^? diInvFilter . _Just str <- he ^? filtget . _Just
return $ case str of return $ case str of
(_ : _) -> (_ : _) ->
he & diInvFilter . _Just %~ init he & filtset . _Just %~ init
& diSelection ?~ (x, 0,mempty) & selset ?~ (x, 0,mempty)
[] -> he & diInvFilter .~ Nothing [] -> he & filtset .~ Nothing
trybackspace'' x he = fromMaybe he $ do
str <- he ^? diCloseFilter . _Just
return $ case str of
(_ : _) ->
he & diCloseFilter . _Just %~ init
& diSelection ?~ (x, 0,mempty)
[] -> he & diCloseFilter .~ Nothing
trybackspace' x ci = fromMaybe ci $ do
str <- ci ^? ciFilter . _Just
return $ case str of
(_ : _) ->
ci & ciFilter . _Just %~ init
& ciSelection ?~ (x, 0,mempty)
[] -> ci & ciFilter .~ Nothing
di = w ^. hud . hudElement di = w ^. hud . hudElement
updateEnterRegex :: World -> World updateEnterRegex :: World -> World