Advertisement
massacring

Button

Jan 5th, 2025 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. local Button = {}
  2. Button.__index = Button
  3.  
  4. function Button.new(label, clickEvent, x, y, width, height, labelPad, backgroundColorNormal, borderColor, textColorNormal)
  5.     local button = setmetatable({}, Button)
  6.     button.isActive = false
  7.     button.clickEvent = clickEvent or function() print("Click!") end
  8.     button.x = x or 1
  9.     button.y = y or 1
  10.     button.width = width or 3
  11.     button.height = height or 3
  12.     button.isPressed = false
  13.     button.backgroundColorCurrent = backgroundColorNormal or colors.black
  14.     button.backgroundColorNormal = backgroundColorNormal or colors.black
  15.     button.borderColor = borderColor
  16.     button.label = label or "Press"
  17.     button.labelPad = labelPad or 0
  18.     button.textColorCurrent = textColorNormal or colors.lightGray
  19.     button.textColorNormal = textColorNormal or colors.lightGray
  20.  
  21.     button.width = button.width + (button.labelPad * 2)
  22.     button.height = button.height + (button.labelPad * 2)
  23.     if button.borderColor then
  24.         button.width = button.width + 2
  25.         button.height = button.height + 2
  26.     end
  27.  
  28.     return button
  29. end
  30.  
  31. function Button:displayOnScreen(drawSquare, write)
  32.     local x_offset, y_offset = self.labelPad, self.labelPad
  33.  
  34.     if self.borderColor then
  35.         x_offset = x_offset + 1
  36.         y_offset = y_offset + 1
  37.         drawSquare(self.x, self.y, self.width, self.height, self.borderColor, true)
  38.     end
  39.  
  40.     drawSquare(self.x+1, self.y+1, self.width-2, self.height-2, self.backgroundColorCurrent, true)
  41.  
  42.     write(self.label, self.x + x_offset, self.y + y_offset, self.textColorCurrent, self.backgroundColorCurrent, true)
  43.  
  44.     self.isActive = true
  45. end
  46.  
  47. function Button:collides(x, y)
  48.     return ((x >= self.x) and (x < (self.x + self.width))) and ((y >= self.y) and (y < (self.y + self.height)))
  49. end
  50.  
  51. return Button
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement