Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2 Monitor Deluxe Edition
- -- Light Theme + Crafting + Storage Recap + Sorted Items
- -- By ChatGPT
- -- Find peripherals
- local monitor = peripheral.find("monitor")
- local me = peripheral.find("meBridge")
- if not monitor then error("No monitor connected!") end
- if not me then error("No ME Bridge connected!") end
- -- SETTINGS
- monitor.setTextScale(0.5)
- local w, h = monitor.getSize()
- local itemsPerPage = h - 8
- local pageCrafting = 1
- local pageStorage = 1
- local activeTab = "crafting"
- local blinkState = true
- -- COLORS
- local bgColor = colors.white
- local textColor = colors.black
- local tabActive = colors.lightBlue
- local tabInactive = colors.gray
- local footerColor = colors.lightGray
- local busyColor = colors.blue
- local overloadedColor = colors.red
- local idleColor = colors.green
- -- UTILITIES
- local function centerText(text, y, textCol, bgCol)
- monitor.setCursorPos(math.floor((w - #text) / 2) + 1, y)
- monitor.setTextColor(textCol or textColor)
- monitor.setBackgroundColor(bgCol or bgColor)
- monitor.write(text)
- end
- local function drawBlock(x1, y1, x2, y2, bg)
- monitor.setBackgroundColor(bg)
- for y = y1, y2 do
- monitor.setCursorPos(x1, y)
- monitor.write(string.rep(" ", x2 - x1 + 1))
- end
- end
- local function drawButton(x1, x2, label, enabled)
- monitor.setBackgroundColor(enabled and colors.lightBlue or colors.gray)
- local mid = math.floor((x1 + x2) / 2) - math.floor(#label / 2)
- monitor.setCursorPos(mid, h)
- monitor.setTextColor(colors.black)
- monitor.write(label)
- end
- local function drawTabs()
- local half = math.floor(w / 2)
- drawBlock(1, 1, w, 2, footerColor)
- -- Left tab
- drawBlock(1, 1, half, 2, activeTab == "crafting" and tabActive or tabInactive)
- centerText("Crafting", 1, colors.white, activeTab == "crafting" and tabActive or tabInactive)
- -- Right tab
- drawBlock(half + 1, 1, w, 2, activeTab == "storage" and tabActive or tabInactive)
- monitor.setCursorPos(half + math.floor((half - 7) / 2), 1)
- monitor.setTextColor(colors.white)
- monitor.write("Storage")
- end
- local function drawProgressBar(x, y, width, percentage, fgColor, bgColor)
- local fillWidth = math.floor(width * percentage + 0.5)
- monitor.setCursorPos(x, y)
- monitor.setBackgroundColor(fgColor)
- monitor.write(string.rep(" ", fillWidth))
- monitor.setBackgroundColor(bgColor)
- monitor.write(string.rep(" ", width - fillWidth))
- end
- local function drawCraftingTab()
- monitor.setBackgroundColor(bgColor)
- monitor.clear()
- drawTabs()
- centerText("Ongoing Crafting Jobs", 4, textColor, bgColor)
- local cpus = me.getCraftingCPUs() or {}
- local totalPages = math.max(1, math.ceil(#cpus / itemsPerPage))
- pageCrafting = math.min(pageCrafting, totalPages)
- local startIdx = (pageCrafting - 1) * itemsPerPage + 1
- local endIdx = math.min(#cpus, startIdx + itemsPerPage - 1)
- local y = 6
- for idx = startIdx, endIdx do
- local cpu = cpus[idx]
- if cpu then
- local name = cpu.name or ("CPU #" .. idx)
- if name == "" then name = "Unnamed CPU" end
- local busy = cpu.isBusy
- local cpuColor = idleColor
- if busy then
- cpuColor = blinkState and busyColor or overloadedColor
- end
- drawBlock(2, y, w - 1, y + 2, footerColor)
- monitor.setCursorPos(4, y + 1)
- monitor.setTextColor(textColor)
- monitor.write(name:sub(1, w - 15))
- -- Simulate CPU usage
- local usage = busy and 0.6 or 0.1 -- fake usage %
- monitor.setCursorPos(w - 15, y + 1)
- drawProgressBar(w - 15, y + 1, 13, usage, cpuColor, colors.gray)
- y = y + 4
- end
- end
- drawBlock(1, h, w, h, footerColor)
- drawButton(1, math.floor(w / 2) - 1, "< Prev", pageCrafting > 1)
- drawButton(math.floor(w / 2) + 1, w, "Next >", pageCrafting < totalPages)
- centerText("Page " .. pageCrafting .. "/" .. totalPages, h - 1, textColor, footerColor)
- end
- local function sortByAmountDesc(a, b)
- return (a.amount or 0) > (b.amount or 0)
- end
- local function drawStorageTab()
- monitor.setBackgroundColor(bgColor)
- monitor.clear()
- drawTabs()
- centerText("Storage Overview", 4, textColor, bgColor)
- local storage = me.listItems() or {}
- table.sort(storage, sortByAmountDesc)
- local totalPages = math.max(1, math.ceil(#storage / itemsPerPage))
- pageStorage = math.min(pageStorage, totalPages)
- local startIdx = (pageStorage - 1) * itemsPerPage + 1
- local endIdx = math.min(#storage, startIdx + itemsPerPage - 1)
- local y = 6
- for idx = startIdx, endIdx do
- local item = storage[idx]
- local name = item.displayName or "Unknown"
- local amount = item.amount or 0
- if idx - startIdx < 5 then
- -- Top 5 highlighted
- drawBlock(2, y, w - 1, y, colors.lightBlue)
- monitor.setTextColor(colors.white)
- else
- monitor.setBackgroundColor(bgColor)
- monitor.setTextColor(textColor)
- end
- monitor.setCursorPos(3, y)
- monitor.write(name:sub(1, 20))
- monitor.setCursorPos(w - 10, y)
- monitor.write("x" .. amount)
- y = y + 1
- end
- drawBlock(1, h, w, h, footerColor)
- drawButton(1, math.floor(w / 2) - 1, "< Prev", pageStorage > 1)
- drawButton(math.floor(w / 2) + 1, w, "Next >", pageStorage < totalPages)
- centerText("Page " .. pageStorage .. "/" .. totalPages, h - 1, textColor, footerColor)
- end
- local function handleTouch(x, y)
- x = tonumber(x)
- if not x then return end
- local half = math.floor(w / 2)
- if y <= 2 then
- if x <= half then
- activeTab = "crafting"
- pageCrafting = 1
- else
- activeTab = "storage"
- pageStorage = 1
- end
- elseif y == h then
- if activeTab == "crafting" then
- local cpus = me.getCraftingCPUs() or {}
- if x < w / 2 and pageCrafting > 1 then
- pageCrafting = pageCrafting - 1
- elseif x >= w / 2 and pageCrafting < math.ceil(#cpus / itemsPerPage) then
- pageCrafting = pageCrafting + 1
- end
- elseif activeTab == "storage" then
- local storage = me.listItems() or {}
- if x < w / 2 and pageStorage > 1 then
- pageStorage = pageStorage - 1
- elseif x >= w / 2 and pageStorage < math.ceil(#storage / itemsPerPage) then
- pageStorage = pageStorage + 1
- end
- end
- end
- end
- -- MAIN LOOP
- parallel.waitForAny(
- function()
- while true do
- blinkState = not blinkState
- if activeTab == "crafting" then
- drawCraftingTab()
- elseif activeTab == "storage" then
- drawStorageTab()
- end
- sleep(0.5) -- faster refresh
- end
- end,
- function()
- while true do
- local _, side, x, y = os.pullEvent("monitor_touch")
- handleTouch(x, y)
- -- Immediately refresh
- if activeTab == "crafting" then
- drawCraftingTab()
- elseif activeTab == "storage" then
- drawStorageTab()
- end
- end
- end
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement