Advertisement
Xlonix

Button API

Feb 24th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. local buttons = {};
  2. local buttonX = {{},{}};
  3. local buttonY = {{},{}};
  4. local lastClicked = 1;
  5.  
  6. function newButton(name, x, y, maxX, maxY)
  7.   table.insert(buttons, name);
  8.   table.insert(buttonX[1], x);
  9.   table.insert(buttonY[1], y);
  10.   table.insert(buttonX[2], maxX);
  11.   table.insert(buttonY[2], maxY);
  12. end
  13.  
  14. function termDetect()
  15.   lastClicked = "none";
  16.   event, button, x, y = os.pullEvent("mouse_click");
  17.   --Check if in bounds of any button
  18.   for i=1, #buttons do
  19.     if((x >= buttonX[1][i]) and (x <= buttonX[2][i])) then
  20.       if((y >= buttonY[1][i]) and (y <= buttonY[2][i])) then
  21.         lastClicked = buttons[i];
  22.         return buttons[i];
  23.       end
  24.     end
  25.   end
  26.   return "none";
  27. end
  28.  
  29. function rmButton(button)
  30.   for i=1, #buttons do
  31.     if(buttons[i] == button) then
  32.       table.remove(buttons, i);
  33.     end
  34.   end
  35. end
  36.  
  37. function monDetect()
  38.   lastClicked = "none";
  39.   event, side, x, y = os.pullEvent("monitor_touch");
  40.   for i=1, #buttons do
  41.     if((x >= buttonX[1][i]) and (x <= buttonX[2][i])) then
  42.       if((y >= buttonY[1][i]) and (y <= button[2][i])) then
  43.         lastClicked = buttons[i];
  44.         return buttons[i];
  45.       end
  46.     end
  47.   end
  48.   return "none";
  49. end
  50.  
  51. function getLastClicked()
  52.   return lastClicked;
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement