fork/m-overlay

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

BkacjiosImplemented a way to customize music file weights (#62/#119) Fixed some issues with the GUI backend.ce51c77

v2.1.4
3.1 KiB128 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
12PANEL:ACCESSOR("NeedsFocus", "m_bNeedsFocus", false)-- if we need focus for scrollwheel events
13
14function PANEL:Slider()
15	self:super() -- Initialize our baseclass
16	self:SetWidth(128)
17
18	self:SetFocusable(true)
19
20	self.m_bGrabbed = false
21end
22
23function PANEL:Paint(w,h)
24	gui.skinHook("Paint", "Slider", self, w, h)
25
26	-- Draw a line down the middle
27	graphics.setColor(self.NotchColor)
28	graphics.line(0, h/2, w, h/2)
29
30	local x = 0
31
32	if self.m_iIncrements > 0 then
33		local range = self:GetRange()
34
35		local increments = (range/self.m_iIncrements)
36		local gap = w/increments
37
38		for i=1, increments-1 do
39			x = 0 + gap * i
40			graphics.line(x, 8, x, h - 8)
41		end
42	end
43
44	if self.m_iNotches > 0 then
45		gap = w/self.m_iNotches
46
47		for i=1, self.m_iNotches-1 do
48			x = 0 + gap * i
49			graphics.line(x, 4, x, h - 4)
50		end
51	end
52end
53
54function PANEL:PaintOverlay(w, h)
55	gui.skinHook("PaintOverlay", "FocusPanel", self, w, h)
56
57	local perct = (self.m_iValue - self.m_iMin) / (self.m_iMax - self.m_iMin)
58	local xpos = perct*w
59
60	graphics.setColor(self.GrabberColor)
61	graphics.rectangle("fill", xpos - 4, 4, 8, h - 8)
62
63	graphics.setColor(color_black)
64	graphics.rectangle("line", xpos - 4, 4, 8, h - 8)
65end
66
67function PANEL:SetValue(i)
68	-- Round value to nearest step
69	local value = math.floor((i / self.m_iSteps) + 0.5) * self.m_iSteps
70	-- Clamp value to min/max
71	self.m_iValue = math.min(self.m_iMax, math.max(self.m_iMin, value))
72	self:OnValueChanged(self.m_iValue)
73end
74
75function PANEL:GetValue()
76	return self.m_iValue
77end
78
79function PANEL:GetRange()
80	return self.m_iMax - self.m_iMin
81end
82
83function PANEL:SetValueFromMouseX(x)
84	local w = self:GetWidth()
85	local range = self:GetRange()
86	self:SetValue(math.floor(self.m_iMin + ((x/w) * range) + 0.5))
87end
88
89function PANEL:OnMousePressed(x, y, but)
90	if not self:IsEnabled() or but ~= 1 then return end
91
92	local w = self:GetWidth()
93
94	local perct = (self.m_iValue - self.m_iMin) / (self.m_iMax - self.m_iMin)
95	local xpos = perct*w
96
97	if x <= xpos + 4 and x >= xpos - 4 then
98		self.m_bGrabbed = true
99	else
100		self:SetValueFromMouseX(x)
101	end
102	return true
103end
104
105function PANEL:OnMouseMoved(x, y, dx, dy, istouch)
106	if self.m_bGrabbed then
107		self:SetValueFromMouseX(x)
108	end
109end
110
111function PANEL:OnMouseWheeled(x, y)
112	if not self:IsEnabled() then return end
113	if not self.m_bNeedsFocus or (self.m_bNeedsFocus and self:HasFocus()) then
114		self:SetValue(self.m_iValue + (y * self.m_iSteps))
115		return true -- Only catch if we are updating value
116	end
117end
118
119function PANEL:OnMouseReleased(x, y, but)
120	if not self:IsEnabled() or but ~= 1 then return end
121	if self.m_bGrabbed then
122		self.m_bGrabbed = false
123	end
124end
125
126function PANEL:OnValueChanged(i)
127	
128end