{- Functions that affect the world according to what pressed buttons are stored in a Set. -} module Dodge.Update.UsingInput ( updateUsingInput, ) where import Control.Lens import qualified Data.Map.Strict as M import Dodge.Base.You import Dodge.Combine import Dodge.Creature.Impulse.UseItem import Dodge.Data.World import Dodge.InputFocus import Dodge.Inventory import Dodge.Inventory.Add import Dodge.Terminal.LeftButton import Geometry import ListHelp import SDL updateUsingInput :: World -> World updateUsingInput w = case w ^. cWorld . lWorld . hud . hudElement of DisplayInventory subinv -> pressedMBEffects subinv (_mouseButtons (_input w)) w DisplayCarte -> updatePressedButtonsCarte (_mouseButtons (_input w)) w pressedMBEffects :: SubInventory -> M.Map MouseButton Bool -> World -> World pressedMBEffects subinv pkeys w = case subinv of NoSubInventory | ButtonLeft `M.member` pkeys && w ^?! input . hammers . ix SubInvHam /= HammerUp -> w & input . hammers . ix SubInvHam .~ HammerDown | otherwise -> pressedMBEffectsNoInventory pkeys w CombineInventory mi | pkeys ^? ix ButtonLeft == Just False -> maybeexitcombine (maybe id doCombine mi w) & input . hammers . ix SubInvHam .~ HammerDown DisplayTerminal tmid | pkeys ^? ix ButtonLeft == Just False && inTermFocus w -> doTerminalEffectLB (w ^?! cWorld . lWorld . terminals . ix tmid) w & input . hammers . ix SubInvHam .~ HammerDown | pkeys ^? ix ButtonLeft == Just False -> w & cWorld . lWorld . terminals . ix tmid . tmInput . tiFocus %~ const True & input . hammers . ix SubInvHam .~ HammerDown _ | ButtonLeft `M.member` pkeys -> w & input . hammers . ix SubInvHam .~ HammerDown _ -> w where maybeexitcombine | ButtonRight `M.member` _mouseButtons (_input w) = id | otherwise = cWorld . lWorld . hud . hudElement .~ DisplayInventory NoSubInventory pressedMBEffectsNoInventory :: M.Map MouseButton Bool -> World -> World pressedMBEffectsNoInventory pkeys w | isDown ButtonLeft && isDown ButtonRight && inTopInv = useItem (you w) w & input . hammers . ix DoubleMouseHam .~ HammerDown | justPressedDown ButtonLeft && inTopInv && w ^?! input . hammers . ix DoubleMouseHam == HammerUp = useLeftItem 0 w | isDown ButtonLeft && inTopInv = w & input . hammers . ix DoubleMouseHam .~ HammerDown | isDown ButtonRight && inTopInv = w | isDown ButtonMiddle = w & cWorld . camPos . camRot -~ rotation & input . clickMousePos .~ _mousePos (_input w) | isDown ButtonLeft = w & input . hammers . ix DoubleMouseHam .~ HammerDown | otherwise = w where inTopInv = case w ^. cWorld . lWorld . hud . hudElement of DisplayInventory NoSubInventory -> True _ -> False justPressedDown but = (pkeys ^. at but) == Just False isDown but = but `M.member` pkeys rotation = angleBetween (_mousePos (_input w)) (_clickMousePos (_input w)) -- note "sort" on the inventory indices; otherwise -- lower items may be shifted up and items below these removed instead doCombine :: Int -> World -> World doCombine i w = case combineItemListYou w ^? ix i of Nothing -> w Just (is, it) -> -- set (hud . hudElement) (DisplayInventory NoSubInventory) selectinv --enterCombineInv . createPutItem it $ foldr (rmInvItem 0) w (sort is) where selectinv (Just i', w') = w' & cWorld . lWorld . creatures . ix 0 . crInvSel .~ InvSel i' NoInvSelAction selectinv (Nothing, w') = w' updatePressedButtonsCarte :: M.Map MouseButton Bool -> World -> World updatePressedButtonsCarte pkeys w | isDown ButtonRight = w & input . clickMousePos .~ _mousePos (_input w) & cWorld . lWorld . hud . carteCenter %~ (-.- trans) | isDown ButtonLeft = w & input . clickMousePos .~ _mousePos (_input w) & cWorld . lWorld . hud . carteRot -~ rot & cWorld . lWorld . hud . carteZoom *~ czoom | otherwise = w where isDown but = but `M.member` pkeys rot = angleBetween (_mousePos (_input w)) (_clickMousePos (_input w)) czoom = magV (_mousePos (_input w)) / magV (_clickMousePos (_input w)) trans = rotateV (w ^. cWorld . lWorld . hud . carteRot) $ 1 / (w ^. cWorld . lWorld . hud . carteZoom) *.* (_mousePos (_input w) -.- _clickMousePos (_input w))