Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Advanced Computer + Monitor + Bundled Cable Control Panel
- -- Configuration
- local monitor = peripheral.wrap("top") -- replace "right" if your monitor is elsewhere
- local redstoneSide = "left" -- bundled cable on this side
- monitor.setTextScale(1)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.clear()
- -- Redirect paintutils and write functions to the monitor
- local oldTerm = term.current()
- term.redirect(monitor)
- -- Device control buttons
- local devices = {
- {label = "FAN", color = colors.blue, active = true },
- {label = "LIGHT", color = colors.white, active = false},
- {label = "MIXER", color = colors.red, active = true },
- {label = "MONSTERS", color = colors.green, active = true }
- }
- -- Position helpers
- local function getDeviceButtonArea(index)
- local x1 = 3
- local y1 = 2 + (index - 1) * 4
- local x2 = x1 + 20
- local y2 = y1 + 2
- return x1, y1, x2, y2
- end
- -- Update bundled cable signal
- local function updateBundledOutput()
- local signal = 0
- for _, dev in ipairs(devices) do
- if dev.active then
- signal = bit.bor(signal, dev.color)
- end
- end
- redstone.setBundledOutput(redstoneSide, signal)
- end
- -- Draw device toggle button
- local function drawDeviceButton(index)
- local dev = devices[index]
- local x1, y1, x2, y2 = getDeviceButtonArea(index)
- local bgColor = dev.active and dev.color or colors.gray
- paintutils.drawFilledBox(x1, y1, x2, y2, bgColor)
- local label = dev.label .. " [" .. (dev.active and "ON" or "OFF") .. "]"
- local labelX = x1 + math.floor((x2 - x1 - #label) / 2)
- term.setCursorPos(labelX, y1 + 1)
- term.setTextColor(colors.black)
- term.write(label)
- end
- -- Redraw everything
- local function drawAllButtons()
- term.setBackgroundColor(colors.black)
- term.clear()
- for i = 1, #devices do
- drawDeviceButton(i)
- end
- end
- -- Detect click on any button
- local function getClickedButton(x, y)
- for i = 1, #devices do
- local x1, y1, x2, y2 = getDeviceButtonArea(i)
- if x >= x1 and x <= x2 and y >= y1 and y <= y2 then
- return i
- end
- end
- return -1
- end
- -- Initialize UI
- drawAllButtons()
- updateBundledOutput()
- -- Event loop
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- local index = getClickedButton(x, y)
- if index >= 0 then
- devices[index].active = not devices[index].active
- drawDeviceButton(index)
- updateBundledOutput()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment