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

Improve user-generated entity/wirelink outputs #2891

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions lua/entities/gmod_wire_egp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ function ENT:Initialize()

self.RenderTable = {}

WireLib.CreateOutputs( self, { "User (Outputs the player who used the screen for a single tick) [ENTITY]" } )
WireLib.CreateWirelinkOutput( nil, self, {true} )
WireLib.CreateOutputs( self, { "User (Outputs the player who used the screen for a single tick) [ENTITY]", "wirelink [WIRELINK]" } )

self.xScale = { 0, 512 }
self.yScale = { 0, 512 }
Expand Down
6 changes: 4 additions & 2 deletions lua/entities/gmod_wire_egp_hud/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ function ENT:Initialize()
self:SetUseType(SIMPLE_USE)
self:AddEFlags( EFL_FORCE_CHECK_TRANSMIT )

self.Inputs = WireLib.CreateInputs( self, { "0 to 512 (If enabled, changes the resolution of the egp hud to be between 0 and 512 instead of the user's monitor's resolution.\nWill cause objects to look stretched out on most screens, so your UI will need to be designed with this in mind.\nIt's recommended to use the egpScrW, egpScrH, and egpScrSize functions instead.)" } )
WireLib.CreateWirelinkOutput( nil, self, {true} )
self.Inputs = WireLib.CreateInputs( self, {
"0 to 512 (If enabled, changes the resolution of the egp hud to be between 0 and 512 instead of the user's monitor's resolution.\nWill cause objects to look stretched out on most screens, so your UI will need to be designed with this in mind.\nIt's recommended to use the egpScrW, egpScrH, and egpScrSize functions instead.)",
"wirelink [WIRELINK]"
})

self.xScale = { 0, 512 }
self.yScale = { 0, 512 }
Expand Down
5 changes: 1 addition & 4 deletions lua/entities/gmod_wire_expression2/core/custom/wiring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ __e2setcost(5)
e2function wirelink entity:wirelink()
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end

if not this.extended then
WireLib.CreateWirelinkOutput( self.player, this, {true} )
end

return this
end

Expand Down
3 changes: 0 additions & 3 deletions lua/entities/gmod_wire_expression2/core/wirelink.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,6 @@

--- Return E2 wirelink -- and create it if none created yet
e2function wirelink wirelink()
if not self.entity.extended then
WireLib.CreateWirelinkOutput( self.player, self.entity, {true} )
end
Comment on lines -279 to -281
Copy link
Contributor

@Fasteroid Fasteroid Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2940, just posting review to make it official

return self.entity
end

Expand All @@ -297,7 +294,7 @@
--- Returns an array of all the inputs that <this> has without their types. Returns an empty array if it has none
e2function array wirelink:inputs()
if not validWirelink(self, this) then return {} end
if(!this.Inputs) then return {} end

Check warning on line 297 in lua/entities/gmod_wire_expression2/core/wirelink.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of '!' and 'not'

local InputNames = {}
for k,v in pairs_sortvalues(this.Inputs, WireLib.PortComparator) do
Expand All @@ -309,7 +306,7 @@
--- Returns an array of all the outputs that <this> has without their types. Returns an empty array if it has none
e2function array wirelink:outputs()
if not validWirelink(self, this) then return {} end
if(!this.Outputs) then return {} end

Check warning on line 309 in lua/entities/gmod_wire_expression2/core/wirelink.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of '!' and 'not'

local OutputNames = {}
for k,v in pairs_sortvalues(this.Outputs, WireLib.PortComparator) do
Expand All @@ -321,7 +318,7 @@
--- Returns the type of input that <Input> is in lowercase. ( "NORMAL" is changed to "number" )
e2function string wirelink:inputType(string Input)
if not validWirelink(self, this) then return "" end
if(!this.Inputs or !this.Inputs[Input]) then return "" end

Check warning on line 321 in lua/entities/gmod_wire_expression2/core/wirelink.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of '!' and 'not'

local Type = this.Inputs[Input].Type or ""
if Type == "NORMAL" then Type = "number" end
Expand All @@ -330,7 +327,7 @@

--- Returns the type of output that <Output> is in lowercase. ( "NORMAL" is changed to "number" )
e2function string wirelink:outputType(string Output)
if not validWirelink(self, this) then return "" end

Check warning on line 330 in lua/entities/gmod_wire_expression2/core/wirelink.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'not' and '!'
if(!this.Outputs or !this.Outputs[Output]) then return "" end

local Type = this.Outputs[Output].Type or ""
Expand All @@ -344,7 +341,7 @@

[deprecated]
e2function number wirelink:writeCell(address, value)
if not validWirelink(self, this) then return 0 end

Check warning on line 344 in lua/entities/gmod_wire_expression2/core/wirelink.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'not' and '!'

