From 99114c73514f68cb9e60f01d83421e0f7e4b83b1 Mon Sep 17 00:00:00 2001 From: justin Date: Tue, 31 Dec 2024 13:24:10 +0000 Subject: [PATCH] Refactor hotkey assignation --- src/Dodge/Creature/YourControl.hs | 70 +++++++++++++------------------ src/Dodge/Data/Item.hs | 1 - 2 files changed, 30 insertions(+), 41 deletions(-) diff --git a/src/Dodge/Creature/YourControl.hs b/src/Dodge/Creature/YourControl.hs index 97f6327a5..3b245ceaa 100644 --- a/src/Dodge/Creature/YourControl.hs +++ b/src/Dodge/Creature/YourControl.hs @@ -2,20 +2,15 @@ module Dodge.Creature.YourControl ( yourControl, ) where -import Control.Monad import Data.Foldable import qualified Data.Map.Strict as M import Data.Maybe import Dodge.Base.Coordinate import Dodge.Creature.Impulse.Movement import Dodge.Creature.Impulse.UseItem -import Dodge.Data.ComposedItem -import Dodge.Data.DoubleTree import Dodge.Data.World -import Dodge.HeldUse import Dodge.Hotkey import Dodge.InputFocus -import Dodge.Item.Grammar import Dodge.WASD import Geometry import LensHelp @@ -40,8 +35,11 @@ yourControl _ w handleHotkeys :: World -> World handleHotkeys w | SDL.ScancodeLShift `M.member` _pressedKeys (_input w) - || SDL.ScancodeRShift `M.member` _pressedKeys (_input w) = - foldl' tryAssignHotkey w allHotkeys + || SDL.ScancodeRShift `M.member` _pressedKeys (_input w) = fromMaybe w $ do + let hks = mapMaybe scancodeToHotkey $ M.keys $ w ^. input . pressedKeys + invid <- w ^? cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . imSelectedItem + itid <- w ^? cWorld . lWorld . creatures . ix 0 . crInv . ix invid . itID + return $ w & cWorld . lWorld %~ \lw -> foldl' (flip $ assignHotkey itid) lw hks | otherwise = M.foldl' useHotkey @@ -50,16 +48,13 @@ handleHotkeys w where thehotkeys = M.mapKeys hotkeyToScancode $ w ^. cWorld . lWorld . hotkeys -useHotkey :: World -> (NewInt ItmInt,PressType) -> World -useHotkey w (NInt itid,pt) = fromMaybe w $ do +useHotkey :: World -> (NewInt ItmInt, PressType) -> World +useHotkey w (NInt itid, pt) = fromMaybe w $ do invid <- w ^? cWorld . lWorld . itemLocations . ix itid . ilInvID return $ useItem invid pt w --- cr <- w ^? cWorld . lWorld . creatures . ix 0 --- itmloc <- allInvLocs (cr ^. crInv) ^? ix invid . _2 . locLDT --- return $ heldEffectNoHammerCheck (bimap (^. iatType) (^. _1) itmloc) cr w -allHotkeys :: [SDL.Scancode] -allHotkeys = map hotkeyToScancode [minBound .. maxBound] +--allHotkeys :: [SDL.Scancode] +--allHotkeys = map hotkeyToScancode [minBound .. maxBound] hotkeyToScancode :: Hotkey -> SDL.Scancode hotkeyToScancode x = case x of @@ -76,30 +71,21 @@ hotkeyToScancode x = case x of Hotkey9 -> SDL.Scancode9 Hotkey0 -> SDL.Scancode0 -scancodeToHotkey :: SDL.Scancode -> Hotkey +scancodeToHotkey :: SDL.Scancode -> Maybe Hotkey scancodeToHotkey x = case x of - SDL.ScancodeQ -> HotkeyQ - SDL.ScancodeE -> HotkeyE - SDL.Scancode1 -> Hotkey1 - SDL.Scancode2 -> Hotkey2 - SDL.Scancode3 -> Hotkey3 - SDL.Scancode4 -> Hotkey4 - SDL.Scancode5 -> Hotkey5 - SDL.Scancode6 -> Hotkey6 - SDL.Scancode7 -> Hotkey7 - SDL.Scancode8 -> Hotkey8 - SDL.Scancode9 -> Hotkey9 - SDL.Scancode0 -> Hotkey0 - _ -> undefined - -tryAssignHotkey :: World -> SDL.Scancode -> World -tryAssignHotkey w sc = fromMaybe w $ do - pt <- w ^? input . pressedKeys . ix sc - guard (pt == InitialPress) - cr <- w ^? cWorld . lWorld . creatures . ix 0 - invid <- cr ^? crManipulation . manObject . imSelectedItem - itid <- cr ^? crInv . ix invid . itID - return $ w & cWorld . lWorld %~ assignHotkey itid (scancodeToHotkey sc) + SDL.ScancodeQ -> Just HotkeyQ + SDL.ScancodeE -> Just HotkeyE + SDL.Scancode1 -> Just Hotkey1 + SDL.Scancode2 -> Just Hotkey2 + SDL.Scancode3 -> Just Hotkey3 + SDL.Scancode4 -> Just Hotkey4 + SDL.Scancode5 -> Just Hotkey5 + SDL.Scancode6 -> Just Hotkey6 + SDL.Scancode7 -> Just Hotkey7 + SDL.Scancode8 -> Just Hotkey8 + SDL.Scancode9 -> Just Hotkey9 + SDL.Scancode0 -> Just Hotkey0 + _ -> Nothing {- | The order of these MAY be important, in particular the setting of crMvAim within wasdMovement should probably be done first @@ -178,14 +164,18 @@ pressedMBEffectsTopInventory pkeys w , Just rtime <- pkeys ^? ix SDL.ButtonRight , ltime <= rtime && inTopInv = fromMaybe w $ do - invid <- w ^? cWorld . lWorld . creatures . ix 0 - . crManipulation . manObject . imSelectedItem + invid <- + w + ^? cWorld . lWorld . creatures . ix 0 + . crManipulation + . manObject + . imSelectedItem return $ useItem invid (f ltime) w | otherwise = w where f 0 = InitialPress f _ = ShortPress --- youhammerdown = set (cWorld . lWorld . creatures . ix 0 . crHammerPosition) HammerDown + -- youhammerdown = set (cWorld . lWorld . creatures . ix 0 . crHammerPosition) HammerDown inTopInv = case w ^. hud . hudElement of DisplayInventory{_subInventory = NoSubInventory{}} -> True _ -> False diff --git a/src/Dodge/Data/Item.hs b/src/Dodge/Data/Item.hs index 7aa357122..2f65c9d30 100644 --- a/src/Dodge/Data/Item.hs +++ b/src/Dodge/Data/Item.hs @@ -41,7 +41,6 @@ data Consumables data Item = Item { _itUse :: ItemUse , _itUseCondition :: UseCondition --- , _itUseFocus :: UseFocus , _itConsumables :: Consumables , _itType :: ItemType , _itID :: NewInt ItmInt