2024-06-03 21:36:47 +00:00
|
|
|
ListFrames = {
|
|
|
|
frame = term.current(),
|
|
|
|
drawEntry = nil,
|
|
|
|
backingList = {},
|
|
|
|
subFrames = {},
|
|
|
|
length = 0,
|
|
|
|
estimatedTotalHeight = 0,
|
|
|
|
-- pagination start line (not number of entries!)
|
|
|
|
paginationPos = 1,
|
|
|
|
}
|
2024-06-02 14:41:54 +00:00
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
function ListFrames:new(o)
|
|
|
|
o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
function ListFrames:repositionEntriesAfter(idx)
|
|
|
|
local tw, th = self.frame.getSize()
|
|
|
|
local entryFrame = self.subFrames[idx]
|
2024-06-02 14:41:54 +00:00
|
|
|
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
|
2024-06-03 21:36:47 +00:00
|
|
|
entryFrame = self.subFrames[idx]
|
2024-06-02 14:41:54 +00:00
|
|
|
if entryFrame == nil then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
xPos, yPos = entryFrame.getPosition()
|
|
|
|
xSize, ySize = entryFrame.getSize()
|
|
|
|
if yPos == yStartNext then
|
|
|
|
break
|
|
|
|
else
|
2024-06-03 21:36:47 +00:00
|
|
|
entryFrame.reposition(1, yStartNext, tw, ySize, self.frame)
|
2024-06-02 14:41:54 +00:00
|
|
|
entryFrame.setVisible(true)
|
|
|
|
end
|
|
|
|
yStartNext = yPos + ySize
|
|
|
|
end
|
|
|
|
idx = idx + 1
|
2024-06-03 21:36:47 +00:00
|
|
|
entryFrame = self.subFrames[idx]
|
2024-06-02 14:41:54 +00:00
|
|
|
while entryFrame ~= nil and entryFrame.isVisible() do
|
|
|
|
entryFrame.setVisible(false)
|
|
|
|
idx = idx + 1
|
2024-06-03 21:36:47 +00:00
|
|
|
entryFrame = self.subFrames[idx]
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
function ListFrames:redrawEntry(idx)
|
|
|
|
local entryFrame = self.subFrames[idx]
|
2024-06-02 14:41:54 +00:00
|
|
|
if entryFrame ~= nil then
|
|
|
|
entryFrame.setCursorPos(1,1)
|
2024-06-03 21:36:47 +00:00
|
|
|
if self.drawEntry then
|
|
|
|
self.drawEntry(entryFrame, self.backingList[idx])
|
2024-06-02 14:41:54 +00:00
|
|
|
else
|
|
|
|
entryFrame.setBackgroundColor(colors.red)
|
|
|
|
entryFrame.clearLine()
|
|
|
|
entryFrame.setBackgroundColor(colors.black)
|
|
|
|
end
|
2024-06-03 21:36:47 +00:00
|
|
|
local tw, th = self.frame.getSize()
|
2024-06-02 14:41:54 +00:00
|
|
|
local xPos, yPos = entryFrame.getPosition()
|
|
|
|
local xSize, ySize = entryFrame.getSize()
|
2024-06-02 18:41:58 +00:00
|
|
|
local yEnd = yPos+ySize-1
|
|
|
|
entryFrame.setVisible(yEnd > 0 and yPos <= th)
|
2024-06-03 21:36:47 +00:00
|
|
|
yEnd = yEnd+self.paginationPos-1
|
|
|
|
if yEnd > self.estimatedTotalHeight then
|
|
|
|
self.estimatedTotalHeight = yEnd
|
2024-06-02 18:41:58 +00:00
|
|
|
end
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
function ListFrames:repositionOrCreateEntryFrame(idx)
|
|
|
|
local previousFrame = self.subFrames[idx-1]
|
|
|
|
local yPos = 2-self.paginationPos
|
2024-06-02 14:41:54 +00:00
|
|
|
if previousFrame ~= nil then
|
|
|
|
local xPos
|
|
|
|
xPos, yPos = previousFrame.getPosition()
|
2024-06-02 14:49:29 +00:00
|
|
|
local xSize, ySize = previousFrame.getSize()
|
|
|
|
yPos = yPos + ySize
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
2024-06-03 21:36:47 +00:00
|
|
|
local tw, th = self.frame.getSize()
|
|
|
|
local entryFrame = self.subFrames[idx]
|
2024-06-02 14:41:54 +00:00
|
|
|
if entryFrame == nil then
|
2024-06-03 21:36:47 +00:00
|
|
|
self.subFrames[idx] = window.create(self.frame, 1, yPos, tw, 1, false)
|
2024-06-02 14:41:54 +00:00
|
|
|
else
|
|
|
|
entryFrame.setVisible(false)
|
|
|
|
local xSize, ySize = entryFrame.getSize()
|
2024-06-03 21:36:47 +00:00
|
|
|
entryFrame.reposition(1, yPos, tw, ySize, self.frame)
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
function ListFrames:updateItemAt(idx, item)
|
|
|
|
self.backingList[idx] = item
|
|
|
|
self:repositionOrCreateEntryFrame(idx)
|
|
|
|
if idx > self.length then
|
|
|
|
self.length = idx
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
2024-06-03 21:36:47 +00:00
|
|
|
self:redrawEntry(idx)
|
|
|
|
self:repositionEntriesAfter(idx)
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
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)
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
end
|
2024-06-03 21:36:47 +00:00
|
|
|
self.length = idx-1
|
|
|
|
for y = idx-self.paginationPos+1, th, 1 do
|
|
|
|
self.frame.setCursorPos(1, y)
|
|
|
|
self.frame.clearLine()
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
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)
|
2024-06-02 15:39:21 +00:00
|
|
|
if yPos <= th then
|
2024-06-03 21:36:47 +00:00
|
|
|
self:repositionOrCreateEntryFrame(idx)
|
|
|
|
self:redrawEntry(idx)
|
|
|
|
local xSize, ySize = self.subFrames[idx].getSize()
|
2024-06-02 14:41:54 +00:00
|
|
|
yPos = yPos + ySize
|
2024-06-03 21:36:47 +00:00
|
|
|
elseif self.subFrames[idx] ~= nil then
|
|
|
|
self.subFrames[idx].setVisible(false)
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2024-06-03 21:36:47 +00:00
|
|
|
self.frame.setBackgroundColor(colors.red)
|
|
|
|
self.frame.clear()
|
|
|
|
self.frame.setBackgroundColor(colors.black)
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
function ListFrames:updatePage(scrollPos)
|
|
|
|
if scrollPos ~= self.paginationPos then
|
|
|
|
self.paginationPos = scrollPos
|
|
|
|
self:redraw()
|
2024-06-02 14:41:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
function ListFrames:estimatedHeight()
|
|
|
|
local lastEntryFrame = self.subFrames[self.length]
|
2024-06-02 18:41:58 +00:00
|
|
|
if lastEntryFrame ~= nil and lastEntryFrame.isVisible() then
|
2024-06-02 15:51:21 +00:00
|
|
|
local xPos, yPos = lastEntryFrame.getPosition()
|
|
|
|
local xSize, ySize = lastEntryFrame.getSize()
|
2024-06-03 21:36:47 +00:00
|
|
|
self.estimatedTotalHeight = yPos+ySize-1+self.paginationPos-1
|
2024-06-02 15:51:21 +00:00
|
|
|
end
|
2024-06-03 21:36:47 +00:00
|
|
|
return self.estimatedTotalHeight
|
2024-06-02 15:51:21 +00:00
|
|
|
end
|
|
|
|
|
2024-06-02 14:41:54 +00:00
|
|
|
|
2024-06-03 21:36:47 +00:00
|
|
|
return ListFrames
|