fork/m-overlay

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

BkacjiosAdded a center button to music probability. Only use weights if the values differ.9ce8403

v2.1.1a
9.5 KiB330 linesraw
1local PANEL = class.create("MusicFileSlider", "Panel")
2
3PANEL:ACCESSOR("FilePath", "m_strFilePath", "")
4
5function PANEL:MusicFileSlider()
6	self:super()
7
8	self:SetHeight(56)
9
10	self.m_pSlider = self:Add("SliderPanel")
11	self.m_pSlider:Dock(DOCK_RIGHT)
12	self.m_pSlider:SetWidth(128)
13	self.m_pSlider:SetNeedsFocus(true)
14
15	self.m_pFileName = self:Add("LabelIcon")
16	self.m_pFileName:Dock(DOCK_FILL)
17	self.m_pFileName:SetTextAlignmentX("left")
18	self.m_pFileName:SetTextAlignmentY("center")
19	self.m_pFileName:SetImage("textures/gui/page.png")
20	self.m_pFileName:SetWrapped(true)
21	self.m_pFileName:TextMargin(28, 7, 4, 4)
22
23	self:InheritMethods(self.m_pSlider)
24	self:SetTextFormat("%d%%")
25end
26
27function PANEL:SetFileName(str)
28	self.m_pFileName:SetText(str)
29end
30
31function PANEL:GetFileName()
32	return self.m_pFileName:GetText()
33end
34
35local PANEL = class.create("MusicDirectory", "Panel")
36
37function PANEL:MusicDirectory()
38	self:super()
39
40	self.m_pDirectoryLabel = self:Add("ButtonIcon")
41	self.m_pDirectoryLabel:SetFocusable(true)
42	self.m_pDirectoryLabel:Dock(DOCK_TOP)
43	self.m_pDirectoryLabel:SetTextAlignmentX("left")
44	self.m_pDirectoryLabel:SetImage("textures/gui/folder_link.png")
45
46	function self.m_pDirectoryLabel:OnClick()
47		love.system.openURL(("file://%s/%s"):format(love.filesystem.getSaveDirectory(), self:GetText()))
48	end
49
50	self.m_pFiles = self:Add("Panel")
51	self.m_pFiles:Dock(DOCK_FILL)
52	self.m_pFiles:DockPadding(0,0,0,0)
53	self.m_pFiles:DockMargin(0,0,0,0)
54	self.m_pFiles:SetDrawPanel(false)
55end
56
57function PANEL:SetPath(str)
58	self.m_pDirectoryLabel:SetText(str)
59end
60
61function PANEL:AddFile(name)
62	local file = self.m_pFiles:Add("MusicFileSlider")
63	file:Dock(DOCK_TOP)
64	file:SetFileName(name)
65	return file
66end
67
68function PANEL:GetFiles()
69	return self.m_pFiles:GetChildren()
70end
71
72function PANEL:PerformLayout()
73	self.m_pFiles:SizeToChildren(false, true)
74	self:SizeToChildren(false, true)
75end
76
77function PANEL:Search(text)
78	local numVis = 0
79	for k, child in ipairs(self.m_pFiles:GetChildren()) do
80		local vis = string.find(child:GetFileName():lower(), text:lower(), 1, true) ~= nil
81		if vis then numVis = numVis + 1 end
82		child:SetVisible(vis)
83	end
84	self:SetVisible(numVis > 0)
85end
86
87local PANEL = class.create("MusicProbability", "Panel")
88
89local music = require("music")
90
91local MODE_GLOBAL = 0
92local MODE_STATE = 1
93
94function PANEL:UpdatePlaylist()
95	local mode = self.PROBMODE:GetOption()
96	local playlistTree = music.getPlaylistTree()
97
98	self.PLAYLIST:Clear()
99
100	for folder, entries in pairs(playlistTree) do
101		local directory = self.PLAYLIST:Add("MusicDirectory")
102		directory:Dock(DOCK_TOP)
103		directory:SetPath(folder)
104
105		for k, entry in ipairs(entries) do
106			local file = directory:AddFile(entry.FILENAME)
107			local path = entry.FILEPATH
108
109			file:SetFilePath(path)
110
111			self.m_tblStateValues[MODE_GLOBAL][path] = music.getGlobalFileProbability(path)
112			self.m_tblStateValues[MODE_STATE][path] = music.getStateFileProbability(path)
113
114			file:SetValue(self.m_tblStateValues[mode][path])
115		end
116	end
117end
118
119function PANEL:GetTableValues()
120	local tbl = {}
121	for k, directory in ipairs(self.PLAYLIST:GetCanvas():GetChildren()) do
122		for k, file in ipairs(directory:GetFiles()) do
123			tbl[file:GetFilePath()] = file:GetValue()
124		end
125	end
126	return tbl
127end
128
129function PANEL:SetTableValues(tbl)
130	for k, directory in ipairs(self.PLAYLIST:GetCanvas():GetChildren()) do
131		for k, file in ipairs(directory:GetFiles()) do
132			local path = file:GetFilePath()
133			if tbl[path] then
134				file:SetValue(tbl[path])
135			end
136		end
137	end
138end
139
140function PANEL:ResetAll(mode)
141	for k, directory in ipairs(self.PLAYLIST:GetCanvas():GetChildren()) do
142		for k, file in ipairs(directory:GetFiles()) do
143			if file:IsVisible() then
144				local path = file:GetFilePath()
145
146				local value
147
148				if mode == MODE_GLOBAL then
149					value = music.getGlobalFileProbability(path)
150				else
151					value = music.getStateFileProbability(path)
152				end
153
154				file:SetValue(value)
155			end
156		end
157	end
158end
159
160function PANEL:SetAllValue(value)
161	for k, directory in ipairs(self.PLAYLIST:GetCanvas():GetChildren()) do
162		for k, file in ipairs(directory:GetFiles()) do
163			if file:IsVisible() then
164				file:SetValue(value)
165			end
166		end
167	end
168end
169
170function PANEL:MusicProbability()
171	self:super()
172
173	self.TOPBAR = self:Add("Panel")
174	self.TOPBAR:Dock(DOCK_TOP)
175	self.TOPBAR:SetHeight(28)
176	self.TOPBAR:DockPadding(0,0,0,0)
177	self.TOPBAR:DockMargin(0,0,0,0)
178	self.TOPBAR:SetBorderColor(color_blank)
179
180	self.SEARCH = self.TOPBAR:Add("TextEntry")
181	self.SEARCH:Dock(DOCK_FILL)
182	self.SEARCH:SetTextHint("Search..")
183
184	self.SEARCH.OnTextChanged = function(this, text, add)
185		self.PLAYLIST:SetScroll(0)
186		for k, child in ipairs(self.PLAYLIST:GetCanvas():GetChildren()) do
187			child:Search(text)
188		end
189		return true
190	end
191
192	self.m_tblStateValues = {
193		[MODE_GLOBAL] = {},
194		[MODE_STATE] = {},
195	}
196
197	self.REFRESH = self.TOPBAR:Add("ButtonIcon")
198	self.REFRESH:SetImage("textures/gui/arrow_refresh.png")
199	self.REFRESH:Dock(DOCK_RIGHT)
200	self.REFRESH:SetText("Refresh")
201	self.REFRESH:SetWidth(24)
202	self.REFRESH:SetTooltipTitle("REFRESH")
203	self.REFRESH:SetTooltipBody([[This button will refetch all the songs for the given state of the game.
204
205NOTE: If you are on the menus, pressing this button will display all songs that can play on the menu. If you are on a stage, it will display all songs that can play when on that stage.]])
206
207	self.PROBMODE = self.TOPBAR:Add("RadioPanel")
208	self.PROBMODE:SetHorizontal(true)
209	self.PROBMODE:SetDrawPanel(false)
210	self.PROBMODE:SetDrawLabel(false)
211	self.PROBMODE:Dock(DOCK_RIGHT)
212	self.PROBMODE:DockMargin(0, 0, 0, 0)
213	self.PROBMODE:DockPadding(0, 0, 0, 0)
214
215	local global = self.PROBMODE:AddOption(MODE_GLOBAL, "Global", true)
216	global:SetWidth(70)
217	global:SetTooltipParent(self.PROBMODE)
218	global:SetTooltipTitle("GLOBAL PROBABILITY")
219	global:SetTooltipBody([[When in global mode, you can adjust the probability the given song will play at a global level. This will apply across all possible game states.
220
221NOTE: A song that has been adjusted in "state" mode will override the probability of its "global" mode value.]])
222
223	local state = self.PROBMODE:AddOption(MODE_STATE, "State")
224	state:SetWidth(66)
225	state:SetTooltipParent(self.PROBMODE)
226	state:SetTooltipTitle("STATE PROBABILITY")
227	state:SetTooltipBody([[When in state mode, you can adjust the probability the given song will play during the currently active state.
228
229NOTE: A "state" refers to the state of the game, such as being on the menus, or within a stage. Each stage is considered its own "state" and will allow you to fine-tune the probability of a single song across multiple states.]])
230
231	self.PROBMODE.OnSelectOption = function(this, mode, previous)
232		self.m_tblStateValues[previous] = self:GetTableValues()
233		self:SetTableValues(self.m_tblStateValues[mode])
234	end
235
236	self.REFRESH.OnClick = function(this)
237		self:UpdatePlaylist()
238	end
239
240	self.PLAYLIST = self:Add("ScrollPanel")
241	self.PLAYLIST:Dock(DOCK_FILL)
242	self.PLAYLIST:DockPadding(0,0,0,0)
243
244	self.OPTIONS = self:Add("Panel")
245	self.OPTIONS:Dock(DOCK_BOTTOM)
246	self.OPTIONS:SetHeight(28)
247	self.OPTIONS:DockPadding(0,0,0,0)
248	self.OPTIONS:DockMargin(0,0,0,0)
249	self.OPTIONS:SetBorderColor(color_blank)
250
251	self.ZERO = self.OPTIONS:Add("ButtonIcon")
252	self.ZERO:SetImage("textures/gui/bullet_arrow_down.png")
253	self.ZERO:TextMargin(24, 0, 0, 0)
254	self.ZERO:Dock(DOCK_LEFT)
255	self.ZERO:SetText("Zero")
256	self.ZERO:SetWidth(24)
257	self.ZERO:SetTooltipTitle("ZERO")
258	self.ZERO:SetTooltipBody([[Sets all songs on this playlist to have 0% chance of playing.]])
259
260	self.ZERO.OnClick = function(this)
261		self:SetAllValue(0)
262	end
263
264	self.MID = self.OPTIONS:Add("ButtonIcon")
265	self.MID:SetImage("textures/gui/bullet_arrow_center.png")
266	self.MID:TextMargin(24, 0, 0, 0)
267	self.MID:Dock(DOCK_LEFT)
268	self.MID:SetText("Center")
269	self.MID:SetWidth(24)
270	self.MID:SetTooltipTitle("CENTER")
271	self.MID:SetTooltipBody([[Sets all songs on this playlist to have 50% chance of playing.]])
272
273	self.MID.OnClick = function(this)
274		self:SetAllValue(50)
275	end
276
277	self.MAX = self.OPTIONS:Add("ButtonIcon")
278	self.MAX:SetImage("textures/gui/bullet_arrow_up.png")
279	self.MAX:TextMargin(24, 0, 0, 0)
280	self.MAX:Dock(DOCK_LEFT)
281	self.MAX:SetText("Max")
282	self.MAX:SetWidth(24)
283	self.MAX:SetTooltipTitle("MAX")
284	self.MAX:SetTooltipBody([[Sets all songs on this playlist to have 100% chance of playing.]])
285
286	self.MAX.OnClick = function(this)
287		self:SetAllValue(100)
288	end
289
290	self.RESET = self.OPTIONS:Add("ButtonIcon")
291	self.RESET:SetImage("textures/gui/arrow_undo.png")
292	self.RESET:TextMargin(24, 0, 0, 0)
293	self.RESET:Dock(DOCK_LEFT)
294	self.RESET:SetText("Reset")
295	self.RESET:SetWidth(24)
296	self.RESET:SetTooltipTitle("RESET")
297	self.RESET:SetTooltipBody([[Sets all songs on this playlist to their previously known value.]])
298
299	self.RESET.OnClick = function(this)
300		self:ResetAll(self.PROBMODE:GetOption())
301	end
302
303	self.OKAY = self.OPTIONS:Add("ButtonIcon")
304	self.OKAY:SetImage("textures/gui/disk.png")
305	self.OKAY:Dock(DOCK_RIGHT)
306	self.OKAY:SetText("Save")
307	self.OKAY:SetWidth(64)
308	self.OKAY:SetTooltipTitle("SAVE")
309	self.OKAY:SetTooltipBody([[Save all changes to disk and exit.]])
310
311	self.OKAY.OnClick = function(this)
312		self:SetVisible(false)
313		self.m_tblStateValues[self.PROBMODE:GetOption()] = self:GetTableValues()
314		music.updateGlobalProbabilities(self.m_tblStateValues[MODE_GLOBAL])
315		music.updateStateProbabilities(self.m_tblStateValues[MODE_STATE])
316		music.saveProbabilities()
317	end
318
319	self.CANCEL = self.OPTIONS:Add("ButtonIcon")
320	self.CANCEL:SetImage("textures/gui/cancel.png")
321	self.CANCEL:Dock(DOCK_RIGHT)
322	self.CANCEL:SetText("Cancel")
323	self.CANCEL:SetWidth(76)
324	self.CANCEL:SetTooltipTitle("CANCEL")
325	self.CANCEL:SetTooltipBody([[Disregard any changes made and exit.]])
326
327	self.CANCEL.OnClick = function(this)
328		self:SetVisible(false)
329	end
330end