Jyzarc

ButtonTest

Jan 23rd, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. local buttons = {}
  2. buttons.event = {}
  3.  
  4. button.items = {
  5.     [1] = {
  6.             text = "test";
  7.             minX = 1;
  8.             maxX = 10;
  9.             minY = 5;
  10.             maxX = 15;
  11.             toggle = true;
  12.             pressTime = 0;
  13.             func = printStuff;
  14.         }
  15. }
  16.  
  17. function buttons.draw()
  18.     for i = 1,#buttons.items do
  19.         term.setTextColor(1)
  20.         if buttons.items[i].active then
  21.             term.setBackgroundColor(colors.green)
  22.         else
  23.             term.setBackgroundColor(colors.red)
  24.         end
  25.         buttons.xSpot = math.floor(math.abs(((buttons.items[i].maxX-buttons.items[i].minX)-string.len(buttons.items[i].text))/2))
  26.         buttons.ySpot = math.floor(math.abs((buttons.items[i].maxY-buttons.items[i].maxY)/2))
  27.         for y = buttons.items[i].minY,buttons.items[i].maxY do
  28.             term.setCursorPos(buttons.items[i].minX,y)
  29.             if y == buttons.ySpot then
  30.                 for x = buttons.items[i].minX,buttons.items[i].maxX do
  31.                     if x == buttons.xSpot then
  32.                         term.write(" ")
  33.                     else
  34.                         term.write(buttons.items[i].text)
  35.                     end
  36.                 end
  37.             end
  38.         end
  39.     end    
  40. end
  41.  
  42. function buttons.checkClick(sX,sY)
  43.     for i = 1,#buttons.items do
  44.         if buttons.items[i].minX <= sX
  45.         and buttons.items[i].maxX >= sX
  46.         and buttons.items[i].minY <= sY
  47.         and buttons.items[i].maxY >= sY then
  48.             return i
  49.         end
  50.     end
  51. end
  52.  
  53. function buttons.run(sIndex)
  54.     buttons.func = buttons.items[sIndex].func
  55.     buttons.func()
  56. end
  57.  
  58. --Main loop
  59. while true do
  60.     buttons.draw()
  61.     buttons.event[1],buttons.event[2],buttons.event[3],buttons.event[4] = os.pullEvent()
  62.     if buttons.event[1] == "mouse_click" then
  63.         buttons.index = checkClick(buttons.event[3],buttons.event[4])
  64.         if buttons.items[buttons.index].active then
  65.             if buttons.items[buttons.index].toggle then
  66.                 buttons.items[buttons.index].active = false
  67.             end
  68.         else
  69.             buttons.run(buttons.index)
  70.             buttons.items[buttons.index].active = true
  71.             if not buttons.items[buttons.index].toggle then
  72.                 buttons.items[buttons.index].timer = os.startTimer(buttons.items[buttons.index].pressTime)
  73.             end
  74.         end
  75.     elseif buttons.event[1] == "timer" then
  76.         for i = 1,#buttons.items do
  77.             if buttons.items[i].timer == buttons.event[2]
  78.                 buttons.index = i
  79.             end
  80.         end
  81.         buttons.items[buttons.index].active = false
  82.     end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment