Stawlie0

Untitled

Oct 10th, 2023
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. local baseLib = require("base")
  2. local unicode = require("unicode")
  3.  
  4. local spaceChar = ' '
  5.  
  6. --Разделение строки по символу. Возвращает массив строк и максимальную ширину строки
  7. local function split(str, sep)
  8.     local maxWidthText = 0
  9.     local result = {}
  10.     for currentStr in str:gmatch("([^"..sep.."]+)") do
  11.         local currentWidth = unicode.len(currentStr)
  12.         if currentWidth > maxWidthText then
  13.             maxWidthText = currentWidth
  14.         end
  15.         table.insert(result, currentStr)
  16.     end
  17.     return result, maxWidthText
  18. end
  19.  
  20. --Класс метки
  21. local Label = {}
  22.  
  23. function Label:new(style)
  24.     local obj= {}
  25.     obj.id = baseLib.getId()
  26.     obj.typeControl = "label"
  27.     obj.x = style.x or 0
  28.     obj.y = style.y or 0
  29.     obj.width = style.width or 0
  30.     obj.height = style.height or 0
  31.     obj.text = style.text or obj.typeControl .. tostring(obj.id)
  32.     obj.padding = style.padding or {0, 0, 0, 0}
  33.     obj.drawBuffer = {}
  34.     obj.clickBuffer = {}
  35.     obj.foreColor = style.foreColor or 0xffffff
  36.     obj.backColor = style.backColor or 0x000000
  37.  
  38.     function obj:bild()
  39.         --Текст
  40.         local textLines, maxWidthText = split(self.text, "\n") -- Массив строк, и максимальная ширина текста
  41.         local heightText = #textLines --Высота текста
  42.  
  43.         self.width = self.padding[4] + maxWidthText + self.padding[2]
  44.         self.height = self.padding[1] + heightText + self.padding[3]
  45.  
  46.         --Буфер отрисовки + буфер нажатий
  47.         --Фон
  48.         self.drawBuffer = {}
  49.         self.clickBuffer = {}
  50.         for i = self.y, self.y + self.height - 1 do
  51.             for j = self.x, self.x + self.width - 1 do
  52.                 table.insert(self.drawBuffer, spaceChar) --Символ
  53.                 table.insert(self.drawBuffer, self.foreColor) --Цвет переднего фона
  54.                 table.insert(self.drawBuffer, self.backColor) --Цвет заднего фона
  55.  
  56.                 table.insert(self.clickBuffer, self.id)
  57.             end
  58.         end
  59.  
  60.         --Буфер
  61.         --Текст
  62.         for i = 1, #textLines do
  63.             local textLine = textLines[i]
  64.             for j = 1, unicode.len(textLine) do
  65.                 local char = unicode.sub(textLine, j, j)
  66.                 local relativeX, relativeY = self.padding[4] + j, self.padding[1] + i
  67.                 local index = baseLib.getIndexByCoords(relativeX, relativeY, 3, self.width)
  68.                 self.drawBuffer[index + 1] = char
  69.             end
  70.         end
  71.     end
  72.  
  73.     function obj:update(style)
  74.         for k, v in pairs(style) do
  75.             if self[k] ~= style[k] then
  76.                 self[k] = style[k]
  77.             end
  78.         end
  79.  
  80.         self:bild()
  81.     end
  82.  
  83.     obj:bild()
  84.  
  85.     setmetatable(obj, self)
  86.     self.__index = self; return obj
  87. end
  88.  
  89. return Label
  90.  
Advertisement
Add Comment
Please, Sign In to add comment