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 connected = false -- true once first state received
-------------------------------------------------
-- Shared UI helpers
-------------------------------------------------
local ui = dofile("lib/ui.lua")
-------------------------------------------------
-- UI State (local to client)
-------------------------------------------------
@@ -135,47 +141,13 @@ local smelterMonName = nil
local networkModem = nil
local function setupMonitor()
mon = peripheral.wrap(MONITOR_SIDE)
if mon and mon.setTextScale then
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
mon, monName = ui.setupMonitor(MONITOR_SIDE, SMELTER_MONITOR_SIDE)
return mon ~= nil
end
local function setupSmelterMonitor()
smelterMon = peripheral.wrap(SMELTER_MONITOR_SIDE)
if smelterMon and smelterMon.setTextScale then
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
smelterMon, smelterMonName = ui.setupSmelterMonitor(SMELTER_MONITOR_SIDE, monName)
return smelterMon ~= nil
end
-------------------------------------------------
@@ -183,18 +155,7 @@ end
-------------------------------------------------
local function getFilteredItems()
local filtered = {}
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
return ui.getFilteredItems(cache.itemList, searchQuery)
end
-------------------------------------------------
@@ -202,140 +163,69 @@ end
-------------------------------------------------
local function addZone(x1, y1, x2, y2, action, data)
table.insert(pendingZones, {
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
action = action, data = data
})
ui.addZone(pendingZones, x1, y1, x2, y2, action, data)
end
local function hitTest(x, y)
for _, zone in ipairs(touchZones) do
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
return ui.hitTest(touchZones, x, y)
end
-------------------------------------------------
-- Crafting helpers (display-only, no peripheral calls)
-- Crafting helpers (delegated to shared ui module)
-------------------------------------------------
local function getRecipeIngredients(recipe)
local ingredients = {}
for _, item in ipairs(recipe.grid) do
if item then
ingredients[item] = (ingredients[item] or 0) + 1
end
--- Get stock total for an item from itemList
local function getItemTotal(itemName)
for _, item in ipairs(cache.itemList) do
if item.name == itemName then return item.total end
end
return ingredients
return 0
end
local function getRecipeIngredients(recipe)
return ui.getRecipeIngredients(recipe)
end
local function canCraftRecipe(recipe)
local ingredients = getRecipeIngredients(recipe)
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
return ui.canCraftRecipe(recipe, getItemTotal)
end
local function maxCraftBatches(recipe)
local ingredients = getRecipeIngredients(recipe)
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
return ui.maxCraftBatches(recipe, getItemTotal)
end
local function getMissingIngredients(recipe)
local ingredients = getRecipeIngredients(recipe)
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
return ui.getMissingIngredients(recipe, getItemTotal)
end
-------------------------------------------------
-- Touch zone helpers
-- Smelter touch zone helpers
-------------------------------------------------
local function addSmelterZone(x1, y1, x2, y2, action, data)
table.insert(smelterPendingZones, {
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
action = action, data = data
})
ui.addZone(smelterPendingZones, x1, y1, x2, y2, action, data)
end
local function smelterHitTest(x, y)
for _, zone in ipairs(smelterTouchZones) do
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
return ui.hitTest(smelterTouchZones, x, y)
end
-------------------------------------------------
-- Drawing helpers
-- Drawing helpers (delegated to shared ui module)
-------------------------------------------------
local draw = nil
local function monWrite(x, y, text, fg, bg)
draw.setCursorPos(x, y)
if fg then draw.setTextColor(fg) end
if bg then draw.setBackgroundColor(bg) end
draw.write(text)
local function setDrawTarget(target)
draw = target
ui.draw = target
end
local function monFill(y, color)
local w, _ = draw.getSize()
draw.setCursorPos(1, y)
draw.setBackgroundColor(color)
draw.write(string.rep(" ", w))
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
local function monWrite(x, y, text, fg, bg) ui.monWrite(x, y, text, fg, bg) end
local function monFill(y, color) ui.monFill(y, color) end
local function monCenter(y, text, fg, bg) ui.monCenter(y, text, fg, bg) end
local function monBar(x, y, w, r, bc, bgc) ui.monBar(x, y, w, r, bc, bgc) end
local function drawButton(x, y, t, fg, bg, pl, pr) return ui.drawButton(x, y, t, fg, bg, pl, pr) end
-------------------------------------------------
-- Send command to master
@@ -403,7 +293,7 @@ local function drawDashboard()
local w, h = mon.getSize()
pendingZones = {}
draw = window.create(mon, 1, 1, w, h, false)
setDrawTarget(window.create(mon, 1, 1, w, h, false))
draw.setBackgroundColor(colors.black)
draw.clear()
@@ -709,7 +599,7 @@ local function drawSmelterDashboard()
local w, h = smelterMon.getSize()
smelterPendingZones = {}
draw = window.create(smelterMon, 1, 1, w, h, false)
setDrawTarget(window.create(smelterMon, 1, 1, w, h, false))
draw.setBackgroundColor(colors.black)
draw.clear()