-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabel.lua
More file actions
115 lines (88 loc) · 2.97 KB
/
Label.lua
File metadata and controls
115 lines (88 loc) · 2.97 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
gui.label = class('gui.label')
function gui.label:created(shouldFit, fitAxis)
self.text = ""
self.fontSize = 32
self.color = color(255)
self.align = CENTER|MIDDLE
self.style = 0
self.shadowOffset = vec2(1, 1)
self.shadowSoftner = 5
--self.shadowColor = color(37, 24, 23, 72)
self.borderWidth = 0
self.borderColor = color(0,0)
self.font = "ArialMT"
--self.entity.sprite = asset.Square
--self.entity.color = color(59)
self.shouldFit = shouldFit
self.fitAxis = gui.horizontal | gui.vertical
self.truncate = false
self.truncatePadding = 0
end
function gui.label:computeSize()
if self.shouldFit then
local w, h = self:getSize(self.text)
local size = self.entity.size
if self.fitAxis & gui.vertical == gui.vertical then
size.x = w
end
if self.fitAxis & gui.vertical == gui.vertical then
size.y = h
end
self.entity.size = size
end
end
function gui.label:getSize(txt)
style.fontSize(self.fontSize)
style.textAlign(self.align).textStyle(self.style)
style.strokeWidth(self.borderWidth).stroke(self.borderColor)
style.textAlign(self.align)
style.font(self.font)
return textSize(txt)
end
function gui.label:draw()
if self.entity.visible then
style.fontSize(self.fontSize)
style.fill(self.color )
style.textAlign(self.align).textStyle(self.style)
if self.font then style.font(self.font) end
if self.shadowColor then
style.textShadow(self.shadowColor).textShadowOffset(self.shadowOffset).textShadowSoftner(self.shadowSoftner)
else
style.noTextShadow()
end
style.strokeWidth(self.borderWidth).stroke(self.borderColor)
if self.borderWidth == 0 then
style.stroke(self.color)
end
style.textAlign(self.align)
if self.truncate then
text(self:cutToFit(self.text), 0, 0, self.entity.size:unpack())
else
text(self.text, 0, 0, self.entity.size:unpack())
end
end
end
function gui.label:setStyle(lis)
for k, v in pairs(lis) do
self[k] = v
end
end
function gui.label:cutToFit(name)
local w = textSize(name)
local frameWidth = self.entity.size.x - self.truncatePadding
if w <= frameWidth then
return name
end
if self.oldSize ~= frameWidth then
local ellipsis = "..."
local ellipsisWidth = textSize(ellipsis)
local truncatedText = name
while textSize(truncatedText) + ellipsisWidth > frameWidth do
truncatedText = truncatedText:sub(1, -2) -- Remove the last character
end
self.truncatedString = truncatedText .. ellipsis
self.oldSize = self.entity.size.x - self.truncatePadding
end
return self.truncatedString
end
Profiler.wrapClass(gui.label)