From 7d5f448d44f20858defae58c9744ea1cf3a33d4e Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 1 Aug 2022 21:15:58 +0200 Subject: [PATCH] v1.7.1 - clean code --- config.json | 2 +- source/unit/onStart.lua | 15 +-------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/config.json b/config.json index 71255c7..63ad150 100644 --- a/config.json +++ b/config.json @@ -1 +1 @@ -{"events":[],"handlers":[{"code":"--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\nif coroutine.status(MainCoroutine) == \"dead\" then\n MainCoroutine = coroutine.create(runCoroutines)\nend\nif coroutine.status(MainCoroutine) == \"suspended\" then\n assert(coroutine.resume(MainCoroutine))\nend","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"2"},{"code":"--[[\n\tLUA PARAMETERS\n]]\nfontSize = 20 --export: the size of the text for all the screen\nmaxVolumeForHub = 0 --export: the max volume from a hub (can't get it from the lua) if 0, the content volume will be displayed on the screen\nverticalMode = false --export: rotate the screen 90deg\nverticalModeBottomSide = \"right\" --export: when vertical mode is enabled, on which side the bottom of the screen is positioned (\"left\" or \"right\")\n--[[\n\tINIT\n]]\n\nlocal version = '1.7.0'\n\nsystem.print(\"------------------------------------\")\nsystem.print(\"DU-Container-Monitoring version \" .. version)\nsystem.print(\"------------------------------------\")\n\nlocal renderScript = [[\nlocal json = require('dkjson')\nlocal data = json.decode(getInput()) or {}\nlocal vmode = ]] .. tostring(verticalMode) .. [[\n\nlocal vmode_side = \"]] .. verticalModeBottomSide .. [[\"\nif items == nil or data[1] then items = {} end\nif page == nil or data[1] then page = 1 end\nif sorting == nil or data[1] then sorting = 0 end\n\nlocal images = {}\n\nif data[5] ~= nil then\n items[data[5][1] ] = data[5]\n setOutput(data[5][1])\n data[5] = nil\nend\n\nlocal rx,ry = getResolution()\nlocal cx, cy = getCursor()\nif vmode then\n ry,rx = getResolution()\n cy, cx = getCursor()\n cx = rx - cx\n if vmode_side == \"right\" then\n cy = ry - cy\n cx = rx - cx\n end\nend\n\nlocal back=createLayer()\nlocal front=createLayer()\n\nfont_size = ]] .. fontSize .. [[\n\nlocal small=loadFont('Play',14)\nlocal smallBold=loadFont('Play-Bold',18)\nlocal itemName=loadFont('Play-Bold',font_size)\nlocal big=loadFont('Play',38)\n\nsetBackgroundColor( 15/255,24/255,29/255)\n\nsetDefaultStrokeColor( back,Shape_Line,0,0,0,0.5)\nsetDefaultShadow( back,Shape_Line,6,0,0,0,0.5)\n\nsetDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1)\nsetDefaultFillColor( front,Shape_Text,0,0,0,1)\nsetDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)\n\nfunction format_number(a)local b=a;while true do b,k=string.gsub(b,\"^(-?%d+)(%d%d%d)\",'%1 %2')if k==0 then break end end;return b end\n\nfunction round(a,b)if b then return utils.round(a/b)*b end;return a>=0 and math.floor(a+0.5)or math.ceil(a-0.5)end\n\nfunction getRGBGradient(a,b,c,d,e,f,g,h,i,j)a=-1*math.cos(a*math.pi)/2+0.5;local k=0;local l=0;local m=0;if a>=.5 then a=(a-0.5)*2;k=e-a*(e-h)l=f-a*(f-i)m=g-a*(g-j)else a=a*2;k=b-a*(b-e)l=c-a*(c-f)m=d-a*(d-g)end;return k,l,m end\n\nlocal storageBar = createLayer()\nsetDefaultFillColor(storageBar,Shape_Text,110/255,166/255,181/255,1)\nsetDefaultFillColor(storageBar,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor(storageBar,Shape_Line,1,1,1,1)\n\nlocal buttonHover = createLayer()\nsetDefaultFillColor(buttonHover,Shape_Box,249/255,212/255,123/255,1)\nsetDefaultFillColor(buttonHover,Shape_Text,0,0,0,1)\n\nlocal colorLayer = createLayer()\nlocal imagesLayer = createLayer()\n\nif vmode then\n local r = 90\n local tx = ry\n local ty = 0\n if vmode_side == \"left\" then\n r = r + 180\n tx = 0\n ty = rx\n end\n setLayerTranslation(back, tx,ty)\n setLayerRotation(back, math.rad(r))\n setLayerTranslation(front, tx, ty)\n setLayerRotation(front, math.rad(r))\n setLayerTranslation(storageBar, tx, ty)\n setLayerRotation(storageBar, math.rad(r))\n setLayerTranslation(colorLayer, tx, ty)\n setLayerRotation(colorLayer, math.rad(r))\n setLayerTranslation(imagesLayer, tx, ty)\n setLayerRotation(imagesLayer, math.rad(r))\n setLayerTranslation(buttonHover, tx, ty)\n setLayerRotation(buttonHover, math.rad(r))\nend\nlocal percent_fill = 0\nlocal r = 110/255\nlocal g = 166/255\nlocal b = 181/255\nif data[2] > 0 then\n percent_fill = data[3]*100/data[2]\n if percent_fill > 100 then percent_fill = 100 end\n r,g,b = getRGBGradient(percent_fill/100,177/255,42/255,42/255,249/255,212/255,123/255,34/255,177/255,76/255)\nend\nsetDefaultFillColor(colorLayer,Shape_Box,r,g,b,1)\nsetDefaultFillColor(colorLayer,Shape_Text,r,g,b,1)\nsetDefaultTextAlign(colorLayer, AlignH_Center, AlignV_Middle)\n\nlocal from_side = rx*0.05\nfunction renderHeader(title)\n local h = 35\n addLine(back,0,h+12,rx,h+12)\n addBox(front,0,12,rx,h)\n addText(front,smallBold,title,from_side,h)\nend\nfunction renderFooter()\n local h = 35\n local y=ry-h-25\n addLine(back,0,y+h+12,rx,y+h+12)\n addBox(front,0,y+12,rx,h)\n setNextTextAlign(front, AlignH_Right, AlignV_Bottom)\n addText(front,small,\"Next query possible in \" .. round(data[4]) .. ' seconds',rx-from_side,y+h+2)\nend\nfunction renderProgressBar(percent)\n if data[2] > 0 then\n addText(colorLayer, itemName, format_number(round(percent*100)/100) ..\"%\", rx/2, 90)\n local w=(rx-2-from_side*2)*(percent)/100\n local x=from_side\n local y=55\n local h=25\n addBox(storageBar,x,y,rx-from_side*2,h)\n addBox(colorLayer,x+1,y+1,w,h-2)\n else\n addText(colorLayer, itemName, format_number(round(data[3]*100)/100) ..\" L\", rx/2, 80)\n end\nend\nfunction renderResistanceBar(item_id, title, quantity, x, y, w, h, withTitle, withIcon)\n local quantity_x_pos = font_size * 6.7\n local percent_x_pos = font_size * 2\n addBox(storageBar,x,y,w,h)\n if withTitle then\n local title1_text = 'ITEMS'\n local title1_layer = storageBar\n local title1_width = 50\n if sorting == 1 then\n title1_text = title1_text .. ' - ASC'\n title1_layer = buttonHover\n title1_width = 100\n elseif sorting == 2 then\n title1_text = title1_text .. ' - DESC'\n title1_layer = buttonHover\n title1_width = 100\n end\n if cx >= (x-5) and cx <= (x+title1_width) and cy >= (y-20) and cy <= y then\n title1_layer = buttonHover\n if getCursorPressed() then\n if sorting == 0 or sorting > 2 then sorting = 1\n elseif sorting == 1 then sorting = 2\n elseif sorting == 2 then sorting = 0\n end\n end\n end\n addBox(title1_layer, x-5, y-20, title1_width, 20)\n addText(title1_layer, small, title1_text, x, y-5)\n\n local title2_text = 'QUANTITY'\n local title2_layer = storageBar\n local title2_width = 75\n if sorting == 3 then\n title2_text = title2_text .. ' - ASC'\n title2_layer = buttonHover\n title2_width = 120\n elseif sorting == 4 then\n title2_text = title2_text .. ' - DESC'\n title2_layer = buttonHover\n title2_width = 120\n end\n if cx >= (rx-x-title2_width) and cx <= (rx-x+5) and cy >= (y-20) and cy <= y then\n title2_layer = buttonHover\n if getCursorPressed() then\n if sorting <= 2 then sorting = 3\n elseif sorting == 3 then sorting = 4\n elseif sorting == 4 then sorting = 0\n end\n end\n end\n addBox(title2_layer, rx-x-title2_width, y-20, title2_width+5, 20)\n setNextTextAlign(title2_layer, AlignH_Right, AlignV_Bottom)\n addText(title2_layer, small, title2_text, rx-x, y-5)\n end\n if item_id and tonumber(item_id) > 0 and images[item_id] and withIcon then\n addImage(imagesLayer, images[item_id], x+10, y+font_size*.1, font_size*1.3, font_size*1.2)\n end\n\n local pos_y = y+(h/2)-2\n\n setNextTextAlign(storageBar, AlignH_Left, AlignV_Middle)\n addText(storageBar, itemName, title, x+20+font_size, pos_y)\n \n setNextTextAlign(storageBar, AlignH_Right, AlignV_Middle)\n addText(storageBar, itemName, format_number(quantity), rx-from_side-10, pos_y)\nend\n\nrenderHeader('Container Monitoring v]] .. version .. [[')\n\nrenderFooter()\n\nstart_h = 100\n\nlocal h = font_size + font_size / 2\nlocal byPage = math.floor((ry-180)/(h+5))\nlocal max_pages = math.ceil(#items/byPage)\nlocal end_index = page * byPage\nlocal start_index = end_index - byPage + 1\n\nlocal sorted_items = {}\nfor i,v in pairs(items) do\n table.insert(sorted_items, v)\nend\n\nif sorting == 1 then table.sort(sorted_items, function(a, b) return a[3] < b[3] end)\nelseif sorting == 2 then table.sort(sorted_items, function(a, b) return a[3] > b[3] end)\nelseif sorting == 3 then table.sort(sorted_items, function(a, b) return a[4] < b[4] end)\nelseif sorting == 4 then table.sort(sorted_items, function(a, b) return a[4] > b[4] end)\nend\n--if sorting > 0 then\n-- table.sort(sorted_items, function(a, b)\n-- if sorting == 1 then\n-- return a[3]:lower() < b[3]:lower()\n-- elseif soting == 2 then\n-- return a[3]:lower() > b[3]:lower()\n-- elseif soting == 3 then\n-- return tonumber(a[4]) < tonumber(b[4])\n-- elseif soting == 4 then\n-- return tonumber(a[4]) > tonumber(b[4])\n-- end\n-- end)\n--end\n\nlocal item_to_display = {}\nfor index = start_index, end_index do\n table.insert(item_to_display, sorted_items[index])\nend\n\nlocal loadedImages = 0\nfor _,item in ipairs(item_to_display) do\n if images[item[2] ] == nil and loadedImages <= 15 then\n loadedImages = loadedImages + 1\n images[item[2] ] = loadImage(item[5])\n end\nend\n\nfor i,item in ipairs(item_to_display) do\n renderResistanceBar(item[2], item[3], item[4], from_side, start_h, rx-from_side*2, h, i==1, i<=16)\n start_h = start_h+h+5\n if i >= byPage then\n break\n end\nend\nif #items > byPage then\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n local paginationText = 'page ' .. page .. '/' .. max_pages\n if rx > ry then\n paginationText = paginationText .. \" (from \" .. start_index .. \" to \" .. end_index .. \" on \" .. #items .. \")\"\n end\n addText(storageBar, itemName, paginationText , rx/2, ry-70)\n if page > 1 then\n local b1_layer = storageBar\n if cx >= from_side and cx <= (from_side+h) and cy >= (ry-85) and cy <= (ry-85+h) then\n if getCursorPressed() then\n page = page - 1\n end\n b1_layer = buttonHover\n end\n addBox(b1_layer, from_side, ry-85, h, h)\n addText(b1_layer, itemName, '<' , from_side+h/4, ry-65)\n end\n if page < max_pages then\n local b2_layer = storageBar\n if cx >= (rx-from_side-h) and cx <= (rx-from_side) and cy >= (ry-85) and cy <= (ry-85+h) then\n if getCursorPressed() then\n page = page + 1\n end\n b2_layer = buttonHover\n end\n addBox(b2_layer, rx-from_side-h, ry-85, h, h)\n addText(b2_layer, itemName, '>' , rx-from_side-h+h/4, ry-65)\n end\nend\nrenderProgressBar(percent_fill)\nrequestAnimationFrame(100)\n]]\n\nscreens = {}\ndatabank = nil\nfor slot_name, slot in pairs(unit) do\n if type(slot) == \"table\"\n and type(slot.export) == \"table\"\n and slot.getClass\n then\n if\n slot.getClass():lower() == 'screenunit'\n or slot.getClass():lower() == 'screensignunit'\n then\n slot.slotname = slot_name\n table.insert(screens,slot)\n slot.setRenderScript(renderScript)\n elseif slot.getClass():lower() == 'databankunit' then\n databank = slot\n end\n end\nend\nif #screens == 0 then\n system.print(\"No Screen Detected\")\n unit.exit()\nelse\n table.sort(screens, function(a,b) return a.slotname < b.slotname end)\n local plural = \"\"\n if #screens > 1 then plural = \"s\" end\n system.print(#screens .. \" screen\" .. plural .. \" Connected\")\nend\nif container == nil then\n system.print('No Container or Hub dectected')\n unit.exit()\nelse\n system.print('Storage connected')\nend\n\nscreen_data={0,0,0,{}}\nrequest_time = 0\nitems = {}\nupdate_screen = false\n\n--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\ncoroutinesTable = {}\n--all functions here will become a coroutine\nMyCoroutines = {\n function()\n request_time = math.ceil(container.updateContent())\n local max_vol = container.getMaxVolume()\n if max_vol == 0 then\n max_vol = maxVolumeForHub\n end\n local screen_data = {update_screen, max_vol, container.getItemsVolume(), request_time, nil}\n if update_screen then\n for i,item in ipairs(items) do\n screen_data[5] = {\n i,\n item[1],\n item[2],\n item[3],\n item[4]\n }\n for _,s in pairs(screens) do\n s.setScriptInput(json.encode(screen_data))\n while tonumber(s.getScriptOutput()) ~= i do\n coroutine.yield(coroutinesTable[1])\n end\n end\n update_screen = false\n screen_data[1] = false\n end\n unit.exit()\n else\n for _,s in pairs(screens) do\n s.setScriptInput(json.encode(screen_data))\n end\n end\n end,\n}\n\nfunction initCoroutines()\n for _,f in pairs(MyCoroutines) do\n local co = coroutine.create(f)\n table.insert(coroutinesTable, co)\n end\nend\n\ninitCoroutines()\n\nrunCoroutines = function()\n for i,co in ipairs(coroutinesTable) do\n if coroutine.status(co) == \"dead\" then\n coroutinesTable[i] = coroutine.create(MyCoroutines[i])\n end\n if coroutine.status(co) == \"suspended\" then\n assert(coroutine.resume(co))\n end\n end\nend\n\nMainCoroutine = coroutine.create(runCoroutines)","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"1"},{"code":"local list = container.getContent()\nscreen_data[4] = {}\nitems = {}\nfor _,v in ipairs(list) do\n local quantity = math.floor(v.quantity*100)/100\n local item = system.getItem(v.id)\n local item_data = {\n v.id,\n item.displayNameWithSize,\n quantity,\n item.iconPath\n }\n table.insert(items, item_data)\nend\nupdate_screen = true","filter":{"args":[],"signature":"onContentUpdate()","slotKey":"0"},"key":"0"}],"methods":[],"slots":{"0":{"name":"container","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}}}} +{"events":[],"handlers":[{"code":"--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\nif coroutine.status(MainCoroutine) == \"dead\" then\n MainCoroutine = coroutine.create(runCoroutines)\nend\nif coroutine.status(MainCoroutine) == \"suspended\" then\n assert(coroutine.resume(MainCoroutine))\nend","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"2"},{"code":"--[[\n\tLUA PARAMETERS\n]]\nfontSize = 20 --export: the size of the text for all the screen\nmaxVolumeForHub = 0 --export: the max volume from a hub (can't get it from the lua) if 0, the content volume will be displayed on the screen\nverticalMode = false --export: rotate the screen 90deg\nverticalModeBottomSide = \"right\" --export: when vertical mode is enabled, on which side the bottom of the screen is positioned (\"left\" or \"right\")\n--[[\n\tINIT\n]]\n\nlocal version = '1.7.1'\n\nsystem.print(\"------------------------------------\")\nsystem.print(\"DU-Container-Monitoring version \" .. version)\nsystem.print(\"------------------------------------\")\n\nlocal renderScript = [[\nlocal json = require('dkjson')\nlocal data = json.decode(getInput()) or {}\nlocal vmode = ]] .. tostring(verticalMode) .. [[\n\nlocal vmode_side = \"]] .. verticalModeBottomSide .. [[\"\nif items == nil or data[1] then items = {} end\nif page == nil or data[1] then page = 1 end\nif sorting == nil or data[1] then sorting = 0 end\n\nlocal images = {}\n\nif data[5] ~= nil then\n items[data[5][1] ] = data[5]\n setOutput(data[5][1])\n data[5] = nil\nend\n\nlocal rx,ry = getResolution()\nlocal cx, cy = getCursor()\nif vmode then\n ry,rx = getResolution()\n cy, cx = getCursor()\n cx = rx - cx\n if vmode_side == \"right\" then\n cy = ry - cy\n cx = rx - cx\n end\nend\n\nlocal back=createLayer()\nlocal front=createLayer()\n\nfont_size = ]] .. fontSize .. [[\n\nlocal small=loadFont('Play',14)\nlocal smallBold=loadFont('Play-Bold',18)\nlocal itemName=loadFont('Play-Bold',font_size)\nlocal big=loadFont('Play',38)\n\nsetBackgroundColor( 15/255,24/255,29/255)\n\nsetDefaultStrokeColor( back,Shape_Line,0,0,0,0.5)\nsetDefaultShadow( back,Shape_Line,6,0,0,0,0.5)\n\nsetDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1)\nsetDefaultFillColor( front,Shape_Text,0,0,0,1)\nsetDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)\n\nfunction format_number(a)local b=a;while true do b,k=string.gsub(b,\"^(-?%d+)(%d%d%d)\",'%1 %2')if k==0 then break end end;return b end\n\nfunction round(a,b)if b then return utils.round(a/b)*b end;return a>=0 and math.floor(a+0.5)or math.ceil(a-0.5)end\n\nfunction getRGBGradient(a,b,c,d,e,f,g,h,i,j)a=-1*math.cos(a*math.pi)/2+0.5;local k=0;local l=0;local m=0;if a>=.5 then a=(a-0.5)*2;k=e-a*(e-h)l=f-a*(f-i)m=g-a*(g-j)else a=a*2;k=b-a*(b-e)l=c-a*(c-f)m=d-a*(d-g)end;return k,l,m end\n\nlocal storageBar = createLayer()\nsetDefaultFillColor(storageBar,Shape_Text,110/255,166/255,181/255,1)\nsetDefaultFillColor(storageBar,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor(storageBar,Shape_Line,1,1,1,1)\n\nlocal buttonHover = createLayer()\nsetDefaultFillColor(buttonHover,Shape_Box,249/255,212/255,123/255,1)\nsetDefaultFillColor(buttonHover,Shape_Text,0,0,0,1)\n\nlocal colorLayer = createLayer()\nlocal imagesLayer = createLayer()\n\nif vmode then\n local r = 90\n local tx = ry\n local ty = 0\n if vmode_side == \"left\" then\n r = r + 180\n tx = 0\n ty = rx\n end\n setLayerTranslation(back, tx,ty)\n setLayerRotation(back, math.rad(r))\n setLayerTranslation(front, tx, ty)\n setLayerRotation(front, math.rad(r))\n setLayerTranslation(storageBar, tx, ty)\n setLayerRotation(storageBar, math.rad(r))\n setLayerTranslation(colorLayer, tx, ty)\n setLayerRotation(colorLayer, math.rad(r))\n setLayerTranslation(imagesLayer, tx, ty)\n setLayerRotation(imagesLayer, math.rad(r))\n setLayerTranslation(buttonHover, tx, ty)\n setLayerRotation(buttonHover, math.rad(r))\nend\nlocal percent_fill = 0\nlocal r = 110/255\nlocal g = 166/255\nlocal b = 181/255\nif data[2] > 0 then\n percent_fill = data[3]*100/data[2]\n if percent_fill > 100 then percent_fill = 100 end\n r,g,b = getRGBGradient(percent_fill/100,177/255,42/255,42/255,249/255,212/255,123/255,34/255,177/255,76/255)\nend\nsetDefaultFillColor(colorLayer,Shape_Box,r,g,b,1)\nsetDefaultFillColor(colorLayer,Shape_Text,r,g,b,1)\nsetDefaultTextAlign(colorLayer, AlignH_Center, AlignV_Middle)\n\nlocal from_side = rx*0.05\nfunction renderHeader(title)\n local h = 35\n addLine(back,0,h+12,rx,h+12)\n addBox(front,0,12,rx,h)\n addText(front,smallBold,title,from_side,h)\nend\nfunction renderFooter()\n local h = 35\n local y=ry-h-25\n addLine(back,0,y+h+12,rx,y+h+12)\n addBox(front,0,y+12,rx,h)\n setNextTextAlign(front, AlignH_Right, AlignV_Bottom)\n addText(front,small,\"Next query possible in \" .. round(data[4]) .. ' seconds',rx-from_side,y+h+2)\nend\nfunction renderProgressBar(percent)\n if data[2] > 0 then\n addText(colorLayer, itemName, format_number(round(percent*100)/100) ..\"%\", rx/2, 90)\n local w=(rx-2-from_side*2)*(percent)/100\n local x=from_side\n local y=55\n local h=25\n addBox(storageBar,x,y,rx-from_side*2,h)\n addBox(colorLayer,x+1,y+1,w,h-2)\n else\n addText(colorLayer, itemName, format_number(round(data[3]*100)/100) ..\" L\", rx/2, 80)\n end\nend\nfunction renderResistanceBar(item_id, title, quantity, x, y, w, h, withTitle, withIcon)\n local quantity_x_pos = font_size * 6.7\n local percent_x_pos = font_size * 2\n addBox(storageBar,x,y,w,h)\n if withTitle then\n local title1_text = 'ITEMS'\n local title1_layer = storageBar\n local title1_width = 50\n if sorting == 1 then\n title1_text = title1_text .. ' - ASC'\n title1_layer = buttonHover\n title1_width = 100\n elseif sorting == 2 then\n title1_text = title1_text .. ' - DESC'\n title1_layer = buttonHover\n title1_width = 100\n end\n if cx >= (x-5) and cx <= (x+title1_width) and cy >= (y-20) and cy <= y then\n title1_layer = buttonHover\n if getCursorPressed() then\n if sorting == 0 or sorting > 2 then sorting = 1\n elseif sorting == 1 then sorting = 2\n elseif sorting == 2 then sorting = 0\n end\n end\n end\n addBox(title1_layer, x-5, y-20, title1_width, 20)\n addText(title1_layer, small, title1_text, x, y-5)\n\n local title2_text = 'QUANTITY'\n local title2_layer = storageBar\n local title2_width = 75\n if sorting == 3 then\n title2_text = title2_text .. ' - ASC'\n title2_layer = buttonHover\n title2_width = 120\n elseif sorting == 4 then\n title2_text = title2_text .. ' - DESC'\n title2_layer = buttonHover\n title2_width = 120\n end\n if cx >= (rx-x-title2_width) and cx <= (rx-x+5) and cy >= (y-20) and cy <= y then\n title2_layer = buttonHover\n if getCursorPressed() then\n if sorting <= 2 then sorting = 3\n elseif sorting == 3 then sorting = 4\n elseif sorting == 4 then sorting = 0\n end\n end\n end\n addBox(title2_layer, rx-x-title2_width, y-20, title2_width+5, 20)\n setNextTextAlign(title2_layer, AlignH_Right, AlignV_Bottom)\n addText(title2_layer, small, title2_text, rx-x, y-5)\n end\n if item_id and tonumber(item_id) > 0 and images[item_id] and withIcon then\n addImage(imagesLayer, images[item_id], x+10, y+font_size*.1, font_size*1.3, font_size*1.2)\n end\n\n local pos_y = y+(h/2)-2\n\n setNextTextAlign(storageBar, AlignH_Left, AlignV_Middle)\n addText(storageBar, itemName, title, x+20+font_size, pos_y)\n \n setNextTextAlign(storageBar, AlignH_Right, AlignV_Middle)\n addText(storageBar, itemName, format_number(quantity), rx-from_side-10, pos_y)\nend\n\nrenderHeader('Container Monitoring v]] .. version .. [[')\n\nrenderFooter()\n\nstart_h = 100\n\nlocal h = font_size + font_size / 2\nlocal byPage = math.floor((ry-180)/(h+5))\nlocal max_pages = math.ceil(#items/byPage)\nlocal end_index = page * byPage\nlocal start_index = end_index - byPage + 1\n\nlocal sorted_items = {}\nfor i,v in pairs(items) do\n table.insert(sorted_items, v)\nend\n\nif sorting == 1 then table.sort(sorted_items, function(a, b) return a[3] < b[3] end)\nelseif sorting == 2 then table.sort(sorted_items, function(a, b) return a[3] > b[3] end)\nelseif sorting == 3 then table.sort(sorted_items, function(a, b) return a[4] < b[4] end)\nelseif sorting == 4 then table.sort(sorted_items, function(a, b) return a[4] > b[4] end)\nend\n\nlocal item_to_display = {}\nfor index = start_index, end_index do\n table.insert(item_to_display, sorted_items[index])\nend\n\nlocal loadedImages = 0\nfor _,item in ipairs(item_to_display) do\n if images[item[2] ] == nil and loadedImages <= 15 then\n loadedImages = loadedImages + 1\n images[item[2] ] = loadImage(item[5])\n end\nend\n\nfor i,item in ipairs(item_to_display) do\n renderResistanceBar(item[2], item[3], item[4], from_side, start_h, rx-from_side*2, h, i==1, i<=16)\n start_h = start_h+h+5\n if i >= byPage then\n break\n end\nend\nif #items > byPage then\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n local paginationText = 'page ' .. page .. '/' .. max_pages\n if rx > ry then\n paginationText = paginationText .. \" (from \" .. start_index .. \" to \" .. end_index .. \" on \" .. #items .. \")\"\n end\n addText(storageBar, itemName, paginationText , rx/2, ry-70)\n if page > 1 then\n local b1_layer = storageBar\n if cx >= from_side and cx <= (from_side+h) and cy >= (ry-85) and cy <= (ry-85+h) then\n if getCursorPressed() then\n page = page - 1\n end\n b1_layer = buttonHover\n end\n addBox(b1_layer, from_side, ry-85, h, h)\n addText(b1_layer, itemName, '<' , from_side+h/4, ry-65)\n end\n if page < max_pages then\n local b2_layer = storageBar\n if cx >= (rx-from_side-h) and cx <= (rx-from_side) and cy >= (ry-85) and cy <= (ry-85+h) then\n if getCursorPressed() then\n page = page + 1\n end\n b2_layer = buttonHover\n end\n addBox(b2_layer, rx-from_side-h, ry-85, h, h)\n addText(b2_layer, itemName, '>' , rx-from_side-h+h/4, ry-65)\n end\nend\nrenderProgressBar(percent_fill)\nrequestAnimationFrame(100)\n]]\n\nscreens = {}\ndatabank = nil\nfor slot_name, slot in pairs(unit) do\n if type(slot) == \"table\"\n and type(slot.export) == \"table\"\n and slot.getClass\n then\n if\n slot.getClass():lower() == 'screenunit'\n or slot.getClass():lower() == 'screensignunit'\n then\n slot.slotname = slot_name\n table.insert(screens,slot)\n slot.setRenderScript(renderScript)\n elseif slot.getClass():lower() == 'databankunit' then\n databank = slot\n end\n end\nend\nif #screens == 0 then\n system.print(\"No Screen Detected\")\n unit.exit()\nelse\n table.sort(screens, function(a,b) return a.slotname < b.slotname end)\n local plural = \"\"\n if #screens > 1 then plural = \"s\" end\n system.print(#screens .. \" screen\" .. plural .. \" Connected\")\nend\nif container == nil then\n system.print('No Container or Hub dectected')\n unit.exit()\nelse\n system.print('Storage connected')\nend\n\nscreen_data={0,0,0,{}}\nrequest_time = 0\nitems = {}\nupdate_screen = false\n\n--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\ncoroutinesTable = {}\n--all functions here will become a coroutine\nMyCoroutines = {\n function()\n request_time = math.ceil(container.updateContent())\n local max_vol = container.getMaxVolume()\n if max_vol == 0 then\n max_vol = maxVolumeForHub\n end\n local screen_data = {update_screen, max_vol, container.getItemsVolume(), request_time, nil}\n if update_screen then\n for i,item in ipairs(items) do\n screen_data[5] = {\n i,\n item[1],\n item[2],\n item[3],\n item[4]\n }\n for _,s in pairs(screens) do\n s.setScriptInput(json.encode(screen_data))\n while tonumber(s.getScriptOutput()) ~= i do\n coroutine.yield(coroutinesTable[1])\n end\n end\n update_screen = false\n screen_data[1] = false\n end\n unit.exit()\n else\n for _,s in pairs(screens) do\n s.setScriptInput(json.encode(screen_data))\n end\n end\n end,\n}\n\nfunction initCoroutines()\n for _,f in pairs(MyCoroutines) do\n local co = coroutine.create(f)\n table.insert(coroutinesTable, co)\n end\nend\n\ninitCoroutines()\n\nrunCoroutines = function()\n for i,co in ipairs(coroutinesTable) do\n if coroutine.status(co) == \"dead\" then\n coroutinesTable[i] = coroutine.create(MyCoroutines[i])\n end\n if coroutine.status(co) == \"suspended\" then\n assert(coroutine.resume(co))\n end\n end\nend\n\nMainCoroutine = coroutine.create(runCoroutines)","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"1"},{"code":"local list = container.getContent()\nscreen_data[4] = {}\nitems = {}\nfor _,v in ipairs(list) do\n local quantity = math.floor(v.quantity*100)/100\n local item = system.getItem(v.id)\n local item_data = {\n v.id,\n item.displayNameWithSize,\n quantity,\n item.iconPath\n }\n table.insert(items, item_data)\nend\nupdate_screen = true","filter":{"args":[],"signature":"onContentUpdate()","slotKey":"0"},"key":"0"}],"methods":[],"slots":{"0":{"name":"container","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}}}} diff --git a/source/unit/onStart.lua b/source/unit/onStart.lua index 96ed8a5..c5c5b4d 100644 --- a/source/unit/onStart.lua +++ b/source/unit/onStart.lua @@ -9,7 +9,7 @@ verticalModeBottomSide = "right" --export: when vertical mode is enabled, on whi INIT ]] -local version = '1.7.0' +local version = '1.7.1' system.print("------------------------------------") system.print("DU-Container-Monitoring version " .. version) @@ -235,19 +235,6 @@ elseif sorting == 2 then table.sort(sorted_items, function(a, b) return a[3] > b elseif sorting == 3 then table.sort(sorted_items, function(a, b) return a[4] < b[4] end) elseif sorting == 4 then table.sort(sorted_items, function(a, b) return a[4] > b[4] end) end ---if sorting > 0 then --- table.sort(sorted_items, function(a, b) --- if sorting == 1 then --- return a[3]:lower() < b[3]:lower() --- elseif soting == 2 then --- return a[3]:lower() > b[3]:lower() --- elseif soting == 3 then --- return tonumber(a[4]) < tonumber(b[4]) --- elseif soting == 4 then --- return tonumber(a[4]) > tonumber(b[4]) --- end --- end) ---end local item_to_display = {} for index = start_index, end_index do