Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The `drawButton` function draws a button at the specified position with the specified label text.
- -- It returns a table that contains the position and dimensions of the button.
- local monitor = peripheral.find("monitor")
- local buttons = {}
- function drawButton(x, y, label)
- buttons[label] = {}
- buttons[label].x1 = x
- buttons[label].y1 = y
- buttons[label].width = #label + 2
- buttons[label].height = 3
- buttons[label].x2 = x + buttons[label].width - 1
- buttons[label].y2 = y + buttons[label].height - 1
- -- Draw the button frame and centered label
- monitor.setCursorPos(x, y)
- monitor.write("+" .. string.rep("-", buttons[label].width-2) .. "+")
- monitor.setCursorPos(x, y+1)
- monitor.write("|" .. centerText(label, buttons[label].width-2) .. "|")
- monitor.setCursorPos(x, y+2)
- monitor.write("+" .. string.rep("-", buttons[label].width-2) .. "+")
- end
- -- The `centerText` function centers the specified text within the specified width.
- function centerText(text, width)
- local spacing = width - #text
- local leftSpacing = math.floor(spacing / 2)
- local rightSpacing = math.ceil(spacing / 2)
- return string.rep(" ", leftSpacing) .. text .. string.rep(" ", rightSpacing)
- end
- -- The `processClick` function checks if the specified position is within any of the buttons
- -- and if so, calls the attached function.
- function processClick(x, y)
- for label, button in pairs(buttons) do
- if x >= button.x1 and x <= button.x2 and y >= button.y1 and y <= button.y2 then
- button.func()
- return
- end
- end
- end
- -- The `attachFunction` function attaches the specified function to the specified button label.
- function attach(label, func)
- buttons[label].func = func
- end
- -- The `drawLabel` function draws a label at the specified position, centered within the specified width.
- function drawLabel(x, y, width, label)
- monitor.setCursorPos(x, y)
- monitor.write(centerText(label, width))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement