Guest User

buttons

a guest
May 1st, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. buttons = {} --Button Array of Sorts
  2. function addButton(name,displaytext,func,coloron,coloroff,textcolor,xmin,xmax,ymin,ymax)
  3.   buttons[name] = {}
  4.   buttons[name]["displaytext"] = displaytext
  5.   buttons[name]["func"] = func
  6.   buttons[name]["xmin"] = xmin
  7.   buttons[name]["xmax"] = xmax
  8.   buttons[name]["ymin"] = ymin
  9.   buttons[name]["ymax"] = ymax
  10.   buttons[name]["coloron"] = coloron
  11.   buttons[name]["coloroff"] = coloroff
  12.   buttons[name]["textcolor"] = textcolor
  13.   buttons[name]["active"] = false
  14. end
  15. function listButtons()
  16.   for name,data in pairs(buttons) do
  17.     name = tostring(name)
  18.     print(name)
  19.   end
  20. end
  21. function fillButton(text,bData,color)
  22.   term.setBackgroundColor(color)
  23.   local yspot = math.floor((bData["ymin"] + bData["ymax"])/2)
  24.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text))/2)+1
  25.   for j = bData["ymin"],bData["ymax"] do
  26.     term.setCursorPos(bData["xmin"],j)
  27.     if j == yspot then
  28.       for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  29.         if k == xspot then
  30.           term.setTextColor(bData["textcolor"])
  31.           if bData["displaytext"] == yes then
  32.             term.write(text)
  33.           elseif bData["displaytext"] == no then
  34.             term.write(" ")
  35.           end
  36.         else
  37.           term.write(" ")
  38.         end
  39.       end
  40.     else
  41.       for i = bData["xmin"], bData["xmax"] do
  42.         term.write(" ")
  43.       end
  44.     end
  45.   end
  46.   term.setBackgroundColor(colors.black)
  47. end
  48. function displayButton()
  49.   local currColor
  50.   for name,data in pairs(buttons) do
  51.     local on = data["active"]
  52.     if on == true then currColor = data["coloron"] else currColor = data["coloroff"] end
  53.     fillButton(name,data,currColor)
  54.   end
  55. end
  56. function xyCheck(x,y)
  57.   for name,data in pairs(button) do
  58.     if y>=data["ymin"] and y<=data["ymax"] then
  59.       data["func"]()
  60.       data["active"] = not data["active"]
  61.       print(name)
  62.     end
  63.   end
  64. end
  65. function buttonLoop()
  66.   displayButton()
  67.   local e,side,x,y = os.pullEvent("monitor_touch")
  68.   xyCheck(x,y)
  69.   sleep(.1)
  70. end
  71. buttons.listButtons()
Advertisement
Add Comment
Please, Sign In to add comment