Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Monitor connection and size
- local monitor, monitorWidth, monitorHeight
- --Get connected monitor and its width + height
- function getMonitor ()
- for _, side in ipairs(rs.getSides()) do
- if peripheral.getType(side) == "monitor" then
- print("Connected to monitor on ", side, " side")
- monitor = peripheral.wrap(side)
- monitorWidth, monitorHeight = monitor.getSize()
- monitor.clear()
- monitor.setCursorPos(1, 1)
- return
- end
- end
- end
- getMonitor()
- --Button dimensions
- local buttonWidth = 12
- local buttonHeight = 3
- --Colors
- local guiColors = {
- ['background'] = colors.white,
- ['headerText'] = colors.black,
- ['buttonText'] = colors.white,
- ['buttonShadow'] = colors.gray,
- ['inactiveButton'] = colors.red,
- ['activeButton'] = colors.lime
- }
- --Button actions--
- function drawHeader (text)
- --Set background color
- monitor.setBackgroundColor(guiColors['background'])
- --Set text color
- monitor.setTextColor(guiColors['headerText'])
- --Reset cursor
- monitor.setCursorPos(1, 1)
- --Delete current header
- for i = 1, monitorWidth do
- monitor.write(" ")
- end
- --Set cursor
- monitor.setCursorPos(17, 1)
- --Write new header
- monitor.write(text)
- end
- function toggleSignal (side, color)
- --Current bundled cable signal
- outputColors = rs.getBundledOutput(side)
- --Test if color is currently on
- if colors.test(outputColors, color) then
- --If on, turn off
- outputColors = colors.subtract(outputColors, color)
- else
- --If off, turn on
- outputColors = colors.combine(outputColors, color)
- end
- --Set new bundled cable outpout signal
- rs.setBundledOutput(side, outputColors)
- end
- --Define buttons
- local buttons = {
- [1] = {
- ['label'] = "Hvidt lys",
- ['action'] = function()
- toggleSignal("top", colors.white)
- end,
- ['inaction'] = function()
- toggleSignal("top", colors.white)
- end,
- ['row'] = 2,
- ['column'] = 1,
- ['active'] = false,
- ['toggleable'] = true
- },
- [2] = {
- ['label'] = "Orange lys",
- ['action'] = function()
- toggleSignal("top", colors.orange)
- end,
- ['inaction'] = function()
- toggleSignal("top", colors.orange)
- end,
- ['row'] = 2,
- ['column'] = 3,
- ['active'] = false,
- ['toggleable'] = false
- }--[[,
- [3] = {
- ['label'] = "Label 3",
- ['action'] = function()
- drawHeader("Button 3 pressed")
- end,
- ['row'] = 1,
- ['column'] = 3,
- ['active'] = false,
- ['toggleable'] = false
- },
- [4] = {
- ['label'] = "Label 4",
- ['action'] = function()
- drawHeader("Button 4 pressed")
- end,
- ['row'] = 2,
- ['column'] = 1,
- ['active'] = false,
- ['toggleable'] = false
- },
- [5] = {
- ['label'] = "Label 5",
- ['action'] = function()
- drawHeader("Button 5 pressed")
- end,
- ['row'] = 2,
- ['column'] = 2,
- ['active'] = false,
- ['toggleable'] = false
- },
- [6] = {
- ['label'] = "Label 6",
- ['action'] = function()
- drawHeader("Button 6 pressed")
- end,
- ['row'] = 2,
- ['column'] = 3,
- ['active'] = false,
- ['toggleable'] = false
- },
- [7] = {
- ['label'] = "Label 7",
- ['action'] = function()
- drawHeader("Button 7 pressed")
- end,
- ['row'] = 3,
- ['column'] = 1,
- ['active'] = false,
- ['toggleable'] = false
- },
- [8] = {
- ['label'] = "Label 8",
- ['action'] = function()
- drawHeader("Button 8 pressed")
- end,
- ['row'] = 3,
- ['column'] = 2,
- ['active'] = false,
- ['toggleable'] = false
- },
- [9] = {
- ['label'] = "Label 9",
- ['action'] = function()
- drawHeader("Button 9 pressed")
- end,
- ['row'] = 3,
- ['column'] = 3,
- ['active'] = false,
- ['toggleable'] = false
- },
- [10] = {
- ['label'] = "Label 10",
- ['action'] = function()
- drawHeader("Button 10 pressed")
- end,
- ['row'] = 4,
- ['column'] = 1,
- ['active'] = false,
- ['toggleable'] = false
- },
- [11] = {
- ['label'] = "Label 11",
- ['action'] = function()
- drawHeader("Button 11 pressed")
- end,
- ['row'] = 4,
- ['column'] = 2,
- ['active'] = false,
- ['toggleable'] = false
- },
- [12] = {
- ['label'] = "Label 12",
- ['action'] = function()
- drawHeader("Button 12 pressed")
- end,
- ['row'] = 4,
- ['column'] = 3,
- ['active'] = false,
- ['toggleable'] = false
- },
- [13] = {
- ['label'] = "Label 13",
- ['action'] = function()
- drawHeader("Button 13 pressed")
- end,
- ['row'] = 5,
- ['column'] = 1,
- ['active'] = false,
- ['toggleable'] = false
- },
- [14] = {
- ['label'] = "Label 14",
- ['action'] = function()
- drawHeader("Button 14 pressed")
- end,
- ['row'] = 5,
- ['column'] = 2,
- ['active'] = false,
- ['toggleable'] = false
- },
- [15] = {
- ['label'] = "Label 15",
- ['action'] = function()
- drawHeader("Button 15 pressed")
- end,
- ['row'] = 5,
- ['column'] = 3,
- ['active'] = false,
- ['toggleable'] = false
- }
- ]]--
- }
- --Define button coordinates
- local columns = {
- [1] = {
- ['x1'] = 3,
- ['x2'] = 15
- },
- [2] = {
- ['x1'] = 19,
- ['x2'] = 31
- },
- [3] = {
- ['x1'] = 35,
- ['x2'] = 47
- }
- }
- local rows = {
- [1] = {
- ['y1'] = 2,
- ['y2'] = 5
- },
- [2] = {
- ['y1'] = 7,
- ['y2'] = 10
- },
- [3] = {
- ['y1'] = 12,
- ['y2'] = 15
- },
- [4] = {
- ['y1'] = 17,
- ['y2'] = 20
- },
- [5] = {
- ['y1'] = 22,
- ['y2'] = 25
- }
- }
- --Draw a filled box for e.g. a button or shadow
- function drawBox (x, y, width, height, color)
- monitor.setBackgroundColor(color)
- monitor.setTextColor(color)
- for i = 0, height - 1 do
- monitor.setCursorPos(x, y + i)
- for j = 1, width do
- monitor.write(" ")
- end
- end
- end
- function drawBackground (color)
- monitor.setBackgroundColor(guiColors['background'])
- monitor.clear()
- end
- function drawLabel (x, y, label, color)
- monitor.setTextColor(color)
- monitor.setCursorPos(x, y)
- monitor.write(label)
- end
- function drawInactiveButton (x, y, width, height, label)
- --Draw button shadow
- drawBox(
- x + 1, y + 1, width, height,
- guiColors['buttonShadow']
- )
- --Draw button box
- drawBox(
- x, y, width, height,
- guiColors['inactiveButton']
- )
- --Draw button label
- drawLabel(
- (x + 1), (y + 1), label,
- guiColors['buttonText']
- )
- end
- function drawActiveButton (x, y, width, height, label)
- --Clear button area
- drawBox(
- x, y, width + 1, height + 1,
- guiColors['background']
- )
- --Draw indented button box
- drawBox(
- x + 1, y + 1, width, height,
- guiColors['activeButton']
- )
- --Draw button label
- drawLabel(
- x + 2, y + 2, label,
- guiColors['buttonText']
- )
- end
- function renderButtons ()
- for index, button in ipairs(buttons) do
- x1 = columns[button['column']]['x1']
- x2 = columns[button['column']]['x2']
- y1 = rows[button['row']]['y1']
- y2 = rows[button['row']]['y2']
- width = x2 - x1
- height = y2 - y1
- if button['active'] then
- drawActiveButton(x1, y1, width, height, button['label'])
- else
- drawInactiveButton(x1, y1, width, height, button['label'])
- end
- end
- end
- function pushButton (button)
- x1 = columns[button['column']]['x1']
- x2 = columns[button['column']]['x2']
- y1 = rows[button['row']]['y1']
- y2 = rows[button['row']]['y2']
- width = x2 - x1
- height = y2 - y1
- drawActiveButton(x1, y1, width, height, button['label'])
- --Activate button state
- button['active'] = true
- buttonAction(button)
- sleep(0.2)
- --Deactivate button state
- button['active'] = false
- buttonAction(button)
- drawInactiveButton(x1, y1, width, height, button['label'])
- end
- function buttonAction (button)
- --Check if button was just activated
- if button['active'] then
- if button['action'] then
- --Call button activation function
- button['action']()
- end
- else
- if button['inaction'] then
- --Call button deactivation function
- button['inaction']()
- end
- end
- end
- function touchButton (x, y)
- for index, button in ipairs(buttons) do
- x1 = columns[button['column']]['x1']
- x2 = columns[button['column']]['x2']
- y1 = rows[button['row']]['y1']
- y2 = rows[button['row']]['y2']
- width = x2 - x1
- height = y2 - y1
- if x >= x1 and x <= x2 and y >= y1 and y<= y2 then
- if button['toggleable'] then
- --Toggle button active state
- button['active'] = not button['active']
- --Redraw all buttons
- renderButtons()
- --Do action/inaction
- buttonAction(button)
- else
- pushButton(button)
- end
- return
- end
- end
- end
- function touchLoop()
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- touchButton(x, y)
- end
- end
- --Main program
- drawBackground()
- print("Rendering...")
- renderButtons()
- print("Complete!")
- touchLoop()
Advertisement
Add Comment
Please, Sign In to add comment