Thanks guys, All done now, heres the final code, Let me know if there are any things I can do to make it better!
function viewClients()
changePage("clientPage")
term.clear()
drawTitleBar()
printer.centered("Please Wait While We Ping Your Clients", 6)
closeButton = btnInit("Cancel", nil, nil, termX/2-#"Cancel"/2, 18, 1, colors.red, 1, 256, function() os.queueEvent('cancel') end, false, nil, nil, nil, nil)
local function pingClients()
sleep(0.6)
for _, i in ipairs(current.devices.Client) do
printer.centered("Pinging ID: "..i, 17)
rednet.send(i, "PingRequest")
local senderID, message, protocol = rednet.receive(5)
if senderID == i then
--Correct ID
if message == "ClientReady" then
LogFile.i("Client "..i.." Ready", runningProgram)
else
LogFile.w("Invalid Message, Client Is Off-line", runningProgram)
end
else
LogFile.w("No Response From ID: "..i.." Message From: "..senderID, runningProgram)
--Invalid return ID, malicious
end
end
end
local function cancel()
while true do
e, p1, p2, p3 = os.pullEventRaw()
if e == "cancel" then drawTitleBar() printer.centered('Returning...', 6) return 'cancel' elseif e=="mouse_click" then
checkClick(e, p1, p2, p3)
end
end
end
parallel.waitForAny(pingClients, loadingIcon, function() state = cancel() end)
if state == "cancel" then
changePage("main")
return state
end
changePage("clientList")
displayButtons = {}
buttonBlacklist = {""}
local function tableClear()
tablesToRemove = {displayButtons, elements}
for i, v in ipairs (tablesToRemove) do
LogFile.i("Clearing "..tostring(v).." Table, Please Wait...", runningProgram)
local k = next(v)
while k do
v[k] = nil
k = next(v)
end
LogFile.i(tostring(v).." Table Amount: "..tostring(#v), runningProgram)
end
end
local function clientList(title, ...)
local title = "Choose the device you want to manage"
local tArgs = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"}
if #tArgs < 1 then
drawTitleBar()
printer.centered("You Haven't Registered Any Clients!", 6)
printer.centered("Register Some First", 7)
printer.centered("Click To Return", 18)
os.pullEvent("mouse_click")
return
end
local pages = {[1]={}}
for i = 1, #tArgs, 1 do
if #pages[ #pages ] == 6 then
pages[ #pages + 1 ] = {}
end
pages[ #pages ][ #pages[#pages] + 1 ] = tArgs[ i ]
end
local maxLen = 0
for k, v in ipairs( tArgs ) do
if #v > maxLen then maxLen = #v end
end
local maxx, maxy = term.getSize()
if maxLen > maxx - 20 then
error('The Largest String In The Array Is Too LARGE To Display!')
end
local centerx = math.ceil( maxx/2 )
local centery
local page = 1
local selected = 1
local function render()
local tbl = pages[ page ]
centery = (maxy/2) - #tbl/2
printer.centered(title, 4)
local xValue = 2
local Count = 1
local yValue = 8
tableClear()
LogFile.i("Length Of PreRender: "..tostring(#tbl), runningProgram)
if pages[page-1] then prevBtn = btnInit("Previous Page", nil, nil, 2, 18, 1, colors.cyan, 1, 256, function() os.queueEvent("PreviousPage") end, false, nil, nil, nil, nil) end
if pages[page+1] then nxtBtn = btnInit("Next Page", nil, nil, termX - 2 - #"Next Page", 18, 1, colors.cyan, 1, 256, function() os.queueEvent("NextPage") end, false, nil, nil, nil, nil) end
for i = 1, #tbl do
LogFile.i("Loop: "..tostring(i), runningProgram)
if Count == 1 then x = 2 y = 8
elseif Count == 2 then y = 8 x = termX-4-#"Manage Client"-#tbl[i]
elseif Count == 3 then y = 11 x = 2
elseif Count == 4 then y = 11 x = termX-4-#"Manage Client"-#tbl[i]
elseif Count == 5 then y = 14 x = 2
elseif Count == 6 then y = 14 x = termX-4-#"Manage Client"-#tbl[i] Count = 1
end
displayButtons[tbl[i]] = btnInit("Manage Client "..tbl[i], nil, nil, x, y, 1, colors.cyan, 1, 256, function() error(tbl[i]) end, false, nil, nil, nil, nil)
Count = Count + 1
end
if pages[ page - 1 ] then
element.opacity(prevBtn, true)
end
if pages[ page + 1 ] then
element.opacity(nxtBtn, true)
end
local str = "(" .. page .. "/" .. #pages .. ")"
term.setCursorPos( centerx - (#str/2), maxy )
term.write( str )
end
while true do
render()
local event, param1, p2, p3 = os.pullEvent()
if event == "mouse_click" then
checkClick(event, param1, p2, p3)
elseif event == "PreviousPage" then
if pages[page-1] then
drawTitleBar()
printer.centered("Loading, Please Wait...", 4)
page = page -1
hideElement("-a")
element.opacity(prevBtn, false)
element.opacity(nxtBtn, false)
tableClear()
drawTitleBar()
else
prevBtn.visible = false
LogFile.w("Previous Button Fired When On First Page!", runningProgram)
end
elseif event == "NextPage" then
if pages[page+1] then
drawTitleBar()
printer.centered("Loading, Please Wait...", 4)
page = page + 1
element.opacity(prevBtn, false)
element.opacity(nxtBtn, false)
tableClear()
drawTitleBar()
else
nxtBtn.visible = false
LogFile.w("Next Button Fired When On Last Page!", runningProgram)
end
end
end
end
clientList()
changePage("main")
end