Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --wrap the monitor as a peripheral
- mon = peripheral.wrap("left")
- mainBackground = colors.black
- mainText = colors.white
- --table that stores the buttons
- buttons = {}
- --define a new button
- function newButton(lbl,txt,x,y,text,bg,border)--newButton(string,string,integer,integer,colors api,colors api,boolean)
- if type(lbl) ~= "string" then
- error("function newButton expects a string to label the button as its first argument")
- elseif type(txt) ~= "string" then
- error("function newButton expects a string to tell it what text to display as its second argument")
- elseif type(x) ~= "number" then
- error("function newButton expects an integer to determine the x coordinate of the button as its third argument")
- elseif type(y) ~= "number" then
- error("function newButton expects an integer to determine the y coordinate of the button as its forth argument")
- elseif type(text) ~= "number" then
- error("function newButton 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 newButton 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 newButton 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")
- end
- buttons[lbl] = {txt,x,y,false,lbl,text,bg,border}
- end
- --internal function to draw a button
- local function draw(button)
- buttons[button][4] = true
- mon.setBackgroundColor(buttons[button][7])
- mon.setTextColor(buttons[button][6])
- mon.setCursorPos(buttons[button][2],buttons[button][3] + 1)
- mon.write(" "..buttons[button][1].." ")
- if buttons[button][8] then
- mon.setCursorPos(buttons[button][2],buttons[button][3])
- for i=1,string.len(buttons[button][1]) + 2 do
- mon.write(" ")
- end
- mon.setCursorPos(buttons[button][2],buttons[button][3] + 2)
- for i=1,string.len(buttons[button][1]) + 2 do
- mon.write(" ")
- end
- end
- end
- --internal function to erase a button
- local function erase(button)
- buttons[button][4] = false
- mon.setBackgroundColor(mainBackground)
- mon.setTextColor(mainText)
- mon.setCursorPos(buttons[button][2],buttons[button][3] + 1)
- for i=1,string.len(buttons[button][1]) + 2 do
- mon.write(" ")
- end
- if buttons[button][8] then
- mon.setCursorPos(buttons[button][2],buttons[button][3] + 2)
- for i=1,string.len(buttons[button][1]) + 2 do
- mon.write(" ")
- end
- mon.setCursorPos(buttons[button][2],buttons[button][3])
- for i=1,string.len(buttons[button][1]) + 2 do
- mon.write(" ")
- end
- end
- end
- --draw either a button or all buttons
- function drawButton(button)
- if type(button) ~= "string" then
- error("function drawButton expects the string label of the button you would like to draw or the string all to draw all buttons")
- end
- if button == "all" then
- for b in pairs(buttons) do
- draw(b)
- end
- else
- if buttons[button] ~= nil then
- draw(button)
- else
- error("button: "..button.." does not exist")
- end
- end
- end
- --erase either a button or all buttons
- function eraseButton(button)
- if type(button) ~= "string" then
- error("function eraseButton expects the string label of the button you would like to erase or the string all to erase all buttons")
- end
- if button == "all" then
- for b in pairs(buttons) do
- erase(b)
- end
- else
- if buttons[button] ~= nil then
- erase(button)
- else
- error("button: "..button.." does not exist")
- end
- end
- end
- --clear the monitor and erase all buttons
- function monDefault()
- mon.setBackgroundColor(mainBackground)
- mon.setTextColor(mainText)
- eraseButton("all")
- mon.clear()
- mon.setCursorPos(1,1)
- end
- function getClick(timeout)
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement