This is the API:
--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)
buttons[lbl] = {txt,x,y,false,lbl,text,bg,border}
end
--draw a button
function drawButton(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
--erase a button
function eraseButton(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
--clear the monitor and erase all buttons
function monDefault()
mon.setBackgroundColor(mainBackground)
mon.setTextColor(mainText)
for button in pairs(buttons) do
eraseButton(button)
end
mon.clear()
mon.setCursorPos(1,1)
end
--the problematic function
function getClick()
while true do
event,side,xPos,yPos = os.pullEvent("monitor_touch")
for button in pairs(buttons) do
if buttons[button][8] then
if 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 buttons[button][5]
elseif 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 buttons[button][5]
end
end
end
end
end
This is the sample code that calls the API:
button.newButton("test","test",5,5,colors.red,colors.white,true)
button.drawButton("test")
print(button.getClick())