bobmarley12345

button-api

Mar 2nd, 2022 (edited)
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.04 KB | None | 0 0
  1. -- this is all completely untested so it might not work xd
  2.  
  3. local BUTTONS = {}
  4. local CLICKED = {}
  5.  
  6. function createButton(id, x, y, w, h, callback)
  7.     local x2 = x + w - 1
  8.     local y2 = y + h - 1
  9.     local button = {
  10.         x1 = x, y1 = y,
  11.         x2 = x2, y2 = y2,
  12.         id = id, callback = callback
  13.     }
  14.  
  15.     table.insert(BUTTONS, id, button)
  16.     return button
  17. end
  18.  
  19. function getHitScan(x, y)
  20.     for key, value in pairs(BUTTONS) do
  21.         if (x >= value.x1 and x <= value.x2) then
  22.             if (y >= value.y1 and y <= value.y2) then
  23.                 return value
  24.             end
  25.         end
  26.     end
  27.  
  28.     return nil
  29. end
  30.  
  31. function onScreenHit(x, y)
  32.     local hit = getHitScan(x, y)
  33.     if (hit == nil) then
  34.         return
  35.     end
  36.  
  37.     table.insert(CLICKED, hit.id, hit)
  38.     hit.callback(x, y, hit.id)
  39. end
  40.  
  41. function pullClickEvent()
  42.     local mouse,x,y = os.pullEvent("mouse_click")
  43.     onScreenHit(x, y)
  44. end
  45.  
  46. function getButtons()
  47.     return BUTTONS
  48. end
  49.  
  50. function getClickedButtons()
  51.     return CLICKED
  52. end
  53.  
Add Comment
Please, Sign In to add comment