Add listframes for multiline lists, refactor order list

This commit is contained in:
Ben 2024-06-02 16:41:54 +02:00
parent 35a5aeabb8
commit 7616a5bed3
Signed by: ben
GPG key ID: 0F54A7ED232D3319
5 changed files with 240 additions and 80 deletions

152
listframes.lua Normal file
View file

@ -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,
}

View file

@ -2,22 +2,22 @@ local backingList = {}
local length = 0 local length = 0
local paginationOffset = 1 local paginationOffset = 1
local frame = term.current() local frame = term.current()
local drawLine = nil local drawEntry = nil
local function setTerm(termlike) local function setTerm(termlike)
frame = termlike frame = termlike
end end
local function setDrawLineFunc(drawLineFunc) local function setDrawEntryFunc(drawLineFunc)
drawLine = drawLineFunc drawEntry = drawLineFunc
end end
local function redrawLine(idx) local function redrawEntry(idx)
local tw, th = frame.getSize() local tw, th = frame.getSize()
if idx >= paginationOffset and idx-paginationOffset <= th then if idx >= paginationOffset and idx-paginationOffset <= th then
frame.setCursorPos(1, idx-paginationOffset+1) frame.setCursorPos(1, idx-paginationOffset+1)
if drawLine then if drawEntry then
drawLine(frame, backingList[idx]) drawEntry(frame, backingList[idx])
else else
frame.setBackgroundColor(colors.red) frame.setBackgroundColor(colors.red)
frame.clearLine() frame.clearLine()
@ -35,7 +35,7 @@ local function updateItemAt(idx, item)
if idx > length then if idx > length then
length = idx length = idx
end end
redrawLine(idx) redrawEntry(idx)
end end
local function clearFrom(idx) local function clearFrom(idx)
@ -51,7 +51,7 @@ local function clearFrom(idx)
end end
local function redraw() local function redraw()
if drawLine then if drawEntry then
local tw, th = frame.getSize() local tw, th = frame.getSize()
local backingIdx = paginationOffset local backingIdx = paginationOffset
for idx = 1, th, 1 do for idx = 1, th, 1 do
@ -59,7 +59,7 @@ local function redraw()
if backingList[backingIdx] == nil then if backingList[backingIdx] == nil then
frame.clearLine() frame.clearLine()
else else
drawLine(frame, backingList[backingIdx]) drawEntry(frame, backingList[backingIdx])
end end
backingIdx = backingIdx + 1 backingIdx = backingIdx + 1
end end
@ -78,13 +78,13 @@ local function updatePage(newOffset)
end end
local function updatePartial(filter) local function updatePartial(filter)
if drawLine then if drawEntry then
local tw, th = frame.getSize() local tw, th = frame.getSize()
local backingIdx = paginationOffset local backingIdx = paginationOffset
for idx = 1, th, 1 do for idx = 1, th, 1 do
frame.setCursorPos(1, idx) frame.setCursorPos(1, idx)
if backingList[backingIdx] ~= nil and filter(backingList[backingIdx]) then if backingList[backingIdx] ~= nil and filter(backingList[backingIdx]) then
drawLine(frame, backingList[backingIdx]) drawEntry(frame, backingList[backingIdx])
end end
backingIdx = backingIdx + 1 backingIdx = backingIdx + 1
end end
@ -94,7 +94,7 @@ end
return { return {
setTerm=setTerm, setTerm=setTerm,
setDrawLineFunc=setDrawLineFunc, setDrawEntryFunc=setDrawEntryFunc,
itemAt=itemAt, itemAt=itemAt,
updateItemAt=updateItemAt, updateItemAt=updateItemAt,
clearFrom=clearFrom, clearFrom=clearFrom,

View file

@ -1,18 +1,20 @@
local config = require "config" local config = require "config"
local listframes = require "listframes"
local winhlp = require "winhlp"
local colony = peripheral.wrap(config.colony_interface_side) local colony = peripheral.wrap(config.colony_interface_side)
local orders = {}
local frame = term.current() local frame = term.current()
local function setTerm(termlike) local function setTerm(termlike)
frame = termlike frame = termlike
listframes.setTerm(frame)
end end
local function fetch(wait_for) local function fetch(wait_for)
orders = {} local currentIdx = 1
local colony_orders = colony.getWorkOrders() if colony.isInColony() then
if colony_orders ~= nil then for i, wo in pairs(colony.getWorkOrders()) do
for i, wo in pairs(colony_orders) do
local resources = colony.getWorkOrderResources(wo.id) local resources = colony.getWorkOrderResources(wo.id)
local res_status = "?/?/0" local res_status = "?/?/0"
if resources ~= nil then if resources ~= nil then
@ -28,53 +30,57 @@ local function fetch(wait_for)
end end
res_status = unavailable_count .. "/" .. deliver_count .. "/" .. #resources res_status = unavailable_count .. "/" .. deliver_count .. "/" .. #resources
end 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) wait_for(config.step_sleep_time)
end end
end end
listframes.clearFrom(currentIdx)
end end
local function displayTab(self) local function drawEntry(win, obj)
frame.clear() local winX, winY = win.getPosition()
local tw,th = frame.getSize() local sizeX, sizeY = win.getSize()
for i, val in pairs(orders) do win.reposition(winX, winY, sizeX, 2)
local line_num = (i-self.scrollPos)*2+1 win.setBackgroundColor(colors.black)
if line_num > 0 and line_num < th-1 then win.setTextColor(colors.lightGray)
frame.setTextColor(colors.lightGray) win.write(obj.id)
frame.setCursorPos(1,line_num) win.write(" ")
frame.write(val.id) win.setTextColor(colors.yellow)
frame.write(" ") if obj.pos then
if val.pos then win.write(obj.pos.x .. "," .. obj.pos.y .. "," .. obj.pos.z)
frame.write(val.pos.x .. "," .. val.pos.y .. "," .. val.pos.z) win.write(" ")
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
end 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 end
local function onTouch(self, touch_x, touch_y) local function onTouch(self, touch_x, touch_y)

View file

@ -21,37 +21,38 @@ local function setTerm(termlike)
end end
local function fetch(wait_for) local function fetch(wait_for)
local reqs = colony.getRequests()
local me_items = nil local me_items = nil
if config.has_me then if config.has_me then
me_items = main_me.listItems() me_items = main_me.listItems()
end end
local currentIdx = 1 local currentIdx = 1
for i, req in pairs(reqs) do if colony.isInColony() then
local found = false for i, req in pairs(colony.getRequests()) do
if me_items then local found = false
local left_amount = req.count if me_items then
for j, it in pairs(req.items) do local left_amount = req.count
for k, have_it in pairs(me_items) do for j, it in pairs(req.items) do
-- TODO: check NBT for k, have_it in pairs(me_items) do
if have_it.name == it.name then -- TODO: check NBT
found = true if have_it.name == it.name then
listview.updateItemAt(currentIdx, {req=req, answer=have_it}) found = true
currentIdx = currentIdx + 1 listview.updateItemAt(currentIdx, {req=req, answer=have_it})
if left_amount > 0 and permset.has(allowed_items, it.name) then currentIdx = currentIdx + 1
local export_amount = math.min(have_it.amount, left_amount) if left_amount > 0 and permset.has(allowed_items, it.name) then
left_amount = left_amount - export_amount local export_amount = math.min(have_it.amount, left_amount)
main_me.exportItem({name=it.name, count=export_amount}, config.main_me_inventory_side) 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
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 end
if found == false then
listview.updateItemAt(currentIdx, {req=req, answer=nil})
currentIdx = currentIdx + 1
end
wait_for(config.step_sleep_time)
end end
listview.clearFrom(currentIdx) listview.clearFrom(currentIdx)
end end
@ -85,7 +86,7 @@ local function drawLine(termlike, obj)
termlike.write("] ") termlike.write("] ")
end end
end end
listview.setDrawLineFunc(drawLine) listview.setDrawEntryFunc(drawLine)
local function displayTab(self) local function displayTab(self)
listview.updatePage(self.scrollPos) listview.updatePage(self.scrollPos)

View file

@ -32,6 +32,7 @@ if update_update() then
download_file("winhlp.lua") download_file("winhlp.lua")
download_file("tabview.lua") download_file("tabview.lua")
download_file("listview.lua") download_file("listview.lua")
download_file("listframes.lua")
download_file("requests.lua") download_file("requests.lua")
download_file("orders.lua") download_file("orders.lua")
download_file("test.lua") download_file("test.lua")