if not this.WriteCell then return 0 end
if this:WriteCell(address, value) then return 1 else return 0 end
Expand Down
20 changes: 6 additions & 14 deletions lua/weapons/gmod_tool/stools/wire_adv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@
-- Duplicator modifiers
-----------------------------------------------------------------
function WireLib.CreateWirelinkOutput( ply, ent, data )
duplicator.StoreEntityModifier(ent, "CreateWirelinkOutput", data)
if data[1] == true then
if ent.Outputs then
local names = {}
local types = {}
local descs = {}
local x = 0
for k,v in pairs( ent.Outputs ) do
for _,v in pairs( ent.Outputs ) do
x = x + 1
local num = v.Num
names[num] = v.Name
Expand All @@ -74,30 +75,26 @@
descs[num] = v.Desc
end

names[x+1] = "wirelink"
types[x+1] = "WIRELINK"
descs[x+1] = ""

WireLib.AdjustSpecialOutputs( ent, names, types, descs )
WireLib.AdjustSpecialOutputs(ent, names, types, descs)
else
WireLib.CreateSpecialOutputs( ent, { "wirelink" }, { "WIRELINK" } )
end

ent.extended = true
WireLib.TriggerOutput( ent, "wirelink", ent )
end
duplicator.StoreEntityModifier( ent, "CreateWirelinkOutput", data )
end
duplicator.RegisterEntityModifier( "CreateWirelinkOutput", WireLib.CreateWirelinkOutput )

function WireLib.CreateEntityOutput( ply, ent, data )
duplicator.StoreEntityModifier(ent, "CreateEntityOutput", data)
if data[1] == true then
if ent.Outputs then
local names = {}
local types = {}
local descs = {}
local x = 0
for k,v in pairs( ent.Outputs ) do
for _,v in pairs( ent.Outputs ) do
x = x + 1
local num = v.Num
names[num] = v.Name
Expand All @@ -106,18 +103,13 @@
descs[num] = v.Desc
end

names[x+1] = "entity"
types[x+1] = "ENTITY"
descs[x+1] = ""

WireLib.AdjustSpecialOutputs( ent, names, types, descs )
else
WireLib.CreateSpecialOutputs( ent, { "entity" }, { "ENTITY" } )
end

WireLib.TriggerOutput( ent, "entity", ent )
end
duplicator.StoreEntityModifier( ent, "CreateEntityOutput", data )
end
duplicator.RegisterEntityModifier( "CreateEntityOutput", WireLib.CreateEntityOutput )

Expand Down Expand Up @@ -249,7 +241,7 @@
if outputs then
local found = false
for i=1,#outputs do
if outputs[i][2] == "WIRELINK" then found = true break end
if outputs[i] and outputs[i][2] == "WIRELINK" then found = true break end
end
if not found then
outputs = table.Copy(outputs) -- we don't want to modify the original table
Expand Down Expand Up @@ -659,7 +651,7 @@
if not game.SinglePlayer() and not IsFirstTimePredicted() then return end

if self:GetStage() == 0 and IsValid( trace.Entity ) and WireLib.HasPorts( trace.Entity ) then
local inputs, outputs = self:GetPorts( trace.Entity )

Check warning on line 654 in lua/weapons/gmod_tool/stools/wire_adv.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: outputs
if not isTableEmpty(inputs) then return end
if self:GetOwner():KeyDown( IN_WALK ) then
local t = {}
Expand All @@ -686,7 +678,7 @@
local check = self:GetStage() == 0 and inputs or outputs
if #check == 0 then return end

local b = false

Check warning on line 681 in lua/weapons/gmod_tool/stools/wire_adv.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: b
local oldport = self.CurrentWireIndex

if self:GetStage() == 2 then
Expand Down Expand Up @@ -884,7 +876,7 @@
return self.CurrentWireIndex == idx -- Highlight selected output
end
elseif name == "Selected" and self:GetStage() == 2 and alt then -- highlight all selected inputs that will be wired
local inputs, outputs = self:GetPorts( ent )

Check warning on line 879 in lua/weapons/gmod_tool/stools/wire_adv.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: inputs

local inputname = tbl[idx][1]
local inputtype = tbl[idx][2]
Expand Down Expand Up @@ -919,7 +911,7 @@
end
return true
elseif name == "Selected" and self:GetStage() == 2 then
local inputs, outputs = self:GetPorts( ent )

Check warning on line 914 in lua/weapons/gmod_tool/stools/wire_adv.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: inputs
if not isTableEmpty(outputs) then return false end
if self:GetOwner():KeyDown( IN_WALK ) then -- Gray out the ones that won't be able to be wired to any input
for i=1,#outputs do
Expand Down Expand Up @@ -1246,7 +1238,7 @@
return (outputname == inputname and outputtype == inputtype) or (inputtype == "WIRELINK" and (outputname == "wirelink" or outputname == "Create Wirelink") and outputtype == "WIRELINK") or
(inputtype == "ENTITY" and (outputname == "entity" or outputname == "Create Entity") and outputtype == "ENTITY")
else
return (outputname == inputname and outputtype == inputtype)

Check warning on line 1241 in lua/weapons/gmod_tool/stools/wire_adv.lua

View workflow job for this annotation

GitHub Actions / lint

"Unnecessary parentheses"

Unnecessary parentheses
end
end

