Refactor monitor setup and drawing functions to utilize shared UI module

This commit is contained in:
MayaTheShy
2026-03-22 01:58:13 -04:00
parent 39b95b3663
commit 11c38dbabb

View File

@@ -88,6 +88,12 @@ local CRAFTABLE = {} -- populated from master broadcast
local craftTurtleOk = false local craftTurtleOk = false
local connected = false -- true once first state received local connected = false -- true once first state received
-------------------------------------------------
-- Shared UI helpers
-------------------------------------------------
local ui = dofile("lib/ui.lua")
------------------------------------------------- -------------------------------------------------
-- UI State (local to client) -- UI State (local to client)
------------------------------------------------- -------------------------------------------------
@@ -135,47 +141,13 @@ local smelterMonName = nil
local networkModem = nil local networkModem = nil
local function setupMonitor() local function setupMonitor()
mon = peripheral.wrap(MONITOR_SIDE) mon, monName = ui.setupMonitor(MONITOR_SIDE, SMELTER_MONITOR_SIDE)
if mon and mon.setTextScale then return mon ~= nil
monName = MONITOR_SIDE
else
mon = nil
end
if not mon then
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "monitor" and name ~= SMELTER_MONITOR_SIDE then
mon = peripheral.wrap(name)
monName = name
break
end
end
end
if not mon then return false end
mon.setTextScale(0.5)
mon.clear()
return true
end end
local function setupSmelterMonitor() local function setupSmelterMonitor()
smelterMon = peripheral.wrap(SMELTER_MONITOR_SIDE) smelterMon, smelterMonName = ui.setupSmelterMonitor(SMELTER_MONITOR_SIDE, monName)
if smelterMon and smelterMon.setTextScale then return smelterMon ~= nil
smelterMonName = SMELTER_MONITOR_SIDE
else
smelterMon = nil
end
if not smelterMon then
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "monitor" and name ~= monName then
smelterMon = peripheral.wrap(name)
smelterMonName = name
break
end
end
end
if not smelterMon then return false end
smelterMon.setTextScale(0.5)
smelterMon.clear()
return true
end end
------------------------------------------------- -------------------------------------------------
@@ -183,18 +155,7 @@ end
------------------------------------------------- -------------------------------------------------
local function getFilteredItems() local function getFilteredItems()
local filtered = {} return ui.getFilteredItems(cache.itemList, searchQuery)
for _, item in ipairs(cache.itemList) do
if searchQuery == "" then
table.insert(filtered, item)
else
local lower = item.name:lower():gsub("minecraft:", ""):gsub("_", " ")
if lower:find(searchQuery:lower(), 1, true) then
table.insert(filtered, item)
end
end
end
return filtered
end end
------------------------------------------------- -------------------------------------------------
@@ -202,140 +163,69 @@ end
------------------------------------------------- -------------------------------------------------
local function addZone(x1, y1, x2, y2, action, data) local function addZone(x1, y1, x2, y2, action, data)
table.insert(pendingZones, { ui.addZone(pendingZones, x1, y1, x2, y2, action, data)
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
action = action, data = data
})
end end
local function hitTest(x, y) local function hitTest(x, y)
for _, zone in ipairs(touchZones) do return ui.hitTest(touchZones, x, y)
if x >= zone.x1 and x <= zone.x2 and y >= zone.y1 and y <= zone.y2 then
return zone.action, zone.data
end
end
return nil, nil
end end
------------------------------------------------- -------------------------------------------------
-- Crafting helpers (display-only, no peripheral calls) -- Crafting helpers (delegated to shared ui module)
------------------------------------------------- -------------------------------------------------
local function getRecipeIngredients(recipe) --- Get stock total for an item from itemList
local ingredients = {} local function getItemTotal(itemName)
for _, item in ipairs(recipe.grid) do for _, item in ipairs(cache.itemList) do
if item then if item.name == itemName then return item.total end
ingredients[item] = (ingredients[item] or 0) + 1
end
end end
return ingredients return 0
end
local function getRecipeIngredients(recipe)
return ui.getRecipeIngredients(recipe)
end end
local function canCraftRecipe(recipe) local function canCraftRecipe(recipe)
local ingredients = getRecipeIngredients(recipe) return ui.canCraftRecipe(recipe, getItemTotal)
local itemTotals = {}
for _, item in ipairs(cache.itemList) do
itemTotals[item.name] = item.total
end
for itemName, needed in pairs(ingredients) do
if (itemTotals[itemName] or 0) < needed then return false end
end
return true
end end
local function maxCraftBatches(recipe) local function maxCraftBatches(recipe)
local ingredients = getRecipeIngredients(recipe) return ui.maxCraftBatches(recipe, getItemTotal)
local itemTotals = {}
for _, item in ipairs(cache.itemList) do
itemTotals[item.name] = item.total
end
local minBatches = math.huge
for itemName, needed in pairs(ingredients) do
local batches = math.floor((itemTotals[itemName] or 0) / needed)
if batches < minBatches then minBatches = batches end
end
if minBatches == math.huge then return 0 end
return minBatches
end end
local function getMissingIngredients(recipe) local function getMissingIngredients(recipe)
local ingredients = getRecipeIngredients(recipe) return ui.getMissingIngredients(recipe, getItemTotal)
local itemTotals = {}
for _, item in ipairs(cache.itemList) do
itemTotals[item.name] = item.total
end
local missing = {}
for itemName, needed in pairs(ingredients) do
local have = itemTotals[itemName] or 0
if have < needed then
table.insert(missing, { name = itemName, have = have, need = needed })
end
end
return missing
end end
------------------------------------------------- -------------------------------------------------
-- Touch zone helpers -- Smelter touch zone helpers
------------------------------------------------- -------------------------------------------------
local function addSmelterZone(x1, y1, x2, y2, action, data) local function addSmelterZone(x1, y1, x2, y2, action, data)
table.insert(smelterPendingZones, { ui.addZone(smelterPendingZones, x1, y1, x2, y2, action, data)
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
action = action, data = data
})
end end
local function smelterHitTest(x, y) local function smelterHitTest(x, y)
for _, zone in ipairs(smelterTouchZones) do return ui.hitTest(smelterTouchZones, x, y)
if x >= zone.x1 and x <= zone.x2 and y >= zone.y1 and y <= zone.y2 then
return zone.action, zone.data
end
end
return nil, nil
end end
------------------------------------------------- -------------------------------------------------
-- Drawing helpers -- Drawing helpers (delegated to shared ui module)
------------------------------------------------- -------------------------------------------------
local draw = nil local draw = nil
local function monWrite(x, y, text, fg, bg) local function setDrawTarget(target)
draw.setCursorPos(x, y) draw = target
if fg then draw.setTextColor(fg) end ui.draw = target
if bg then draw.setBackgroundColor(bg) end
draw.write(text)
end end
local function monFill(y, color) local function monWrite(x, y, text, fg, bg) ui.monWrite(x, y, text, fg, bg) end
local w, _ = draw.getSize() local function monFill(y, color) ui.monFill(y, color) end
draw.setCursorPos(1, y) local function monCenter(y, text, fg, bg) ui.monCenter(y, text, fg, bg) end
draw.setBackgroundColor(color) local function monBar(x, y, w, r, bc, bgc) ui.monBar(x, y, w, r, bc, bgc) end
draw.write(string.rep(" ", w)) local function drawButton(x, y, t, fg, bg, pl, pr) return ui.drawButton(x, y, t, fg, bg, pl, pr) end
end
local function monCenter(y, text, fg, bg)
local w, _ = draw.getSize()
local x = math.floor((w - #text) / 2) + 1
monWrite(x, y, text, fg, bg)
end
local function monBar(x, y, width, ratio, barColor, bgColor)
local filled = math.floor(ratio * width)
draw.setCursorPos(x, y)
draw.setBackgroundColor(barColor)
draw.write(string.rep(" ", filled))
draw.setBackgroundColor(bgColor)
draw.write(string.rep(" ", width - filled))
end
local function drawButton(x, y, text, fg, bg, padLeft, padRight)
padLeft = padLeft or 1
padRight = padRight or 1
local full = string.rep(" ", padLeft) .. text .. string.rep(" ", padRight)
monWrite(x, y, full, fg, bg)
return x, y, x + #full - 1, y
end
------------------------------------------------- -------------------------------------------------
-- Send command to master -- Send command to master
@@ -403,7 +293,7 @@ local function drawDashboard()
local w, h = mon.getSize() local w, h = mon.getSize()
pendingZones = {} pendingZones = {}
draw = window.create(mon, 1, 1, w, h, false) setDrawTarget(window.create(mon, 1, 1, w, h, false))
draw.setBackgroundColor(colors.black) draw.setBackgroundColor(colors.black)
draw.clear() draw.clear()
@@ -709,7 +599,7 @@ local function drawSmelterDashboard()
local w, h = smelterMon.getSize() local w, h = smelterMon.getSize()
smelterPendingZones = {} smelterPendingZones = {}
draw = window.create(smelterMon, 1, 1, w, h, false) setDrawTarget(window.create(smelterMon, 1, 1, w, h, false))
draw.setBackgroundColor(colors.black) draw.setBackgroundColor(colors.black)
draw.clear() draw.clear()