Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/sound_setting.lua

raw
1 --[[ Copyright (c) 2025 Stephen Baker
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 class "UISoundSettings" (UIResizable)
22
23 ---@type UISoundSettings
24 local UISoundSettings = _G["UISoundSettings"]
25
26 -- Constants for most button's width and height
27 local LBL_X = 20
28 local LBL_WIDTH = 160
29 local LBL_HEIGHT = 20
30 local BTN_WIDTH = 400
31 local BTN_HEIGHT = 20
32 local BTN_X = LBL_X + LBL_WIDTH + 5
33 local BIG_BTN_WIDTH = 565
34 local BIG_BTN_HEIGHT = 30
35
36 local col_bg = {
37 red = 154,
38 green = 146,
39 blue = 198,
40 }
41
42 local col_shadow = {
43 red = 134,
44 green = 126,
45 blue = 178,
46 }
47
48 local col_caption = {
49 red = 174,
50 green = 166,
51 blue = 218,
52 }
53
54 --! Midi port list in format expected by UIDropdown
55 --!param app (App)
56 local function midi_port_options(app)
57 local ports = app.audio:getMidiPortList()
58
59 local res = {{ text = _S.audio_window.default_midi_port }}
60 for _, p in ipairs(ports) do
61 res[#res + 1] = { text = p, value = p }
62 end
63
64 return res
65 end
66
67 --! Construct new UISoundSettings window
68 --!param ui (UI) The game ui
69 --!param mode (string) 'menu' or 'game', depending on whether the window is
70 -- displayed from the main menu or in game.
71 function UISoundSettings:UISoundSettings(ui, mode)
72 self:UIResizable(ui, 605, 295, col_bg)
73
74 local app = ui.app
75 self.mode = mode
76 self.modal_class = mode == "menu" and "main menu" or "options" or "folders"
77 self.on_top = mode == "menu"
78 self.esc_closes = true
79 self.resizable = false
80 self:setDefaultPosition(0.5, 0.25)
81 self.default_button_sound = "selectx.wav"
82 self.app = ui.app
83 local built_in = app.gfx:loadMenuFont()
84
85
86 self.volume_options = { { text = _S.customise_window.option_off, volume = 0 } }
87 for i = 10, 100, 10 do
88 self.volume_options[#self.volume_options + 1] = {
89 text = _S.menu_options_volume[i],
90 volume = i / 100
91 }
92 end
93
94 self.midi_api_options = { { text = _S.audio_window.default_midi_port, value = nil } }
95 for _, api in ipairs(app.audio:getMidiApiList()) do
96 self.midi_api_options[#self.midi_api_options + 1] = { text = api, value = api }
97 end
98
99 self.midi_port_options = midi_port_options(app)
100
101 local y = 10
102
103 -- Title
104 self:addBevelPanel(200, y, 245, 20, col_caption):setLabel(_S.audio_window.caption)
105 .lowered = true
106
107 y = y + 30
108
109 -- global audio on/off
110 self:addBevelPanel(LBL_X, y, LBL_WIDTH, LBL_HEIGHT, col_shadow, col_bg, col_bg)
111 :setLabel(_S.audio_window.audio)
112 :setTooltip(_S.tooltip.audio_window.audio_button).lowered = true
113 self.onoff_panel = self:addBevelPanel(BTN_X, y, BTN_WIDTH, BTN_HEIGHT, col_bg)
114 :setLabel(app.config.audio and _S.customise_window.option_on or _S.customise_window.option_off)
115 self.onoff_button = self.onoff_panel
116 :makeToggleButton(0, 0, BTN_WIDTH, BTN_HEIGHT, nil, self.buttonAudioGlobal)
117 :setToggleState(app.config.audio)
118 :setTooltip(_S.tooltip.audio_window.audio_toggle)
119
120 y = y + 25
121
122 -- sound volume
123
124 local sound_volume_label = app.config.play_sounds and _S.menu_options_volume[app.config.sound_volume * 100] or
125 _S.customise_window.option_off
126 self:addBevelPanel(LBL_X, y, LBL_WIDTH, LBL_HEIGHT, col_shadow, col_bg, col_bg)
127 :setLabel(_S.audio_window.sound_volume)
128 :setTooltip(_S.tooltip.audio_window.sound_volume).lowered = true
129 self.sound_volume_panel = self:addBevelPanel(BTN_X, y, BTN_WIDTH, BTN_HEIGHT, Colours.Setting, nil, nil, nil, Colours.SettingActive)
130 :setLabel(sound_volume_label)
131 self.sound_volume_button = self.sound_volume_panel
132 :makeToggleButton(0, 0, BTN_WIDTH, BTN_HEIGHT, nil, self.dropdownVolume)
133 :setTooltip(_S.tooltip.audio_window.sound_volume)
134
135 y = y + 25
136
137 local announcement_volume_label = app.config.play_announcements and
138 _S.menu_options_volume[app.config.announcement_volume * 100] or _S.customise_window.option_off
139 self:addBevelPanel(LBL_X, y, LBL_WIDTH, LBL_HEIGHT, col_shadow, col_bg, col_bg)
140 :setLabel(_S.audio_window.announcement_volume)
141 :setTooltip(_S.tooltip.audio_window.announcement_volume).lowered = true
142 self.announcement_volume_panel = self:addBevelPanel(BTN_X, y, BTN_WIDTH, BTN_HEIGHT, Colours.Setting, nil, nil, nil, Colours.SettingActive)
143 :setLabel(announcement_volume_label)
144 self.announcement_volume_button = self.announcement_volume_panel
145 :makeToggleButton(0, 0, BTN_WIDTH, BTN_HEIGHT, nil, self.dropdownVolume)
146 :setTooltip(_S.tooltip.audio_window.announcement_volume)
147
148 y = y + 25
149
150 local music_volume_label_text = app.config.play_music and
151 _S.menu_options_volume[app.config.music_volume * 100] or
152 _S.customise_window.option_off
153 self:addBevelPanel(LBL_X, y, LBL_WIDTH, LBL_HEIGHT, col_shadow, col_bg, col_bg)
154 :setLabel(_S.audio_window.music_volume)
155 :setTooltip(_S.tooltip.audio_window.music_volume).lowered = true
156 self.music_volume_panel = self:addBevelPanel(BTN_X, y, BTN_WIDTH, BTN_HEIGHT, Colours.Setting, nil, nil, nil, Colours.SettingActive)
157 :setLabel(music_volume_label_text)
158 self.music_volume_button = self.music_volume_panel
159 :makeToggleButton(0, 0, BTN_WIDTH, BTN_HEIGHT, nil, self.dropdownVolume)
160 :setTooltip(_S.tooltip.audio_window.music_volume)
161
162 y = y + 25
163
164 local midi_api_label = app.config.midi_api or _S.audio_window.default_midi_api
165 self:addBevelPanel(LBL_X, y, LBL_WIDTH, LBL_HEIGHT, col_shadow, col_bg, col_bg)
166 :setLabel(_S.audio_window.midi_api)
167 :setTooltip(_S.tooltip.audio_window.midi_api).lowered = true
168 self.midi_api_panel = self:addBevelPanel(BTN_X, y, BTN_WIDTH, BTN_HEIGHT, Colours.Setting, nil, nil, nil, Colours.SettingActive)
169 :setLabel(midi_api_label)
170 self.midi_api_button = self.midi_api_panel
171 :makeToggleButton(0, 0, BTN_WIDTH, BTN_HEIGHT, nil, self.dropdownMidiApi)
172 :setTooltip(_S.tooltip.audio_window.midi_api)
173
174 y = y + 25
175
176 -- Location of soundfont file (only for default api)
177 local soundfont_label_panel = self:addBevelPanel(LBL_X, y, LBL_WIDTH, LBL_HEIGHT, col_shadow, col_bg, col_bg)
178 soundfont_label_panel
179 :setLabel(_S.audio_window.soundfont)
180 :setTooltip(_S.tooltip.audio_window.soundfont_location)
181 :setVisible(not app.config.midi_api)
182 soundfont_label_panel.lowered = true
183 local tooltip_soundfont = app.config.soundfont and
184 _S.tooltip.audio_window.browse_soundfont:format(app.config.soundfont) or
185 _S.tooltip.audio_window.no_soundfont_specified
186 local soundfont_path = app.config.soundfont
187 local soundfont_button_panel = self:addBevelPanel(BTN_X, y, BTN_WIDTH, BTN_HEIGHT, col_bg)
188 soundfont_button_panel
189 :setLabel(soundfont_path or tooltip_soundfont,
190 soundfont_path and built_in)
191 :setAutoClip(true)
192 :setVisible(not app.config.midi_api)
193 local soundfont_button = soundfont_button_panel
194 :makeButton(0, 0, BTN_WIDTH, BTN_HEIGHT, nil, self.buttonBrowseForSoundfont)
195 :setTooltip(tooltip_soundfont)
196 :setVisible(not app.config.midi_api)
197 :enable(not app.config.midi_api)
198
199 -- midi port (only for non-default api)
200 local midi_port_label_text = app.config.midi_port or _S.audio_window.default_midi_port
201 local midi_port_label_panel = self:addBevelPanel(LBL_X, y, LBL_WIDTH, LBL_HEIGHT, col_shadow, col_bg, col_bg)
202 midi_port_label_panel
203 :setLabel(_S.audio_window.midi_port)
204 :setTooltip(_S.tooltip.audio_window.midi_port)
205 :setVisible(not not app.config.midi_api)
206 midi_port_label_panel.lowered = true
207
208 self.midi_port_panel = self:addBevelPanel(BTN_X, y, BTN_WIDTH, BTN_HEIGHT, Colours.Setting, nil, nil, nil, Colours.SettingActive)
209 :setLabel(midi_port_label_text)
210 :setVisible(not not app.config.midi_api)
211 self.midi_port_button = self.midi_port_panel
212 :makeToggleButton(0, 0, BTN_WIDTH, BTN_HEIGHT, nil, self.dropdownMidiPort)
213 :setTooltip(_S.tooltip.audio_window.midi_port)
214 :setVisible(not not app.config.midi_api)
215 :enable(not not app.config.midi_api)
216
217 self.default_api_panels = { soundfont_label_panel, soundfont_button_panel, soundfont_button }
218 self.midi_api_panels = { midi_port_label_panel, self.midi_port_panel, self.midi_port_button }
219
220 -- jukebox
221 self:addBevelPanel(20, 220, BIG_BTN_WIDTH, BIG_BTN_HEIGHT, col_bg)
222 :setLabel(_S.audio_window.jukebox)
223 :makeButton(0, 0, BIG_BTN_WIDTH, BIG_BTN_HEIGHT, nil, self.buttonJukebox)
224 :setTooltip(_S.tooltip.audio_window.jukebox)
225
226 -- back
227 self:addBevelPanel(20, 255, BIG_BTN_WIDTH, BIG_BTN_HEIGHT, col_bg)
228 :setLabel(_S.audio_window.back)
229 :makeButton(0, 0, BIG_BTN_WIDTH, BIG_BTN_HEIGHT, nil, self.buttonBack)
230 :setTooltip(_S.tooltip.audio_window.back)
231 end
232
233 --! Reinitialize the game audio
234 -- Allows all the changed audio settings to take effect by shutting down the
235 -- game audio and starting it up again.
236 function UISoundSettings:reinitAudio()
237 local app = self.ui.app
238 app.audio:stopBackgroundTrack()
239 app.audio:destroy()
240 app.audio:init()
241 app:initLanguage()
242 app.audio:playRandomBackgroundTrack()
243 end
244
245 --! Close any UIDropdown that may be open
246 function UISoundSettings:closeAllDropdowns()
247 self:dropdownVolume(false)
248 self:dropdownMidiApi(false)
249 self:dropdownMidiPort(false)
250 end
251
252 --! Click action for the global audio toggle
253 -- Turns all game audio off or on.
254 function UISoundSettings:buttonAudioGlobal()
255 local app = self.ui.app
256 app.config.audio = not app.config.audio
257 app:saveConfig()
258 self.onoff_button:setLabel(app.config.audio and _S.customise_window.option_on or _S.customise_window.option_off)
259 self:reinitAudio()
260 end
261
262 --! Click action for any of the audio buttons
263 -- Displays a drop down of volume options.
264 --!param activate (bool) true when the control is is activated, false when
265 -- deactivated
266 --!param btn (UIButton) the button clicked to fire this event
267 function UISoundSettings:dropdownVolume(activate, btn)
268 if activate then
269 self:closeAllDropdowns()
270 btn:setToggleState(true)
271
272 local select_callback
273 if btn == self.sound_volume_button then
274 select_callback = self.selectSoundVolume
275 elseif btn == self.announcement_volume_button then
276 select_callback = self.selectAnnouncementVolume
277 elseif btn == self.music_volume_button then
278 select_callback = self.selectMusicVolume
279 end
280
281 self.volume_dropdown = UIDropdown(self.ui, self, btn, self.volume_options, select_callback, Colours.SettingActive, Colours.Scrollbar)
282 self:addWindow(self.volume_dropdown)
283 else
284 self.sound_volume_button:setToggleState(false)
285 self.announcement_volume_button:setToggleState(false)
286 self.music_volume_button:setToggleState(false)
287 if self.volume_dropdown then
288 self.volume_dropdown:close()
289 self.volume_dropdown = nil
290 end
291 end
292 end
293
294 --! Click action for midi api button
295 -- Shows a drop down list of MIDI API options that are supported by the
296 -- current build.
297 --!param activate true when the control is is activated, false when deactivated
298 function UISoundSettings:dropdownMidiApi(activate)
299 if activate then
300 self:closeAllDropdowns()
301 self.midi_api_button:setToggleState(true)
302
303 self.midi_api_dropdown = UIDropdown(self.ui, self, self.midi_api_button, self.midi_api_options, self.selectMidiApi, Colours.SettingActive, Colours.Scrollbar)
304 self:addWindow(self.midi_api_dropdown)
305 else
306 self.midi_api_button:setToggleState(false)
307 if self.midi_api_dropdown then
308 self.midi_api_dropdown:close()
309 self.midi_api_dropdown = nil
310 end
311 end
312 end
313
314 --! Click action for the midi port button
315 -- Shows a drop down list of the MIDI ports available for the selected API.
316 --!param activate true when the control is is activated, false when deactivated
317 function UISoundSettings:dropdownMidiPort(activate)
318 if activate then
319 self:closeAllDropdowns()
320 self.midi_port_button:setToggleState(true)
321
322 self.midi_port_dropdown = UIDropdown(self.ui, self, self.midi_port_button, self.midi_port_options, self.selectMidiPort, Colours.SettingActive, Colours.Scrollbar)
323 self:addWindow(self.midi_port_dropdown)
324 else
325 self.midi_port_button:setToggleState(false)
326 if self.midi_port_dropdown then
327 self.midi_port_dropdown:close()
328 self.midi_port_dropdown = nil
329 end
330 end
331 end
332
333 --! Triggered when a sound volume option is selected from the dropdown
334 -- Sets the sound volume or turns sound off in the config.
335 --!param index (number) the index of the option selected
336 function UISoundSettings:selectSoundVolume(index)
337 local vol = self.volume_options[index].volume
338 if vol == 0 then
339 self.app.audio:playSoundEffects(false)
340 else
341 self.app.audio:playSoundEffects(true)
342 self.app.audio:setSoundVolume(vol)
343 end
344 self.sound_volume_panel:setLabel(self.volume_options[index].text)
345 self.app:saveConfig()
346 end
347
348 --! Triggered when an announcement volume option is selected from the dropdown
349 -- Sets the announcement volume or turns announcements off in the config.
350 --!param index (number) the index of the option selected
351 function UISoundSettings:selectAnnouncementVolume(index)
352 local vol = self.volume_options[index].volume
353 if vol == 0 then
354 self.app.config.play_announcements = false
355 else
356 self.app.config.play_announcements = true
357 self.app.audio:setAnnouncementVolume(vol)
358 end
359 self.announcement_volume_panel:setLabel(self.volume_options[index].text)
360 self.app:saveConfig()
361 end
362
363 --! Triggered when a music volume option is selected from the dropdown
364 -- Sets the music volume or turns announcements off in the config.
365 --!param index (number) the index of the option selected
366 function UISoundSettings:selectMusicVolume(index)
367 local vol = self.volume_options[index].volume
368 if vol == 0 then
369 self.app.config.play_music = false
370 self.app.audio:stopBackgroundTrack()
371 else
372 self.app.audio:setBackgroundVolume(vol)
373 if not self.app.config.play_music then
374 self.app.config.play_music = true
375 self.app.audio:playRandomBackgroundTrack()
376 end
377 end
378 self.music_volume_panel:setLabel(self.volume_options[index].text)
379 self.app:saveConfig()
380 end
381
382 --! Triggered when a MIDI API is selected from the dropdown
383 -- Sets the MIDI API in the config and resets the MIDI port selection,
384 -- since the MIDI port is specific to the selected API.
385 --!param index (number) the index of the option selected
386 function UISoundSettings:selectMidiApi(index)
387 local value = self.midi_api_options[index].value
388
389 self.app.config.midi_api = value
390 self.app.config.midi_port = nil
391 self.app:saveConfig()
392
393 self:reinitAudio()
394 self.midi_port_button:setLabel(_S.audio_window.default_midi_port)
395 self.midi_port_options = midi_port_options(self.app)
396
397 for _, p in ipairs(self.default_api_panels) do
398 p:setVisible(not value)
399 if p.enable then
400 p:enable(not value)
401 end
402 end
403 for _, p in ipairs(self.midi_api_panels) do
404 p:setVisible(not not value)
405 if p.enable then
406 p:enable(not not value)
407 end
408 end
409 end
410
411 --! Triggered when a MIDI port is selected from the dropdown
412 -- Sets the MIDI port and resets the audio.
413 --!param index (number) the index of the option selected
414 function UISoundSettings:selectMidiPort(index)
415 local value = self.midi_port_options[index].value
416 self.app.config.midi_port = value
417 self.app:saveConfig()
418 self:reinitAudio()
419 end
420
421 --! Button handler for soundfont
422 -- Opens a file chooser dialog for selecting a soundfont.
423 function UISoundSettings:buttonBrowseForSoundfont()
424 local browser = UIChooseSoundfont(self.ui, self.mode, self, self.selectSoundfont)
425 self.ui:addWindow(browser)
426 end
427
428 --! Callback when a soundfont is chosen
429 -- Saves the soundfont and reinitializes audio.
430 function UISoundSettings:selectSoundfont(name)
431 self.app.config.soundfont = name
432 self.app:saveConfig()
433 self:reinitAudio()
434 end
435
436 --! Callback for Jukebox button
437 -- Opens the jukebox window.
438 function UISoundSettings:buttonJukebox()
439 if self.app.config.audio then
440 self.ui:addWindow(UIJukebox(self.app))
441 end
442 end
443
444 --! Callback for back button
445 -- Opens the UIOptions window again.
446 function UISoundSettings:buttonBack()
447 self:close()
448 local window = UIOptions(self.ui, "menu")
449 self.ui:addWindow(window)
450 end
451
452 --! Close window
453 function UISoundSettings:close()
454 UIResizable.close(self)
455 if self.mode == "menu" then
456 self.ui:addWindow(UIMainMenu(self.ui))
457 end
458 end