Expand Down
69 changes: 69 additions & 0 deletions lua/weapons/gmod_tool/stools/wire_outputremover.lua
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do something like shift+r with the wire tool removes outputs? Making a whole new tool for this is kind of messy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so. I didn't want to touch the wire tool because I figured it had enough features.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
TOOL.Category = "Tools"
TOOL.Name = "Output Remover"
TOOL.Tab = "Wire"

if CLIENT then
language.Add("Tool.wire_outputremover.name", "Wire Output Remover")
language.Add("Tool.wire_outputremover.desc", "Removes auto-generated entity or wirelink outputs.")
language.Add("Tool.wire_outputremover.desc2", "Used to connect wirable props.")
language.Add("Tool.wire_outputremover.left", "Remove entity output")
language.Add("Tool.wire_outputremover.right","Remove wirelink output")
language.Add("Tool.wire_outputremover.reload", "Remove both")
TOOL.Information = { "left", "right", "reload" }

TOOL.Wire_ToolMenuIcon = "icon16/disconnect.png"
end

if SERVER then
local function removeWirelinkOutput(ent)
if ent.EntityMods and ent.EntityMods.CreateWirelinkOutput and ent.Outputs and ent.Outputs.wirelink then
WireLib.DisconnectOutput(ent, "wirelink")
ent.Outputs.wirelink = nil
WireLib.RemoveOutPort(ent, "wirelink")
duplicator.ClearEntityModifier(ent, "CreateWirelinkOutput")
WireLib._SetOutputs(ent)
end
end

local function removeEntityOutput(ent)
if ent.EntityMods and ent.EntityMods.CreateEntityOutput and ent.Outputs and ent.Outputs.entity then
WireLib.DisconnectOutput(ent, "entity")
ent.Outputs.entity = nil
WireLib.RemoveOutPort(ent, "entity")
duplicator.ClearEntityModifier(ent, "CreateEntityOutput")
WireLib._SetOutputs(ent)
end
end

function TOOL:LeftClick(trace)
local ent = trace.Entity
if ent:IsValid() then
removeEntityOutput(ent)
return true
end
return false
end

function TOOL:RightClick(trace)
local ent = trace.Entity
if ent:IsValid() then
removeWirelinkOutput(ent)
return true
end
return false
end

function TOOL:Reload(trace)
local ent = trace.Entity
if ent:IsValid() then
removeEntityOutput(ent)
removeWirelinkOutput(ent)
return true
end
return false
end
else
function TOOL.BuildCPanel(panel)
panel:AddControl("Header", { Text = "#Tool.wire_outputremover.name", Description = "#Tool.wire_outputremover.desc" })
end
end
26 changes: 20 additions & 6 deletions lua/wire/server/wirelib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,25 @@ function WireLib.AdjustSpecialOutputs(ent, names, types, descs)

local ent_ports = ent.Outputs or {}

if ent_ports.wirelink then
local n = #names+1
local ent_mods = ent.EntityMods
if ent_mods then
local n = #names
if ent_mods.CreateEntityOutput then
n = n + 1

names[n] = "entity"
types[n] = "ENTITY"
end
if ent_mods.CreateWirelinkOutput then
n = n + 1

names[n] = "wirelink"
types[n] = "WIRELINK"
names[n] = "wirelink"
types[n] = "WIRELINK"
end
end


local i = 0
for n,v in ipairs(names) do
local name, desc, tp = ParsePortName(v, types[n] or "NORMAL", descs and descs[n])

Expand All @@ -352,10 +364,11 @@ function WireLib.AdjustSpecialOutputs(ent, names, types, descs)
WireLib.DisconnectOutput(ent, name)
ent_ports[name].Type = tp
end
WireLib.RemoveOutPort(ent, name)
ent_ports[name].Keep = true
ent_ports[name].Num = n
ent_ports[name].Desc = desc
else
i = i + 1
local port = {
Keep = true,
Name = name,
Expand All @@ -364,7 +377,7 @@ function WireLib.AdjustSpecialOutputs(ent, names, types, descs)
Value = WireLib.GetDefaultForType(tp),
Connected = {},
TriggerLimit = 8,
Num = n,
Num = i,
}

local idx = 1
Expand All @@ -383,6 +396,7 @@ function WireLib.AdjustSpecialOutputs(ent, names, types, descs)
port.Keep = nil
else
WireLib.DisconnectOutput(ent, portname)
WireLib.RemoveOutPort(ent, portname)
ent_ports[portname] = nil
end
end
Expand Down
11 changes: 11 additions & 0 deletions lua/wire/wireshared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,17 @@ if SERVER then
return ents_with_inputs[eid], ents_with_outputs[eid]
end

function WireLib.RemoveOutPort(ent, name)
local outputs = ents_with_outputs[ent:EntIndex()]
if outputs then
for k, v in ipairs(outputs) do
if v[1] == name then
table.remove(outputs, k)
end
end
end
end

function WireLib._RemoveWire(eid, DontSend) -- To remove the inputs without to remove the entity. Very important for IsWire checks!
local hasinputs, hasoutputs = ents_with_inputs[eid], ents_with_outputs[eid]
if hasinputs or hasoutputs then
Expand Down
Loading