Advertisement
massacring

Screen

Feb 1st, 2024 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. local Button = require('Button')
  2.  
  3. local Screen = {
  4.     monitor = nil,
  5.     name = "Screen",
  6.     buttonsRegistry = {},
  7.     backgroundColor = colors.black,
  8. }
  9.  
  10. function Screen:new(_o, name, monitor, backgroundColor)
  11.     assert(type(monitor) == "table", "monitor must be a table.")
  12.     local o = _o or {}
  13.     setmetatable(o, self)
  14.     self.__index = self
  15.     o.name = name
  16.     o.monitor = monitor
  17.     o.width, o.height = monitor.getSize()
  18.     o.backgroundColor = backgroundColor or colors.black
  19.     o.buttonsRegistry = {}
  20.  
  21.     return o
  22. end
  23.  
  24. function Screen:loadRainbow()
  25.     local colorTable = {
  26.         colors.red;
  27.         colors.orange;
  28.         colors.yellow;
  29.         colors.lime;
  30.         colors.cyan;
  31.         colors.blue;
  32.         colors.magenta;
  33.         colors.purple;
  34.     }
  35.  
  36.     for t = 1, 100, 1 do
  37.         for i = 2, self.width-1, 1 do
  38.             for j = 2, self.height-1, 1 do
  39.                 self.monitor.setCursorPos(i, j)
  40.                 self.monitor.setBackgroundColor(colorTable[(i+j+t) % #colorTable + 1])
  41.                 self.monitor.write(" ")
  42.             end
  43.         end
  44.         sleep(0.05)
  45.     end
  46. end
  47.  
  48. function Screen:clearWindow(color)
  49.     local monitor = self.monitor
  50.     monitor.setTextScale(1)
  51.     self.width, self.height = monitor.getSize()
  52.     monitor.setBackgroundColor(color)
  53.     monitor.clear()
  54.     monitor.setCursorPos(1, 1)
  55. end
  56.  
  57. function Screen:getCenter(x, y, x_content, y_content)
  58.     local x_offset = math.floor(self.width  / 2) - math.floor(x_content / 2) + 1
  59.     local y_offset = math.floor(self.height / 2) - math.floor(y_content / 2) + 1
  60.     return x_offset + x, y_offset + y
  61. end
  62.  
  63. function Screen:registerButton(button)
  64.     assert(button.isButton, "You can only register buttons.")
  65.     table.insert(self.buttonsRegistry, button)
  66. end
  67.  
  68. function Screen:createButton(label, clickEvent, _x, _y, _height, labelPad, backgroundColorNormal, backgroundColorPressed, borderColorNormal, borderColorPressed, textColorNormal, textColorPressed, isCenter)
  69.     assert(type(clickEvent) == "function", "clickEvent is not a function.")
  70.  
  71.     local x = _x or 0
  72.     local y = _y or 0
  73.  
  74.     if isCenter then
  75.         x, y = self:getCenter(x, y, (#label + 4), 5)
  76.     end
  77.  
  78.     local button = Button:new(nil, self.monitor, clickEvent, x, y, #label, _height, label, labelPad, backgroundColorNormal, backgroundColorPressed, borderColorNormal, borderColorPressed, textColorNormal, textColorPressed)
  79.     self:registerButton(button)
  80.     return button
  81. end
  82.  
  83. function Screen:placeButtons()
  84.     for index, button in pairs(self.buttonsRegistry) do
  85.         button:displayOnScreen()
  86.     end
  87. end
  88.  
  89. function Screen:loadScreen()
  90.     self:clearWindow(self.backgroundColor)
  91.     self:placeButtons()
  92. end
  93.  
  94. return Screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement