feat: add multi-label support in text widget

This commit is contained in:
Kazhnuz Klappsthul 2020-12-05 09:27:40 +01:00
parent 49509001ab
commit 72829eb02a
1 changed files with 27 additions and 5 deletions

View File

@ -26,13 +26,24 @@ local TextWidget = BaseWidget:extend()
-- TEXT WIDGET
-- Simple text widget
function TextWidget:new(menuName, font, label)
function TextWidget:new(menuName, font, label, position)
TextWidget.super.new(self, menuName)
self.font = font
self.label = label
self.labels = {}
-- We add the first label
local position = position or "center"
self:addLabel(label, position)
self:setSelectedColor(1, 1, 1)
end
function TextWidget:addLabel(label, position)
local complexLabel = {}
complexLabel.label = label
complexLabel.position = position
table.insert(self.labels, complexLabel)
end
function TextWidget:setSelectedColor(r, g, b)
self.selectedColor = {}
self.selectedColor.r = r
@ -46,9 +57,20 @@ end
function TextWidget:drawCanvas()
local w, h
w = math.floor(self.width / 2)
h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
self.font:draw(self.label, w, h, -1, "center")
local font = self.assets:getFont(self.font)
h = math.floor(self.height / 2) - (font:getHeight() / 2)
for _, complexLabel in pairs(self.labels) do
if (complexLabel.position == "center") then
w = math.floor(self.width / 2)
elseif (complexLabel.position == "left") then
w = 0
elseif (complexLabel.position == "right") then
w = math.floor(self.width)
end
font:draw(complexLabel.label, w, h, -1, complexLabel.position)
end
end
function TextWidget:drawSelected(x, y, w, h)