Advertisement
Freack100

Untitled

Jul 25th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. --ButtonAPI by Freack100
  2. local x,y = term.getSize()
  3. local buttons = {}
  4.  
  5. function draw()
  6.     for buttonCount = 1, #buttons do
  7.         if buttons[buttonCount]["visible"] then
  8.             term.setCursorPos(buttons[buttonCount]["sX"],buttons[buttonCount]["sY"])
  9.             term.setBackgroundColor(buttons[buttonCount]["bgcol"])
  10.             term.setTextColor(buttons[buttonCount]["txtcol"])
  11.             for i = buttons[buttonCount]["sY"], buttons[buttonCount]["eY"] do
  12.                 term.setCursorPos(buttons[buttonCount]["sX"], i)
  13.                 for u = buttons[buttonCount]["sX"], buttons[buttonCount]["eX"] do
  14.                     term.write(" ")
  15.                 end
  16.             end
  17.             term.setCursorPos(((buttons[buttonCount]["sX"]+buttons[buttonCount]["eX"])/2)-(string.len(buttons[buttonCount]["txt"])/2), (buttons[buttonCount]["sY"]+buttons[buttonCount]["eY"])/2)
  18.             term.write(buttons[buttonCount]["txt"])
  19.         end
  20.     end
  21. end
  22.  
  23. function create(sX,sY,eX,eY,bgcol,txtcol,txt,func)
  24.     buttons[#buttons+1] = {["visible"] = true,["sX"]=sX,["sY"]=sY,["eX"]=eX,["eY"]=eY,["bgcol"]=bgcol,["txtcol"]=txtcol,["txt"]=txt,["func"]=func}
  25.     return #buttons --This is the ID
  26. end
  27.  
  28. function changeVisibility(ID)
  29.     if not buttons[ID] then error("No button with this ID.",2) end
  30.     buttons[ID]["visible"] = not buttons[ID]["visible"]
  31. end
  32.  
  33. function deleteButton(ID)
  34.     if not buttons[ID] then error("Buttons doesn't exists.",2) end
  35.     buttons[ID] = nil
  36. end
  37.  
  38. function getVisibility(ID)
  39.   if not buttons[ID] then error("Button doesn't exists.",2) end
  40.   return buttons[ID]["visible"]
  41. end
  42.  
  43. function waitForEvents()
  44.     while true do
  45.         local evt,btn,pX,pY = os.pullEvent()
  46.         if evt == "mouse_click" then
  47.             for i = 1, #buttons do
  48.                 if buttons[i]["visible"] then
  49.                     if pX >= buttons[i]["sX"] and pX <= buttons[i]["eX"] and pY >= buttons[i]["sY"] and pY <= buttons[i]["eY"] then
  50.                         buttons[i]["func"]()
  51.                         return
  52.                     end
  53.                 end
  54.             end
  55.         end
  56.         sleep(0)
  57.     end
  58. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement