Advertisement
massacring

Button

Feb 1st, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.48 KB | None | 0 0
  1. local Button = {
  2.     isButton = true,
  3.     isActive = false,
  4.     monitor = nil,
  5.     clickEvent = function() print("Click!") end,
  6.     x = 1,
  7.     y = 1,
  8.     width = 7,
  9.     height = 3,
  10.     isPressed = false,
  11.     backgroundColorCurrent = colors.black,
  12.     backgroundColorNormal = colors.black,
  13.     backgroundColorPressed = colors.gray,
  14.     hasBorder = false,
  15.     borderColorCurrent = nil,
  16.     borderColorNormal = nil,
  17.     borderColorPressed = nil,
  18.     label = "Press",
  19.     labelPad = 0,
  20.     textColorCurrent = colors.lightGray,
  21.     textColorNormal = colors.lightGray,
  22.     textColorPressed = colors.white
  23. }
  24.  
  25. function Button:new(_o, monitor, clickEvent, x, y, width, height, label, labelPad, backgroundColorNormal, backgroundColorPressed, borderColorNormal, borderColorPressed, textColorNormal, textColorPressed)
  26.     assert(type(monitor) == "table", "display must be a table.")
  27.     local o = _o or {}
  28.     setmetatable(o, self)
  29.     self.__index = self
  30.     o.isButton = true
  31.     o.isActive = false
  32.     o.monitor = monitor
  33.     o.clickEvent = clickEvent or function() print("Click!") end
  34.     o.x = x or 1
  35.     o.y = y or 1
  36.     o.width = width or 3
  37.     o.height = height or 3
  38.     o.isPressed = false
  39.     o.backgroundColorCurrent = backgroundColorNormal or colors.black
  40.     o.backgroundColorNormal = backgroundColorNormal or colors.black
  41.     o.backgroundColorPressed = backgroundColorPressed or colors.gray
  42.     o.hasBorder = borderColorNormal and borderColorPressed
  43.     if (label == "Rainbow") then o.hasBorder = false end
  44.     if o.hasBorder then
  45.         o.borderColorCurrent = borderColorNormal
  46.         o.borderColorNormal = borderColorNormal
  47.         o.borderColorPressed = borderColorPressed
  48.     else
  49.         o.borderColorCurrent = nil
  50.         o.borderColorNormal = nil
  51.         o.borderColorPressed = nil
  52.     end
  53.     o.label = label or "Press"
  54.     o.labelPad = labelPad or 0
  55.     o.textColorCurrent = textColorNormal or colors.lightGray
  56.     o.textColorNormal = textColorNormal or colors.lightGray
  57.     o.textColorPressed = textColorPressed or colors.white
  58.  
  59.     o.width = o.width + (o.labelPad * 2)
  60.     o.height = o.height + (o.labelPad * 2)
  61.     if o.hasBorder then
  62.         o.width = o.width + 2
  63.         o.height = o.height + 2
  64.     end
  65.  
  66.     return o
  67. end
  68.  
  69. local function loadRainbowButton(button, x_offset, y_offset)
  70.     local monitor = button.monitor
  71.     local colorTable = {
  72.         colors.red;
  73.         colors.orange;
  74.         colors.yellow;
  75.         colors.lime;
  76.         colors.cyan;
  77.         colors.blue;
  78.         colors.magenta;
  79.         colors.purple;
  80.     }
  81.  
  82.     for i = 0, button.width-1, 1 do
  83.         for j = 0, button.height-1, 1 do
  84.             monitor.setCursorPos(button.x + i, button.y + j)
  85.             monitor.setBackgroundColor(colorTable[(i+j) % #colorTable + 1])
  86.             monitor.write(" ")
  87.         end
  88.     end
  89.  
  90.     local function split(str)
  91.         if #str>0 then return str:sub(1,1),split(str:sub(2)) end
  92.     end
  93.  
  94.     local labelChars={split(button.label)}
  95.     monitor.setCursorPos(
  96.         button.x + x_offset,
  97.         button.y + y_offset
  98.     )
  99.     for i, char in pairs(labelChars) do
  100.         monitor.setTextColor(colorTable[(i+y_offset + 5) % #colorTable + 1])
  101.         monitor.setBackgroundColor(colorTable[(i+y_offset) % #colorTable + 1])
  102.         monitor.write(char)
  103.     end
  104.  
  105.     button.isActive = true
  106. end
  107.  
  108. function Button:displayOnScreen()
  109.     local x_offset, y_offset = self.labelPad, self.labelPad
  110.     if self.label == "Rainbow" then
  111.         loadRainbowButton(self, x_offset, y_offset)
  112.         return
  113.     end
  114.  
  115.     local monitor = self.monitor
  116.     monitor.setBackgroundColor(self.backgroundColorCurrent)
  117.     for i = 0, self.height-1, 1 do
  118.         monitor.setCursorPos(self.x, self.y + i)
  119.         monitor.write(string.rep(" ", self.width))
  120.     end
  121.  
  122.     if self.hasBorder then
  123.         x_offset = x_offset + 1
  124.         y_offset = y_offset + 1
  125.         monitor.setBackgroundColor(self.borderColorCurrent)
  126.         for i = 1, self.width, 1 do
  127.             for j = 1, self.height, 1 do
  128.                 if not ((i == 1 or j == 1) or (i == self.width or j == self.height)) then goto continue end
  129.  
  130.                 monitor.setCursorPos(self.x + (i-1), self.y + (j-1))
  131.                 monitor.write(" ")
  132.  
  133.                 ::continue::
  134.             end
  135.         end
  136.         monitor.setBackgroundColor(self.backgroundColorCurrent)
  137.     end
  138.  
  139.     monitor.setCursorPos(
  140.         self.x + x_offset,
  141.         self.y + y_offset
  142.     )
  143.     monitor.setTextColor(self.textColorCurrent)
  144.     monitor.write(self.label)
  145.     self.isActive = true
  146. end
  147.  
  148. function Button:clear(color)
  149.     local monitor = self.monitor
  150.     monitor.setBackgroundColor(color)
  151.     for i = 0, self.height-1, 1 do
  152.         monitor.setCursorPos(self.x, self.y + i)
  153.         monitor.write(string.rep(" ", self.width))
  154.     end
  155.     self.isActive = false
  156. end
  157.  
  158. function Button:move(x, y, color)
  159.     self:clear(color or colors.black)
  160.     self.x = x
  161.     self.y = y
  162.     self:displayOnScreen()
  163. end
  164.  
  165. function Button:toggle()
  166.     self.isPressed = not self.isPressed
  167.     if self.isPressed then
  168.         self.backgroundColorCurrent = self.backgroundColorPressed
  169.         self.borderColorCurrent = self.borderColorPressed
  170.         self.textColorCurrent = self.textColorPressed
  171.     else
  172.         self.backgroundColorCurrent = self.backgroundColorNormal
  173.         self.borderColorCurrent = self.borderColorNormal
  174.         self.textColorCurrent = self.textColorNormal
  175.     end
  176.     self:displayOnScreen()
  177. end
  178.  
  179. return Button
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement