fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosBugfixes + Dark Mode skina8034e9

v2.1.1b
4.3 KiB159 linesraw
1local PANEL = class.create("Label", "Panel")
2
3PANEL:ACCESSOR("Text", "m_sText", "Label")
4PANEL:ACCESSOR("TextColor", "m_cTextColor", color_black)
5PANEL:ACCESSOR("FontFile", "m_sFontFile")
6PANEL:ACCESSOR("FontSize", "m_iFontSize")
7PANEL:ACCESSOR("FontHint", "m_iFontHint")
8PANEL:ACCESSOR("ShadowDistance", "m_iShadowDistance", 1)
9PANEL:ACCESSOR("ShadowColor", "m_cShadowColor", color_lightgrey)
10PANEL:ACCESSOR("OutlineThickness", "m_iOutlineThickness")
11PANEL:ACCESSOR("OutlineColor", "m_cOutlineColor", color_black)
12PANEL:ACCESSOR("Wrapped", "m_bWrapped", false)
13PANEL:ACCESSOR("TextAlignmentX", "m_sAlignmentX", "left")
14PANEL:ACCESSOR("TextAlignmentY", "m_sAlignmentY", "center")
15PANEL:ACCESSOR("TextAlignmentWrap", "m_sTextAlignmentWrap", "left")
16PANEL:ACCESSOR("DrawLabel", "m_bDrawLabel", true)
17
18function PANEL:Label()
19	self:super() -- Initialize our baseclass
20
21	self.m_tTextMargins = { left = 0, top = 0, right = 0, bottom = 0 }
22
23	self:SetDrawPanel(false)
24	self:SetFocusable(false)
25	
26	self.m_pFont = graphics.newFont("fonts/melee.otf", 12)
27
28	self:SetLineHeight(0.55)
29
30	self:SetText(self.m_sText)
31end
32
33function PANEL:Skin()
34	self:super("Skin")
35	gui.skinHook("Init", "Label", self)
36end
37
38function PANEL:TextMargin(left, top, right, bottom)
39	self.m_tTextMargins.left = left
40	self.m_tTextMargins.top = top
41	self.m_tTextMargins.right = right
42	self.m_tTextMargins.bottom = bottom
43end
44
45function PANEL:GetTextMargin()
46	return self.m_tTextMargins
47end
48
49function PANEL:Think(dt)
50	self:super("Think", dt)
51	if not self.m_pFont and self.m_sFontFile then
52		self.m_pFont = graphics.newFont(self.m_sFontFile, self.m_iFontSize, self.m_iFontHint)
53	end
54end
55
56function PANEL:SetText(text)
57	self.m_sText = text
58	if self.m_bWrapped then
59		self.m_iTextWidth, self.m_tTextWrap = self.m_pFont:getWrap(self.m_sText, self:GetWidth() - self.m_tTextMargins.left - self.m_tTextMargins.right)
60	else
61		self.m_iTextWidth, self.m_tTextWrap = self.m_pFont:getWidth(self.m_sText), {self.m_sText}
62	end
63end
64
65function PANEL:PerformLayout()
66end
67
68function PANEL:SetLineHeight(height)
69	self.m_pFont:setLineHeight(height)
70end
71
72function PANEL:GetLineHeight()
73	return self.m_pFont:getLineHeight()
74end
75
76function PANEL:SetFont(filename, size, hinting)
77	self.m_pFont = graphics.newFont(filename, size, hinting)
78	self.m_sFontFile = filename
79	self.m_iFontSize = size
80	self.m_iFontHint = hinting
81end
82
83local floor = math.floor
84
85function PANEL:PaintLabel(w, h)
86	if not self.m_bDrawLabel then return end
87
88	graphics.setColor(self.m_cTextColor)
89	graphics.setFont(self.m_pFont)
90
91	local x, y = self.m_tTextMargins.left, self.m_tTextMargins.top
92
93	w = w - self.m_tTextMargins.right - self.m_tTextMargins.left
94	h = h - self.m_tTextMargins.top - self.m_tTextMargins.bottom
95
96	local tw, th = self.m_pFont:getWidth(self.m_sText), self.m_pFont:getAscent() - self.m_pFont:getDescent()
97
98	if self.m_bWrapped then
99		local sd = tonumber(self.m_iShadowDistance)
100
101		if sd and sd > 0 then
102			graphics.setColor(self.m_cShadowColor)
103			graphics.printf(self.m_sText, x + sd, y + sd, w, self.m_sTextAlignmentWrap)
104		end
105
106		graphics.setColor(self.m_cTextColor)
107		graphics.printf(self.m_sText, x, y, w, self.m_sTextAlignmentWrap)
108	else
109		--print(self.m_pFont:getHeight(), self.m_pFont:getLineHeight(), self.m_pFont:getDescent(), self.m_pFont:getAscent(), self.m_pFont:getBaseline())
110
111		-- Set alignment for non-wrapped text
112		if self.m_sAlignmentX == "center" then
113			x = x + floor(w/2 - (tw/2))
114		elseif self.m_sAlignmentX == "right" then
115			x = x + w - tw
116		end
117
118		-- Set alignment for non-wrapped text
119		if self.m_sAlignmentY == "center" then
120			y = y + floor(h/2 - (th/2))
121		elseif self.m_sAlignmentY == "bottom" then
122			y = y + h - th
123		end
124
125		local ol = tonumber(self.m_iOutlineThickness)
126
127		if ol and ol > 0 then
128			graphics.setColor(self.m_cOutlineColor)
129			graphics.textOutline(self.m_sText, ol, x, y)
130		end
131
132		local sd = tonumber(self.m_iShadowDistance)
133
134		if sd and sd > 0 then
135			graphics.setColor(self.m_cShadowColor)
136			graphics.print(self.m_sText, x + sd, y + sd)
137		end
138
139		graphics.setColor(self.m_cTextColor)
140		graphics.print(self.m_sText, x, y)
141	end
142end
143
144function PANEL:Paint(w, h)
145	self:PaintLabel(w, h)
146end
147
148function PANEL:SizeToText()
149	self:WidthToText()
150	self:HeightToText()
151end
152
153function PANEL:WidthToText()
154	self:SetWidth(self.m_pFont:getWidth(self.m_sText))
155end
156
157function PANEL:HeightToText()
158	self:SetHeight(self.m_pFont:getHeight() * self:GetLineHeight() * #self.m_tTextWrap)
159end