fork/m-overlay

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

BkacjiosStylize the Tabs on a TabPanel.be5d99b

settings-overhaul
3.9 KiB141 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("TextAlignment", "m_sAlignment", "left")
14
15function PANEL:Label()
16	self:super() -- Initialize our baseclass
17
18	self.m_tTextMargins = { left = 0, top = 0, right = 0, bottom = 0 }
19
20	self:SetDrawPanel(false)
21	self:SetFocusable(false)
22	
23	self.m_pFont = graphics.newFont("fonts/melee.otf", 12)
24
25	self:SetLineHeight(0.55)
26
27	self:SetText(self.m_sText)
28end
29
30function PANEL:TextMargin(left, top, right, bottom)
31	self.m_tTextMargins.left = left
32	self.m_tTextMargins.top = top
33	self.m_tTextMargins.right = right
34	self.m_tTextMargins.bottom = bottom
35end
36
37function PANEL:GetTextMargin()
38	return self.m_tTextMargins
39end
40
41function PANEL:Think(dt)
42	self:super("Think", dt)
43	if not self.m_pFont and self.m_sFontFile then
44		self.m_pFont = graphics.newFont(self.m_sFontFile, self.m_iFontSize, self.m_iFontHint)
45	end
46end
47
48function PANEL:SetText(text)
49	self.m_sText = text
50	if self.m_bWrapped then
51		self.m_iTextWidth, self.m_tTextWrap = self.m_pFont:getWrap(self.m_sText, self:GetWidth())
52	else
53		self.m_iTextWidth, self.m_tTextWrap = self.m_pFont:getWidth(self.m_sText), {self.m_sText}
54	end
55end
56
57function PANEL:PerformLayout()
58end
59
60function PANEL:SetLineHeight(height)
61	self.m_pFont:setLineHeight(height)
62end
63
64function PANEL:GetLineHeight()
65	return self.m_pFont:getLineHeight()
66end
67
68function PANEL:SetFont(filename, size, hinting)
69	self.m_pFont = graphics.newFont(filename, size, hinting)
70	self.m_sFontFile = filename
71	self.m_iFontSize = size
72	self.m_iFontHint = hinting
73end
74
75local floor = math.floor
76
77function PANEL:Paint(w, h)
78	self:super("Paint", w, h)
79
80	graphics.setColor(self.m_cTextColor)
81	graphics.setFont(self.m_pFont)
82
83	local x, y = self.m_tTextMargins.left, self.m_tTextMargins.top
84
85	w = w - self.m_tTextMargins.right - self.m_tTextMargins.left
86	h = h - self.m_tTextMargins.top - self.m_tTextMargins.bottom
87
88	local tw,th = self.m_pFont:getWidth(self.m_sText), self.m_pFont:getAscent() - self.m_pFont:getDescent()
89
90	if self.m_bWrapped then
91		local sd = tonumber(self.m_iShadowDistance)
92
93		if sd and sd > 0 then
94			graphics.setColor(self.m_cShadowColor)
95			graphics.printf(self.m_sText, x + sd, y + sd, self:GetWidth(), self.m_sAlignment)
96		end
97
98		graphics.setColor(self.m_cTextColor)
99		graphics.printf(self.m_sText, x, y, self:GetWidth(), self.m_sAlignment)
100	else
101		--print(self.m_pFont:getHeight(), self.m_pFont:getLineHeight(), self.m_pFont:getDescent(), self.m_pFont:getAscent(), self.m_pFont:getBaseline())
102
103		-- Set alignment for non-wrapped text
104		if self.m_sAlignment == "center" then
105			x, y = x + floor(w/2 - (tw/2)), y + floor(h/2 - (th/2))
106		elseif self.m_sAlignment == "right" then
107			x, y = x + w - tw, y + floor(h/2 - (th/2))
108		else -- Assume left
109			y = y + floor(h/2 - (th/2))
110		end
111
112		local ol = tonumber(self.m_iOutlineThickness)
113
114		if ol and ol > 0 then
115			graphics.setColor(self.m_cOutlineColor)
116			graphics.textOutline(self.m_sText, ol, x, y)
117		end
118
119		local sd = tonumber(self.m_iShadowDistance)
120
121		if sd and sd > 0 then
122			graphics.setColor(self.m_cShadowColor)
123			graphics.print(self.m_sText, x + sd, y + sd)
124		end
125
126		graphics.setColor(self.m_cTextColor)
127		graphics.print(self.m_sText, x, y)
128	end
129end
130
131function PANEL:SizeToText()
132	self:SetSize(self.m_iTextWidth, self.m_pFont:getHeight() * self:GetLineHeight() * #self.m_tTextWrap)
133end
134
135function PANEL:WidthToText()
136	self:SetWidth(self.m_pFont:getWidth(self.m_sText))
137end
138
139function PANEL:HeightToText()
140	self:SetHeight(self.m_pFont:getHeight() * self:GetLineHeight() * #self.m_tTextWrap)
141end