-
Notifications
You must be signed in to change notification settings - Fork 6
/
label.lua
95 lines (75 loc) · 2.1 KB
/
label.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
--
-- Label Widget.
-- @copyright Jefferson Gonzalez
-- @license MIT
--
local style = require "core.style"
local Widget = require "libraries.widget"
---@class widget.label : widget
---@overload fun(parent:widget?, label?:string):widget.label
---@field clickable boolean
local Label = Widget:extend()
---Constructor
---@param parent widget
---@param label string
function Label:new(parent, label)
Label.super.new(self, parent)
self.type_name = "widget.label"
self.clickable = false
self.border.width = 0
self.custom_size = {x = 0, y = 0}
self:set_label(label or "")
end
---@param width? integer
---@param height? integer
function Label:set_size(width, height)
Label.super.set_size(self, width, height)
self.custom_size.x = self.size.x
self.custom_size.y = self.size.y
end
---Set the label text and recalculates the widget size.
---@param text string|widget.styledtext
function Label:set_label(text)
Label.super.set_label(self, text)
local font = self:get_font()
if self.custom_size.x <= 0 then
if type(text) == "table" then
self.size.x, self.size.y = self:draw_styled_text(text, 0, 0, true)
else
self.size.x = font:get_width(self.label)
self.size.y = font:get_height()
end
if self.border.width > 0 then
self.size.x = self.size.x + style.padding.x
self.size.y = self.size.y + style.padding.y
end
end
end
function Label:update()
if not Label.super.update(self) then return false end
if self.custom_size.x <= 0 then
-- update the size
self:set_label(self.label)
end
return true
end
function Label:draw()
if not self:is_visible() then return false end
self:draw_border()
local px = self.border.width > 0 and (style.padding.x / 2) or 0
local py = self.border.width > 0 and (style.padding.y / 2) or 0
local posx, posy = self.position.x + px, self.position.y + py
if type(self.label) == "table" then
self:draw_styled_text(self.label, posx, posy)
else
renderer.draw_text(
self:get_font(),
self.label,
posx,
posy,
self.foreground_color or style.text
)
end
return true
end
return Label