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

Overall optimization (it should work without testing but testing would be better) #101

Merged
merged 11 commits into from
Apr 13, 2021
79 changes: 37 additions & 42 deletions lua/autorun/client/lib-gui-panel-client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,39 @@ end
------------------------CLIENT / SERVER COMMUNICATIONS------------------------------

--For enabling / disabling panel (draw code)
function umGetPanelState(um)
local ent = um:ReadEntity()
ent.panelEnabled = um:ReadBool()
function umGetPanelState()
local ent = net.ReadEntity()
ent.panelEnabled = net.ReadBool()
end
usermessage.Hook("umsgPanelState", umGetPanelState)
net.Receive("umsgPanelState", umGetPanelState)

--For waking a panel for the first time (use once)
function umPanelWake(um)
local testnum = um:ReadShort()
--Msg("testnum = "..testnum.."\n")
local ent = um:ReadEntity()
function umPanelWake()
local ent = net.ReadEntity()
guiP_PanelEnable(ent, true)
ent.panelWoken = true
--Msg("panel woken, ")
--Msg("ent = "..tostring(ent).."\n")
end
usermessage.Hook("umsgPanelWake", umPanelWake)
net.Receive("umsgPanelWake", umPanelWake)

--For setting the panel colour scheme
function umGetPanelScheme(um)
local ent = um:ReadEntity()
local sNum = um:ReadShort()
--Msg(string.format("cl using scheme #%d (%s)\n", sNum, guiP_schemeTable[sNum]))
function umGetPanelScheme()
local ent = net.ReadEntity()
local sNum = net.ReadInt(16)
ent.currentScheme = guiP_colourScheme[guiP_schemeTable[sNum]]
end
usermessage.Hook("umsgPanelScheme", umGetPanelScheme)
net.Receive("umsgPanelScheme", umGetPanelScheme)

--For initializing entity (use once)
function umClientPanelInit(um)
function umClientPanelInit()
--Msg("is this used?\n")
local ent = um:ReadEntity()
local newID = um:ReadShort()
local ent = net.ReadEntity()
local newID = net.ReadInt(16)
--call auto re-trying init function
entInit(ent, newID)
--need to make this use a failed concommand instread?

end
usermessage.Hook("umsgClientPanelInit", umClientPanelInit)
net.Receive("umsgClientPanelInit", umClientPanelInit)

function entInit(ent, entID)
--print ("and this?\n")
Expand All @@ -63,25 +58,25 @@ function entInit(ent, entID)
end

--For updating variables for the draw code - i.e change the appearance of a widget
function umDrawUpdate(um)
local ent = um:ReadEntity()
local modIndex = um:ReadShort()
local paramNum = um:ReadShort()
local isString = um:ReadBool()
function umDrawUpdate()
local ent = net.ReadEntity()
local modIndex = net.ReadInt(16)
local paramNum = net.ReadInt(16)
local isString = net.ReadBool()
local value
if isString then
value = um:ReadString()
value = net.ReadString()
else
value = um:ReadFloat()
value = net.ReadFloat()
end
ent.pWidgets[modIndex].modType.drawUpdate(ent.pWidgets[modIndex], paramNum, value)
end
usermessage.Hook("umsgDrawUpdate", umDrawUpdate)
net.Receive("umsgDrawUpdate", umDrawUpdate)

--Recieve panel config data from server (when using server defined panels)
function umPanelConfig(um)
function umPanelConfig()
--this sould be better in some form of startup routine
local ent = um:ReadEntity()
local ent = net.ReadEntity()
guiP_ClearWidgets(ent)

if (table.Count(guiP_widgetLookup) == 0) then
Expand All @@ -94,22 +89,22 @@ function umPanelConfig(um)
end

ent.pWidgets = {}
ent.numWidgets = um:ReadShort()
ent.numWidgets = net.ReadInt(16)
for i=1, ent.numWidgets, 1 do
--local modType = panelWidget[um:ReadString()]
local widT = guiP_widgetLookup[um:ReadShort()]
--local modType = panelWidget[net.ReadString()]
local widT = guiP_widgetLookup[net.ReadInt(16)]
local modType = panelWidget[widT]

Msg("read modtype as "..widT.."\n")
local X = um:ReadShort()
local Y = um:ReadShort()
local W = um:ReadShort()
local H = um:ReadShort()
local numParams = um:ReadShort()
local X = net.ReadInt(16)
local Y = net.ReadInt(16)
local W = net.ReadInt(16)
local H = net.ReadInt(16)
local numParams = net.ReadInt(16)
local paramTable = {}
for i=1, numParams, 1 do
local pNum = um:ReadShort()
paramTable[pNum] = um:ReadString()
local pNum = net.ReadInt(16)
paramTable[pNum] = net.ReadString()
Msg(string.format("(client) param #%d = %s\n", pNum, paramTable[pNum]))
end

Expand All @@ -120,9 +115,9 @@ function umPanelConfig(um)

end

