From 4150516dc6de5345d79a63e9aa77fcf4716ec71a Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Mon, 31 Mar 2025 00:22:49 +0200 Subject: [PATCH] Add zoomies --- QuickStackToBag/Lua/Autorun/init.lua | 1 + QuickStackToBag/Lua/Cyka/utils.lua | 8 +- QuickStackToBag/Lua/Cyka/zoom.lua | 106 +++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 QuickStackToBag/Lua/Cyka/zoom.lua diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index e8efb17..5f350d9 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -62,6 +62,7 @@ local cursormacroer = require("Cyka.cursormacroer") local quickunload = require("Cyka.quickunload") local quickreload= require("Cyka.quickreload") require("Cyka.xpticker") +require("Cyka.zoom") print(MyModGlobal.MOD_NAME .. " v" .. MyModGlobal.MOD_VERSION .. " loaded!") diff --git a/QuickStackToBag/Lua/Cyka/utils.lua b/QuickStackToBag/Lua/Cyka/utils.lua index 8d3bc01..821e187 100644 --- a/QuickStackToBag/Lua/Cyka/utils.lua +++ b/QuickStackToBag/Lua/Cyka/utils.lua @@ -312,9 +312,14 @@ local function getSlotsUnderCursor() for _, container in ipairs(openContainers) do local containerInventories = container.OwnInventories for containerInventory in containerInventories do + local slot + if not containerInventory or not containerInventory.visualSlots then + MyModGlobal.debugPrint("Container inventory has no visual slots") + goto continue + end for i, visualSlot in ipairs(containerInventory.visualSlots) do if visualSlot:MouseOn() then - local slot = containerInventory.slots[i] + slot = containerInventory.slots[i] mouseoverSlots[#mouseoverSlots + 1] = { inventory = containerInventory, slotIndex = i, @@ -322,6 +327,7 @@ local function getSlotsUnderCursor() } end end + ::continue:: end end diff --git a/QuickStackToBag/Lua/Cyka/zoom.lua b/QuickStackToBag/Lua/Cyka/zoom.lua new file mode 100644 index 0000000..59bba79 --- /dev/null +++ b/QuickStackToBag/Lua/Cyka/zoom.lua @@ -0,0 +1,106 @@ +-- luacheck: globals SERVER Hook Keys LuaUserData PlayerInput Character Descriptors +-- luacheck: max line length 420 +if SERVER then return end + +local isToggle = true -- toggle or hold behaviour +local smoothZoom = false -- smooth or step + +local zStep = 0.5 -- step size for when smoothZoom=false +local zSpeed = 0.02 -- speed for when smoothZoom=true +local zMin = 0.1 -- minimum zoom modifier +local zMax = 2.5 -- maximum zoom modifier +local zStart = 1.5 -- default zoom level + +local zKey = Keys.P -- zoom key +local dKey = Keys.NumPad1 -- decrease zoom key +local iKey = Keys.NumPad2 -- increase zoom key +local rKey = Keys.Back -- reset zoom key +-- customization end +--end + +local zoomOn = false -- default zoom state +local gzsDefault = false +zStart = math.max(math.min(zMax, zStart), zMin) +local gzsNew = zStart +local gzsMin = zMin +local gzsMax = zMax +local gzsUpd = false + +local dHeld = false +local iHeld = false +local zHeld = false + +LuaUserData.MakeFieldAccessible(Descriptors["Barotrauma.Camera"], "globalZoomScale") +LuaUserData.MakeMethodAccessible(Descriptors["Barotrauma.Camera"], "CreateMatrices") + +Hook.HookMethod("Barotrauma.Camera", "CreateMatrices", function(instance, _) + gzsDefault = instance.globalZoomScale + gzsMin = math.max(zMin, gzsDefault * zMin) + gzsMax = math.min(zMax, gzsDefault * zMax) + gzsNew = math.max(math.min(gzsMax, gzsDefault * zStart), gzsMin) + gzsUpd = true + instance.MinZoom = math.min(gzsMin / 2, instance.MinZoom) + instance.MaxZoom = math.max(gzsMax * 2, instance.MaxZoom) +end, Hook.HookMethodType.After) + +Hook.HookMethod("Barotrauma.Character", "ControlLocalPlayer", function(_, ptable) + gzsUpd = false + if not gzsDefault then + ptable.cam.CreateMatrices() + else + if not Character.DisableControls and Character.Controlled then + if zoomOn then + if PlayerInput.KeyDown(dKey) then + if smoothZoom then + gzsNew = math.max(gzsMin, gzsNew * (1 - zSpeed)) + gzsUpd = true + elseif not dHeld then + gzsNew = math.max(gzsMin, gzsNew - zStep) + dHeld = true + gzsUpd = true + end + else + dHeld = false + end + if PlayerInput.KeyDown(iKey) then + if smoothZoom then + gzsNew = math.min(gzsMax, gzsNew * (1 + zSpeed)) + gzsUpd = true + elseif not iHeld then + gzsNew = math.min(gzsMax, gzsNew + zStep) + iHeld = true + gzsUpd = true + end + else + iHeld = false + end + if PlayerInput.KeyDown(rKey) then + gzsNew = gzsDefault * zStart + gzsUpd = true + end + end + if PlayerInput.KeyDown(zKey) then + if isToggle then + if not zHeld then + zoomOn = not zoomOn + zHeld = true + gzsUpd = true + end + else + zoomOn = true + gzsUpd = true + end + elseif isToggle then + zHeld = false + elseif zoomOn then + zoomOn = false + gzsUpd = true + end + else + zoomOn = false + end + if gzsUpd then + ptable.cam.globalZoomScale = zoomOn and gzsNew or gzsDefault + end + end +end, Hook.HookMethodType.After)