Implement pages

This commit is contained in:
Gabriel Tofvesson 2024-10-04 15:57:21 +02:00
parent 24d60850e8
commit b9582d8d4b

View File

@ -70,15 +70,7 @@ local function loadState(fname, node)
end
local accessNode = Chest:fromPeripheral("minecraft:trapped_chest", true)
---@diagnostic disable-next-line: need-check-nil
local controller = loadState(CACHE_FILE, accessNode:getInventory())
local monitor = peripheral.find("monitor")
local width, height = monitor.getSize()
local function itemList(groups, wBudget)
local function itemList(groups, wBudget, onClick)
local bgColors = {
colors.gray,
colors.black
@ -119,9 +111,7 @@ local function itemList(groups, wBudget)
countLabel
},
vertical = false,
onClick = function(table, x, y, source)
print("Clicked: "..group:getDisplayName())
end
onClick = onClick
}
table.insert(entries, list)
end
@ -132,18 +122,79 @@ local function itemList(groups, wBudget)
}
end
local found = ItemGroup.collectStacks(controller:find(function(stack)
return not stack:isEmpty()
end))
local function renderDefault(state, rootElement)
local event = {os.pullEvent()}
local handled = rootElement:handleEvent(event)
if not handled and event[1] == "monitor_resize" then
local width, height = state.monitor.getSize()
if state.width ~= width or state.height ~= height then
state.width = width
state.height = height
table.sort(found, function(a, b) return a:getItemCount() > b:getItemCount() end)
-- Trigger re-render
rootElement:setDirty(true)
end
end
local subset = {}
for i=1,math.min(13, #found) do
table.insert(subset, found[i])
if not handled then
os.queueEvent(table.unpack(event))
end
end
local listResult = itemList(subset, width)
local PAGES = {
MAIN = function(state)
local found = ItemGroup.collectStacks(state.controller:find(function(stack)
return not stack:isEmpty()
end))
listResult:setParent(monitor)
listResult:draw()
table.sort(found, function(a, b) return a:getItemCount() > b:getItemCount() end)
local subset = {}
for i=1,math.min(13, #found) do
table.insert(subset, found[i])
end
local listResult = itemList(subset, state.width, function(group, x, y, source)
print("Clicked: "..group:getDisplayName())
end)
listResult:setParent(state.monitor)
return function()
renderDefault(state, listResult)
end
end,
GROUP_DETAIL = function(state)
end,
REQUEST = function(state)
end
}
local accessNode = Chest:fromPeripheral("minecraft:trapped_chest", true)
---@diagnostic disable-next-line: need-check-nil
local controller = loadState(CACHE_FILE, accessNode:getInventory())
local monitor = peripheral.find("monitor")
local width, height = monitor.getSize()
local CONTROLLER_STATE = {
controller = controller,
width = width,
height = height,
monitor = monitor,
currentPage = "MAIN",
exit = false
}
os.queueEvent("dummy_event")
while not CONTROLLER_STATE.exit do
PAGES[CONTROLLER_STATE.currentPage](CONTROLLER_STATE)
if CONTROLLER_STATE.nextPage ~= nil then
CONTROLLER_STATE.currentPage = CONTROLLER_STATE.nextPage
CONTROLLER_STATE.nextPage = nil
end
end