diff --git a/lua/entities/gmod_wire_egp/lib/egplib/objectcontrol.lua b/lua/entities/gmod_wire_egp/lib/egplib/objectcontrol.lua deleted file mode 100644 index 67ee1274a9..0000000000 --- a/lua/entities/gmod_wire_egp/lib/egplib/objectcontrol.lua +++ /dev/null @@ -1,343 +0,0 @@ --------------------------------------------------------- --- Objects --------------------------------------------------------- -local EGP = EGP - -local egpObjects = {} -egpObjects.Names = {} -egpObjects.Names_Inverted = {} -EGP.Objects = egpObjects - --- This object is not used. It's only a base -local baseObj = {} -baseObj.ID = 0 -baseObj.x = 0 -baseObj.y = 0 -baseObj.angle = 0 -baseObj.r = 255 -baseObj.g = 255 -baseObj.b = 255 -baseObj.a = 255 -baseObj.filtering = TEXFILTER.ANISOTROPIC -baseObj.parent = 0 -if SERVER then - baseObj.material = "" - baseObj.EGP = NULL -- EGP entity parent -else - baseObj.material = false -end -function baseObj:Transmit() - EGP.SendPosAng(self) - EGP:SendColor( self ) - EGP:SendMaterial(self) - if self.filtering then net.WriteUInt(math.Clamp(self.filtering,0,3), 2) end - net.WriteInt( self.parent, 16 ) -end -function baseObj:Receive() - local tbl = {} - EGP.ReceivePosAng(tbl) - EGP:ReceiveColor( tbl, self ) - EGP:ReceiveMaterial( tbl ) - if self.filtering then tbl.filtering = net.ReadUInt(2) end - tbl.parent = net.ReadInt(16) - return tbl -end -function baseObj:DataStreamInfo() - return { x = self.x, y = self.y, angle = self.angle, w = self.w, h = self.h, r = self.r, g = self.g, b = self.b, a = self.a, material = self.material, parent = self.parent } -end -function baseObj:Contains(x, y) - return false -end -function baseObj:EditObject(args) - local ret = false - if args.x or args.y or args.angle then - ret = self:SetPos(args.x or self.x, args.y or self.y, args.angle or self.angle) - args.x, args.y, args.angle = nil, nil, nil - end - for k, v in pairs(args) do - if self[k] ~= nil and self[k] ~= v then - self[k] = v - ret = true - end - end - return ret -end -baseObj.Initialize = baseObj.EditObject -function baseObj:SetPos(x, y, angle) - local ret = false - if x and self.x ~= x then self.x, ret = x, true end - if y and self.y ~= y then self.y, ret = y, true end - if angle then - angle = angle % 360 - if self.angle ~= angle then self.angle, ret = angle, true end - end - if SERVER and self._x then - if x then self._x = x end - if y then self._y = y end - if angle then self._angle = angle end - end - return ret -end -function baseObj:Set(member, value) - if self[member] and self[member] ~= value then - self[member] = value - return true - else - return false - end -end -local M_EGPObject = {__tostring = function(self) return "[EGPObject] ".. self.Name end} -setmetatable(baseObj, M_EGPObject) -EGP.Objects.Base = baseObj - -local M_NULL_EGPOBJECT = { __tostring = function(self) return "[EGPObject] NULL" end, __eq = function(a, b) return getmetatable(a) == getmetatable(b) end } -local NULL_EGPOBJECT = setmetatable({}, M_NULL_EGPOBJECT) -EGP.NULL_EGPOBJECT = NULL_EGPOBJECT - ----------------------------- --- Get Object ----------------------------- - -function EGP:GetObjectByID( ID ) - for _, v in pairs( EGP.Objects ) do - if (v.ID == ID) then return table.Copy( v ) end - end - ErrorNoHalt( "[EGP] Error! Object with ID '" .. ID .. "' does not exist. Please post this bug message in the EGP thread on the wiremod forums.\n" ) -end - ----------------------------- --- Load all objects ----------------------------- - -function EGP:NewObject(name, super) - local lower = name:lower() -- Makes my life easier - if not super then super = baseObj end - if self.Objects[lower] then return self.Objects[lower] end - - -- Create table - self.Objects[lower] = {} - -- Set info - self.Objects[lower].Name = name - table.Inherit(self.Objects[lower], super) - - -- Create lookup table - local ID = table.Count(self.Objects) - self.Objects[lower].ID = ID - self.Objects.Names[name] = ID - - -- Inverted lookup table - self.Objects.Names_Inverted[ID] = lower - - return setmetatable(self.Objects[lower], M_EGPObject) -end - -local folder = "entities/gmod_wire_egp/lib/objects/" - -function EGP.ObjectInherit(to, from) - local super = egpObjects[from:lower()] - if super then - return EGP:NewObject(to, super) - else - local path = folder .. from .. ".lua" - if file.Exists(path, "LUA") then - super = include(path) - AddCSLuaFile(path) - return EGP:NewObject(to, super) - else - ErrorNoHalt(string.format("EGP couldn't find object '%s' to inherit from (to object '%s').\n", from, to)) - end - end -end - -do - local files = file.Find(folder.."*.lua", "LUA") - for _, v in ipairs(files) do - if not egpObjects[v:sub(1, #v - 4):lower()] then -- Remove the extension and check if the object already exists. - include(folder .. v) - AddCSLuaFile(folder .. v) - end - end -end - ----------------------------- --- Object existance check ----------------------------- -function EGP:HasObject( Ent, index ) - if not EGP:ValidEGP(Ent) then return false end - if SERVER then index = math.Round(math.Clamp(index or 1, 1, self.ConVars.MaxObjects:GetInt())) end - if not Ent.RenderTable or #Ent.RenderTable == 0 then return false end - for k,v in pairs( Ent.RenderTable ) do - if (v.index == index) then - return true, k, v - end - end - return false -end - ----------------------------- --- Object order changing ----------------------------- -function EGP:SetOrder(ent, from, to, dir) - if not ent.RenderTable or #ent.RenderTable == 0 then return false end - dir = dir or 0 - - if ent.RenderTable[from] then - to = math.Clamp(math.Round(to or 1),1,#ent.RenderTable) - if SERVER then ent.RenderTable[from].ChangeOrder = {target=to,dir=dir} end - return true - end - return false -end - -local already_reordered = {} -function EGP:PerformReorder_Ex(ent, originIdx, maxn) - local obj = ent.RenderTable[originIdx] - local idx = obj.index - if obj then - -- Check if this object has already been reordered - if already_reordered[idx] then - -- if yes, get its new position (or old position if it didn't change) - return already_reordered[idx] - end - - -- Set old position (to prevent recursive loops) - already_reordered[idx] = originIdx - - if obj.ChangeOrder then - local target = obj.ChangeOrder.target - local dir = obj.ChangeOrder.dir - - local targetIdx = 0 - if dir == 0 then - -- target is absolute position - targetIdx = target - else - -- target is relative position - local bool, k = self:HasObject(ent, target) - if bool then - -- Check for order dependencies - k = self:PerformReorder_Ex(ent, k, maxn) or k - - targetIdx = k + dir - else - targetIdx = target - end - end - - if targetIdx > 0 then - -- Make a copy of the object and insert it at the new position - targetIdx = math.Clamp(targetIdx, 1, maxn) - if originIdx ~= targetIdx then - local ob = table.remove(ent.RenderTable, originIdx) - table.insert(ent.RenderTable, targetIdx, ob) - end - - obj.ChangeOrder = nil - - -- Update already reordered reference to new position - already_reordered[idx] = targetIdx - - return targetIdx - else - return originIdx - end - end - end -end - -function EGP:PerformReorder(ent) - -- Reset, just to be sure - already_reordered = {} - - -- Now we remove and create at the same time! - local maxn = #ent.RenderTable - for i, _ in ipairs(ent.RenderTable) do - self:PerformReorder_Ex(ent, i, maxn) - end - - -- Clear some memory - already_reordered = {} -end - ----------------------------- --- Create / edit objects ----------------------------- - -function EGP:CreateObject( Ent, ObjID, Settings ) - if not self:ValidEGP(Ent) then return false, NULL_EGPOBJECT end - - if not self.Objects.Names_Inverted[ObjID] then - ErrorNoHalt("Trying to create nonexistant object! Please report this error to Divran at wiremod.com. ObjID: " .. ObjID .. "\n") - return false, NULL_EGPOBJECT - end - - if SERVER then Settings.index = math.Round(math.Clamp(Settings.index or 1, 1, self.ConVars.MaxObjects:GetInt())) end - Settings.EGP = Ent - - local bool, k, v = self:HasObject( Ent, Settings.index ) - if (bool) then -- Already exists. Change settings: - if v.ID ~= ObjID then -- Not the same kind of object, create new - local Obj = self:GetObjectByID( ObjID ) - Obj:Initialize(Settings) - Obj.index = Settings.index - Ent.RenderTable[k] = Obj - return true, Obj - else - return v:EditObject(Settings), v - end - else -- Did not exist. Create: - local Obj = self:GetObjectByID( ObjID ) - Obj:Initialize(Settings) - Obj.index = Settings.index - table.insert( Ent.RenderTable, Obj ) - return true, Obj - end -end - -function EGP:EditObject(obj, settings) - return obj:EditObject(settings) -end - - - --------------------------------------------------------- --- Homescreen --------------------------------------------------------- - -EGP.HomeScreen = {} - -local mat -if CLIENT then mat = Material else mat = function( str ) return str end end - --- Create table -local tbl = { - { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, h = 356, w = 356, material = mat("expression 2/cog"), r = 150, g = 34, b = 34, a = 255 } }, - { ID = EGP.Objects.Names["Text"], Settings = {x = 256, y = 256, text = "EGP 3", font = "WireGPU_ConsoleFont", valign = 1, halign = 1, size = 50, r = 135, g = 135, b = 135, a = 255 } } -} - ---[[ Old homescreen (EGP v2 home screen design contest winner) -local tbl = { - { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 362, h = 362, material = true, angle = 135, r = 75, g = 75, b = 200, a = 255 } }, - { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 340, h = 340, material = true, angle = 135, r = 10, g = 10, b = 10, a = 255 } }, - { ID = EGP.Objects.Names["Text"], Settings = { x = 229, y = 28, text = "E", size = 100, fontid = 4, r = 200, g = 50, b = 50, a = 255 } }, - { ID = EGP.Objects.Names["Text"], Settings = { x = 50, y = 200, text = "G", size = 100, fontid = 4, r = 200, g = 50, b = 50, a = 255 } }, - { ID = EGP.Objects.Names["Text"], Settings = { x = 400, y = 200, text = "P", size = 100, fontid = 4, r = 200, g = 50, b = 50, a = 255 } }, - { ID = EGP.Objects.Names["Text"], Settings = { x = 228, y = 375, text = "2", size = 100, fontid = 4, r = 200, g = 50, b = 50, a = 255 } }, - { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 256, h = 256, material = mat("expression 2/cog"), angle = 45, r = 255, g = 50, b = 50, a = 255 } }, - { ID = EGP.Objects.Names["Box"], Settings = { x = 128, y = 241, w = 256, h = 30, material = true, r = 10, g = 10, b = 10, a = 255 } }, - { ID = EGP.Objects.Names["Box"], Settings = { x = 241, y = 128, w = 30, h = 256, material = true, r = 10, g = 10, b = 10, a = 255 } }, - { ID = EGP.Objects.Names["Circle"], Settings = { x = 256, y = 256, w = 70, h = 70, material = true, r = 255, g = 50, b = 50, a = 255 } }, - { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 362, h = 362, material = mat("gui/center_gradient"), angle = 135, r = 75, g = 75, b = 200, a = 75 } }, - { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 362, h = 362, material = mat("gui/center_gradient"), angle = 135, r = 75, g = 75, b = 200, a = 75 } } -} -]] - --- Convert table -for k,v in pairs( tbl ) do - local obj = EGP:GetObjectByID( v.ID ) - obj.index = k - for k2,v2 in pairs( v.Settings ) do - if obj[k2] ~= nil then obj[k2] = v2 end - end - table.insert( EGP.HomeScreen, obj ) -end diff --git a/lua/entities/gmod_wire_egp/lib/egplib/objects.lua b/lua/entities/gmod_wire_egp/lib/egplib/objects.lua new file mode 100644 index 0000000000..5e36965e8f --- /dev/null +++ b/lua/entities/gmod_wire_egp/lib/egplib/objects.lua @@ -0,0 +1,464 @@ +local EGP = E2Lib.EGP +---@type { [string]: EGPObject, [integer]: EGPObject } +local objects = {} +--- All implemented objects. The array part represents EGP Object IDs. The table part represents EGP Object names. +EGP.Objects = objects + +local validEGP +local maxObjects +EGP.HookPostInit(function() + validEGP = EGP.ValidEGP + maxObjects = EGP.ConVars.MaxObjects +end) + +---@class EGPObject +---@field ID integer? nil when NULL +---@field index integer +local baseObj = { + ID = 0, + x = 0, + y = 0, + angle = 0, + r = 255, + g = 255, + b = 255, + a = 255, + filtering = TEXFILTER.ANISOTROPIC, + parent = 0 +} +if SERVER then + baseObj.material = "" + baseObj.EGP = NULL --[[@as Entity]] -- EGP entity parent +else + baseObj.material = false +end + +--- Used in a net writing context to transmit the object's entire data. +---@see EGPObject.Receive +function baseObj:Transmit() + EGP.SendPosAng(self) + EGP.SendColor(nil, self) + EGP.SendMaterial(nil, self) + if self.filtering then net.WriteUInt(math.Clamp(self.filtering, 0, 3), 2) end + net.WriteInt(self.parent, 16) +end +--- Used in a net reading context to read the object's entire data. +---@see EGPObject.Transmit +function baseObj:Receive() + local tbl = {} + EGP.ReceivePosAng(tbl) + EGP.ReceiveColor(nil, tbl, self) + EGP.ReceiveMaterial(nil, tbl) + if self.filtering then tbl.filtering = net.ReadUInt(2) end + tbl.parent = net.ReadInt(16) + return tbl +end + +--- Returns a table of data that needs to be transferred in EGP messages. +function baseObj:DataStreamInfo() + return { x = self.x, y = self.y, angle = self.angle, r = self.r, g = self.g, b = self.b, a = self.a, material = self.material, parent = self.parent } +end +--- Returns `true` if the object contains the point. +---@param x number +---@param y number +---@return boolean +function baseObj:Contains(x, y) + return false +end + +if false then + --- Called when the object is removed. + function baseObj:OnRemove() end +end + +--- Edits the fields of the EGPObject with the given table. Returns `true` if a field changed. +--- Use `SetPos` for setting position directly. Use `Set` to set a single field. +---@param args { [string]: any } The fields to edit on the object. Values are *not* guaranteed to be type checked or sanity checked! +---@return boolean # Whether the object changed +---@see EGPObject.SetPos +---@see EGPObject.Set +function baseObj:EditObject(args) + local ret = false + if args.x or args.y or args.angle then + ret = self:SetPos(args.x or self.x, args.y or self.y, args.angle or self.angle) + args.x, args.y, args.angle = nil, nil, nil + end + for k, v in pairs(args) do + if self[k] ~= nil and self[k] ~= v then + self[k] = v + ret = true + end + end + return ret +end + +--- A helper method for objects that may need to do something on initialization. Calls `EditObject` by default. +---@param args { [string]: any } +---@see EGPObject.EditObject +function baseObj:Initialize(args) self:EditObject(args) end + +--- Sets the position of the EGPObject directly. This method should be overridden if special behavior is needed. +--- Call this method when you need to change position. +---@param x number +---@param y number +---@param angle number In degrees +---@return boolean # Whether the position changed +---@see EGPObject.EditObject +function baseObj:SetPos(x, y, angle) + local ret = false + if x and self.x ~= x then self.x, ret = x, true end + if y and self.y ~= y then self.y, ret = y, true end + if angle then + angle = angle % 360 + if self.angle ~= angle then self.angle, ret = angle, true end + end + if SERVER and self._x then + if x then self._x = x end + if y then self._y = y end + if angle then self._angle = angle end + end + return ret +end + +--- Sets a single field of the EGP Object. Do **not** use this for position. Use `SetPos` instead. +---@param k string +---@param v any +---@return boolean # Whether the field changed +function baseObj:Set(k, v) + local kx, ky, ka = k == "x", k == "y", k == "angle" + if kx or ky or ka then + return self:SetPos(kx and v or self.x, ky and v or self.y, ka and v or self.angle) + end + if self[k] and self[k] ~= v then + self[k] = v + return true + else + return false + end +end + +local EGPObject = {} +EGPObject.__index = EGPObject +function EGPObject:__tostring() + return "[EGPObject] ".. (self.Name or "NULL") +end +function EGPObject:__eq(a, b) + return a and b and a.ID == b.ID +end +function EGPObject:IsValid() + return self and self.ID ~= nil +end + +-- The EGPObject metatable +EGP.EGPObject = EGPObject +setmetatable(baseObj, EGPObject) +objects.Base = baseObj + +---@type EGPObject +local NULL_EGPOBJECT = setmetatable({}, EGPObject) +--- An invalid EGPObject +EGP.Objects.NULL_EGPOBJECT = NULL_EGPOBJECT + +--- Returns true if the input is an EGP Object +local function isEGPObject(obj) + return istable(obj) and getmetatable(obj) == EGPObject +end +EGP.IsEGPObject = isEGPObject + +---------------------------- +-- Load all objects +---------------------------- +do + local yielded = {} + + ---@generic NewEGPObject:EGPObject + --- Creates a new EGPObject class and returns it reference. If you want to inherit from another class, see `EGP.ObjectInherit`, which properly handles out-of-order loading. + ---@param name string The name of the class. Case sensitive. + ---@param super EGPObject? The superclass of the class. If nil, defaults to base object. + ---@return NewEGPObject # The EGPObject class + ---@see EGP.ObjectInherit + local function newObject(name, super) + if objects[name] then return objects[name] end + + if not super then super = baseObj end + + local newObj = {} + + newObj.Name = name + table.Inherit(newObj, super) + + local ID = #objects + 1 + newObj.ID = ID + + newObj = setmetatable(newObj, EGPObject) + + objects[name] = newObj + objects[ID] = newObj + + return newObj + end + EGP.NewObject = newObject + + ---@generic NewEGPObject:EGPObject + --- Used to inherit from another EGPObject class. This uses EGP.NewObject internally, so you should not call that. + ---@param to string The new class name + ---@param from string The superclass name + ---@return NewEGPObject # The EGPObject class with inheritance + function EGP.ObjectInherit(to, from) + local super = objects[from] + if super then + return newObject(to, super) + else + error({ from }) + end + end + + local FOLDER = "entities/gmod_wire_egp/lib/objects/" + local files = file.Find(FOLDER .. "*.lua", "LUA") + for _, v in ipairs(files) do + local p = FOLDER .. v + local fn = CompileFile(p) + local wrap = function() + local ok, super = pcall(fn) + if ok then + AddCSLuaFile(p) + return + elseif istable(super) then + return super[1] + else + ErrorNoHalt(super .. "\n") -- Rethrow the error + end + end + local ret = wrap() + if ret ~= nil then + local t = yielded[ret] + if not t then + t = {} + yielded[ret] = t + end + table.insert(t, wrap) + end + end + + for name, t in pairs(yielded) do + if objects[name] then + for _, v in ipairs(t) do + if yielded[name] then + v() + end + end + else + ErrorNoHalt("EGP Error: Missing dependency " .. name .. ". " .. #t .. " objects will not be loaded.\n") + end + end +end + +--- Checks if the object exists on the screen. +---@param egp Entity +---@param index EGPObject|integer The EGPObject or EGP index to find +---@return boolean found Whether the object exists +---@return integer? index The Render Table index if it exists +---@return EGPObject? object The found EGPObject if it exists +local function hasObject(egp, index) + if not validEGP(nil, egp) then return false end + local renderTable = egp.RenderTable + if not renderTable or #renderTable == 0 then return false end + + if isEGPObject(index) then + if index.EGP == egp then + index = index.index + else + return false + end + end + ---@cast index -EGPObject + + if SERVER then index = math.Round(math.Clamp(index or 1, 1, maxObjects:GetInt())) end + + for k, v in ipairs(renderTable) do + if v.index == index then + return true, k, v + end + end + return false +end +EGP.HasObject = hasObject + +---------------------------- +-- Object order changing +---------------------------- + +function EGP.SetOrder(ent, from, to, dir) + if not ent.RenderTable or #ent.RenderTable == 0 then return false end + dir = dir or 0 + + if ent.RenderTable[from] then + to = math.Clamp(math.Round(to or 1), 1, #ent.RenderTable) + if SERVER then ent.RenderTable[from].ChangeOrder = { target = to, dir = dir } end + return true + end + return false +end + +local already_reordered = {} +local function performReorder_ex(ent, originIdx, maxn) + local obj = ent.RenderTable[originIdx] + local idx = obj.index + if obj then + -- Check if this object has already been reordered + if already_reordered[idx] then + -- if yes, get its new position (or old position if it didn't change) + return already_reordered[idx] + end + + -- Set old position (to prevent recursive loops) + already_reordered[idx] = originIdx + + if obj.ChangeOrder then + local target = obj.ChangeOrder.target + local dir = obj.ChangeOrder.dir + + local targetIdx = 0 + if dir == 0 then + -- target is absolute position + targetIdx = target + else + -- target is relative position + local bool, k = hasObject(ent, target) + if bool then + -- Check for order dependencies + k = performReorder_ex(ent, k, maxn) or k + + targetIdx = k + dir + else + targetIdx = target + end + end + + if targetIdx > 0 then + -- Make a copy of the object and insert it at the new position + targetIdx = math.Clamp(targetIdx, 1, maxn) + if originIdx ~= targetIdx then + local ob = table.remove(ent.RenderTable, originIdx) + table.insert(ent.RenderTable, targetIdx, ob) + end + + obj.ChangeOrder = nil + + -- Update already reordered reference to new position + already_reordered[idx] = targetIdx + + return targetIdx + else + return originIdx + end + end + end +end + +function EGP.PerformReorder(ent) + -- Reset, just to be sure + already_reordered = {} + + -- Now we remove and create at the same time! + local maxn = #ent.RenderTable + for i, _ in ipairs(ent.RenderTable) do + performReorder_ex(ent, i, maxn) + end + + -- Clear some memory + already_reordered = {} +end + +---------------------------- +-- Create / edit objects +---------------------------- + +--- Attempts to create an instance of an EGPObject. Returns `true` and the object if the operation succeeded. +---@param id string|integer The EGPObject class name or ID +---@param settings { [string]: any } The data to initialize the object with +---@param egp Entity The EGP to create the object on +---@return boolean +---@return EGPObject +local function create(id, settings, egp) + if not validEGP(nil, egp) then return false, NULL_EGPOBJECT end + + local class = objects[id] + if not class then + ErrorNoHalt(string.format("Trying to create nonexistant object! Object %s: %s\n", isnumber(id) and "ID" or "name", + isnumber(id) and tostring(id) or id)) + return false, NULL_EGPOBJECT + end + + if SERVER then settings.index = math.Round(math.Clamp(settings.index or 1, 1, maxObjects:GetInt())) end + settings.EGP = egp + + local index = settings.index + settings.index = nil + + local bool, k, obj = hasObject(egp, index) + if bool then -- Already exists. Change settings: + ---@cast obj -? + if obj.ID ~= class.ID then -- Not the same kind of object, create new + obj = table.Copy(class) + obj:Initialize(settings) + obj.index = index + egp.RenderTable[k] = obj + return true, obj + else + return obj:EditObject(settings), obj + end + else -- Did not exist. Create: + ---@type EGPObject + obj = table.Copy(class) + obj:Initialize(settings) + obj.index = index + table.insert(egp.RenderTable, obj) + return true, obj + end +end +EGP.Create = create + +-------------------------------------------------------- +-- Homescreen +-------------------------------------------------------- + +--- The EGP homescreen that appears when you first create an EGP +---@type EGPObject[] +EGP.HomeScreen = {} + +local mat +if CLIENT then mat = Material else mat = function( str ) return str end end + +-- Create table +local tbl = { + { objects.Box, { x = 256, y = 256, h = 356, w = 356, material = mat("expression 2/cog"), r = 150, g = 34, b = 34, a = 255 } }, + { objects.Text, {x = 256, y = 256, text = "EGP 3", font = "WireGPU_ConsoleFont", valign = 1, halign = 1, size = 50, r = 135, g = 135, b = 135, a = 255 } } +} + +--[[ Old homescreen (EGP v2 home screen design contest winner) +local tbl = { + { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 362, h = 362, material = true, angle = 135, r = 75, g = 75, b = 200, a = 255 } }, + { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 340, h = 340, material = true, angle = 135, r = 10, g = 10, b = 10, a = 255 } }, + { ID = EGP.Objects.Names["Text"], Settings = { x = 229, y = 28, text = "E", size = 100, fontid = 4, r = 200, g = 50, b = 50, a = 255 } }, + { ID = EGP.Objects.Names["Text"], Settings = { x = 50, y = 200, text = "G", size = 100, fontid = 4, r = 200, g = 50, b = 50, a = 255 } }, + { ID = EGP.Objects.Names["Text"], Settings = { x = 400, y = 200, text = "P", size = 100, fontid = 4, r = 200, g = 50, b = 50, a = 255 } }, + { ID = EGP.Objects.Names["Text"], Settings = { x = 228, y = 375, text = "2", size = 100, fontid = 4, r = 200, g = 50, b = 50, a = 255 } }, + { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 256, h = 256, material = mat("expression 2/cog"), angle = 45, r = 255, g = 50, b = 50, a = 255 } }, + { ID = EGP.Objects.Names["Box"], Settings = { x = 128, y = 241, w = 256, h = 30, material = true, r = 10, g = 10, b = 10, a = 255 } }, + { ID = EGP.Objects.Names["Box"], Settings = { x = 241, y = 128, w = 30, h = 256, material = true, r = 10, g = 10, b = 10, a = 255 } }, + { ID = EGP.Objects.Names["Circle"], Settings = { x = 256, y = 256, w = 70, h = 70, material = true, r = 255, g = 50, b = 50, a = 255 } }, + { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 362, h = 362, material = mat("gui/center_gradient"), angle = 135, r = 75, g = 75, b = 200, a = 75 } }, + { ID = EGP.Objects.Names["Box"], Settings = { x = 256, y = 256, w = 362, h = 362, material = mat("gui/center_gradient"), angle = 135, r = 75, g = 75, b = 200, a = 75 } } +} +]] + +-- Convert table +for k, v in ipairs(tbl) do + local obj = table.Copy(v[1]) + obj.index = k + for k2, v2 in pairs(v[2]) do + if obj[k2] ~= nil then obj[k2] = v2 end + end + table.insert(EGP.HomeScreen, obj) +end \ No newline at end of file diff --git a/lua/entities/gmod_wire_egp/lib/egplib/parenting.lua b/lua/entities/gmod_wire_egp/lib/egplib/parenting.lua index d2722f3ff7..20a400331d 100644 --- a/lua/entities/gmod_wire_egp/lib/egplib/parenting.lua +++ b/lua/entities/gmod_wire_egp/lib/egplib/parenting.lua @@ -1,9 +1,14 @@ -------------------------------------------------------- -- Parenting functions -------------------------------------------------------- -local EGP = EGP +local EGP = E2Lib.EGP EGP.ParentingFuncs = {} +local hasObject +EGP.HookPostInit(function() + hasObject = EGP.HasObject +end) + local function addUV( v, t ) -- Polygon u v fix if (v.verticesindex) then local t2 = v[v.verticesindex] @@ -97,7 +102,7 @@ local function GetGlobalPos(self, Ent, index) obj = index bool = true else - bool, _, obj = self:HasObject(Ent, index) + bool, _, obj = hasObject(Ent, index) end if bool then if obj.parent and obj.parent ~= 0 then -- Object is parented @@ -111,7 +116,7 @@ local function GetGlobalPos(self, Ent, index) local vec, ang = LocalToWorld(Vector(obj._x, obj._y, 0), Angle(0, obj._angle or 0, 0), Vector(x, y, 0), angle_zero) return obj.verticesindex ~= nil, { x = vec.x, y = vec.y, angle = -ang.y } else - local _, data = GetGlobalPos(Ent, select(3, EGP:HasObject(Ent, obj.parent))) + local _, data = GetGlobalPos(Ent, select(3, hasObject(Ent, obj.parent))) local vec, ang = LocalToWorld(Vector(obj._x, obj._y, 0), Angle(0, -(obj._angle or 0), 0), Vector(data.x, data.y, 0), Angle(0, -(data.angle or 0), 0)) return obj.verticesindex ~= nil, { x = vec.x, y = vec.y, angle = -ang.y } end @@ -205,7 +210,7 @@ end function EGP:SetParent( Ent, index, parentindex ) local bool, v if isnumber(index) then - bool, _, v = self:HasObject(Ent, index) + bool, _, v = hasObject(Ent, index) else bool, v = index ~= nil, index end @@ -214,7 +219,7 @@ function EGP:SetParent( Ent, index, parentindex ) if (self:EditObject( v, { parent = parentindex } )) then return true, v end else if isnumber(parentindex) then - bool = self:HasObject(Ent, parentindex) + bool = hasObject(Ent, parentindex) else bool, parentindex = parentindex ~= nil, parentindex.index end @@ -257,7 +262,7 @@ end function EGP:UnParent( Ent, index ) local bool, v = false if isnumber(index) then - bool, _, v = self:HasObject( Ent, index ) + bool, _, v = hasObject(Ent, index) else bool = istable(index) v = index @@ -273,4 +278,4 @@ function EGP:UnParent( Ent, index ) if v:EditObject(data) then return true, v end end -end +end \ No newline at end of file diff --git a/lua/entities/gmod_wire_egp/lib/egplib/queuesystem.lua b/lua/entities/gmod_wire_egp/lib/egplib/queuesystem.lua index e3034dc34b..999f0130ca 100644 --- a/lua/entities/gmod_wire_egp/lib/egplib/queuesystem.lua +++ b/lua/entities/gmod_wire_egp/lib/egplib/queuesystem.lua @@ -1,7 +1,7 @@ -------------------------------------------------------- -- EGP Queue System -------------------------------------------------------- -local EGP = EGP +local EGP = E2Lib.EGP EGP.Queue = WireLib.RegisterPlayerTable() @@ -17,7 +17,7 @@ function EGP:AddQueueObject( Ent, ply, Function, Object ) LastItem.Args[1][k] = Object elseif (v.ID ~= Object.ID) then -- Not the same kind of object, create new if (v.OnRemove) then v:OnRemove() end - local Obj = EGP:GetObjectByID( Object.ID ) + local Obj = table.Copy(EGP.Objects[Object.ID]) Obj:Initialize(Object:DataStreamInfo()) Obj.index = v.index LastItem.Args[1][k] = Obj diff --git a/lua/entities/gmod_wire_egp/lib/egplib/transmitreceive.lua b/lua/entities/gmod_wire_egp/lib/egplib/transmitreceive.lua index 2e0b676598..6a3570e802 100644 --- a/lua/entities/gmod_wire_egp/lib/egplib/transmitreceive.lua +++ b/lua/entities/gmod_wire_egp/lib/egplib/transmitreceive.lua @@ -1,7 +1,12 @@ -------------------------------------------------------- -- Queue Functions -------------------------------------------------------- -local EGP = EGP +local EGP = E2Lib.EGP + +local hasObject +EGP.HookPostInit(function() + hasObject = EGP.HasObject +end) if (SERVER) then util.AddNetworkString( "EGP_Transmit_Data" ) @@ -91,7 +96,7 @@ if (SERVER) then return end - local bool, _, v = EGP:HasObject( Ent, index ) + local bool, _, v = hasObject(Ent, index) if (bool) then if not EGP.umsg.Start("EGP_Transmit_Data", ply) then return end net.WriteEntity( Ent ) @@ -123,7 +128,7 @@ if (SERVER) then return end - local bool, _, v = EGP:HasObject( Ent, index ) + local bool, _, v = hasObject(Ent, index) if (bool) then if not EGP.umsg.Start("EGP_Transmit_Data", ply) then return end net.WriteEntity( Ent ) @@ -154,7 +159,7 @@ if (SERVER) then return end - if EGP:HasObject(Ent, index) then + if hasObject(Ent, index) then if not EGP.umsg.Start("EGP_Transmit_Data", ply) then return end net.WriteEntity( Ent ) net.WriteString( "AddText" ) @@ -175,7 +180,7 @@ if (SERVER) then return end - if EGP:HasObject(Ent, index) then + if hasObject(Ent, index) then if not EGP.umsg.Start("EGP_Transmit_Data", ply) then return end net.WriteEntity( Ent ) net.WriteString( "SetText" ) @@ -239,11 +244,6 @@ if (SERVER) then net.WriteUInt( #DataToSend, 16 ) -- Send estimated number of objects to be sent for k,v in ipairs( DataToSend ) do - -- Check if the object doesn't exist serverside anymore (It may have been removed by a command in the queue before this, like egpClear or egpRemove) - --if not EGP:HasObject( Ent, v.index ) then - -- EGP:CreateObject( Ent, v.ID, v ) - --end - net.WriteInt( v.index, 16 ) -- Send index of object if (v.remove == true) then @@ -282,7 +282,7 @@ if (SERVER) then -- Change order now if order_was_changed then - EGP:PerformReorder( Ent ) + EGP.PerformReorder(Ent) end EGP:SendQueueItem( ply ) @@ -399,20 +399,20 @@ else -- SERVER/CLIENT elseif (Action == "SetText") then local index = net.ReadInt(16) local text = net.ReadString() - local bool,_,v = EGP:HasObject( Ent, index ) + local bool,_,v = hasObject(Ent, index) if (bool) then if (EGP:EditObject( v, { text = text } )) then Ent:EGP_Update() end end elseif (Action == "AddText") then local index = net.ReadInt(16) local text = net.ReadString() - local bool,_,v = EGP:HasObject( Ent, index ) + local bool,_,v = hasObject(Ent, index) if (bool) then if (EGP:EditObject( v, { text = v.text .. text } )) then Ent:EGP_Update() end end elseif (Action == "SetVertex") then local index = net.ReadInt(16) - local bool, _, v = EGP:HasObject( Ent, index ) + local bool, _, v = hasObject(Ent, index) if (bool) then local vertices = {} @@ -432,7 +432,7 @@ else -- SERVER/CLIENT end elseif (Action == "AddVertex") then local index = net.ReadInt(16) - local bool, _, v = EGP:HasObject( Ent, index ) + local bool, _, v = hasObject(Ent, index) if (bool) then local vertices = table.Copy(v.vertices) @@ -464,7 +464,7 @@ else -- SERVER/CLIENT local ID = net.ReadUInt(8) if (ID == 0) then -- Remove object - local bool, k, v = EGP:HasObject( Ent, index ) + local bool, k, v = hasObject(Ent, index) if (bool) then if (v.OnRemove) then v:OnRemove() end @@ -487,11 +487,11 @@ else -- SERVER/CLIENT end local current_obj - local bool, k, v = self:HasObject( Ent, index ) + local bool, k, v = hasObject(Ent, index) if (bool) then -- Object already exists if (v.ID ~= ID) then -- Not the same kind of object, create new if (v.OnRemove) then v:OnRemove() end - local Obj = self:GetObjectByID( ID ) + local Obj = table.Copy(EGP.Objects[ID]) local data = Obj:Receive() Obj:Initialize(data) Obj.index = index @@ -512,7 +512,7 @@ else -- SERVER/CLIENT -- For EGP HUD v.res = nil else -- Object does not exist. Create new - local Obj = self:GetObjectByID( ID ) + local Obj = table.Copy(EGP.Objects[ID]) Obj:Initialize(Obj:Receive()) Obj.index = index if (Obj.OnCreate) then Obj:OnCreate() end @@ -532,7 +532,7 @@ else -- SERVER/CLIENT -- Change order now if order_was_changed then - self:PerformReorder( Ent ) + EGP.PerformReorder(Ent) end Ent:EGP_Update() @@ -663,7 +663,7 @@ else Ent.GPU.texture_filtering = decoded.Filtering or TEXFILTER.ANISOTROPIC end for _,v in pairs( Objects ) do - local Obj = self:GetObjectByID(v.ID) + local Obj = table.Copy(EGP.Objects[v.ID]) Obj:Initialize(v.Settings) -- If parented, reset the parent indexes if (Obj.parent and Obj.parent ~= 0) then @@ -682,4 +682,4 @@ else net.Receive("EGP_Request_Transmit", function(len,ply) EGP:ReceiveDataStream(net.ReadTable()) end) -end +end \ No newline at end of file diff --git a/lua/entities/gmod_wire_egp/lib/egplib/umsgsystem.lua b/lua/entities/gmod_wire_egp/lib/egplib/umsgsystem.lua index 4bb797dda7..7f4b5552f7 100644 --- a/lua/entities/gmod_wire_egp/lib/egplib/umsgsystem.lua +++ b/lua/entities/gmod_wire_egp/lib/egplib/umsgsystem.lua @@ -1,7 +1,7 @@ -------------------------------------------------------- -- Custom umsg System -------------------------------------------------------- -local EGP = EGP +local EGP = E2Lib.EGP local CurSender = NULL local LastErrorTime = 0 diff --git a/lua/entities/gmod_wire_egp/lib/egplib/usefulfunctions.lua b/lua/entities/gmod_wire_egp/lib/egplib/usefulfunctions.lua index c5d74c278a..54d7965ecd 100644 --- a/lua/entities/gmod_wire_egp/lib/egplib/usefulfunctions.lua +++ b/lua/entities/gmod_wire_egp/lib/egplib/usefulfunctions.lua @@ -1,7 +1,12 @@ -------------------------------------------------------- -- e2function Helper functions -------------------------------------------------------- -local EGP = EGP +local EGP = E2Lib.EGP + +local hasObject +EGP.HookPostInit(function() + hasObject = EGP.HasObject +end) ---------------------------- -- Table IsEmpty @@ -90,7 +95,7 @@ function EGP.MoveTopLeft(ent, obj) if obj.angle then t.angle = -ang.yaw end end if obj.IsParented then - local bool, _, parent = EGP:HasObject(ent, obj.parent) + local bool, _, parent = hasObject(ent, obj.parent) if bool and parent.CanTopLeft and parent.w and parent.h then if not t then t = { x = obj.x, y = obj.y, angle = obj.angle } end t.x = t.x - parent.w / 2 diff --git a/lua/entities/gmod_wire_egp/lib/init.lua b/lua/entities/gmod_wire_egp/lib/init.lua index 6852e36958..07e5e1b4f7 100644 --- a/lua/entities/gmod_wire_egp/lib/init.lua +++ b/lua/entities/gmod_wire_egp/lib/init.lua @@ -1,30 +1,47 @@ -EGP = {} +E2Lib = E2Lib or { EGP = {} } +--- The EGP library +local EGP = E2Lib.EGP +if not EGP then + EGP = {} + E2Lib.EGP = EGP +end --------------------------------------------------------- --- Include all other files --------------------------------------------------------- +_G.EGP = EGP -- To be after refactor is done + +EGP.ConVars = { + MaxObjects = CreateConVar("wire_egp_max_objects", 300, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE }, "Maximum objects on EGP screen"), + MaxPerSec = CreateConVar("wire_egp_max_bytes_per_sec", 10000, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE }, "Maximum amount of data to transfer for EGP updates"), -- Keep between 2500-40000 + MaxVertices = CreateConVar("wire_egp_max_poly_vertices", 1024, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE }, "Maximum amount of vertices on a polygon"), + AllowEmitter = CreateConVar("wire_egp_allow_emitter", 1, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE }, "Should EGP emitters be allowed?"), + AllowHUD = CreateConVar("wire_egp_allow_hud", 1, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE }, "Should EGP HUDs be allowed?"), + AllowScreen = CreateConVar("wire_egp_allow_screen", 1, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE }, "Should EGP screens be allowed?"), +} -function EGP:Initialize() - local Folder = "entities/gmod_wire_egp/lib/egplib/" - local entries = file.Find( Folder .. "*.lua", "LUA") - for _, entry in ipairs( entries ) do - if (SERVER) then - AddCSLuaFile( Folder .. entry ) - end - include( Folder .. entry ) +-- Include all other files +local postinit = {} +--- Calls argument after EGP library finishes initializing. If initialization already finished, calls callback immediately. +---@param callback function +function EGP.HookPostInit(callback) + if postinit then + table.insert(postinit, callback) + else + callback() end end -EGP:Initialize() - -local EGP = EGP +local FOLDER = "entities/gmod_wire_egp/lib/egplib/" +local entries = file.Find(FOLDER .. "*.lua", "LUA") -EGP.ConVars = {} -EGP.ConVars.MaxObjects = CreateConVar( "wire_egp_max_objects", 300, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE } ) -EGP.ConVars.MaxPerSec = CreateConVar( "wire_egp_max_bytes_per_sec", 10000, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE } ) -- Keep between 2500-40000 - -EGP.ConVars.MaxVertices = CreateConVar( "wire_egp_max_poly_vertices", 1024, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE } ) +for _, entry in ipairs(entries) do + local p = FOLDER .. entry + if SERVER then + AddCSLuaFile(p) + end + include(p) +end -EGP.ConVars.AllowEmitter = CreateConVar( "wire_egp_allow_emitter", 1, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE } ) -EGP.ConVars.AllowHUD = CreateConVar( "wire_egp_allow_hud", 1, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE } ) -EGP.ConVars.AllowScreen = CreateConVar( "wire_egp_allow_screen", 1, { FCVAR_NOTIFY, FCVAR_SERVER_CAN_EXECUTE, FCVAR_ARCHIVE } ) +-- Run PostInit callbacks +for _, v in ipairs(postinit) do + v() +end +postinit = nil diff --git a/lua/entities/gmod_wire_egp/lib/objects/3dtracker.lua b/lua/entities/gmod_wire_egp/lib/objects/3dtracker.lua index 2d6478368a..29b8eb3df9 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/3dtracker.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/3dtracker.lua @@ -1,5 +1,6 @@ -- Author: Divran -local Obj = EGP:NewObject( "3DTracker" ) + +local Obj = E2Lib.EGP.NewObject("3DTracker") Obj.material = nil Obj.filtering = nil Obj.target_x = 0 diff --git a/lua/entities/gmod_wire_egp/lib/objects/box.lua b/lua/entities/gmod_wire_egp/lib/objects/box.lua index 5d85c5cfbc..6d960a544b 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/box.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/box.lua @@ -1,5 +1,6 @@ -- Author: Divran -local Obj = EGP:NewObject( "Box" ) + +local Obj = E2Lib.EGP.NewObject("Box") Obj.CanTopLeft = true Obj.w = 0 Obj.h = 0 @@ -38,7 +39,7 @@ function Obj:Contains(x, y) if self.EGP.TopLeft then x, y = x - w, y - h end return -w <= x and x <= w and - -h <= y and y <= h + -h <= y and y <= h end return Obj \ No newline at end of file diff --git a/lua/entities/gmod_wire_egp/lib/objects/boxoutline.lua b/lua/entities/gmod_wire_egp/lib/objects/boxoutline.lua index cee45e7c2b..5257862056 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/boxoutline.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/boxoutline.lua @@ -1,5 +1,5 @@ -- Author: Divran -local Obj = EGP.ObjectInherit("BoxOutline", "Box") +local Obj = E2Lib.EGP.ObjectInherit("BoxOutline", "Box") Obj.size = 1 local base = Obj.BaseClass diff --git a/lua/entities/gmod_wire_egp/lib/objects/line.lua b/lua/entities/gmod_wire_egp/lib/objects/line.lua index c482f7a605..739b92fa1f 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/line.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/line.lua @@ -1,5 +1,5 @@ -- Author: Divran -local Obj = EGP:NewObject( "Line" ) +local Obj = E2Lib.EGP.NewObject("Line") Obj.material = nil Obj.filtering = nil Obj.x2 = 0 diff --git a/lua/entities/gmod_wire_egp/lib/objects/linestrip.lua b/lua/entities/gmod_wire_egp/lib/objects/linestrip.lua index 02102fd448..e9a8dc79b0 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/linestrip.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/linestrip.lua @@ -1,5 +1,6 @@ -- Author: sk8 (& Divran) -local Obj = EGP.ObjectInherit("LineStrip", "PolyOutline") + +local Obj = E2Lib.EGP.ObjectInherit("LineStrip", "PolyOutline") Obj.Draw = function( self ) local n = #self.vertices diff --git a/lua/entities/gmod_wire_egp/lib/objects/poly.lua b/lua/entities/gmod_wire_egp/lib/objects/poly.lua index 0c0353f50e..241fd7a12a 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/poly.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/poly.lua @@ -1,5 +1,5 @@ -- Author: Divran -local Obj = EGP:NewObject("Poly") +local Obj = E2Lib.EGP.NewObject("Poly") Obj.vertices = {} Obj.verticesindex = "vertices" Obj.HasUV = true diff --git a/lua/entities/gmod_wire_egp/lib/objects/roundedboxoutline.lua b/lua/entities/gmod_wire_egp/lib/objects/roundedboxoutline.lua index 0b7e7ca42e..9900c0b87a 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/roundedboxoutline.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/roundedboxoutline.lua @@ -1,5 +1,5 @@ -- Author: sk8 (& Divran) -local Obj = EGP.ObjectInherit("RoundedBoxOutline", "RoundedBox") +local Obj = E2Lib.EGP.ObjectInherit("RoundedBoxOutline", "RoundedBox") Obj.size = 1 Obj.filtering = nil diff --git a/lua/entities/gmod_wire_egp/lib/objects/text.lua b/lua/entities/gmod_wire_egp/lib/objects/text.lua index 155a26131f..730705870e 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/text.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/text.lua @@ -1,5 +1,5 @@ -- Author: Divran -local Obj = EGP:NewObject( "Text" ) +local Obj = E2Lib.EGP.NewObject("Text") Obj.material = nil Obj.text = "" Obj.font = "WireGPU_ConsoleFont" diff --git a/lua/entities/gmod_wire_egp/lib/objects/textlayout.lua b/lua/entities/gmod_wire_egp/lib/objects/textlayout.lua index e0216a5edd..c0c7b44762 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/textlayout.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/textlayout.lua @@ -1,5 +1,5 @@ -- Author: Divran -local Obj = EGP.ObjectInherit("TextLayout", "Text") +local Obj = E2Lib.EGP.ObjectInherit("TextLayout", "Text") Obj.h = 512 Obj.w = 512 Obj.CanTopLeft = true diff --git a/lua/entities/gmod_wire_egp/lib/objects/wedge.lua b/lua/entities/gmod_wire_egp/lib/objects/wedge.lua index 8728737343..9bb3ecea57 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/wedge.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/wedge.lua @@ -1,5 +1,5 @@ -- Author: Divran -local Obj = EGP.ObjectInherit("Wedge", "Circle") +local Obj = E2Lib.EGP.ObjectInherit("Wedge", "Circle") Obj.size = 45 local rad, cos, sin = math.rad, math.cos, math.sin diff --git a/lua/entities/gmod_wire_egp/lib/objects/wedgeoutline.lua b/lua/entities/gmod_wire_egp/lib/objects/wedgeoutline.lua index f1f45172db..c806e6f629 100644 --- a/lua/entities/gmod_wire_egp/lib/objects/wedgeoutline.lua +++ b/lua/entities/gmod_wire_egp/lib/objects/wedgeoutline.lua @@ -1,5 +1,5 @@ -- Author: Divran -local Obj = EGP.ObjectInherit("WedgeOutline", "Wedge") +local Obj = E2Lib.EGP.ObjectInherit("WedgeOutline", "Wedge") local rad, cos, sin = math.rad, math.cos, math.sin Obj.Draw = function( self ) diff --git a/lua/entities/gmod_wire_expression2/core/e2lib.lua b/lua/entities/gmod_wire_expression2/core/e2lib.lua index 4530b57fd3..e267a8cb58 100644 --- a/lua/entities/gmod_wire_expression2/core/e2lib.lua +++ b/lua/entities/gmod_wire_expression2/core/e2lib.lua @@ -31,12 +31,12 @@ AddCSLuaFile() ---@field Functions table ---@field Methods table> -E2Lib = { +E2Lib = table.Merge(E2Lib or {}, { Env = { ---@type EnvEvent[] Events = {} } -} +}) local type = type local function checkargtype(argn, value, argtype) diff --git a/lua/entities/gmod_wire_expression2/core/egpfunctions.lua b/lua/entities/gmod_wire_expression2/core/egpfunctions.lua index 951c9830d5..ba3bd165c0 100644 --- a/lua/entities/gmod_wire_expression2/core/egpfunctions.lua +++ b/lua/entities/gmod_wire_expression2/core/egpfunctions.lua @@ -1,5 +1,8 @@ - -local NULL_EGPOBJECT = EGP.NULL_EGPOBJECT +local EGP = E2Lib.EGP +local NULL_EGPOBJECT = EGP.Objects.NULL_EGPOBJECT +local hasObject = EGP.HasObject +local isAllowed = EGP.IsAllowed +local egp_create = EGP.Create local function Update(self,this) self.data.EGP.UpdatesNeeded[this] = true @@ -70,51 +73,46 @@ end -- Order -------------------------------------------------------- -e2function void wirelink:egpOrder( number index, number order ) - if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) - if (bool) then - local bool2 = EGP:SetOrder( this, k, order ) - if (bool2) then - EGP:DoAction( this, self, "SendObject", v ) +e2function void wirelink:egpOrder(number index, number order) + if not isAllowed(nil, self, this) then return end + local bool, k, v = hasObject(this, index) + if bool then + if EGP.SetOrder(this, k, order) then + EGP:DoAction(this, self, "SendObject", v) Update(self,this) end end end e2function number wirelink:egpOrder( number index ) - if (!EGP:IsAllowed( self, this )) then return -1 end - local bool, k, v = EGP:HasObject( this, index ) - if (bool) then + if not isAllowed(nil, self, this) then return -1 end + local bool, k = hasObject(this, index) + if bool then return k end return -1 end -e2function void wirelink:egpOrderAbove( number index, number abovethis ) - if not EGP:IsAllowed( self, this ) then return end - local bool, k, v = EGP:HasObject( this, index ) +e2function void wirelink:egpOrderAbove(number index, number abovethis) + if not isAllowed(nil, self, this) then return end + local bool, k, v = hasObject(this, index) if bool then - local bool2, k2, v2 = EGP:HasObject( this, abovethis ) - if bool2 then - local bool3 = EGP:SetOrder( this, k, abovethis, 1 ) - if bool3 then - EGP:DoAction( this, self, "SendObject", v ) - Update(self,this) + if hasObject(this, abovethis) then + if EGP.SetOrder(this, k, abovethis, 1) then + EGP:DoAction(this, self, "SendObject", v) + Update(self, this) end end end end e2function void wirelink:egpOrderBelow( number index, number belowthis ) - if not EGP:IsAllowed( self, this ) then return end - local bool, k, v = EGP:HasObject( this, index ) + if not isAllowed(nil, self, this) then return end + local bool, k, v = hasObject(this, index) if bool then - local bool2, k2, v2 = EGP:HasObject( this, belowthis ) - if bool2 then - local bool3 = EGP:SetOrder( this, k, belowthis, -1 ) - if bool3 then - EGP:DoAction( this, self, "SendObject", v ) + if hasObject(this, belowthis) then + if EGP.SetOrder(this, k, belowthis, -1) then + EGP:DoAction(this, self, "SendObject", v) Update(self,this) end end @@ -128,7 +126,7 @@ __e2setcost(15) -------------------------------------------------------- e2function egpobject wirelink:egpBox( number index, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Box"], { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, self.player ) + local bool, obj = egp_create("Box", { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -138,7 +136,7 @@ end -------------------------------------------------------- e2function egpobject wirelink:egpBoxOutline( number index, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["BoxOutline"], { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, self.player ) + local bool, obj = egp_create("BoxOutline", { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -148,16 +146,16 @@ end -------------------------------------------------------- e2function egpobject wirelink:egpRoundedBox( number index, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["RoundedBox"], { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, self.player ) + local bool, obj = egp_create("RoundedBox", { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end e2function void wirelink:egpRadius( number index, number radius ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { radius = radius } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ radius = radius }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end @@ -166,7 +164,7 @@ end -------------------------------------------------------- e2function egpobject wirelink:egpRoundedBoxOutline( number index, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["RoundedBoxOutline"], { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, self.player ) + local bool, obj = egp_create("RoundedBoxOutline", { index = index, w = size[1], h = size[2], x = pos[1], y = pos[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -176,14 +174,14 @@ end -------------------------------------------------------- e2function egpobject wirelink:egpText( number index, string text, vector2 pos ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Text"], { index = index, text = text, x = pos[1], y = pos[2] }, self.player ) + local bool, obj = egp_create("Text", { index = index, text = text, x = pos[1], y = pos[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end e2function egpobject wirelink:egpTextLayout( number index, string text, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["TextLayout"], { index = index, text = text, x = pos[1], y = pos[2], w = size[1], h = size[2] }, self.player ) + local bool, obj = egp_create("TextLayout", { index = index, text = text, x = pos[1], y = pos[2], w = size[1], h = size[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -195,9 +193,9 @@ __e2setcost(10) ---------------------------- e2function void wirelink:egpSetText( number index, string text ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { text = text } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ text = text }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end @@ -206,17 +204,17 @@ end ---------------------------- e2function void wirelink:egpAlign( number index, number halign ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { halign = math.Clamp(halign,0,2) } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ halign = math.Clamp(halign,0,2) }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end e2function void wirelink:egpAlign( number index, number halign, number valign ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { valign = math.Clamp(valign,0,2), halign = math.Clamp(halign,0,2) } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ valign = math.Clamp(valign,0,2), halign = math.Clamp(halign,0,2) }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end @@ -225,9 +223,9 @@ end ---------------------------- e2function void wirelink:egpFiltering( number index, number filtering ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { filtering = math.Clamp(filtering,0,3) } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ filtering = math.Clamp(filtering,0,3) }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end @@ -254,18 +252,18 @@ end e2function void wirelink:egpFont( number index, string font ) if (!EGP:IsAllowed( self, this )) then return end if #font > 30 then return self:throw("Font string is too long!", nil) end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { font = font } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ font = font }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end e2function void wirelink:egpFont( number index, string font, number size ) if (!EGP:IsAllowed( self, this )) then return end if #font > 30 then return self:throw("Font string is too long!", nil) end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { font = font, size = size } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ font = font, size = size }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end @@ -298,7 +296,7 @@ e2function egpobject wirelink:egpPoly( number index, ...args ) end end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Poly"], { index = index, vertices = vertices }, self.player ) + local bool, obj = egp_create("Poly", { index = index, vertices = vertices }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -324,7 +322,7 @@ e2function egpobject wirelink:egpPoly( number index, array args ) end end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Poly"], { index = index, vertices = vertices }, self.player ) + local bool, obj = egp_create("Poly", { index = index, vertices = vertices }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -354,7 +352,7 @@ e2function egpobject wirelink:egpPolyOutline( number index, ...args ) end end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["PolyOutline"], { index = index, vertices = vertices }, self.player ) + local bool, obj = egp_create("PolyOutline", { index = index, vertices = vertices }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -380,7 +378,7 @@ e2function egpobject wirelink:egpPolyOutline( number index, array args ) end end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["PolyOutline"], { index = index, vertices = vertices }, self.player ) + local bool, obj = egp_create("PolyOutline", { index = index, vertices = vertices }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -390,7 +388,7 @@ e2function void wirelink:egpAddVertices( number index, array args ) if (!EGP:ValidEGP( this )) then return self:throw("Invalid wirelink!", nil) end if (#args<3) then return end -- No less than 3 - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then local max = maxvertices() @@ -441,7 +439,7 @@ e2function egpobject wirelink:egpLineStrip( number index, ...args ) end end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["LineStrip"], { index = index, vertices = vertices }, self.player ) + local bool, obj = egp_create("LineStrip", { index = index, vertices = vertices }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -467,7 +465,7 @@ e2function egpobject wirelink:egpLineStrip( number index, array args ) end end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["LineStrip"], { index = index, vertices = vertices }, self.player ) + local bool, obj = egp_create("LineStrip", { index = index, vertices = vertices }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -479,7 +477,7 @@ __e2setcost(15) -------------------------------------------------------- e2function egpobject wirelink:egpLine( number index, vector2 pos1, vector2 pos2 ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Line"], { index = index, x = pos1[1], y = pos1[2], x2 = pos2[1], y2 = pos2[2] }, self.player ) + local bool, obj = egp_create("Line", { index = index, x = pos1[1], y = pos1[2], x2 = pos2[1], y2 = pos2[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -489,7 +487,7 @@ end -------------------------------------------------------- e2function egpobject wirelink:egpCircle( number index, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Circle"], { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2] }, self.player ) + local bool, obj = egp_create("Circle", { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -499,7 +497,7 @@ end -------------------------------------------------------- e2function egpobject wirelink:egpCircleOutline( number index, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["CircleOutline"], { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2] }, self.player ) + local bool, obj = egp_create("CircleOutline", { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -510,7 +508,7 @@ end e2function egpobject wirelink:egpTriangle( number index, vector2 v1, vector2 v2, vector2 v3 ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end local vertices = { { x = v1[1], y = v1[2] }, { x = v2[1], y = v2[2] }, { x = v3[1], y = v3[2] } } - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Poly"], { index = index, vertices = vertices }, self.player ) + local bool, obj = egp_create("Poly", { index = index, vertices = vertices }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -521,7 +519,7 @@ end e2function egpobject wirelink:egpTriangleOutline( number index, vector2 v1, vector2 v2, vector2 v3 ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end local vertices = { { x = v1[1], y = v1[2] }, { x = v2[1], y = v2[2] }, { x = v3[1], y = v3[2] } } - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["PolyOutline"], { index = index, vertices = vertices }, self.player ) + local bool, obj = egp_create("PolyOutline", { index = index, vertices = vertices }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -531,43 +529,28 @@ end -------------------------------------------------------- e2function egpobject wirelink:egpWedge( number index, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Wedge"], { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2] }, self.player ) + local bool, obj = egp_create("Wedge", { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end ---[[ I'm sticking to my policy of not spamming pointless functions. -e2function void wirelink:egpWedge( number index, vector2 pos, vector2 size, number angle, number mouthsize ) - if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["Wedge"], { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2], size = mouthsize, angle = angle }, self.player ) - if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end -end -]] - -------------------------------------------------------- -- Wedge Outline -------------------------------------------------------- e2function egpobject wirelink:egpWedgeOutline( number index, vector2 pos, vector2 size ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["WedgeOutline"], { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2] }, self.player ) + local bool, obj = egp_create("WedgeOutline", { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2] }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end ---[[ I'm sticking to my policy of not spamming pointless functions. -e2function void wirelink:egpWedgeOutline( number index, vector2 pos, vector2 size, number angle, number mouthsize ) - if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["WedgeOutline"], { index = index, x = pos[1], y = pos[2], w = size[1], h = size[2], size = mouthsize, angle = angle }, self.player ) - if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end -end -]] -------------------------------------------------------- -- 3DTracker -------------------------------------------------------- e2function egpobject wirelink:egp3DTracker( number index, vector pos ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["3DTracker"], { index = index, target_x = pos[1], target_y = pos[2], target_z = pos[3], directionality = 0 }, self.player ) + local bool, obj = egp_create("3DTracker", { index = index, target_x = pos[1], target_y = pos[2], target_z = pos[3], directionality = 0 }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -581,7 +564,7 @@ e2function egpobject wirelink:egp3DTracker( number index, vector pos, number dir directionality = -1 end - local bool, obj = EGP:CreateObject( this, EGP.Objects.Names["3DTracker"], { index = index, target_x = pos[1], target_y = pos[2], target_z = pos[3], directionality = directionality }, self.player ) + local bool, obj = egp_create("3DTracker", { index = index, target_x = pos[1], target_y = pos[2], target_z = pos[3], directionality = directionality }, this) if (bool) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -590,7 +573,7 @@ __e2setcost(10) e2function void wirelink:egpPos( number index, vector pos ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v:EditObject({ target_x = pos[1], target_y = pos[2], target_z = pos[3] })) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end @@ -607,7 +590,7 @@ __e2setcost(10) ---------------------------- e2function void wirelink:egpSize( number index, vector2 size ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if v:EditObject({ w = size[1], h = size[2] }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end @@ -615,9 +598,9 @@ end e2function void wirelink:egpSize( number index, number size ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { size = size } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ size = size }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end @@ -626,7 +609,7 @@ end ---------------------------- e2function void wirelink:egpPos( number index, vector2 pos ) if (!EGP:IsAllowed( self, this )) then return end - local bool, _, v = EGP:HasObject(this, index) + local bool, _, v = hasObject(this, index) if bool and v:SetPos(pos[1], pos[2]) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end @@ -636,7 +619,7 @@ end e2function void wirelink:egpAngle( number index, number angle ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if v:SetPos(nil, nil, angle) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end @@ -648,7 +631,7 @@ end e2function void wirelink:egpAngle( number index, vector2 worldpos, vector2 axispos, number angle ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.x and v.y) then @@ -672,33 +655,33 @@ end ---------------------------- e2function void wirelink:egpColor( number index, vector4 color ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { r = color[1], g = color[2], b = color[3], a = color[4] } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ r = color[1], g = color[2], b = color[3], a = color[4] }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end e2function void wirelink:egpColor( number index, vector color ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { r = color[1], g = color[2], b = color[3] } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ r = color[1], g = color[2], b = color[3] }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end e2function void wirelink:egpColor( number index, r,g,b,a ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, _, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { r = r, g = g, b = b, a = a } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ r = r, g = g, b = b, a = a }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end e2function void wirelink:egpAlpha( number index, number a ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { a = a } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ a = a }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end @@ -709,17 +692,17 @@ end e2function void wirelink:egpMaterial( number index, string material ) if (!EGP:IsAllowed( self, this )) then return end material = WireLib.IsValidMaterial(material) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { material = material } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ material = material }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end e2function void wirelink:egpMaterialFromScreen( number index, entity gpu ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool and gpu and gpu:IsValid()) then - if (EGP:EditObject( v, { material = gpu } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ material = gpu }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end @@ -728,15 +711,15 @@ end ---------------------------- e2function void wirelink:egpFidelity( number index, number fidelity ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then - if (EGP:EditObject( v, { fidelity = math.Clamp(fidelity,3,180) } )) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end + if v:EditObject({ fidelity = math.Clamp(fidelity,3,180) }) then EGP:DoAction( this, self, "SendObject", v ) Update(self,this) end end end e2function number wirelink:egpFidelity( number index ) if (!EGP:IsAllowed( self, this )) then return -1 end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.fidelity) then return v.fidelity @@ -759,7 +742,7 @@ e2function void wirelink:egpParent( number index, entity parent ) if not parent or not parent:IsValid() then return end if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if bool and v.NeedsConstantUpdate then if v.parententity == parent then return end -- Already parented to that v.parententity = parent @@ -771,7 +754,7 @@ end -- Returns the entity a tracker is parented to e2function entity wirelink:egpTrackerParent( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if bool and v.NeedsConstantUpdate then return (v.parententity and v.parententity:IsValid()) and v.parententity or nil end @@ -790,7 +773,7 @@ e2function void wirelink:egpUnParent( number index ) end e2function number wirelink:egpParent( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.parent) then return v.parent @@ -812,7 +795,7 @@ end e2function void wirelink:egpRemove( number index ) if (!EGP:IsAllowed( self, this )) then return end - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then EGP:DoAction( this, self, "RemoveObject", index ) Update(self,this) @@ -826,7 +809,7 @@ end __e2setcost(5) e2function vector2 wirelink:egpPos( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.x and v.y) then return {v.x, v.y} @@ -842,7 +825,7 @@ e2function vector wirelink:egpGlobalPos( number index ) end e2function array wirelink:egpGlobalVertices( number index ) - local hasobject, _, object = EGP:HasObject(this, index) + local hasobject, _, object = hasObject(this, index) if hasobject and object.verticesindex then local data = EGP:GetGlobalVertices(object) if data.vertices then @@ -865,7 +848,7 @@ end __e2setcost(5) e2function vector2 wirelink:egpSize( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.w and v.h) then return {v.w, v.h} @@ -875,7 +858,7 @@ e2function vector2 wirelink:egpSize( number index ) end e2function number wirelink:egpSizeNum( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.size) then return v.size @@ -885,7 +868,7 @@ e2function number wirelink:egpSizeNum( number index ) end e2function vector4 wirelink:egpColor4( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.r and v.g and v.b and v.a) then return {v.r,v.g,v.b,v.a} @@ -895,7 +878,7 @@ e2function vector4 wirelink:egpColor4( number index ) end e2function vector wirelink:egpColor( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.r and v.g and v.b) then return Vector(v.r, v.g, v.b) @@ -905,7 +888,7 @@ e2function vector wirelink:egpColor( number index ) end e2function number wirelink:egpAlpha( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.a) then return v.a @@ -915,7 +898,7 @@ e2function number wirelink:egpAlpha( number index ) end e2function number wirelink:egpAngle( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.angle) then return v.angle @@ -925,12 +908,12 @@ e2function number wirelink:egpAngle( number index ) end e2function string wirelink:egpMaterial( number index ) - local bool, _, v = EGP:HasObject( this, index ) + local bool, _, v = hasObject(this, index) return bool and v.material and tostring(v.material) or "" end e2function number wirelink:egpRadius( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.radius) then return v.radius @@ -942,7 +925,7 @@ end __e2setcost(10) e2function array wirelink:egpVertices( number index ) - local bool, k, v = EGP:HasObject( this, index ) + local bool, k, v = hasObject(this, index) if (bool) then if (v.vertices) then local ret = {} @@ -993,9 +976,9 @@ end __e2setcost(10) e2function string wirelink:egpObjectType(number index) - local bool, _, v = EGP:HasObject(this, index) + local bool, _, v = hasObject(this, index) if bool then - return EGP.Objects.Names_Inverted[v.ID] or "" + return EGP.Objects[v.ID].Name or "" end return "" end @@ -1008,11 +991,11 @@ __e2setcost(15) e2function egpobject wirelink:egpCopy( index, fromindex ) if (!EGP:IsAllowed( self, this )) then return NULL_EGPOBJECT end - local bool, k, v = EGP:HasObject( this, fromindex ) + local bool, k, v = hasObject(this, fromindex) if (bool) then local copy = table.Copy( v ) copy.index = index - local bool2, obj = EGP:CreateObject( this, v.ID, copy, self.player ) + local bool2, obj = egp_create(v.ID, copy, this) if (bool2) then EGP:DoAction( this, self, "SendObject", obj ) Update(self,this) end return obj end @@ -1045,7 +1028,7 @@ end __e2setcost(15) e2function number wirelink:egpHasObject( index ) - local bool, _, _ = EGP:HasObject( this, index ) + local bool, _, _ = hasObject(this, index) return bool and 1 or 0 end @@ -1053,7 +1036,7 @@ __e2setcost(20) --- Returns 1 if the object with specified index contains the specified point. e2function number wirelink:egpObjectContainsPoint(number index, vector2 point) - local _, _, object = EGP:HasObject(this, index) + local _, _, object = hasObject(this, index) return object and object:Contains(point[1], point[2]) and 1 or 0 end diff --git a/lua/entities/gmod_wire_expression2/core/egpobjects.lua b/lua/entities/gmod_wire_expression2/core/egpobjects.lua index d8df483a1b..ded87d6587 100644 --- a/lua/entities/gmod_wire_expression2/core/egpobjects.lua +++ b/lua/entities/gmod_wire_expression2/core/egpobjects.lua @@ -2,10 +2,13 @@ -- File for EGP Object handling in E2. -- --- Dumb but simple -local NULL_EGPOBJECT = EGP.NULL_EGPOBJECT -local M_NULL_EGPOBJECT = getmetatable(NULL_EGPOBJECT) -local M_EGPObject = getmetatable(EGP.Objects.Base) +local EGP = E2Lib.EGP + +local NULL_EGPOBJECT = EGP.Objects.NULL_EGPOBJECT +local isValid = EGP.EGPObject.IsValid +local hasObject = EGP.HasObject +local egp_create = EGP.Create +local isAllowed = EGP.IsAllowed -- Table of allowed arguments and their types local EGP_ALLOWED_ARGS = @@ -36,10 +39,6 @@ local function Update(self, this) self.data.EGP.UpdatesNeeded[this] = true end -local function isValid(this) - if this and getmetatable(this) ~= M_NULL_EGPOBJECT then return true else return false end -end - ---- Type defintion registerType("egpobject", "xeo", NULL_EGPOBJECT, @@ -47,7 +46,7 @@ registerType("egpobject", "xeo", NULL_EGPOBJECT, nil, nil, function(v) - return not istable(v) or getmetatable(v) ~= M_EGPObject + return not isValid(v) end ) @@ -64,7 +63,7 @@ registerOperator("ass", "xeo", "xeo", function(self, args) end) e2function number operator_is(egpobject egpo) - return (getmetatable(egpo) == M_EGPObject) and 1 or 0 + return isValid(egpo) and 1 or 0 end e2function number operator==(egpobject lhs, egpobject rhs) @@ -100,7 +99,7 @@ __e2setcost(15) e2function void egpobject:setOrder(order) local egp = this.EGP if not EGP:IsAllowed(self, egp) then return end - local bool, k = EGP:HasObject(egp, this.index) + local bool, k = hasObject(egp, this.index) if (bool) then if EGP:SetOrder(egp, k, order) then EGP:DoAction(egp, self, "SendObject", this) @@ -112,18 +111,18 @@ end e2function number egpobject:getOrder() local egp = this.EGP if not EGP:IsAllowed(self, egp) then return -1 end - local bool, k = EGP:HasObject(egp, this.index) + local bool, k = hasObject(egp, this.index) return bool and k or -1 end e2function void egpobject:setOrderAbove(egpobject abovethis) local egp = this.EGP - if not EGP:IsAllowed(self, egp) then return end + if not isAllowed(nil, self, egp) then return end if not (isValid(this) or isValid(abovethis)) then self:throw("Invalid EGP Object") end - local bool, k = EGP:HasObject(egp, this.index) + local bool, k = hasObject(egp, this.index) if bool then - if EGP:HasObject(egp, abovethis.index) then - if EGP:SetOrder(egp, k, abovethis.index, 1) then + if hasObject(egp, abovethis.index) then + if EGP.SetOrder(egp, k, abovethis.index, 1) then EGP:DoAction(egp, self, "SendObject", this) Update(self, egp) end @@ -133,12 +132,12 @@ end e2function void egpobject:setOrderBelow(egpobject belowthis) local egp = this.EGP - if not EGP:IsAllowed(self, egp) then return end + if not isAllowed(self, egp) then return end if not (isValid(this) or isValid(belowthis)) then self:throw("Invalid EGP Object") end - local bool, k = EGP:HasObject(egp, this.index) + local bool, k = hasObject(egp, this.index) if bool then - if EGP:HasObject(egp, belowthis.index) then - if EGP:SetOrder(egp, k, belowthis.index, -1) then + if hasObject(egp, belowthis.index) then + if EGP.SetOrder(egp, k, belowthis.index, -1) then EGP:DoAction(egp, self, "SendObject", this) Update(self, egp) end @@ -512,7 +511,7 @@ end [nodiscard] e2function egpobject egpobject:parent() if not isValid(this) then return self:throw("Invalid EGP Object", NULL_EGPOBJECT) end - local _, _, v = EGP:HasObject(this.EGP, this.parent) + local _, _, v = hasObject(this.EGP, this.parent) return v or NULL_EGPOBJECT end @@ -525,7 +524,6 @@ e2function void wirelink:egpRemove(egpobject obj) if isValid(obj) then EGP:DoAction(this, self, "RemoveObject", obj.index) table.Empty(obj) - setmetatable(obj, M_NULL_EGPOBJECT) Update(self, this) end end @@ -534,10 +532,9 @@ e2function void egpobject:remove() if not isValid(this) then return end local egp = this.EGP if not EGP:IsAllowed(self, egp) then return end - + EGP:DoAction(egp, self, "RemoveObject", this.index) - table.Empty(this) - setmetatable(this, M_NULL_EGPOBJECT) -- In an ideal scenario we would probably want this = NULL_EGPOBJECT instead + table.Empty(this) -- In an ideal scenario we would probably want this = NULL_EGPOBJECT instead Update(self, egp) end @@ -552,7 +549,7 @@ e2function void egpobject:draw() args[k] = v end - if EGP:CreateObject(egp, this.ID, args) then + if egp_create(this.ID, args, egp) then EGP:DoAction(egp, self, "SendObject", this) Update(self, egp) end @@ -719,7 +716,7 @@ e2function egpobject wirelink:egpCopy(number index, egpobject from) if from then local copy = table.Copy(from) copy.index = index - local bool, obj = EGP:CreateObject(this, from.ID, copy, self.player) + local bool, obj = egp_create(from.ID, copy, this) if bool then EGP:DoAction(this, self, "SendObject", obj) Update(self, this) return obj end end end @@ -731,7 +728,7 @@ e2function void egpobject:copyFrom(egpobject from) local copy = table.Copy(from) copy.index = this.index copy.EGP = this.EGP - local bool, obj = EGP:CreateObject(copy.EGP, from.ID, copy, self.player) + local bool, obj = egp_create(from.ID, copy, copy.EGP) if bool then EGP:DoAction(this, self, "SendObject", obj) Update(self, this) return end end end @@ -747,9 +744,9 @@ __e2setcost(5) [nodiscard] e2function egpobject wirelink:egpobject(number index) - if not EGP:IsAllowed(self, this) then return NULL_EGPOBJECT end + if not isAllowed(nil, self, this) then return NULL_EGPOBJECT end if not EGP:ValidEGP(this) then return self:throw("Invalid wirelink!", NULL_EGPOBJECT) end - local _, _, obj = EGP:HasObject(this, index) + local _, _, obj = hasObject(this, index) return obj or NULL_EGPOBJECT end @@ -822,9 +819,8 @@ registerCallback("postinit", function() end) end - local egpCreate = EGP.CreateObject - local workingSet = table.Copy(EGP.Objects.Names) - for name, id in pairs(workingSet) do + for _, v in ipairs(EGP.Objects) do + local name = v.Name -- Indexed table "constructor" registerFunction("egp" .. name, "xwl:nt", "xeo", function(self, args) local this, index, args = args[1], args[2], args[3] @@ -838,7 +834,7 @@ registerCallback("postinit", function() converted.index = index - local bool, obj = egpCreate(EGP, this, id, converted) + local bool, obj = egp_create(name, converted, this) if bool then EGP:DoAction(this, self, "SendObject", obj) Update(self, this) @@ -859,7 +855,7 @@ registerCallback("postinit", function() converted.index = EGP.GetNextIndex(this) - local bool, obj = egpCreate(this, id, converted) + local bool, obj = egp_create(name, converted, this) if bool then EGP:DoAction(this, self, "SendObject", obj) Update(self, this)