From 7616a5bed3e804102799606a7ea4f3718bfbf90f Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Sun, 2 Jun 2024 16:41:54 +0200 Subject: [PATCH] Add listframes for multiline lists, refactor order list --- listframes.lua | 152 +++++++++++++++++++++++++++++++++++++++++++++++++ listview.lua | 24 ++++---- orders.lua | 98 ++++++++++++++++--------------- requests.lua | 45 ++++++++------- update.lua | 1 + 5 files changed, 240 insertions(+), 80 deletions(-) create mode 100644 listframes.lua diff --git a/listframes.lua b/listframes.lua new file mode 100644 index 0000000..435864e --- /dev/null +++ b/listframes.lua @@ -0,0 +1,152 @@ +local backingList = {} +local subFrames = {} +local length = 0 +local frame = term.current() +local drawEntry = nil + +-- pagination offset in number of lines (not number of entries!) +local paginationOffset = 1 + + +local function setDrawEntryFunc(drawLineFunc) + drawEntry = drawLineFunc +end + +local function repositionEntriesAfter(idx) + local tw, th = frame.getSize() + local entryFrame = subFrames[idx] + assert(entryFrame ~= nil, "provided entry at idx=" .. idx .. " has no frame") + local xPos, yPos = entryFrame.getPosition() + local xSize, ySize = entryFrame.getSize() + local yStartNext = yPos + ySize + while yStartNext <= th do + idx = idx + 1 + entryFrame = subFrames[idx] + if entryFrame == nil then + break + end + xPos, yPos = entryFrame.getPosition() + xSize, ySize = entryFrame.getSize() + if yPos == yStartNext then + break + else + entryFrame.reposition(1, yStartNext, tw, ySize, frame) + entryFrame.setVisible(true) + end + yStartNext = yPos + ySize + end + idx = idx + 1 + entryFrame = subFrames[idx] + while entryFrame ~= nil and entryFrame.isVisible() do + entryFrame.setVisible(false) + idx = idx + 1 + entryFrame = subFrames[idx] + end +end + +local function redrawEntry(idx) + local entryFrame = subFrames[idx] + if entryFrame ~= nil then + entryFrame.setCursorPos(1,1) + if drawEntry then + drawEntry(entryFrame, backingList[idx]) + else + entryFrame.setBackgroundColor(colors.red) + entryFrame.clearLine() + entryFrame.setBackgroundColor(colors.black) + end + local tw, th = frame.getSize() + local xPos, yPos = entryFrame.getPosition() + local xSize, ySize = entryFrame.getSize() + entryFrame.setVisible((yPos+ySize-1) >= paginationOffset and yPos <= th) + end +end + +local function repositionOrCreateEntryFrame(idx) + local previousFrame = subFrames[idx-1] + local yPos = 1 + if previousFrame ~= nil then + local xPos + xPos, yPos = previousFrame.getPosition() + yPos = yPos + 1 + end + local tw, th = frame.getSize() + local entryFrame = subFrames[idx] + if entryFrame == nil then + subFrames[idx] = window.create(frame, 1, yPos, tw, 1, false) + else + entryFrame.setVisible(false) + local xSize, ySize = entryFrame.getSize() + entryFrame.reposition(1, yPos, tw, ySize, frame) + end +end + +local function updateItemAt(idx, item) + backingList[idx] = item + repositionOrCreateEntryFrame(idx) + if idx > length then + length = idx + end + redrawEntry(idx) + repositionEntriesAfter(idx) +end + +local function clearFrom(idx) + local tw, th = frame.getSize() + for j = length, idx, -1 do + backingList[j] = nil + if subFrames[j] ~= nil then + subFrames[j].setVisible(false) + end + end + length = idx-1 + for y = idx-paginationOffset+1, th, 1 do + frame.setCursorPos(1, y) + frame.clearLine() + end +end + +local function redraw() + if drawEntry then + local tw, th = frame.getSize() + local yPos = 1 + for idx = 1, length, 1 do + assert(backingList[idx] ~= nil, "nil element at idx="..idx) + if yPos < paginationOffset+th then + repositionOrCreateEntryFrame(idx) + redrawEntry(idx) + local xSize, ySize = subFrames[idx].getSize() + yPos = yPos + ySize + elseif subFrames[idx] ~= nil then + subFrames[idx].setVisible(false) + end + end + else + frame.setBackgroundColor(colors.red) + frame.clear() + frame.setBackgroundColor(colors.black) + end +end + +local function setTerm(termlike) + frame = termlike + -- reposition visible subFrames + redraw() +end + +local function updatePage(newOffset) + if newOffset ~= paginationOffset then + paginationOffset = newOffset + redraw() + end +end + + +return { + setTerm=setTerm, + setDrawEntryFunc=setDrawEntryFunc, + updateItemAt=updateItemAt, + clearFrom=clearFrom, + redraw=redraw, + updatePage=updatePage, +} diff --git a/listview.lua b/listview.lua index 4b53356..7136867 100644 --- a/listview.lua +++ b/listview.lua @@ -2,22 +2,22 @@ local backingList = {} local length = 0 local paginationOffset = 1 local frame = term.current() -local drawLine = nil +local drawEntry = nil local function setTerm(termlike) frame = termlike end -local function setDrawLineFunc(drawLineFunc) - drawLine = drawLineFunc +local function setDrawEntryFunc(drawLineFunc) + drawEntry = drawLineFunc end -local function redrawLine(idx) +local function redrawEntry(idx) local tw, th = frame.getSize() if idx >= paginationOffset and idx-paginationOffset <= th then frame.setCursorPos(1, idx-paginationOffset+1) - if drawLine then - drawLine(frame, backingList[idx]) + if drawEntry then + drawEntry(frame, backingList[idx]) else frame.setBackgroundColor(colors.red) frame.clearLine() @@ -35,7 +35,7 @@ local function updateItemAt(idx, item) if idx > length then length = idx end - redrawLine(idx) + redrawEntry(idx) end local function clearFrom(idx) @@ -51,7 +51,7 @@ local function clearFrom(idx) end local function redraw() - if drawLine then + if drawEntry then local tw, th = frame.getSize() local backingIdx = paginationOffset for idx = 1, th, 1 do @@ -59,7 +59,7 @@ local function redraw() if backingList[backingIdx] == nil then frame.clearLine() else - drawLine(frame, backingList[backingIdx]) + drawEntry(frame, backingList[backingIdx]) end backingIdx = backingIdx + 1 end @@ -78,13 +78,13 @@ local function updatePage(newOffset) end local function updatePartial(filter) - if drawLine then + if drawEntry then local tw, th = frame.getSize() local backingIdx = paginationOffset for idx = 1, th, 1 do frame.setCursorPos(1, idx) if backingList[backingIdx] ~= nil and filter(backingList[backingIdx]) then - drawLine(frame, backingList[backingIdx]) + drawEntry(frame, backingList[backingIdx]) end backingIdx = backingIdx + 1 end @@ -94,7 +94,7 @@ end return { setTerm=setTerm, - setDrawLineFunc=setDrawLineFunc, + setDrawEntryFunc=setDrawEntryFunc, itemAt=itemAt, updateItemAt=updateItemAt, clearFrom=clearFrom, diff --git a/orders.lua b/orders.lua index 0e79d28..98da008 100644 --- a/orders.lua +++ b/orders.lua @@ -1,18 +1,20 @@ local config = require "config" +local listframes = require "listframes" +local winhlp = require "winhlp" local colony = peripheral.wrap(config.colony_interface_side) -local orders = {} + local frame = term.current() local function setTerm(termlike) frame = termlike + listframes.setTerm(frame) end local function fetch(wait_for) - orders = {} - local colony_orders = colony.getWorkOrders() - if colony_orders ~= nil then - for i, wo in pairs(colony_orders) do + local currentIdx = 1 + if colony.isInColony() then + for i, wo in pairs(colony.getWorkOrders()) do local resources = colony.getWorkOrderResources(wo.id) local res_status = "?/?/0" if resources ~= nil then @@ -28,53 +30,57 @@ local function fetch(wait_for) end res_status = unavailable_count .. "/" .. deliver_count .. "/" .. #resources end - table.insert(orders, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, res_status=res_status, claimed=wo.isClaimed}) + listframes.updateItemAt(currentIdx, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, res_status=res_status, claimed=wo.isClaimed}) + currentIdx = currentIdx + 1 wait_for(config.step_sleep_time) end end + listframes.clearFrom(currentIdx) end -local function displayTab(self) - frame.clear() - local tw,th = frame.getSize() - for i, val in pairs(orders) do - local line_num = (i-self.scrollPos)*2+1 - if line_num > 0 and line_num < th-1 then - frame.setTextColor(colors.lightGray) - frame.setCursorPos(1,line_num) - frame.write(val.id) - frame.write(" ") - if val.pos then - frame.write(val.pos.x .. "," .. val.pos.y .. "," .. val.pos.z) - frame.write(" ") - end - frame.setCursorPos(2,line_num+1) - frame.setTextColor(colors.white) - if val.type then - frame.write(val.type) - frame.write(" ") - end - if val.target then - frame.write(val.target) - frame.write(" ") - end - if val.res_status then - frame.setCursorPos(tw-2-#val.res_status, line_num) - frame.write(val.res_status) - else - frame.setCursorPos(tw-2, line_num) - end - frame.write(" ") - if val.claimed then - frame.setTextColor(colors.yellow) - frame.write("C") - frame.setTextColor(colors.white) - else - frame.write(" ") - end - frame.write(" ") - end +local function drawEntry(win, obj) + local winX, winY = win.getPosition() + local sizeX, sizeY = win.getSize() + win.reposition(winX, winY, sizeX, 2) + win.setBackgroundColor(colors.black) + win.setTextColor(colors.lightGray) + win.write(obj.id) + win.write(" ") + win.setTextColor(colors.yellow) + if obj.pos then + win.write(obj.pos.x .. "," .. obj.pos.y .. "," .. obj.pos.z) + win.write(" ") end + if obj.res_status then + winhlp.alignRight(win, #obj.res_status + 4) + win.write(" " .. obj.res_status) + else + winhlp.alignRight(win, 3) + end + win.write(" ") + if obj.claimed then + win.setTextColor(colors.green) + win.write("C") + else + win.write(" ") + end + win.write(" ") + win.setTextColor(colors.gray) + win.setCursorPos(1, 2) + if obj.target then + winhlp.alignRight(win, #obj.target + 1) + win.write(" " .. obj.target) + end + win.setTextColor(colors.white) + win.setCursorPos(1, 2) + if obj.type then + win.write(obj.type) + end +end +listframes.setDrawEntryFunc(drawEntry) + +local function displayTab(self) + listframes.updatePage(self.scrollPos) end local function onTouch(self, touch_x, touch_y) diff --git a/requests.lua b/requests.lua index df1cbc7..a147f61 100644 --- a/requests.lua +++ b/requests.lua @@ -21,37 +21,38 @@ local function setTerm(termlike) end local function fetch(wait_for) - local reqs = colony.getRequests() local me_items = nil if config.has_me then me_items = main_me.listItems() end local currentIdx = 1 - for i, req in pairs(reqs) do - local found = false - if me_items then - local left_amount = req.count - for j, it in pairs(req.items) do - for k, have_it in pairs(me_items) do - -- TODO: check NBT - if have_it.name == it.name then - found = true - listview.updateItemAt(currentIdx, {req=req, answer=have_it}) - currentIdx = currentIdx + 1 - if left_amount > 0 and permset.has(allowed_items, it.name) then - local export_amount = math.min(have_it.amount, left_amount) - left_amount = left_amount - export_amount - main_me.exportItem({name=it.name, count=export_amount}, config.main_me_inventory_side) + if colony.isInColony() then + for i, req in pairs(colony.getRequests()) do + local found = false + if me_items then + local left_amount = req.count + for j, it in pairs(req.items) do + for k, have_it in pairs(me_items) do + -- TODO: check NBT + if have_it.name == it.name then + found = true + listview.updateItemAt(currentIdx, {req=req, answer=have_it}) + currentIdx = currentIdx + 1 + if left_amount > 0 and permset.has(allowed_items, it.name) then + local export_amount = math.min(have_it.amount, left_amount) + left_amount = left_amount - export_amount + main_me.exportItem({name=it.name, count=export_amount}, config.main_me_inventory_side) + end end end end end + if found == false then + listview.updateItemAt(currentIdx, {req=req, answer=nil}) + currentIdx = currentIdx + 1 + end + wait_for(config.step_sleep_time) end - if found == false then - listview.updateItemAt(currentIdx, {req=req, answer=nil}) - currentIdx = currentIdx + 1 - end - wait_for(config.step_sleep_time) end listview.clearFrom(currentIdx) end @@ -85,7 +86,7 @@ local function drawLine(termlike, obj) termlike.write("] ") end end -listview.setDrawLineFunc(drawLine) +listview.setDrawEntryFunc(drawLine) local function displayTab(self) listview.updatePage(self.scrollPos) diff --git a/update.lua b/update.lua index f0c76f7..1ec4c92 100644 --- a/update.lua +++ b/update.lua @@ -32,6 +32,7 @@ if update_update() then download_file("winhlp.lua") download_file("tabview.lua") download_file("listview.lua") + download_file("listframes.lua") download_file("requests.lua") download_file("orders.lua") download_file("test.lua")