From f4f21ffd6c4efbf3e658f013f3f9511d70c1d0b4 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 21 Jan 2021 00:32:05 +0000 Subject: [PATCH] Add support for custom attack/use binds This also includes another scope optimisation or two --- cl_ui3d2d.lua | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/cl_ui3d2d.lua b/cl_ui3d2d.lua index b2003f4..947a41b 100644 --- a/cl_ui3d2d.lua +++ b/cl_ui3d2d.lua @@ -3,27 +3,40 @@ ui3d2d = ui3d2d or {} do --Input handling local getRenderTarget, cursorVisible = render.GetRenderTarget, vgui.CursorVisible - local isMouseDown, isKeyDown = input.IsMouseDown, input.IsKeyDown - local inputEnabled, isPressing, isPressed + local inputCount = 0 - hook.Add("PreRender", "ui3d2d.inputHandler", function() --Check the input state before rendering UIs - if getRenderTarget() then inputEnabled = false return end - if cursorVisible() then inputEnabled = false return end - - inputEnabled = true + hook.Add("KeyPress", "ui3d2d.inputHandler", function(ply, key) + if key ~= IN_USE and key ~= IN_ATTACK then return end + inputCount = inputCount + 1 + end) - local wasPressing = isPressing - isPressing = isMouseDown(MOUSE_LEFT) or isKeyDown(KEY_E) - isPressed = not wasPressing and isPressing + hook.Add("KeyRelease", "ui3d2d.inputHandler", function(ply, key) + if key ~= IN_USE and key ~= IN_ATTACK then return end + inputCount = inputCount - 1 end) - function ui3d2d.isPressing() --Returns true if an input is being held - return inputEnabled and isPressing - end + do + local inputEnabled, isPressing, isPressed + + hook.Add("PreRender", "ui3d2d.inputHandler", function() --Check the input state before rendering UIs + if getRenderTarget() then inputEnabled = false return end + if cursorVisible() then inputEnabled = false return end + + inputEnabled = true - function ui3d2d.isPressed() --Returns true if an input was pressed this frame - return inputEnabled and isPressed + local wasPressing = isPressing + isPressing = inputCount > 0 + isPressed = not wasPressing and isPressing + end) + + function ui3d2d.isPressing() --Returns true if an input is being held + return inputEnabled and isPressing + end + + function ui3d2d.isPressed() --Returns true if an input was pressed this frame + return inputEnabled and isPressed + end end end @@ -94,9 +107,11 @@ do --Rendering context creation and mouse position getters if isObstructed(eyePos, hitPos, ignoredEntity) then return true end - local diff = pos - hitPos - mouseX = diff:Dot(-angles:Forward()) / scale - mouseY = diff:Dot(-angles:Right()) / scale + do + local diff = pos - hitPos + mouseX = diff:Dot(-angles:Forward()) / scale + mouseY = diff:Dot(-angles:Right()) / scale + end return true end