Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mon = peripheral.wrap("left")
- mainBackground = colors.black
- mainText = colors.white
- buttons = {}
- local xs,ys = mon.getSize()
- function define(lbl,txt,x,y,text,bg,border)
- if type(lbl) == "table" then
- error("a button cannot be labeled with a table")
- elseif type(txt) ~= "string" then
- error("function expects a string to tell it what text to display as its second argument")
- elseif type(x) ~= "number" then
- error("function define expects an integer to determine the x coordinate of the button as its third argument")
- elseif type(y) ~= "number" then
- error("function define expects an integer to determine the y coordinate of the button as its forth argument")
- elseif type(text) ~= "number" then
- error("function define expects an integer or colors/colours API value to determine the color of the text as its fifth argument")
- elseif type(bg) ~= "number" then
- error("function define expects an integer or colors/colours API value to determine the background color of the button as its sixth argument")
- elseif type(border) ~= "boolean" then
- error("function define expects a boolean value to determine wether the button has a border as its seventh argument")
- elseif lbl == "all" then
- error("all cannot be used as the label for a button because it is used in other functions")
- elseif lbl == "timed out" then
- error("timed out cannot be used as the label for a button because it is used in other functions")
- elseif x < 1 or y < 1 then
- error("the x and y coordinates must be positive")
- elseif x > xs + 1 or y > ys + 1 then
- error("the button must be on the screen")
- end
- buttons[lbl] = {txt,math.floor(x),math.floor(y),false,lbl,text,bg,border}
- end
- function bg(x,y,xSize,ySize,color)
- mon.setBackgroundColor(color)
- for i=y,y + ySize - 1 do
- mon.setCursorPos(x,i)
- for j=1,xSize do
- mon.write(" ")
- end
- end
- end
- function drawButton(button)
- buttons[button][4] = true
- mon.setBackgroundColor(buttons[button][7])
- mon.setTextColor(buttons[button][6])
- if buttons[button][8] then
- bg(buttons[button][2],buttons[button][3],string.len(buttons[button][1]) + 2,3,buttons[button][7])
- mon.setCursorPos(buttons[button][2] + 1,buttons[button][3] + 1)
- mon.write(buttons[button][1])
- else
- mon.setCursorPos(buttons[button][2],buttons[button][3])
- mon.write(buttons[button][1])
- end
- end
- function eraseButton(button)
- buttons[button][4] = false
- mon.setBackgroundColor(mainBackground)
- mon.setTextColor(mainText)
- if buttons[button][8] then
- for j=0,2 do
- mon.setCursorPos(buttons[button][2],buttons[button][3] + j)
- for i=1,string.len(buttons[button][1]) + 2 do
- mon.write(" ")
- end
- end
- else
- for i=1,string.len(buttons[button][1]) do
- mon.write(" ")
- end
- end
- end
- function draw(button)
- if button == "all" then
- for b in pairs(buttons) do
- drawButton(b)
- end
- elseif type(button) == "table" then
- for k,b in ipairs(button) do
- drawButton(b)
- end
- else
- drawButton(button)
- end
- end
- function erase(button)
- if button == "all" then
- for b in pairs(buttons) do
- eraseButton(b)
- end
- elseif type(button) == "table" then
- for k,b in ipairs(button) do
- eraseButton(b)
- end
- else
- eraseButton(button)
- end
- end
- function default()
- mon.setBackgroundColor(mainBackground)
- mon.setTextColor(mainText)
- erase("all")
- mon.clear()
- mon.setCursorPos(1,1)
- end
- function click(timeout)
- local t = type(timeout)
- if t ~= "number" and t ~= "nil" then
- error("function getClick expects a number to determine the time before timing out or a blank/nil to represent no time limit")
- end
- if t == "number" then
- timeo = os.startTimer(timeout)
- end
- while true do
- local event,id,xPos,yPos = os.pullEvent()
- if event == "timer" and id == timeo then
- return "timed out"
- elseif event == "monitor_touch" then
- for button in pairs(buttons) do
- if buttons[button][8] and xPos >= buttons[button][2] and xPos <= buttons[button][2] + string.len(buttons[button][1]) + 1 and yPos >= buttons[button][3] and yPos <= buttons[button][3] + 2 and buttons[button][4] then
- return button
- elseif (not buttons[button][8]) and xPos >= buttons[button][2] and xPos <= buttons[button][2] + string.len(buttons[button][1]) - 1 and yPos == buttons[button][3] and buttons[button][4] then
- return button
- end
- end
- end
- end
- end
- function deactivate(button)
- if button == "all" then
- for b in pairs(buttons) do
- buttons[b][4] = false
- end
- elseif type(button) == "table" then
- for k,b in ipairs(button) do
- buttons[b][4] = false
- end
- else
- buttons[button][4] = false
- end
- end
- function delete(button)
- if button == "all" then
- for b in pairs(buttons) do
- erase(b)
- end
- buttons = {}
- elseif type(button) == "table" then
- for k,b in ipairs(button) do
- erase(b)
- table.remove(buttons,b)
- end
- else
- erase(button)
- table.remove(buttons,button)
- end
- end
- function relabel(old,new)
- button[new] = button[old]
- delete(old)
- end
- function text(button,txt)
- erase(button)
- buttons[button][1] = txt
- end
- function move(button,x,y)
- erase(button)
- buttons[button][2] = math.floor(x)
- buttons[button][3] = math.floor(y)
- draw(button)
- end
- function activate(button)
- if button == "all" then
- for b in pairs(buttons) do
- buttons[b][4] = true
- end
- elseif type(button) == "table" then
- for k,b in ipairs(button) do
- buttons[b][4] = true
- end
- else
- buttons[button][4] = true
- end
- end
- function color(button,txt,BG)
- buttons[button][6] = txt
- buttons[button][7] = BG
- draw(button)
- end
- print("Button API (by minecraftwarlock) loaded")
Advertisement
Add Comment
Please, Sign In to add comment