Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Button API for ComputerCraft.
- -- @author direwolf20, refactored by Telinc1.
- local monitor = peripheral.find("monitor")
- monitor.setTextScale(1)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.black)
- local buttons = {}
- --- Add a button to the button table.
- -- @param name The name of the button (also used as its label).
- -- @param callback The function to call on clicking the button.
- -- @param x The X position of the button.
- -- @param y The Y position of the button.
- -- @param width The width of the button.
- -- @param height The height of the button.
- function addButton(name, callback, x, y, width, height)
- buttons[name] = {}
- buttons[name]["callback"] = callback
- buttons[name]["active"] = false
- buttons[name]["left"] = x
- buttons[name]["top"] = y
- buttons[name]["right"] = x + width
- buttons[name]["bottom"] = y + height
- end
- --- Remove a button from the button table.
- -- @param name The name of the button to remove.
- function removeButton(name)
- buttons[name] = nil
- end
- --- Remove all buttons from the table and clear the monitor.
- function clearButtons()
- buttons = {}
- monitor.clear()
- end
- --- Draw a piece of button-styled text with the specified background color.
- -- @param text The label of the button.
- -- @param color The color of the button.
- -- @param data The button's data, used for its positioning.
- function draw(text, color, data)
- monitor.setBackgroundColor(color)
- local textX = math.floor((data["right"] - data["left"] - string.len(text)) / 2) + 1
- local textY = math.floor((data["top"] + data["bottom"]) / 2)
- for j = data["top"], data["bottom"] do
- monitor.setCursorPos(data["left"], j)
- if j == textY then
- for k = 0, data["right"] - data["left"] - string.len(text) + 1 do
- if k == textX then
- monitor.write(text)
- else
- monitor.write(" ")
- end
- end
- else
- for i = data["left"], data["right"] do
- monitor.write(" ")
- end
- end
- end
- monitor.setBackgroundColor(colors.black)
- end
- --- Draw all the buttons to the monitor.
- function refresh()
- local currentColor
- for name, data in pairs(buttons) do
- if data["active"] == true then currentColor = colors.lime else currentColor = colors.red end
- draw(name, currentColor, data)
- end
- end
- --- Toggle a button's active state and refresh the screen.
- -- @param name The button to toggle.
- function toggle(name)
- button[name]["active"] = not buttons[name]["active"]
- refresh()
- end
- --- Flip a button's active state for 0.15 seconds.
- -- @param name The button to flash.
- function flash(name)
- toggle(name)
- sleep(0.15)
- toggle(name)
- end
- --- Check (x, y) for a button and execute its callback.
- -- @param x The X coordinate of the point.
- -- @param y The Y coordinate of the point.
- function checkXY(x, y)
- for name, data in pairs(buttons) do
- if y >= data["top"] and y <= data["bottom"] then
- if x >= data["left"] and x <= data["right"] then
- data["callback"]()
- return true
- end
- end
- end
- return false
- end
- --- Draw a centered heading to the monitor.
- -- @param text The heading to draw.
- function heading(text)
- width, height = monitor.getSize()
- monitor.setCursorPos((width - string.len(text)) / 2 + 1, 1)
- monitor.write(text)
- end
- --- Draw a label to the monitor.
- -- @param x The X position of the label.
- -- @param y The Y position of the label.
- -- @param text The label to draw.
- function label(x, y, text)
- monitor.setCursorPos(x, y)
- monitor.write(text)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement