fork/m-overlay

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

BkacjiosStylize the Tabs on a TabPanel.be5d99b

settings-overhaul
2.3 KiB99 linesraw
1local PANEL = class.create("HorizontalSelect", "Label")
2
3PANEL:ACCESSOR("Selection", "m_iSelection", 1)
4
5function PANEL:HorizontalSelect()
6	self:super() -- Initialize our baseclass
7
8	self:SetDrawPanel(true)
9	self:SetFocusable(true)
10	self:DockPadding(0,0,0,0)
11	self:SetBGColor(color_blank)
12	self:SetBorderColor(color_blank)
13	self:SetTextAlignment("center")
14
15	self.m_tOptions = {}
16
17	self.m_pButLeft = self:Add("Button")
18	--self.m_pButLeft:SetDrawLabel(false)
19	self.m_pButLeft:SetText("<")
20	self.m_pButLeft:Dock(DOCK_LEFT)
21	self.m_pButLeft:SetWidth(20)
22	function self.m_pButLeft:OnClick()
23		self:GetParent():SelectLeft()
24	end
25
26	self.m_pButRight = self:Add("Button")
27	--self.m_pButRight:SetDrawLabel(false)
28	self.m_pButRight:SetText(">")
29	self.m_pButRight:Dock(DOCK_RIGHT)
30	self.m_pButRight:SetWidth(20)
31	function self.m_pButRight:OnClick()
32		self:GetParent():SelectRight()
33	end
34
35	gui.skinHook("Init", "HorizontalSelect", self)
36end
37
38-- Override base class SetEnabled with our own
39-- Toggle the left/right buttons at the same time
40function PANEL:SetEnabled(b)
41	self.m_bEnabled = b
42	self.m_pButLeft:SetEnabled(b)
43	self.m_pButRight:SetEnabled(b)
44end
45
46function PANEL:Paint(w, h)
47	self:super("Paint", w, h) -- Paint our label
48end
49
50function PANEL:PaintOverlay(w, h)
51	gui.skinHook("PaintOverlay", "FocusPanel", self, w, h)
52end
53
54function PANEL:AddOption(str, default)
55	table.insert(self.m_tOptions, str)
56	if default then
57		self:SelectOption(#self.m_tOptions)
58	end
59	self:UpdateSelection()
60	return #self.m_tOptions
61end
62
63function PANEL:SelectOption(num, force)
64	if self.m_iSelection == num and not force then return end
65	self.m_iSelection = num
66	self:UpdateSelection()
67	self:OnSelectOption(self.m_iSelection)
68end
69
70function PANEL:OnSelectOption(num)
71	-- Override
72end
73
74function PANEL:OnMousePressed(x, y, but)
75end
76
77function PANEL:UpdateSelection()
78	self:SetText(self.m_tOptions[self.m_iSelection])
79	self.m_pButLeft:SetEnabled(self.m_iSelection > 1)
80	self.m_pButRight:SetEnabled(self.m_iSelection < #self.m_tOptions)
81end
82
83function PANEL:SelectLeft()
84	self:SelectOption(math.max(1, self.m_iSelection - 1))
85end
86
87function PANEL:SelectRight()
88	self:SelectOption(math.min(#self.m_tOptions, self.m_iSelection + 1))
89end
90
91function PANEL:OnMouseWheeled(x, y)
92	if not self:HasFocus() or not self:IsEnabled() then return end
93	if y > 0 then
94		self:SelectLeft()
95	else
96		self:SelectRight()
97	end
98	return true
99end