Advertisement
NiallDoherty

ButtonAPI

May 30th, 2021 (edited)
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.16 KB | None | 0 0
  1. local function draw(self)
  2.     term.setCursorPos(self.x, self.y)
  3.     paintutils.drawFilledBox(self.x, self.y, (self.x + self.width) - 1, (self.y + self.height) - 1, self.colour)
  4.     term.setCursorPos(self.x+1, self.y+1)
  5.     term.setTextColor(colours.black)
  6.     term.write(self.text)
  7.     term.setBackgroundColor(colours.black)
  8. end
  9.  
  10. local function drawButtons(buttons)
  11.     for _, button in pairs(buttons) do
  12.         button:draw()
  13.     end
  14. end
  15.  
  16. local function update(self, colour, text)
  17.     self.colour = colour or self.colour
  18.     self.text = text or self.text
  19.     self:draw()
  20. end
  21.  
  22. local function clicked(self, x, y)
  23.     return x >= self.x and x < (self.x + self.width) and y >= self.y and y < (self.y + self.height)
  24. end
  25.  
  26. local function createButton(x, y, width, height, text, colour, onClickMethod)
  27.     return {
  28.         x = x,
  29.         y = y,
  30.         width = width or #text + 2,
  31.         height = height or 2,
  32.         colour = colour,
  33.         text = text,
  34.         onClickMethod = onClickMethod,
  35.         draw = draw,
  36.         update = update,
  37.         clicked = clicked
  38.     }
  39. end
  40.  
  41. return {
  42.     createButton = createButton,
  43.     drawButtons = drawButtons
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement