Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2 Monitor Deluxe Edition with Sidebar Tabs
- -- 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 sidebarWidth = 15
- local itemsPerPage = h - 8
- local pageCrafting = 1
- local pageStorage = 1
- local pageAutoCrafting = 1
- local pageDisks = 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, x, y, textCol, bgCol)
- monitor.setCursorPos(x, 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 drawSidebar()
- drawBlock(1, 1, sidebarWidth, h, footerColor)
- local tabs = {
- {name = "crafting", label = "Crafting", y = 2},
- {name = "storage", label = "Storage", y = 4},
- {name = "autocrafting", label = "AutoCraft", y = 6},
- {name = "disks", label = "Disks", y = 8},
- {name = "energy", label = "Energy", y = 10}
- }
- for _, tab in ipairs(tabs) do
- local bg = (activeTab == tab.name) and tabActive or tabInactive
- drawBlock(1, tab.y, sidebarWidth, tab.y + 1, bg)
- centerText(tab.label, 2, tab.y, colors.white, bg)
- 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 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
- -- ========== TAB CONTENTS ========== --
- local function drawCraftingTab()
- monitor.setBackgroundColor(bgColor)
- monitor.clear()
- drawSidebar()
- centerText("Ongoing Crafting Jobs", sidebarWidth + 2, 2, 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 = 4
- 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(sidebarWidth + 1, y, w - 1, y + 2, footerColor)
- monitor.setCursorPos(sidebarWidth + 2, y + 1)
- monitor.setTextColor(textColor)
- monitor.write(name:sub(1, w - sidebarWidth - 10))
- -- CPU usage
- monitor.setCursorPos(w - 13, y + 1)
- drawProgressBar(w - 13, y + 1, 10, busy and 0.6 or 0.1, cpuColor, colors.gray)
- y = y + 4
- end
- end
- end
- local function drawStorageTab()
- monitor.setBackgroundColor(bgColor)
- monitor.clear()
- drawSidebar()
- centerText("Storage Overview", sidebarWidth + 2, 2, textColor, bgColor)
- local storage = me.listItems() or {}
- table.sort(storage, function(a, b) return (a.amount or 0) > (b.amount or 0) end)
- 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 = 4
- for idx = startIdx, endIdx do
- local item = storage[idx]
- if item then
- local name = item.displayName or "Unknown"
- local amount = item.amount or 0
- monitor.setCursorPos(sidebarWidth + 2, y)
- monitor.setTextColor(textColor)
- monitor.write(name:sub(1, w - sidebarWidth - 12))
- monitor.setCursorPos(w - 10, y)
- monitor.write("x" .. amount)
- y = y + 1
- end
- end
- end
- local function drawAutoCraftingTab()
- monitor.setBackgroundColor(bgColor)
- monitor.clear()
- drawSidebar()
- centerText("AutoCrafting Plan", sidebarWidth + 2, 2, textColor, bgColor)
- local tasks = me.getCraftingTasks() or {}
- local y = 4
- for idx, task in ipairs(tasks) do
- if y > h then break end
- local name = task.name or "Unnamed"
- monitor.setCursorPos(sidebarWidth + 2, y)
- monitor.setTextColor(textColor)
- monitor.write(name:sub(1, w - sidebarWidth - 2))
- y = y + 1
- end
- end
- local function drawDisksTab()
- monitor.setBackgroundColor(bgColor)
- monitor.clear()
- drawSidebar()
- centerText("Disks Overview", sidebarWidth + 2, 2, textColor, bgColor)
- local disks = me.listStorages() or {}
- local y = 4
- for idx, disk in ipairs(disks) do
- if y > h then break end
- local used = disk.getUsedSpace() or 0
- local total = disk.getMaxSize() or 1
- local percent = used / total
- monitor.setCursorPos(sidebarWidth + 2, y)
- monitor.setTextColor(textColor)
- monitor.write(string.format("Disk %d: %d%%", idx, math.floor(percent * 100)))
- drawProgressBar(w - 15, y, 10, percent, colors.green, colors.gray)
- y = y + 1
- end
- end
- -- ========== DRAW CURRENT TAB ========== --
- local function drawCurrentTab()
- if activeTab == "crafting" then
- drawCraftingTab()
- elseif activeTab == "storage" then
- drawStorageTab()
- elseif activeTab == "autocrafting" then
- drawAutoCraftingTab()
- elseif activeTab == "disks" then
- drawDisksTab()
- end
- end
- -- ========== MAIN LOOP ========== --
- local function handleTouch(x, y)
- if x <= sidebarWidth then
- if y >= 2 and y <= 3 then activeTab = "crafting"
- elseif y >= 4 and y <= 5 then activeTab = "storage"
- elseif y >= 6 and y <= 7 then activeTab = "autocrafting"
- elseif y >= 8 and y <= 9 then activeTab = "disks"
- elseif y >= 10 and y <= 11 then activeTab = "energy"
- end
- end
- end
- while true do
- drawCurrentTab()
- blinkState = not blinkState
- local event, side, x, y = os.pullEvent()
- if event == "monitor_touch" then
- handleTouch(x, y)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement