Alyssa

Button Designer/Planner

Mar 9th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. version = "1.03"
  2. buttons = {}
  3.  
  4. function newButton(name,xmin,xmax,ymin,ymax,tfunc,bColor,bName)
  5.     if name and xmin and xmax and ymin and ymax then
  6.     else
  7.         return false
  8.     end
  9.     if bColor and bName then
  10.         buttons[name] = {xmin,xmax,ymin,ymax,tfunc,bColor,bName}
  11.     else
  12.         buttons[name] = {xmin,xmax,ymin,ymax,tfunc}
  13.     end
  14.     return true
  15. end
  16.  
  17. function deleteButton(name)
  18.     buttons[name] = nil
  19.     return true
  20. end
  21.  
  22. function checkButtons(posx,posy)
  23.     for k,v in pairs(buttons) do
  24.         if posx >= buttons[k][1] and posx <= buttons[k][2] and posy >= buttons[k][3] and posy <= buttons[k][4] then
  25.             return buttons[k][5]()
  26.         end
  27.         sleep(0)
  28.     end
  29. end
  30.  
  31. function deleteAllButtons()
  32.     buttons = {}
  33.     return true
  34. end
  35.  
  36. function displayButtons()
  37.     for k,v in pairs(buttons) do
  38.         if buttons[k][6] then
  39.             drawBox(buttons[k][1],buttons[k][3],buttons[k][2],buttons[k][4],buttons[k][6])
  40.             buttonCX = buttons[k][2] - buttons[k][1]
  41.             buttonCX = buttons[k][2] - math.floor(buttonCX / 2)
  42.             buttonCY = buttons[k][4] - buttons[k][3]
  43.             buttonCY = buttons[k][4] - math.floor(buttonCY / 2)
  44.             if buttons[k][7] then
  45.                 cBText = #buttons[k][7] /2
  46.                 toPosX = buttonCX - cBText
  47.                 term.setCursorPos(toPosX,buttonCY)
  48.                 write(buttons[k][7])
  49.             end
  50.         end
  51.     end
  52. end
  53.  
  54. function clicked()
  55.     return true
  56. end
  57. cn = 0
  58. colnum = 0
  59. cm = false
  60. pos1x,pos1y,pos2x,pos2y = 1,1,1,1
  61. text = ""
  62. nButtons = 0
  63. term.setBackgroundColor(colors.white)
  64. term.clear()
  65. TID = os.startTimer(0.1)
  66. while true do
  67.     e,wc,cx,cy = os.pullEvent()
  68.     term.clear()
  69.     displayButtons()
  70.     term.setBackgroundColor(colors.white)
  71.     term.setCursorPos(1,1)
  72.     term.setTextColor(2^colnum)
  73.     write("Color")
  74.     scrx,scry = term.getSize()
  75.     term.setCursorPos(1,scry)
  76.     term.setTextColor(colors.black)
  77.     write(text)
  78.     term.setCursorPos(pos1x,pos1y)
  79.     term.setTextColor(colors.orange)
  80.     write("+")
  81.     term.setCursorPos(pos2x,pos2y)
  82.     write("+")
  83.     term.setTextColor(colors.black)
  84.     if e == "timer" and wc == TID then
  85.         TID = os.statTimer(0.1)
  86.     end
  87.     if e == "mouse_click" then
  88.         if wc == 1 then
  89.             cn = cn +1
  90.             if cn == 3 then
  91.                 cn = 1
  92.             end
  93.             if cn == 1 then
  94.                 pos1x,pos1y = cx,cy
  95.             elseif cn == 2 then
  96.                 pos2x, pos2y = cx, cy
  97.             end
  98.         elseif wc == 2  then
  99.             if checkButtons(cx,cy) then
  100.                 selected = k
  101.             end
  102.         end
  103.     end
  104.     if e == "char" and cm then
  105.         text = text .. wc
  106.     end
  107.     if e == "key" then
  108.         if wc == keys.up then
  109.             if colnum < 15 then
  110.                 colnum = colnum +1
  111.             else
  112.                 colnum = 0
  113.             end
  114.         elseif wc == keys.down then
  115.             if colnum > 0 then
  116.                 colnum = colnum -1
  117.             else
  118.                 colnum = 15
  119.             end
  120.         elseif wc == keys.enter then
  121.             nButtons = nButtons +1
  122.             if pos1x >= pos2x then
  123.                 txm = pos1x
  124.                 txb = pos2x
  125.             else
  126.                 txm = pos2x
  127.                 txb = pos1x
  128.             end
  129.             if pos1y >= pos2y then
  130.                 tym = pos1y
  131.                 tyb = pos2y
  132.             else
  133.                 tym = pos2y
  134.                 tyb = pos1y
  135.             end
  136.             term.clear()
  137.             newButton(tostring(nButtons),txb,txm,tyb,tym,clicked,2^colnum,text)
  138.             displayButtons()
  139.             term.setBackgroundColor(colors.white)
  140.             text = ""
  141.         elseif wc == keys.rightShift then
  142.             if cm then
  143.                 cm = false
  144.             else
  145.                 cm = true
  146.             end
  147.         elseif wc == keys.backspace then
  148.             if selected then
  149.                 deleteButton(k)
  150.                 selected = false
  151.             elseif cm then
  152.                 text = string.sub(text,1,#text-1)
  153.             end
  154.         end
  155.     end
  156. end
Advertisement
Add Comment
Please, Sign In to add comment