Files
loop/src/Dodge/Update/UsingInput.hs
T
2023-02-19 09:59:09 +00:00

91 lines
3.6 KiB
Haskell

{- 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.Combine
import Dodge.Data.SelectionList
import Dodge.Data.World
import Dodge.InputFocus
--import Dodge.Inventory
--import Dodge.Inventory.Add
import Dodge.Terminal.LeftButton
import Geometry
import SDL
updateUsingInput :: World -> World
updateUsingInput w = case w ^. hud . hudElement of
DisplayInventory {_subInventory = 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 -> pressedMBEffectsNoInventory pkeys w
CombineInventory _ _ sss
| lbinitialpress -> tryCombine sss w -- maybeexitcombine (maybe id doCombine mi w)
DisplayTerminal tmid
| lbinitialpress && inTermFocus w ->
doTerminalEffectLB (w ^?! cWorld . lWorld . terminals . ix tmid) w
| lbinitialpress ->
w & cWorld . lWorld . terminals . ix tmid . tmInput . tiFocus %~ const True
_ -> w
where
lbinitialpress = pkeys ^? ix ButtonLeft == Just False
-- maybeexitcombine w'
-- | ButtonRight `M.member` _mouseButtons (_input w) =
-- (hud . hudElement . subInventory . subInvMap %~ setShownIntMap) $
-- (hud . hudElement . subInventory . subInvMap . smItems .~ combineList w') w'
-- | otherwise = w' & hud . hudElement . subInventory .~ NoSubInventory
tryCombine :: SelectionSections CombinableItem -> World -> World
tryCombine sss w = w
pressedMBEffectsNoInventory :: M.Map MouseButton Bool -> World -> World
pressedMBEffectsNoInventory pkeys w
| isDown ButtonLeft && isDown ButtonRight && inTopInv = useItem (you w) w
| isDown ButtonLeft && inTopInv = useLeftItem 0 w
| isDown ButtonMiddle =
w & cWorld . camPos . camRot -~ rotation
& input . clickMousePos .~ _mousePos (_input w)
| otherwise = w
where
inTopInv = case w ^. hud . hudElement of
DisplayInventory {_subInventory = NoSubInventory} -> True
_ -> 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 = undefined
-- case w ^? hud . hudElement . subInventory . subInvMap . smShownItems . ix i . siPayload of
-- Nothing -> w
-- Just (CombinableItem is it _) -> createAndSelectItem it $ foldr (rmInvItem 0) w (sort is)
updatePressedButtonsCarte :: M.Map MouseButton Bool -> World -> World
updatePressedButtonsCarte pkeys w
| isDown ButtonRight =
w
& input . clickMousePos .~ _mousePos (_input w)
& hud . carteCenter %~ (-.- trans)
| isDown ButtonLeft =
w
& input . clickMousePos .~ _mousePos (_input w)
& hud . carteRot -~ rot
& 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 ^. hud . carteRot)
$ 1 / (w ^. hud . carteZoom) *.* (_mousePos (_input w) -.- _clickMousePos (_input w))