Advertisement
kaibochan

display.lua

Feb 19th, 2025 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1. local Element = require("/apis/gui.element")
  2. local Buffer = require("/apis/gui.buffer")
  3.  
  4. local Display = {
  5.     __name = "Display",
  6.     device = nil,
  7.     is_monitor = false,
  8.     width = 1,
  9.     height = 1,
  10.     window = Element,
  11.     elements = nil,
  12. }
  13.  
  14. function Display:new(o)
  15.     o = o or {}
  16.  
  17.     setmetatable(o, self)
  18.     self.__index = self
  19.  
  20.     o.window = Element:new {
  21.         width = o.width,
  22.         height = o.height,
  23.     }
  24.  
  25.     return o
  26. end
  27.  
  28. function Display:render()
  29.  
  30.     -- draw all elements in breadth-first traversal order
  31.     local index = 1
  32.     self.elements = {{
  33.         element = self.window,
  34.         global_x = self.window.x,
  35.         global_y = self.window.y,
  36.     }}
  37.  
  38.     -- traversal loop
  39.     repeat
  40.         -- add children of current element to elements list
  41.         for _, child in ipairs(self.elements[index].element.children) do
  42.             table.insert(self.elements, {
  43.                 element = child,
  44.                 global_x = self.elements[index].global_x + child.x,
  45.                 global_y = self.elements[index].global_y + child.y,
  46.             })
  47.         end
  48.  
  49.         -- update buffer of current element before copying
  50.         self.elements[index].element:updateBuffer()
  51.  
  52.         -- write the current element's buffer data to screen buffer
  53.         local offset_x = self.elements[index].global_x
  54.         local offset_y = self.elements[index].global_y
  55.        
  56.         for x = 0, self.elements[index].element.width - 1 do
  57.             for y = 0, self.elements[index].element.height - 1 do
  58.                 if (x + offset_x < self.window.width and y + offset_y < self.window.height) then
  59.                     self.window.buffer.cells[x + offset_x][y + offset_y]:copy(
  60.                         self.elements[index].element.buffer.cells[x][y]
  61.                     )
  62.                 end
  63.             end
  64.         end
  65.  
  66.         index = index + 1
  67.     until index > #self.elements
  68.  
  69.     -- write screen buffer data to the display device
  70.     for y = 0, self.height - 1 do
  71.         local characters = ""
  72.         local fg_colors = ""
  73.         local bg_colors = ""
  74.  
  75.         for x = 0, self.width - 1 do
  76.             characters = characters .. self.window.buffer.cells[x][y].character
  77.             fg_colors = fg_colors .. colors.toBlit(self.window.buffer.cells[x][y].fg_color)
  78.             bg_colors = bg_colors ..  colors.toBlit(self.window.buffer.cells[x][y].bg_color)
  79.         end
  80.  
  81.         self.device.setCursorPos(1, y + 1)
  82.         self.device.blit(characters, fg_colors, bg_colors)
  83.     end
  84. end
  85.  
  86. function Display:getSelectedElement(x, y)
  87.     local selection = nil
  88.  
  89.     for i = #self.elements, 1, -1 do
  90.         if (x - 1 >= self.elements[i].global_x
  91.         and x - 1 <  self.elements[i].global_x + self.elements[i].element.width
  92.         and y - 1 >= self.elements[i].global_y
  93.         and y - 1 <  self.elements[i].global_y + self.elements[i].element.height) then
  94.             selection = self.elements[i].element
  95.             break
  96.         end
  97.     end
  98.  
  99.     return selection
  100. end
  101.  
  102. return Display
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement