[listframes] refactor to ListFrames class

This commit is contained in:
Ben 2024-06-03 23:36:47 +02:00
parent 3d3a0956b2
commit 20bdf46ea4
Signed by: ben
GPG key ID: 0F54A7ED232D3319
2 changed files with 116 additions and 131 deletions

View file

@ -1,28 +1,31 @@
local backingList = {}
local subFrames = {}
local length = 0
local frame = term.current()
local drawEntry = nil
local estimatedTotalHeight = 0
ListFrames = {
frame = term.current(),
drawEntry = nil,
backingList = {},
subFrames = {},
length = 0,
estimatedTotalHeight = 0,
-- pagination start line (not number of entries!)
paginationPos = 1,
}
-- pagination start line (not number of entries!)
local paginationPos = 1
local function setDrawEntryFunc(drawLineFunc)
drawEntry = drawLineFunc
function ListFrames:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
local function repositionEntriesAfter(idx)
local tw, th = frame.getSize()
local entryFrame = subFrames[idx]
function ListFrames:repositionEntriesAfter(idx)
local tw, th = self.frame.getSize()
local entryFrame = self.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]
entryFrame = self.subFrames[idx]
if entryFrame == nil then
break
end
@ -31,142 +34,128 @@ local function repositionEntriesAfter(idx)
if yPos == yStartNext then
break
else
entryFrame.reposition(1, yStartNext, tw, ySize, frame)
entryFrame.reposition(1, yStartNext, tw, ySize, self.frame)
entryFrame.setVisible(true)
end
yStartNext = yPos + ySize
end
idx = idx + 1
entryFrame = subFrames[idx]
entryFrame = self.subFrames[idx]
while entryFrame ~= nil and entryFrame.isVisible() do
entryFrame.setVisible(false)
idx = idx + 1
entryFrame = subFrames[idx]
entryFrame = self.subFrames[idx]
end
end
local function redrawEntry(idx)
local entryFrame = subFrames[idx]
function ListFrames:redrawEntry(idx)
local entryFrame = self.subFrames[idx]
if entryFrame ~= nil then
entryFrame.setCursorPos(1,1)
if drawEntry then
drawEntry(entryFrame, backingList[idx])
if self.drawEntry then
self.drawEntry(entryFrame, self.backingList[idx])
else
entryFrame.setBackgroundColor(colors.red)
entryFrame.clearLine()
entryFrame.setBackgroundColor(colors.black)
end
local tw, th = frame.getSize()
local tw, th = self.frame.getSize()
local xPos, yPos = entryFrame.getPosition()
local xSize, ySize = entryFrame.getSize()
local yEnd = yPos+ySize-1
entryFrame.setVisible(yEnd > 0 and yPos <= th)
yEnd = yEnd+paginationPos-1
if yEnd > estimatedTotalHeight then
estimatedTotalHeight = yEnd
yEnd = yEnd+self.paginationPos-1
if yEnd > self.estimatedTotalHeight then
self.estimatedTotalHeight = yEnd
end
end
end
local function repositionOrCreateEntryFrame(idx)
local previousFrame = subFrames[idx-1]
local yPos = 2-paginationPos
function ListFrames:repositionOrCreateEntryFrame(idx)
local previousFrame = self.subFrames[idx-1]
local yPos = 2-self.paginationPos
if previousFrame ~= nil then
local xPos
xPos, yPos = previousFrame.getPosition()
local xSize, ySize = previousFrame.getSize()
yPos = yPos + ySize
end
local tw, th = frame.getSize()
local entryFrame = subFrames[idx]
local tw, th = self.frame.getSize()
local entryFrame = self.subFrames[idx]
if entryFrame == nil then
subFrames[idx] = window.create(frame, 1, yPos, tw, 1, false)
self.subFrames[idx] = window.create(self.frame, 1, yPos, tw, 1, false)
else
entryFrame.setVisible(false)
local xSize, ySize = entryFrame.getSize()
entryFrame.reposition(1, yPos, tw, ySize, frame)
entryFrame.reposition(1, yPos, tw, ySize, self.frame)
end
end
local function updateItemAt(idx, item)
backingList[idx] = item
repositionOrCreateEntryFrame(idx)
if idx > length then
length = idx
function ListFrames:updateItemAt(idx, item)
self.backingList[idx] = item
self:repositionOrCreateEntryFrame(idx)
if idx > self.length then
self.length = idx
end
redrawEntry(idx)
repositionEntriesAfter(idx)
self:redrawEntry(idx)
self:repositionEntriesAfter(idx)
end
local function clearFrom(idx)
estimatedTotalHeight = idx-1
local tw, th = frame.getSize()
for j = length, idx, -1 do
backingList[j] = nil
if subFrames[j] ~= nil then
subFrames[j].setVisible(false)
function ListFrames:clearFrom(idx)
self.estimatedTotalHeight = idx-1
local tw, th = self.frame.getSize()
for j = self.length, idx, -1 do
self.backingList[j] = nil
if self.subFrames[j] ~= nil then
self.subFrames[j].setVisible(false)
end
end
length = idx-1
for y = idx-paginationPos+1, th, 1 do
frame.setCursorPos(1, y)
frame.clearLine()
self.length = idx-1
for y = idx-self.paginationPos+1, th, 1 do
self.frame.setCursorPos(1, y)
self.frame.clearLine()
end
end
local function redraw()
if drawEntry then
frame.clear()
local tw, th = frame.getSize()
local yPos = 2-paginationPos
for idx = 1, length, 1 do
assert(backingList[idx] ~= nil, "nil element at idx="..idx)
function ListFrames:redraw()
if self.drawEntry then
self.frame.clear()
local tw, th = self.frame.getSize()
local yPos = 2-self.paginationPos
for idx = 1, self.length, 1 do
assert(self.backingList[idx] ~= nil, "nil element at idx="..idx)
if yPos <= th then
repositionOrCreateEntryFrame(idx)
redrawEntry(idx)
local xSize, ySize = subFrames[idx].getSize()
self:repositionOrCreateEntryFrame(idx)
self:redrawEntry(idx)
local xSize, ySize = self.subFrames[idx].getSize()
yPos = yPos + ySize
elseif subFrames[idx] ~= nil then
subFrames[idx].setVisible(false)
elseif self.subFrames[idx] ~= nil then
self.subFrames[idx].setVisible(false)
end
end
else
frame.setBackgroundColor(colors.red)
frame.clear()
frame.setBackgroundColor(colors.black)
self.frame.setBackgroundColor(colors.red)
self.frame.clear()
self.frame.setBackgroundColor(colors.black)
end
end
local function setTerm(termlike)
frame = termlike
-- reposition visible subFrames
redraw()
end
local function updatePage(scrollPos)
if scrollPos ~= paginationPos then
paginationPos = scrollPos
redraw()
function ListFrames:updatePage(scrollPos)
if scrollPos ~= self.paginationPos then
self.paginationPos = scrollPos
self:redraw()
end
end
local function estimatedHeight()
local lastEntryFrame = subFrames[length]
function ListFrames:estimatedHeight()
local lastEntryFrame = self.subFrames[self.length]
if lastEntryFrame ~= nil and lastEntryFrame.isVisible() then
local xPos, yPos = lastEntryFrame.getPosition()
local xSize, ySize = lastEntryFrame.getSize()
estimatedTotalHeight = yPos+ySize-1+paginationPos-1
self.estimatedTotalHeight = yPos+ySize-1+self.paginationPos-1
end
return estimatedTotalHeight
return self.estimatedTotalHeight
end
return {
setTerm=setTerm,
setDrawEntryFunc=setDrawEntryFunc,
updateItemAt=updateItemAt,
clearFrom=clearFrom,
redraw=redraw,
updatePage=updatePage,
estimatedHeight=estimatedHeight,
}
return ListFrames

View file

@ -1,46 +1,8 @@
local config = require "config"
local listframes = require "listframes"
local winhlp = require "winhlp"
local colony = peripheral.wrap(config.colony_interface_side)
local frame = term.current()
local function setTerm(termlike)
frame = termlike
listframes.setTerm(frame)
end
local function fetch(wait_for)
local currentIdx = 1
if colony.isInColony() then
for i, wo in pairs(colony.getWorkOrders()) do
local resources = colony.getWorkOrderResources(wo.id)
local resStatus = "?/?/0"
if resources ~= nil then
local unavailable_count = 0
local deliver_count = 0
for j, res in pairs(resources) do
if not res.available then
unavailable_count = unavailable_count + 1
end
if res.delivering then
deliver_count = deliver_count + 1
end
end
resStatus = unavailable_count .. "/" .. deliver_count .. "/" .. #resources
end
listframes.updateItemAt(currentIdx, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, status=resStatus, claimed=wo.isClaimed})
currentIdx = currentIdx + 1
wait_for(config.step_sleep_time)
if not colony.isInColony() then
break
end
end
end
listframes.clearFrom(currentIdx)
end
local function drawEntry(win, obj)
winhlp.setHeight(win, 2)
win.setBackgroundColor(colors.black)
@ -82,14 +44,48 @@ local function drawEntry(win, obj)
win.write(obj.type)
end
end
listframes.setDrawEntryFunc(drawEntry)
local listframes = (require "listframes"):new{drawEntry=drawEntry}
local function setTerm(termlike)
listframes.frame = termlike
end
local function fetch(wait_for)
local currentIdx = 1
if colony.isInColony() then
for i, wo in pairs(colony.getWorkOrders()) do
local resources = colony.getWorkOrderResources(wo.id)
local resStatus = "?/?/0"
if resources ~= nil then
local unavailable_count = 0
local deliver_count = 0
for j, res in pairs(resources) do
if not res.available then
unavailable_count = unavailable_count + 1
end
if res.delivering then
deliver_count = deliver_count + 1
end
end
resStatus = unavailable_count .. "/" .. deliver_count .. "/" .. #resources
end
listframes:updateItemAt(currentIdx, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, status=resStatus, claimed=wo.isClaimed})
currentIdx = currentIdx + 1
wait_for(config.step_sleep_time)
if not colony.isInColony() then
break
end
end
end
listframes:clearFrom(currentIdx)
end
local function displayTab(self)
listframes.updatePage(self.scrollPos)
listframes:updatePage(self.scrollPos)
end
local function estimatedHeight()
return listframes.estimatedHeight()
return listframes:estimatedHeight()
end