fork/m-overlay

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

BkacjiosMore class changes27686d7

settings-overhaul
2.9 KiB125 linesraw
1local PANEL = class.create("Slider", "Panel")
2
3PANEL.NotchColor = color(100, 100, 100)
4PANEL.GrabberColor = color(0, 162, 232)
5
6PANEL:ACCESSOR("Min", "m_iMin", 0)					-- minumum allowed value
7PANEL:ACCESSOR("Max", "m_iMax", 100)				-- maximum allowed value
8PANEL:ACCESSOR("Value", "m_iValue", 0)				-- starting value
9PANEL:ACCESSOR("Steps", "m_iSteps", 1)				-- steps/max = number of potential values
10PANEL:ACCESSOR("Increments", "m_iIncrements", 5)	-- "minor" notches
11PANEL:ACCESSOR("Notches", "m_iNotches", 4)			-- "major" notches
12
13function PANEL:Slider()
14	self:super() -- Initialize our baseclass
15	self:SetWidth(128)
16
17	self:SetFocusable(true)
18
19	self.m_bGrabbed = false
20end
21
22function PANEL:Paint(w,h)
23	gui.skinHook("Paint", "Slider", self, w, h)
24
25	-- Draw a line down the middle
26	graphics.setColor(self.NotchColor)
27	graphics.line(0, h/2, w, h/2)
28
29	local x = 0
30
31	if self.m_iIncrements > 0 then
32		local range = self:GetRange()
33
34		local increments = (range/self.m_iIncrements)
35		local gap = w/increments
36
37		for i=1, increments-1 do
38			x = 0 + gap * i
39			graphics.line(x, 8, x, h - 8)
40		end
41	end
42
43	if self.m_iNotches > 0 then
44		gap = w/self.m_iNotches
45
46		for i=1, self.m_iNotches-1 do
47			x = 0 + gap * i
48			graphics.line(x, 4, x, h - 4)
49		end
50	end
51end
52
53function PANEL:PaintOverlay(w, h)
54	gui.skinHook("PaintOverlay", "FocusPanel", self, w, h)
55
56	local perct = (self.m_iValue - self.m_iMin) / (self.m_iMax - self.m_iMin)
57	local xpos = perct*w
58
59	graphics.setColor(self.GrabberColor)
60	graphics.rectangle("fill", xpos - 4, 4, 8, h - 8)
61
62	graphics.setColor(color_black)
63	graphics.rectangle("line", xpos - 4, 4, 8, h - 8)
64end
65
66function PANEL:SetValue(i)
67	-- Round value to nearest step
68	local value = math.floor((i / self.m_iSteps) + 0.5) * self.m_iSteps
69	-- Clamp value to min/max
70	self.m_iValue = math.min(self.m_iMax, math.max(self.m_iMin, value))
71	self:OnValueChanged(self.m_iValue)
72end
73
74function PANEL:GetValue()
75	return self.m_iValue
76end
77
78function PANEL:GetRange()
79	return self.m_iMax - self.m_iMin
80end
81
82function PANEL:SetValueFromMouseX(x)
83	local w = self:GetWidth()
84	local range = self:GetRange()
85	self:SetValue(math.floor(self.m_iMin + ((x/w) * range) + 0.5))
86end
87
88function PANEL:OnMousePressed(x, y, but)
89	if not self:IsEnabled() or but ~= 1 then return end
90
91	local w = self:GetWidth()
92
93	local perct = (self.m_iValue - self.m_iMin) / (self.m_iMax - self.m_iMin)
94	local xpos = perct*w
95
96	if x <= xpos + 4 and x >= xpos - 4 then
97		self.m_bGrabbed = true
98	else
99		self:SetValueFromMouseX(x)
100	end
101	return true
102end
103
104function PANEL:OnMouseMoved(x, y, dx, dy, istouch)
105	if self.m_bGrabbed then
106		self:SetValueFromMouseX(x)
107	end
108end
109
110function PANEL:OnMouseWheeled(x, y)
111	if not self:IsEnabled() then return end
112	self:SetValue(self.m_iValue + (y * self.m_iSteps))
113	return true
114end
115
116function PANEL:OnMouseReleased(x, y, but)
117	if not self:IsEnabled() or but ~= 1 then return end
118	if self.m_bGrabbed then
119		self.m_bGrabbed = false
120	end
121end
122
123function PANEL:OnValueChanged(i)
124	
125end