guiP_PanelEnable(ent, um:ReadBool())
guiP_PanelEnable(ent, net.ReadBool())
end
usermessage.Hook("umsgPanelConfig", umPanelConfig)
net.Receive("umsgPanelConfig", umPanelConfig)

--------------------------------CLIENT FUNCTIONS-----------------------------------

Expand Down
111 changes: 55 additions & 56 deletions lua/autorun/server/lib-gui-panel-server.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
AddCSLuaFile ("autorun/shared/lib-gui-panel-shared.lua")
AddCSLuaFile ("autorun/client/lib-gui-panel-client.lua")

util.AddNetworkString("umsgClientPanelInit")
util.AddNetworkString("umsgPanelScheme")
util.AddNetworkString("umsgPanelWake")
util.AddNetworkString("umsgPanelState")
util.AddNetworkString("umsgDrawUpdate")
util.AddNetworkString("umsgPanelConfig")

include ("autorun/shared/lib-gui-panel-shared.lua")

guiP_schemeTable = {}
Expand Down Expand Up @@ -32,48 +39,45 @@ end
concommand.Add("guiPinitMe", clientPanelInitRequest)

function umSendPanelInit(ent, entID)
umsg.Start("umsgClientPanelInit", player)
umsg.Entity(ent)
umsg.Short(entID)
umsg.End()
net.Start("umsgClientPanelInit")
net.WriteEntity(ent)
net.WriteInt(entID, 16)
net.Send(player)
end

function umSendPanelScheme(ent, scheme)
umsg.Start("umsgPanelScheme", player)
umsg.Entity(ent)
umsg.Short(guiP_schemeTable[scheme])
umsg.End()
net.Start("umsgPanelScheme")
net.WriteEntity(ent)
net.WriteInt(guiP_schemeTable[scheme], 16)
net.Send(player)
end

function umSendPanelWake(ent)
umsg.Start("umsgPanelWake", player)
umsg.Short(42)
umsg.Entity(ent)
umsg.End()
net.Start("umsgPanelWake")
net.WriteEntity(ent)
net.Send(player)
end

function umSendPanelState(ent, state)
umsg.Start("umsgPanelState", player)
umsg.Entity(ent)
umsg.Bool(state)
umsg.End()
net.Start("umsgPanelState")
net.WriteEntity(ent)
net.WriteBool(state)
net.Send(player)
end

function guiP_cl_drawUpdate(widget, paramNum, value)
local allPlayers = RecipientFilter()
allPlayers:AddAllPlayers()
isString = (type(value) == "string")
umsg.Start("umsgDrawUpdate", allPlayers)
umsg.Entity(widget.parent)
umsg.Short(widget.modIndex)
umsg.Short(paramNum)
umsg.Bool(isString)
net.Start("umsgDrawUpdate")
net.WriteEntity(widget.parent)
net.WriteInt(widget.modIndex, 16)
net.WriteInt(paramNum, 16)
net.WriteBool(isString)
if (isString) then
umsg.String(value)
net.WriteString(value)
else
umsg.Float(value)
net.WriteFloat(value)
end
umsg.End()
net.Broadcast()
end

