Skip to content

Commit

Permalink
Add support for custom attack/use binds
Browse files Browse the repository at this point in the history
This also includes another scope optimisation or two
  • Loading branch information
TomDotBat committed Jan 21, 2021
1 parent 765bea4 commit f4f21ff
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions cl_ui3d2d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f4f21ff

Please sign in to comment.