Allow for swapping inventory items

This commit is contained in:
2023-02-23 14:12:08 +00:00
parent 6fff71cdcc
commit 683acf8a05
9 changed files with 161 additions and 104 deletions
+93 -40
View File
@@ -4,7 +4,6 @@ module Dodge.Inventory (
selSecSelPos,
checkInvSlotsYou,
rmSelectedInvItem,
selNumPos,
selNumTextEndPos,
selSecSelCol,
selNumEndMidHeight,
@@ -13,7 +12,7 @@ module Dodge.Inventory (
updateRBList,
updateTerminal,
closeObjScrollDir,
changeSwapInvSel,
changeSwapSel,
scrollAugInvSel,
crNumFreeSlots,
crInvSize,
@@ -55,8 +54,10 @@ rmInvItem ::
World ->
World
rmInvItem cid invid w = case w ^? getcid . crInv . ix invid . itUse . useAmount of
Just x | x > 1 -> w & pointcid . crInv . ix invid . itUse . useAmount -~ 1
& worldEventFlags . at InventoryChange ?~ ()
Just x
| x > 1 ->
w & pointcid . crInv . ix invid . itUse . useAmount -~ 1
& worldEventFlags . at InventoryChange ?~ ()
_ ->
w
& pointcid %~ crCancelReloading
@@ -112,15 +113,15 @@ rmSelectedInvItem cid w = case w ^? cWorld . lWorld . creatures . ix cid . crMan
Just i -> rmInvItem cid i w
Nothing -> w
selNumPos :: ManipulatedObject -> World -> Maybe Int
selNumPos mo w =
w ^? hud . hudElement . diSections
>>= case mo of
InInventory SortInventory -> selSecSelPos (-1) 0
InInventory (SelItem i _) -> selSecSelPos 0 i
SelNothing -> selSecSelPos 1 0
InNearby SortNearby -> selSecSelPos 2 0
InNearby (SelCloseObject i) -> selSecSelPos 3 i
--selNumPos :: ManipulatedObject -> World -> Maybe Int
--selNumPos mo w =
-- w ^? hud . hudElement . diSections
-- >>= case mo of
-- InInventory SortInventory -> selSecSelPos (-1) 0
-- InInventory (SelItem i _) -> selSecSelPos 0 i
-- SelNothing -> selSecSelPos 1 0
-- InNearby SortNearby -> selSecSelPos 2 0
-- InNearby (SelCloseObject i) -> selSecSelPos 3 i
-- there are still more ListDisplayParams to integrate here
selNumTextEndPos :: Configuration -> ListDisplayParams -> SelectionSections a -> Int -> Int -> Maybe Point2
@@ -327,37 +328,89 @@ closeObjScrollDir x
| x < 0 = over (hud . closeObjects) rotD
| otherwise = id
changeSwapInvSel :: Int -> World -> World
changeSwapInvSel k w = case you w ^? crManipulation . manObject of
Just (InInventory (SelItem i _)) ->
w & cWorld . lWorld . creatures . ix 0 %~ updatecreature i
& worldEventFlags . at InventoryChange ?~ ()
Just (InNearby (SelCloseObject i)) ->
w
& cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . inNearby . ispCloseObject
.~ ((i - k) `mod` numCO)
& hud . closeObjects %~ swapIndices i ((i - k) `mod` numCO)
& worldEventFlags . at InventoryChange ?~ ()
_ -> w
changeSwapSel :: Int -> World -> World
changeSwapSel yi w
| yi == 0 = w
| yi > 0 = foldr ($) w $ replicate yi (changeSwapWith $ f IM.cycleLT)
| otherwise = foldr ($) w $ replicate (negate yi) (changeSwapWith $ f IM.cycleGT)
where
updatecreature i =
(crInv %~ IM.safeSwapKeys (i `mod` n) (swapi i))
. (crLeftInvSel . lisMPos . _Just %~ updateLeftInvSel i)
. (crManipulation . manObject . inInventory . ispItem %~ (`mod` n) . subtract k)
. (crInvEquipped %~ IM.safeSwapKeys i (swapi i))
. swapSite i (swapi i)
. swapSite (swapi i) i
f g i m = fst <$> g i m
changeSwapClose :: (Int -> IM.IntMap (SelectionItem ()) -> Maybe Int)
-> Int -> World -> World
changeSwapClose f i w = fromMaybe w $ do
ss <- w ^? hud . hudElement . diSections . sssSections . ix 3 . ssItems
k <- f i ss
return $ w
& cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . inNearby . ispCloseObject
.~ k
& hud . closeObjects %~ swapIndices i k
& hud . hudElement . diSections . sssExtra . sssSelPos . _Just . _2 .~ k
& worldEventFlags . at InventoryChange ?~ ()
changeSwapInv :: (Int -> IM.IntMap (SelectionItem ()) -> Maybe Int)
-> Int -> World -> World
changeSwapInv f i w = fromMaybe w $ do
ss <- w ^? hud . hudElement . diSections . sssSections . ix 0 . ssItems
k <- f i ss
return $
w & cWorld . lWorld . creatures . ix 0 %~ updatecreature k
& hud . hudElement . diSections . sssExtra . sssSelPos . _Just . _2 .~ k
& worldEventFlags . at InventoryChange ?~ ()
where
updateLeftInvSel k li
| i == li = k
| k == li = i
| otherwise = li
updatecreature k =
(crInv %~ IM.safeSwapKeys i k)
. (crLeftInvSel . lisMPos . _Just %~ updateLeftInvSel k)
. (crManipulation . manObject . inInventory . ispItem .~ k)
. (crInvEquipped %~ IM.safeSwapKeys i k)
. swapSite i k
. swapSite k i
cr = you w
swapSite a b = case cr ^? crInvEquipped . ix a of
Just epos -> crEquipment . ix epos .~ b
Nothing -> id
cr = you w
swapi i = (i - k) `mod` n
updateLeftInvSel i li
| i == li = swapi i
| swapi i == li = i
| otherwise = li
n = length $ _crInv cr
numCO = length $ w ^. hud . closeObjects
changeSwapWith :: (Int -> IM.IntMap (SelectionItem ()) -> Maybe Int) -> World -> World
changeSwapWith f w = case w ^? hud . hudElement . diSections . sssExtra . sssSelPos . _Just of
Just (0,i) -> w & changeSwapInv f i
Just (3,i) -> w & changeSwapClose f i
_ -> w
--changeSwapInvSel' :: Int -> World -> World
--changeSwapInvSel' k w = case you w ^? crManipulation . manObject of
-- Just (InInventory (SelItem i _)) ->
-- w & cWorld . lWorld . creatures . ix 0 %~ updatecreature i
-- & worldEventFlags . at InventoryChange ?~ ()
-- Just (InNearby (SelCloseObject i)) ->
-- w
-- & cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . inNearby . ispCloseObject
-- .~ ((i - k) `mod` numCO)
-- & hud . closeObjects %~ swapIndices i ((i - k) `mod` numCO)
-- & worldEventFlags . at InventoryChange ?~ ()
-- _ -> w
-- where
-- updatecreature i =
-- (crInv %~ IM.safeSwapKeys (i `mod` n) (swapi i))
-- . (crLeftInvSel . lisMPos . _Just %~ updateLeftInvSel i)
-- . (crManipulation . manObject . inInventory . ispItem %~ (`mod` n) . subtract k)
-- . (crInvEquipped %~ IM.safeSwapKeys i (swapi i))
-- . swapSite i (swapi i)
-- . swapSite (swapi i) i
-- swapSite a b = case cr ^? crInvEquipped . ix a of
-- Just epos -> crEquipment . ix epos .~ b
-- Nothing -> id
-- cr = you w
-- swapi i = (i - k) `mod` n
-- updateLeftInvSel i li
-- | i == li = swapi i
-- | swapi i == li = i
-- | otherwise = li
-- n = length $ _crInv cr
-- numCO = length $ w ^. hud . closeObjects
scrollAugInvSel :: Int -> World -> World
scrollAugInvSel yi w