Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local baseLib = require("base")
- local unicode = require("unicode")
- local spaceChar = ' '
- --Разделение строки по символу. Возвращает массив строк и максимальную ширину строки
- local function split(str, sep)
- local maxWidthText = 0
- local result = {}
- for currentStr in str:gmatch("([^"..sep.."]+)") do
- local currentWidth = unicode.len(currentStr)
- if currentWidth > maxWidthText then
- maxWidthText = currentWidth
- end
- table.insert(result, currentStr)
- end
- return result, maxWidthText
- end
- --Класс метки
- local Label = {}
- function Label:new(style)
- local obj= {}
- obj.id = baseLib.getId()
- obj.typeControl = "label"
- obj.x = style.x or 0
- obj.y = style.y or 0
- obj.width = style.width or 0
- obj.height = style.height or 0
- obj.text = style.text or obj.typeControl .. tostring(obj.id)
- obj.padding = style.padding or {0, 0, 0, 0}
- obj.drawBuffer = {}
- obj.clickBuffer = {}
- obj.foreColor = style.foreColor or 0xffffff
- obj.backColor = style.backColor or 0x000000
- function obj:bild()
- --Текст
- local textLines, maxWidthText = split(self.text, "\n") -- Массив строк, и максимальная ширина текста
- local heightText = #textLines --Высота текста
- self.width = self.padding[4] + maxWidthText + self.padding[2]
- self.height = self.padding[1] + heightText + self.padding[3]
- --Буфер отрисовки + буфер нажатий
- --Фон
- self.drawBuffer = {}
- self.clickBuffer = {}
- for i = self.y, self.y + self.height - 1 do
- for j = self.x, self.x + self.width - 1 do
- table.insert(self.drawBuffer, spaceChar) --Символ
- table.insert(self.drawBuffer, self.foreColor) --Цвет переднего фона
- table.insert(self.drawBuffer, self.backColor) --Цвет заднего фона
- table.insert(self.clickBuffer, self.id)
- end
- end
- --Буфер
- --Текст
- for i = 1, #textLines do
- local textLine = textLines[i]
- for j = 1, unicode.len(textLine) do
- local char = unicode.sub(textLine, j, j)
- local relativeX, relativeY = self.padding[4] + j, self.padding[1] + i
- local index = baseLib.getIndexByCoords(relativeX, relativeY, 3, self.width)
- self.drawBuffer[index + 1] = char
- end
- end
- end
- function obj:update(style)
- for k, v in pairs(style) do
- if self[k] ~= style[k] then
- self[k] = style[k]
- end
- end
- self:bild()
- end
- obj:bild()
- setmetatable(obj, self)
- self.__index = self; return obj
- end
- return Label
Advertisement
Add Comment
Please, Sign In to add comment