feat: add multi-label support in text widget
This commit is contained in:
parent
49509001ab
commit
72829eb02a
1 changed files with 27 additions and 5 deletions
|
@ -26,13 +26,24 @@ local TextWidget = BaseWidget:extend()
|
||||||
-- TEXT WIDGET
|
-- TEXT WIDGET
|
||||||
-- Simple text widget
|
-- Simple text widget
|
||||||
|
|
||||||
function TextWidget:new(menuName, font, label)
|
function TextWidget:new(menuName, font, label, position)
|
||||||
TextWidget.super.new(self, menuName)
|
TextWidget.super.new(self, menuName)
|
||||||
self.font = font
|
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)
|
self:setSelectedColor(1, 1, 1)
|
||||||
end
|
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)
|
function TextWidget:setSelectedColor(r, g, b)
|
||||||
self.selectedColor = {}
|
self.selectedColor = {}
|
||||||
self.selectedColor.r = r
|
self.selectedColor.r = r
|
||||||
|
@ -46,9 +57,20 @@ end
|
||||||
|
|
||||||
function TextWidget:drawCanvas()
|
function TextWidget:drawCanvas()
|
||||||
local w, h
|
local w, h
|
||||||
|
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)
|
w = math.floor(self.width / 2)
|
||||||
h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
|
elseif (complexLabel.position == "left") then
|
||||||
self.font:draw(self.label, w, h, -1, "center")
|
w = 0
|
||||||
|
elseif (complexLabel.position == "right") then
|
||||||
|
w = math.floor(self.width)
|
||||||
|
end
|
||||||
|
font:draw(complexLabel.label, w, h, -1, complexLabel.position)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function TextWidget:drawSelected(x, y, w, h)
|
function TextWidget:drawSelected(x, y, w, h)
|
||||||
|
|
Loading…
Reference in a new issue