cc/listframes.lua

173 lines
4.8 KiB
Lua
Raw Normal View History

local backingList = {}
local subFrames = {}
local length = 0
local frame = term.current()
local drawEntry = nil
local estimatedTotalHeight = 0
2024-06-02 15:39:21 +00:00
-- pagination start line (not number of entries!)
local paginationPos = 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()
local yEnd = yPos+ySize-1
entryFrame.setVisible(yEnd > 0 and yPos <= th)
yEnd = yEnd+paginationPos-1
if yEnd > estimatedTotalHeight then
estimatedTotalHeight = yEnd
end
end
end
local function repositionOrCreateEntryFrame(idx)
local previousFrame = subFrames[idx-1]
2024-06-02 15:39:21 +00:00
local yPos = 2-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]
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)
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)
end
end
length = idx-1
2024-06-02 15:39:21 +00:00
for y = idx-paginationPos+1, th, 1 do
frame.setCursorPos(1, y)
frame.clearLine()
end
end
local function redraw()
if drawEntry then
frame.clear()
local tw, th = frame.getSize()
2024-06-02 15:39:21 +00:00
local yPos = 2-paginationPos
for idx = 1, length, 1 do
assert(backingList[idx] ~= nil, "nil element at idx="..idx)
2024-06-02 15:39:21 +00:00
if yPos <= 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
2024-06-02 15:39:21 +00:00
local function updatePage(scrollPos)
if scrollPos ~= paginationPos then
paginationPos = scrollPos
redraw()
end
end
2024-06-02 15:51:21 +00:00
local function estimatedHeight()
local lastEntryFrame = subFrames[length]
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()
estimatedTotalHeight = yPos+ySize-1+paginationPos-1
2024-06-02 15:51:21 +00:00
end
return estimatedTotalHeight
2024-06-02 15:51:21 +00:00
end
return {
setTerm=setTerm,
setDrawEntryFunc=setDrawEntryFunc,
updateItemAt=updateItemAt,
clearFrom=clearFrom,
redraw=redraw,
updatePage=updatePage,
2024-06-02 15:51:21 +00:00
estimatedHeight=estimatedHeight,
}