--[[
Expand Down Expand Up @@ -102,11 +106,10 @@ function gpCursorClick (ply)
if (xval >= 0 and yval >= 0 and xval <= 1 and yval <= 1) then
--Msg ("clicked at ( "..xval.." , "..yval.." )\n")
for k, widget in ipairs(ent.pWidgets) do
if (xval * ent.drawParams.screenWidth > widget.X) && (yval * ent.drawParams.screenHeight > widget.Y) && (xval * ent.drawParams.screenWidth < widget.X + widget.W) && (yval * ent.drawParams.screenHeight < widget.Y + widget.H) then
--Msg("clicked on widget\n")
if (widget.enabled) then
widget.modType.modClicked (ply, widget, (xval * ent.drawParams.screenWidth) - widget.X, (yval * ent.drawParams.screenHeight) - widget.Y)
end
if xval * ent.drawParams.screenWidth > widget.X and yval * ent.drawParams.screenHeight > widget.Y
and xval * ent.drawParams.screenWidth < widget.X + widget.W and yval * ent.drawParams.screenHeight < widget.Y + widget.H
and widget.enabled then
widget.modType.modClicked (ply, widget, (xval * ent.drawParams.screenWidth) - widget.X, (yval * ent.drawParams.screenHeight) - widget.Y)
end
end
end
Expand All @@ -125,7 +128,7 @@ hook.Add( "KeyPress", "guipanelKeyHook", AttackKeyPress )
--------------------------User Functions----------------------------------------------------

function guiP_PanelInit(ent, w, h)
local newID = table.getn(guiP_panelDatabase) + 1
local newID = #guiP_panelDatabase + 1
table.insert(guiP_panelDatabase, ent)
guipLastNewEnt = ent
ent.entID = newID
Expand Down Expand Up @@ -260,7 +263,6 @@ function guiP_fileDataToTable (fileData)
local mTop = tonumber(lineData[4])
local mWidth = tonumber(lineData[5])
local mHeight = tonumber(lineData[6])
local parmTable = {}
local parmStart = 7

--read wire settings
Expand All @@ -275,8 +277,8 @@ function guiP_fileDataToTable (fileData)
parmStart = 9
end
--read extra parameters
if (table.getn(lineData) > parmStart) then
for iv=parmStart, table.getn(lineData), 2 do
if (#lineData > parmStart) then
for iv=parmStart, #lineData, 2 do
newWidget.params[lineData[iv]] = lineData[iv + 1]
end
end
Expand All @@ -289,7 +291,7 @@ function guiP_fileDataToTable (fileData)
newWidget.h = mHeight
table.insert (widgetTable, table.Copy (newWidget))
Msg("widget.y = "..newWidget.w.."\n")
widgetTable[table.getn (widgetTable)].index = table.getn (widgetTable)
widgetTable[#widgetTable].index = #widgetTable
end

end
Expand Down Expand Up @@ -374,8 +376,8 @@ function guiP_LoadWidgetsFromFileData(ent, fileData)
parmStart = 9
end
--read extra parameters
if (table.getn(lineData) > parmStart) then
for iv=parmStart, table.getn(lineData), 2 do
if (#lineData > parmStart) then
for iv=parmStart, #lineData, 2 do
parmTable[lineData[iv]] = lineData[iv + 1]
--Msg("added param "..lineData[iv].." = "..lineData[iv + 1].."\n")
end
Expand All @@ -389,27 +391,24 @@ end

--Send panel config to client
function guiP_SendClientWidgets(ent)
local allPlayers = RecipientFilter()
allPlayers:AddAllPlayers()
umsg.Start("umsgPanelConfig", allPlayers)
net.Start("umsgPanelConfig")
--Msg("starting panel usmg\n")
umsg.Entity(ent)
umsg.Short(table.getn(ent.pWidgets))
net.WriteEntity(ent)
net.WriteInt(#ent.pWidgets, 16)
for key, modu in ipairs(ent.pWidgets) do
Msg(string.format("sending panel #%d\n", key))
--umsg.String(modu.modType.name)
--net.WriteString(modu.modType.name)
Msg("sending type = "..tostring(modu.modType.name).."\n")
Msg("modindex "..tostring(guiP_widgetLookup[modu.modType.name]).."\n")
umsg.Short(guiP_widgetLookup[modu.modType.name])
--umsg.Short(key)
umsg.Short(modu.X)
umsg.Short(modu.Y)
umsg.Short(modu.W)
umsg.Short(modu.H)
net.WriteInt(guiP_widgetLookup[modu.modType.name], 16)
--net.WriteInt(key, 16)
net.WriteInt(modu.X, 16)
net.WriteInt(modu.Y, 16)
net.WriteInt(modu.W, 16)
net.WriteInt(modu.H, 16)
--check extra params
local numParams = table.Count(modu.paramTable)
umsg.Short(numParams)
local keysend = 0
net.WriteInt(numParams, 16)

for pkey, param in pairs(modu.paramTable) do
--Msg(string.format("key = %s, param = %s, cms = '%s'\n", pkey, param, table.concat(modu.modType.paramTable)))
Expand All @@ -422,12 +421,12 @@ function guiP_SendClientWidgets(ent)
--- Msg(ent.errorMsg.."param error\n")
--end
--Msg("(server) param #"..tostring(keysend).." = "..tostring(param).."\n")
umsg.Short(pkey)
umsg.String(tostring(param))
net.WriteInt(pkey, 16)
net.WriteString(tostring(param))
end
end
umsg.Bool(true)
umsg.End()
net.WriteBool(true)
net.Broadcast()
end

--function guiP_PanelEnable(ent)
Expand Down
12 changes: 6 additions & 6 deletions lua/autorun/shared/lib-gui-panel-shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ end

--Add a widget to a panel
function guiP_AddWidget(ent, newName, modType, X, Y, W, H, paramTable)
local newKey = table.getn(ent.pWidgets) + 1
local newKey = #ent.pWidgets + 1
local newParTable = {}
if (panelWidget[modType]) then
local pNum = 1
Expand Down Expand Up @@ -75,16 +75,16 @@ guiP_panelIDRegister = {}

--Create a new panel
function guiPanelCreateNew(ent)
local newID = table.getn(guiP_panelIDRegister) + 1
local newID = #guiP_panelIDRegister + 1
table.insert(guiP_panelIDRegister, ent.Entity)
--Msg("creating new panel with id "..newID.." ("..table.getn(guiP_panelIDRegister)..")\n")
--Msg("creating new panel with id "..newID.." ("..#guiP_panelIDRegister..")\n")
ent.panelID = newID
--Msg("ent = "..tostring(ent).." ")
if CLIENT then
//if CLIENT then
--Msg("client")
else
//else
--Msg("server")
end
//end
--Msg(" verified val = "..ent.panelID.."\n")
end

Expand Down
Loading