Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to check if a peripheral is connected and return it, or raise an error with a custom error message.
- local function checkPeripheral(name, errorMessage)
- local peripheral = peripheral.find(name)
- assert(peripheral, errorMessage)
- return peripheral
- end
- -- Custom colors for button states
- colorButtonOn = colors.lime
- colorButtonOff = colors.gray
- -- Initialize the monitor peripheral
- mon = checkPeripheral("monitor", "Monitor not found.")
- button = {} -- Table to store button data
- -- Function to clear the button table and clear the monitor screen.
- function clearTable()
- button = {}
- mon.clear()
- end
- -- Function to add a button to the button table with its associated function and coordinates.
- function setTable(name, func, xmin, xmax, ymin, ymax)
- button[name] = {}
- button[name]["func"] = func
- button[name]["active"] = false
- button[name]["xmin"] = xmin
- button[name]["ymin"] = ymin
- button[name]["xmax"] = xmax
- button[name]["ymax"] = ymax
- end
- -- Function to fill a button area with text and color.
- function fill(text, color, bData)
- mon.setBackgroundColor(color)
- local yspot = math.floor((bData["ymin"] + bData["ymax"]) / 2)
- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) / 2) + 1
- for j = bData["ymin"], bData["ymax"] do
- mon.setCursorPos(bData["xmin"], j)
- if j == yspot then
- for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
- if k == xspot then
- mon.write(text)
- else
- mon.write(" ")
- end
- end
- else
- for i = bData["xmin"], bData["xmax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- -- Function to update the screen with button states.
- function screen()
- local currColor
- for name, data in pairs(button) do
- local on = data["active"]
- if on == true then
- currColor = colorButtonOn
- else
- currColor = colorButtonOff
- end
- fill(name, currColor, data)
- end
- end
- -- Function to toggle a button's state and update the screen.
- function toggleButton(name)
- for buttonName, data in pairs(button) do
- if buttonName ~= name and data["active"] then
- data["active"] = false
- end
- end
- button[name]["active"] = true
- screen()
- end
- -- Function to temporarily toggle a button's state (for visual feedback) and update the screen.
- function flash(name)
- toggleButton(name)
- screen()
- sleep(0.25)
- toggleButton(name)
- screen()
- end
- -- Function to check if a click falls within any button's coordinates and execute its associated function.
- function checkxy(x, y)
- for name, data in pairs(button) do
- if y >= data["ymin"] and y <= data["ymax"] then
- if x >= data["xmin"] and x <= data["xmax"] then
- if data["func"] then
- data["func"]()
- end
- return true
- end
- end
- end
- return false
- end
- -- Function to draw a colored box on the monitor.
- function drawBox(xStart, yStart, xEnd, yEnd, color)
- local w, h = mon.getSize()
- xStart = math.max(1, xStart)
- xEnd = math.min(w, xEnd)
- yStart = math.max(1, yStart)
- yEnd = math.min(w, yEnd)
- for x = xStart, xEnd do
- mon.setBackgroundColor(color)
- mon.setCursorPos(x, yStart)
- mon.write(" ")
- mon.setCursorPos(x, yEnd)
- mon.write(" ")
- end
- for y = yStart + 1, yEnd - 1 do
- mon.setBackgroundColor(color)
- mon.setCursorPos(xStart, y)
- mon.write(" ")
- mon.setCursorPos(xEnd, y)
- mon.write(" ")
- end
- end
- -- Function to draw button boxes with a specified color.
- function drawButtonBoxes(color)
- drawBox(2, 2, 78, 33, color)
- end
- -- Function to display centered text on the monitor.
- function heading(text, y, bgColor)
- local w, h = mon.getSize()
- mon.setBackgroundColor(colors.black)
- mon.setCursorPos((w - string.len(text)) / 2 + 1, y)
- mon.write(text)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement