fork/m-overlay

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

BkacjiosBugfixes + Dark Mode skina8034e9

v2.1.3
2.4 KiB101 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:SetTextAlignmentX("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
34end
35
36function PANEL:Skin()
37	gui.skinHook("Init", "HorizontalSelect", self)
38end
39
40-- Override base class SetEnabled with our own
41-- Toggle the left/right buttons at the same time
42function PANEL:SetEnabled(b)
43	self.m_bEnabled = b
44	self.m_pButLeft:SetEnabled(b)
45	self.m_pButRight:SetEnabled(b)
46end
47
48function PANEL:Paint(w, h)
49	self:super("Paint", w, h) -- Paint our label
50end
51
52function PANEL:PaintOverlay(w, h)
53	gui.skinHook("PaintOverlay", "FocusPanel", self, w, h)
54end
55
56function PANEL:AddOption(str, default)
57	table.insert(self.m_tOptions, str)
58	if default then
59		self:SelectOption(#self.m_tOptions)
60	end
61	self:UpdateSelection()
62	return #self.m_tOptions
63end
64
65function PANEL:SelectOption(num, force)
66	if self.m_iSelection == num and not force then return end
67	self.m_iSelection = num
68	self:UpdateSelection()
69	self:OnSelectOption(self.m_iSelection)
70end
71
72function PANEL:OnSelectOption(num)
73	-- Override
74end
75
76function PANEL:OnMousePressed(x, y, but)
77end
78
79function PANEL:UpdateSelection()
80	self:SetText(self.m_tOptions[self.m_iSelection])
81	self.m_pButLeft:SetEnabled(self.m_iSelection > 1)
82	self.m_pButRight:SetEnabled(self.m_iSelection < #self.m_tOptions)
83end
84
85function PANEL:SelectLeft()
86	self:SelectOption(math.max(1, self.m_iSelection - 1))
87end
88
89function PANEL:SelectRight()
90	self:SelectOption(math.min(#self.m_tOptions, self.m_iSelection + 1))
91end
92
93function PANEL:OnMouseWheeled(x, y)
94	if not self:HasFocus() or not self:IsEnabled() then return end
95	if y > 0 then
96		self:SelectLeft()
97	else
98		self:SelectRight()
99	end
100	return true
101end