Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keypad improvements #3252

Merged
merged 7 commits into from
Jan 26, 2025
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 104 additions & 89 deletions lua/entities/gmod_wire_keypad.lua
Original file line number Diff line number Diff line change
@@ -1,66 +1,76 @@
AddCSLuaFile()
DEFINE_BASECLASS( "base_wire_entity" )
ENT.PrintName = "Wire Keypad"
DEFINE_BASECLASS("base_wire_entity")
ENT.PrintName = "Wire Keypad"

function ENT:SetupDataTables()
self:NetworkVar("String", 0, "DisplayText")
end

if CLIENT then
local X = -50
local Y = -100
local W = 100
local H = 200

local KeyPos = {
{X+5 , Y+100 , 25, 25, -2.2, 3.45, 1.3 , 0 }, -- 1
{X+37.5, Y+100 , 25, 25, -0.6, 1.85, 1.3 , 0 }, -- 2
{X+70 , Y+100 , 25, 25, 1.0, 0.25, 1.3 , 0 }, -- 3
local keyposes = {
{X + 5, Y + 100, 25, 25, -2.2, 3.45, 1.3, 0}, -- 1
{X + 37.5, Y + 100, 25, 25, -0.6, 1.85, 1.3, 0}, -- 2
{X + 70, Y + 100, 25, 25, 1.0, 0.25, 1.3, 0}, -- 3

{X+5 , Y+132.5, 25, 25, -2.2, 3.45, 2.9 , -1.6}, -- 4
{X+37.5, Y+132.5, 25, 25, -0.6, 1.85, 2.9 , -1.6}, -- 5
{X+70 , Y+132.5, 25, 25, 1.0, 0.25, 2.9 , -1.6}, -- 6
{X + 5, Y + 132.5, 25, 25, -2.2, 3.45, 2.9, -1.6}, -- 4
{X + 37.5, Y + 132.5, 25, 25, -0.6, 1.85, 2.9, -1.6}, -- 5
{X + 70, Y + 132.5, 25, 25, 1.0, 0.25, 2.9, -1.6}, -- 6

{X+5 , Y+165 , 25, 25, -2.2, 3.45, 4.55, -3.3}, -- 7
{X+37.5, Y+165 , 25, 25, -0.6, 1.85, 4.55, -3.3}, -- 8
{X+70 , Y+165 , 25, 25, 1.0, 0.25, 4.55, -3.3}, -- 9
{X + 5, Y + 165, 25, 25, -2.2, 3.45, 4.55, -3.3}, -- 7
{X + 37.5, Y + 165, 25, 25, -0.6, 1.85, 4.55, -3.3}, -- 8
{X + 70, Y + 165, 25, 25, 1.0, 0.25, 4.55, -3.3}, -- 9

{X+5 , Y+ 67.5, 40, 25, -2.2, 4.25, -0.3 , 1.6}, -- abort
{X+55 , Y+ 67.5, 40, 25, 0.3, 1.65, -0.3 , 1.6}, -- ok
{X + 5, Y + 67.5, 40, 25, -2.2, 4.25, -0.3, 1.6}, -- abort
{X + 55, Y + 67.5, 40, 25, 0.3, 1.65, -0.3, 1.6}, -- ok
}

local fontdata = {
font = "Trebuchet MS",
weight = 400,
antialias = true,
additive = false
}
fontdata.size = 34 surface.CreateFont( "Trebuchet34", fontdata )
fontdata.size = 24 surface.CreateFont( "Trebuchet24", fontdata )
surface.CreateFont("WireKeypad", {
font = "Trebuchet MS",
size = 24,
weight = 400,
antialias = true
})

surface.CreateFont("WireKeypad_Big", {
font = "Trebuchet MS",
size = 34,
weight = 400,
antialias = true
})

local color_red = Color(255, 0, 0)
local color_green = Color(0, 255, 0)

local highlight_key, highlight_until
function ENT:Draw()
self:DrawModel()

local Ply = LocalPlayer()
if (Ply:GetShootPos() - self:GetPos()):Length() > 750 then return end
local ply = LocalPlayer()
local entpos = self:GetPos()
if entpos:Distance(ply:GetPos()) > 512 then return end

local pos = self:GetPos() + (self:GetForward() * 1.1)
local ang = self:GetAngles()
local rot = Vector(-90, 90, 0)

ang:RotateAroundAxis(ang:Right(), rot.x)
ang:RotateAroundAxis(ang:Up(), rot.y)
ang:RotateAroundAxis(ang:Forward(), rot.z)
entpos = entpos + self:GetForward() * 1.05

cam.Start3D2D(pos, ang, 0.05)
local trace = Ply:GetEyeTrace()
ang:RotateAroundAxis(ang:Right(), -90)
ang:RotateAroundAxis(ang:Up(), 90)
ang:RotateAroundAxis(ang:Forward(), 0)

cam.Start3D2D(entpos, ang, 0.05)
local trace = ply:GetEyeTrace()
local pos = self:WorldToLocal(trace.HitPos)

surface.SetDrawColor(0, 0, 0, 255)
surface.DrawRect(X-5, Y-5, W+10, H+10)
surface.SetDrawColor(0, 0, 0)
surface.DrawRect(X - 5, Y - 5, W + 10, H + 10)

surface.SetDrawColor(50, 75, 50, 255)
surface.DrawRect(X+5, Y+5, 90, 50)
surface.SetDrawColor(50, 75, 50)
surface.DrawRect(X + 5, Y + 5, 90, 50)

for k,v in pairs(KeyPos) do
for k, v in ipairs(keyposes) do
local text = k
local textx = v[1] + 9
local texty = v[2] + 4
Expand All @@ -72,100 +82,101 @@ if CLIENT then
text = "ABORT"
textx = v[1] + 2
texty = v[2] + 4
surface.SetDrawColor(150, 25, 25, 255)

surface.SetDrawColor(150, 25, 25)
elseif (k == 11) then
text = "OK"
textx = v[1] + 12
texty = v[2] + 5
surface.SetDrawColor(25, 150, 25, 255)

surface.SetDrawColor(25, 150, 25)
else
surface.SetDrawColor(150, 150, 150, 255)
surface.SetDrawColor(150, 150, 150)
end

if highlight_current_key or (trace.Entity == self and x >= 0 and y >= 0 and x <= 1 and y <= 1) then
if (k <= 9) then
surface.SetDrawColor(200, 200, 200, 255)
surface.SetDrawColor(200, 200, 200)
elseif (k == 10) then
surface.SetDrawColor(200, 50, 50, 255)
surface.SetDrawColor(200, 50, 50)
elseif (k == 11) then
surface.SetDrawColor(50, 200, 50, 255)
surface.SetDrawColor(50, 200, 50)
end

if Ply:KeyDown(IN_USE) and not Ply.KeyOnce and not highlight_current_key then
if ply:KeyDown(IN_USE) and not ply.WireKeyPad_Pressed and not highlight_current_key then
net.Start("wire_keypad")
net.WriteEntity(self)
net.WriteUInt(k, 4)
net.SendToServer()
Ply.KeyOnce = true

ply.WireKeyPad_Pressed = true
end
end

surface.DrawRect(v[1], v[2], v[3], v[4])
draw.DrawText(text, "Trebuchet18", textx, texty, Color(0, 0, 0, 255))
draw.DrawText(text, "Trebuchet18", textx, texty, color_black)
end

local Display = self:GetNWString("keypad_display", "")
local Display = self:GetDisplayText()

if Display == "y" then
draw.DrawText("ACCESS", "Trebuchet24", X+17, Y+7, Color(0, 255, 0, 255))
draw.DrawText("GRANTED","Trebuchet24", X+7, Y+27, Color(0, 255, 0, 255))
draw.DrawText("ACCESS", "WireKeypad", X + 17, Y + 7, color_green)
draw.DrawText("GRANTED", "WireKeypad", X + 7, Y + 27, color_green)
elseif Display == "n" then
draw.DrawText("ACCESS", "Trebuchet24", X+17, Y+7, Color(255, 0, 0, 255))
draw.DrawText("DENIED", "Trebuchet24", X+19, Y+27, Color(255, 0, 0, 255))
draw.DrawText("ACCESS", "WireKeypad", X + 17, Y + 7, color_red)
draw.DrawText("DENIED", "WireKeypad", X + 19, Y + 27, color_red)
else
draw.DrawText(Display, "Trebuchet34", X+17, Y+10, Color(255, 255, 255, 255))
draw.DrawText(Display, "WireKeypad_Big", X + 17, Y + 10, color_white)
end
cam.End3D2D()
end

hook.Add("KeyRelease", "Keypad_KeyReleased", function(Ply, key)
Ply.KeyOnce = false
hook.Add("KeyRelease", "Keypad_KeyReleased", function(ply, key)
ply.WireKeyPad_Pressed = false
end)

local binds = {
["+gm_special 1" ] = 1,
["+gm_special 2" ] = 2,
["+gm_special 3" ] = 3,
["+gm_special 4" ] = 4,
["+gm_special 5" ] = 5,
["+gm_special 6" ] = 6,
["+gm_special 7" ] = 7,
["+gm_special 8" ] = 8,
["+gm_special 9" ] = 9,
["+gm_special 1"] = 1,
["+gm_special 2"] = 2,
["+gm_special 3"] = 3,
["+gm_special 4"] = 4,
["+gm_special 5"] = 5,
["+gm_special 6"] = 6,
["+gm_special 7"] = 7,
["+gm_special 8"] = 8,
["+gm_special 9"] = 9,
["+gm_special 11"] = 11,
["+gm_special 12"] = 10,
}

hook.Add("PlayerBindPress", "keypad_PlayerBindPress", function(ply, bind, pressed)
if not pressed then return end

local command = binds[bind]
if not command then return end

local trace = ply:GetEyeTraceNoCursor()
local ent = trace.Entity
if not IsValid(ent) then return end

if ent:GetClass() ~= "gmod_wire_keypad" then return end
local ent = ply:GetEyeTraceNoCursor().Entity
if not IsValid(ent) or ent:GetClass() ~= "gmod_wire_keypad" then return end

net.Start("wire_keypad")
net.WriteEntity(ent)
net.WriteUInt(command, 4)
net.SendToServer()

highlight_key, highlight_until = command, CurTime()+0.5
highlight_key, highlight_until = command, CurTime() + 0.5

return true
end)

return
end -- No more client
end

util.PrecacheSound("buttons/button8.wav")
util.PrecacheSound("buttons/button9.wav")
util.PrecacheSound("buttons/button14.wav")
util.PrecacheSound("buttons/button15.wav")
util.AddNetworkString("wire_keypad")

function ENT:Initialize()
BaseClass.Initialize(self)

self.Outputs = WireLib.CreateOutputs(self, {"Valid", "Invalid"})

self.CurrentNum = 0
end

Expand All @@ -174,37 +185,40 @@ function ENT:Setup(password, securemode)
self.Secure = securemode
end

util.AddNetworkString("wire_keypad")
net.Receive("wire_keypad", function(netlen, ply)
net.Receive("wire_keypad", function(len, ply)
local ent = net.ReadEntity()
if not IsValid(ent) or not ent.Password then return end

if ent.CurrentNum == -1 then return end -- Display still shows ACCESS from a past success
if (ply:GetShootPos() - ent:GetPos()):Length() > 50 then return end
if ply:GetPos():Distance(ent:GetPos()) > 64 then return end
Astralcircle marked this conversation as resolved.
Show resolved Hide resolved

local key = net.ReadUInt(4)

if key == 10 then -- Reset
ent:SetNWString("keypad_display", "")
if key == 10 then
ent:SetDisplayText("")
ent:EmitSound("buttons/button14.wav")

ent.CurrentNum = 0
elseif key == 11 or ent.CurrentNum > 999 then -- Accept
local access = (ent.Password == util.CRC(ent.CurrentNum))
elseif key == 11 or ent.CurrentNum > 999 then
local access = ent.Password == util.CRC(ent.CurrentNum)

if access then
ent:SetNWString("keypad_display", "y")
Wire_TriggerOutput(ent, "Valid", 1)
ent:SetDisplayText("y")
ent:EmitSound("buttons/button9.wav")
else
ent:SetNWString("keypad_display", "n")
Wire_TriggerOutput(ent, "Invalid", 1)
ent:SetDisplayText("n")
ent:EmitSound("buttons/button8.wav")
end

ent.CurrentNum = -1
timer.Create("wire_keypad_"..ent:EntIndex().."_"..tostring(access), 2, 1, function()

timer.Simple(2, function()
if IsValid(ent) then
ent:SetNWString("keypad_display", "")
ent:SetDisplayText("")
ent.CurrentNum = 0

if access then
Wire_TriggerOutput(ent, "Valid", 0)
else
Expand All @@ -216,10 +230,11 @@ net.Receive("wire_keypad", function(netlen, ply)
ent.CurrentNum = ent.CurrentNum * 10 + key

if ent.Secure then
ent:SetNWString("keypad_display", string.rep("*", string.len(ent.CurrentNum)))
ent:SetDisplayText(string.rep("*", string.len(ent.CurrentNum)))
else
ent:SetNWString("keypad_display", tostring(ent.CurrentNum))
ent:SetDisplayText(tostring(ent.CurrentNum))
end

ent:EmitSound("buttons/button15.wav")
end
end)
Expand Down
Loading