Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Button API by Tobolobo (M3inNam3)
- --Side HAS to be changed manually!!!
- local monitor = peripheral.wrap("left")
- local button = {}
- local buttoncount = 0
- --privateDrawButton: A function that should
- --only be used by the API itself. It draws
- --a button.
- function privateDrawButton(colour, text, xMin, xMax, yMin, yMax)
- --Calculate difference
- local ydif = yMax - yMin
- local xdif = xMax - xMin
- --Draw button
- monitor.setBackgroundColor(colour)
- for i = yMin, yMax do
- for j = xMin, xMax do
- monitor.setCursorPos(j, i)
- monitor.write(" ")
- end
- end
- --Write text
- monitor.setCursorPos(xMin + math.floor((xdif + 1 - string.len(text)) / 2), yMin + math.floor(ydif / 2))
- monitor.write(text)
- monitor.setBackgroundColor(colors.black)
- return true
- end
- --register: Registers a single button
- --and draws it on the monitor.
- function register(text, xMin, xMax, yMin, yMax, notActiveColour, activeColour)
- --Fill button table with new row and insert data
- button[buttoncount] = {}
- --The text shown on the button
- button[buttoncount][0] = text
- --The lowest x value of the button
- button[buttoncount][1] = xMin
- --The highest x value of the button
- button[buttoncount][2] = xMax
- --The lowest y value of the button
- button[buttoncount][3] = yMin
- --The highest y value of the button
- button[buttoncount][4] = yMax
- --Is the button enabled?
- button[buttoncount][5] = true
- --Color when not active(when the button is created)
- if notActiveColour == -1 then
- button[buttoncount][6] = colors.red
- else
- button[buttoncount][6] = notActiveColour
- end
- --Color when active (when the button is pressed once)
- if activeColour == -1 then
- button[buttoncount][7] = colors.green
- else
- button[buttoncount][7] = activeColour
- end
- --Is the button active or not?
- button[buttoncount][8] = false
- --Draw the button
- privateDrawButton(button[buttoncount][6], text, xMin, xMax, yMin, yMax)
- buttoncount = buttoncount + 1
- return buttoncount - 1
- end
- --redraw: Redraws a single button.
- function redraw(id)
- --Check if button exists and is enabled
- if (button[id] == nil) or (button[id][5] == false) then
- return false
- end
- --Get data and draw
- local drawColour = 0
- if button[id][8] == false then
- drawColour = button[id][6]
- else
- drawColour = button[id][7]
- end
- privateDrawButton(drawColour, button[id][0], button[id][1], button[id][2], button[id][3], button[id][4])
- return true
- end
- --getCollisions: It tests each enabled button
- --if the x and y coordinates collide with it!
- --It returns the id of the button. So instead
- --of overriding buttons you should disable them!
- --Flaw here: It can only be colliding with one
- --button (the one which registered first). But
- --who would let 2 buttons overlap each other?
- function getCollisions(x, y)
- --Return -1 if no buttons registered yet
- if buttoncount == 0 then
- return -1
- end
- for i = 0, buttoncount - 1 do
- if button[i][5] == true then
- --Check if it collides with a button
- if (button[i][1] <= x) and (button[i][2] >= x) and (button[i][3] <= y) and (button[i][4] >= y) then
- return i
- end
- end
- end
- return -1
- end
- --flash: Flashes the button.
- function flash(id, seconds)
- if button[id] == nil then
- return false
- end
- switch(id)
- --I hate using os.sleep() in this....
- os.sleep(seconds)
- switch(id)
- return true
- end
- function exists(id)
- if button[id] == nil then
- return false
- else
- return true
- end
- end
- --Properties get/set
- --setText: Sets the text of the button.
- function setText(id, text)
- if button[id] == nil then
- return false
- end
- button[id][0] = text
- redraw(id)
- return true
- end
- --getText: Gets the text of the button.
- function getText(id)
- if button[id] == nil then
- return false
- end
- return button[id][0]
- end
- --setPosition: Sets the new position of the button.
- --The old button will be erased!
- function setPosition(id, xMin, xMax, yMin, yMax)
- if button[id] == nil then
- return false
- end
- disable(id)
- if not(xMin == -1) then
- button[id][1] = xMin
- end
- if not(xMin == -1) then
- button[id][2] = xMax
- end
- if not(yMin == -1) then
- button[id][3] = yMin
- end
- if not(yMin == -1) then
- button[id][4] = yMax
- end
- enable(id)
- return true
- end
- --getPosition: Gets the position of the button.
- --Returns an array with left, right, top, bottom
- --coordinates starting at 0
- function getPosition(id)
- if button[id] == nil then
- return false
- end
- local coords = {}
- for i = 0, 3 do
- coords[i] = button[id][i + 1]
- end
- return coords
- end
- --enable: Enables a single button.
- function enable(id)
- if button[id] == nil then
- return false
- end
- button[id][5] = true
- redraw(id)
- return true
- end
- --disable: Disables a single button.
- function disable(id)
- if button[id] == nil then
- return false
- end
- local colourBuffer = button[id][6]
- local textBuffer = button[id][0]
- local activeOrNot = button[id][8]
- button[id][8] = false
- button[id][6] = colors.black
- button[id][0] = ""
- redraw(id)
- button[id][6] = colourBuffer
- button[id][0] = textBuffer
- button[id][8] = activeOrNot
- button[id][5] = false
- return true
- end
- --getEnabled: Gets the enabled-state of
- --a button.
- function getEnabled(id)
- if button[id] == nil then
- return -1
- end
- return button[id][5]
- end
- --setNotActiveColour: Changes the color
- --when the button is not active.
- function setNotActiveColour(id, colour)
- if button[id] == nil then
- return false
- end
- button[id][6] = colour
- if button[id][8] == false then
- redraw(id)
- end
- return true
- end
- --getNotActiveColour: Returns the
- --color of the button when it's not active.
- function getNotActiveColour(id)
- if button[id] == nil then
- return -1
- end
- return button[id][6]
- end
- --setActiveColour: Changes the color
- --when the button is active
- function setActiveColour(id, colour)
- if button[id] == nil then
- return false
- end
- button[id][7] = colour
- if button[id][8] == true then
- redraw(id)
- end
- return true
- end
- --getActiveColour: Returns the
- --color of the button when it's active.
- function getActiveColour(id)
- if button[id] == nil then
- return -1
- end
- return button[id][7]
- end
- --changeCurrentColour: Changes the color
- --of the button!
- function changeCurrentColour(id, colour)
- if button[id] == nil then
- return false
- end
- if button[id][8] == false then
- button[id][6] = colour
- else
- button[id][7] = colour
- end
- redraw(id)
- return true
- end
- --getCurrentColour: Returns the current
- --color of the button.
- function getCurrentColour(id)
- if button[id] == nil then
- return -1
- end
- if button[id][8] == false then
- return button[id][6]
- else
- return button[id][7]
- end
- end
- --switch: Switches the state of the button.
- function switch(id)
- if button[id] == nil then
- return false
- end
- --Switch colors of the button
- button[id][8] = not button[id][8]
- redraw(id)
- return true
- end
- --setActive: Sets the button state to active.
- function setActive(id)
- if button[id] == nil then
- return false
- end
- button[id][8] = true
- redraw(id)
- return true
- end
- --setNotActive: Sets the button state to not active.
- function setNotActive(id)
- if button[id] == nil then
- return false
- end
- button[id][8] = false
- redraw(id)
- return true
- end
- --isActive: Returns if the button is active
- function isActive(id)
- if button[id] == nil then
- return -1
- end
- return button[id][8]
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement