09glich

ButtonAPI

Jun 18th, 2023 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. local ButtonAPI = {}
  2.  
  3. ButtonAPI.Buttons = {}
  4.  
  5. ButtonAPI.createButton = function(Name,Connection,Type,Color1,Color2,PosX,PosY,SizeX,SizeY)
  6.     local newButton = {}
  7.    
  8.     -- big O'l vars
  9.     newButton.Name = Name
  10.     newButton.Connection = Connection
  11.     newButton.ButtonType = Type or "Click"
  12.     newButton.State = false
  13.     newButton.Color1 = Color1
  14.     newButton.Color2 = Color2
  15.     newButton.PosX = PosX
  16.     newButton.PosY = PosY
  17.     newButton.SizeX = SizeX
  18.     newButton.SizeY = SizeY
  19.     newButton.Thread = nil
  20.    
  21.     -- state shit
  22.     newButton.SetState = function(newState)
  23.         newButton.State = newState
  24.     end
  25.     newButton.GetState = function()
  26.         return newButton.State
  27.     end
  28.    
  29.     -- drawing
  30.     newButton.Draw = function()
  31.         term.setCursorPos(newButton.PosX,newButton.PosY)
  32.         if newButton.State == false then
  33.             paintutils.drawFilledBox(
  34.                 newButton.PosX,
  35.                 newButton.PosY,
  36.                 newButton.PosX + newButton.SizeX,
  37.                 newButton.PosY + newButton.SizeY,
  38.                 newButton.Color1
  39.             )
  40.         else
  41.             paintutils.drawFilledBox(
  42.                 newButton.PosX,
  43.                 newButton.PosY,
  44.                 newButton.PosX + newButton.SizeX,
  45.                 newButton.PosY + newButton.SizeY,
  46.                 newButton.Color2
  47.             )
  48.         end
  49.         term.setBackgroundColor(colors.black)
  50.     end
  51.    
  52.     -- dtecet clickty
  53.     newButton.Detection = function()
  54.         local event,side,x,y = os.pullEvent("monitor_touch")
  55.         --print("Pressed")
  56.         -- annoying if'y shit
  57.          if x >= newButton.PosX
  58.         and x <= newButton.PosX + newButton.SizeX
  59.         and y >= newButton.PosY
  60.         and y <= newButton.PosY + newButton.SizeY then
  61.             newButton.State = not newButton.State
  62.             if newButton.ButtonType == "Click" then
  63.                 sleep(1)
  64.                 newButton.State = not newButton.State
  65.                 print("Pressed")
  66.             end
  67.         end
  68.      end
  69.    
  70.     -- add to list of buttons
  71.     table.insert(ButtonAPI.Buttons, newButton)
  72.    
  73.     -- start
  74.     newButton.Detection()
  75. end
  76.  
  77. ButtonAPI.ClearButtons = function()
  78.     ButtonAPI.Buttons = {}
  79. end
  80.  
  81. ButtonAPI.DrawAllButtons = function()
  82.     for i,q in pairs(ButtonAPI.Buttons) do
  83.         q.Draw()
  84.     end
  85. end
  86.  
  87. return ButtonAPI
Advertisement
Add Comment
Please, Sign In to add comment