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
+32 -15
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
@@ -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
@@ -135,8 +138,16 @@ 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)
@@ -149,7 +160,8 @@ 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 =
if null sfclose
then if null sclose then (2, mempty) else makehead "NEARBY ITEMS" 2 then if null sclose then (2, mempty) else makehead "NEARBY ITEMS" 2
else filtclose else filtclose
youx = (1, youitems) youx = (1, youitems)
@@ -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,11 +363,14 @@ 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
+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