Guest User

ButtonApis

a guest
Nov 16th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.34 KB | None | 0 0
  1. local buttons = {}
  2.  
  3. buttonapis = {}
  4.  
  5. bfunctions = {}
  6.  
  7. local co
  8. local running = false
  9. --Creates a button-- Returns: Success (true/false)
  10. function buttonapis.addButton(x1,y1,x2,y2,func,button)
  11.   button = button or 1
  12.   if button == 1 or button == 2 or button == 3 then
  13.     buttons[func] = {}
  14.     buttons[func]["x1"] = x1
  15.     buttons[func]["y1"] = y1
  16.     buttons[func]["x2"] = x2
  17.     buttons[func]["y2"] = y2
  18.     buttons[func]["button"] = button
  19.     return true
  20.   end
  21.   return false
  22. end
  23.  
  24. --Removes a button--
  25. function buttonapis.removeButton(buttonN)
  26.   buttons[buttonN] = nil
  27. end
  28. function buttonapis.clearButtons()
  29.   buttons = {}
  30. end
  31. function buttonapis.removeFunction(func)
  32.   bfunctions[func] = nil
  33. end
  34. function buttonapis.clearFunctions()
  35.   bfunctions = {}
  36. end
  37.  
  38. --Starts Button Detection--Automatically Run--
  39. function buttonapis.startLoop()
  40.   running = true
  41.   while true do
  42.     if running then
  43.       local e,b,x,y = os.pullEvent("mouse_click")
  44.       for i,v in pairs(buttons)do
  45.         if b == v["button"] and x >= v["x1"] and x <= v["x2"] and y >= v["y1"] and y <= v["y2"] then
  46.           if bfunctions[i] then
  47.             bfunctions[i](x,y,i)
  48.           end
  49.         end
  50.       end
  51.     end
  52.     sleep(0)
  53.   end
  54. end
  55.  
  56. function buttonapis.stopLoop()
  57.   running = false
  58. end
  59. function buttonapis.resumeLoop()
  60.   running = true
  61. end
Advertisement
Add Comment
Please, Sign In to add comment