Skip to content

Commit bbc49e7

Browse files
committedFeb 28, 2024
feat: sliders for music and sound volume
Moves the sounds to playing via SoundManager - further DJ clean up can come later. One quirk is that the sounds now overlap rather than interrupting each other - might be slightly annoying, hopefully there's a quick fix around for this.
1 parent 6d5d12d commit bbc49e7

7 files changed

+179
-122
lines changed
 

‎addons/dj/DJ.gd

+7-86
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ func play_sound_opts(sounds, opts = {}):
1616
var s = sounds[i]
1717
if not is_instance_valid(s):
1818
return
19+
var pitch = 1.0
1920
if scale_range > 0 and scale_note != null:
2021
var note = lerp(0.0, 1.0, scale_note/scale_range)
21-
s.pitch_scale = note
22+
pitch = note
2223
elif vary > 0.0:
23-
s.pitch_scale = 1 - (randf() * vary)
24-
s.play()
24+
pitch = 1 - (randf() * vary)
25+
SoundManager.play_sound_with_pitch(s, pitch)
2526

26-
##########################################################
27-
# sound map api
27+
## sound map api ####################################################
2828

2929
func setup_sound(sound, opts = {}):
3030
var s_str
@@ -34,25 +34,9 @@ func setup_sound(sound, opts = {}):
3434
if not sound:
3535
Log.warn("Could not load sound", s_str)
3636
return
37-
var asp = AudioStreamPlayer.new()
38-
asp.set_stream(sound)
39-
asp.name = sound.resource_path.get_file()
40-
add_child(asp, true)
41-
if "is_sound" in opts and opts["is_sound"]:
42-
# how to force no looping for sounds? it's determined by the input rn
43-
asp.set_volume_db(default_sound_vol_db)
37+
return sound
4438

45-
if "vol_db" in opts:
46-
asp.set_volume_db(opts["vol_db"])
47-
48-
return asp
49-
50-
var default_sound_vol_db = -12
51-
var defaults = {
52-
"is_sound": true
53-
}
54-
55-
func setup_sound_map(sound_map, default_opts=defaults):
39+
func setup_sound_map(sound_map):
5640
var playables = {}
5741
for k in sound_map.keys():
5842
playables[k] = []
@@ -64,7 +48,6 @@ func setup_sound_map(sound_map, default_opts=defaults):
6448
if typeof(s) == TYPE_ARRAY:
6549
sound = s[0]
6650
opts = s[1]
67-
opts.merge(default_opts)
6851
var playable = setup_sound(sound, opts)
6952
if playable:
7053
playables[k].append(playable)
@@ -91,64 +74,10 @@ func interrupt_sound(sound_map, name):
9174
else:
9275
Log.warn("no sound for name", name)
9376

94-
95-
#################################################
96-
# music map api
97-
98-
var playing_game_songs = []
99-
var paused_game_songs = []
100-
101-
func play_song(sound_map, name):
102-
if muted_music:
103-
# Log.warn("Cannot play song, music is muted")
104-
return
105-
if name in sound_map:
106-
var songs = sound_map[name]
107-
var i = randi() % songs.size()
108-
var s = songs[i]
109-
# if already playing, do nothing
110-
if not s.playing:
111-
s.play()
112-
playing_game_songs.append(s)
113-
else:
114-
Log.warn("no song for name", name)
115-
116-
func interrupt_song(sound_map, name=null):
117-
var song
118-
if not name == null:
119-
song = interrupt_sound(sound_map, name)
120-
elif len(playing_game_songs) > 0:
121-
song = playing_game_songs[0]
122-
song.stop()
123-
124-
if song:
125-
playing_game_songs.erase(song)
126-
else:
127-
Log.warn("Could not find song to interrupt!", sound_map)
128-
129-
func pause_game_song():
130-
paused_game_songs = []
131-
for song in playing_game_songs:
132-
if song.playing:
133-
song.stop()
134-
paused_game_songs.append([song, song.get_playback_position()])
135-
136-
func resume_game_song():
137-
if muted_music:
138-
# Log.warn("Cannot resume game song, music is muted")
139-
return
140-
# could store and resume at same playback_position
141-
for song_and_pos in paused_game_songs:
142-
var song = song_and_pos[0]
143-
var pos = song_and_pos[1]
144-
song.play(pos)
145-
146-
14777
## mute ######################################################################
14878

14979
var muted_sound = false
15080
var muted_music = false
151-
signal mute_toggle
15281

15382
func mute_all(should_mute=true):
15483
toggle_mute_music(should_mute)
@@ -160,16 +89,8 @@ func toggle_mute_music(should_mute=null):
16089
else:
16190
muted_music = should_mute
16291

163-
if muted_music:
164-
pause_game_song()
165-
else:
166-
resume_game_song()
167-
168-
mute_toggle.emit()
169-
17092
func toggle_mute_sound(should_mute=null):
17193
if should_mute == null:
17294
muted_sound = not muted_sound
17395
else:
17496
muted_sound = should_mute
175-
mute_toggle.emit()

‎addons/dj/DJSoundMap.gd

-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,3 @@ func play(nm, opts={}):
2121

2222
func interrupt(nm):
2323
DJ.interrupt_sound(_sound_map, nm)
24-
25-
func play_song(nm):
26-
DJ.play_song(_sound_map, nm)
27-
28-
func interrupt_song(nm=null):
29-
DJ.interrupt_song(_sound_map, nm)

‎addons/dj/MuteButtonList.gd

+63-28
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,32 @@ extends VBoxContainer
66
@onready var mute_all_button = $%MuteAllButton
77
@onready var unmute_all_button = $%UnmuteAllButton
88

9+
@onready var music_volume_slider = $%MusicVolumeSlider
10+
@onready var sound_volume_slider = $%SoundVolumeSlider
11+
912
var old_music_volume
13+
var old_sound_volume
1014

1115
func _ready():
16+
if not Engine.is_editor_hint():
17+
var music_vol = SoundManager.get_music_volume()
18+
music_volume_slider.set_value(music_vol)
19+
20+
var sound_vol = SoundManager.get_sound_volume()
21+
sound_volume_slider.set_value(sound_vol)
22+
1223
render()
1324

25+
music_volume_slider.value_changed.connect(func(new_vol):
26+
SoundManager.set_music_volume(new_vol))
1427

15-
mute_music_button.pressed.connect(func():
16-
DJ.toggle_mute_music()
17-
18-
if DJ.muted_music:
19-
old_music_volume = SoundManager.get_music_volume()
20-
SoundManager.set_music_volume(0);
21-
else:
22-
SoundManager.set_music_volume(old_music_volume);
23-
render())
24-
mute_sound_button.pressed.connect(func():
25-
DJ.toggle_mute_sound()
26-
render())
27-
mute_all_button.pressed.connect(func():
28-
DJ.mute_all(true)
29-
if DJ.muted_music:
30-
old_music_volume = SoundManager.get_music_volume()
31-
SoundManager.set_music_volume(0);
32-
else:
33-
SoundManager.set_music_volume(old_music_volume);
34-
render())
35-
unmute_all_button.pressed.connect(func():
36-
DJ.mute_all(false)
37-
if DJ.muted_music:
38-
old_music_volume = SoundManager.get_music_volume()
39-
SoundManager.set_music_volume(0);
40-
else:
41-
SoundManager.set_music_volume(old_music_volume);
42-
render())
28+
sound_volume_slider.value_changed.connect(func(new_vol):
29+
SoundManager.set_sound_volume(new_vol))
4330

31+
mute_music_button.pressed.connect(mute_music)
32+
mute_sound_button.pressed.connect(mute_sound)
33+
mute_all_button.pressed.connect(mute_all)
34+
unmute_all_button.pressed.connect(unmute_all)
4435

4536
func render():
4637
if DJ.muted_music:
@@ -64,3 +55,47 @@ func render():
6455
else:
6556
unmute_all_button.hide()
6657
mute_all_button.grab_focus()
58+
59+
func update_music_volume():
60+
if DJ.muted_music:
61+
old_music_volume = SoundManager.get_music_volume()
62+
if old_music_volume < 1:
63+
old_music_volume = 0.3
64+
SoundManager.set_music_volume(0)
65+
music_volume_slider.set_value(0)
66+
else:
67+
SoundManager.set_music_volume(old_music_volume)
68+
music_volume_slider.set_value(old_music_volume)
69+
70+
func update_sound_volume():
71+
if DJ.muted_sound:
72+
old_sound_volume = SoundManager.get_sound_volume()
73+
if old_sound_volume < 1:
74+
old_sound_volume = 0.3
75+
SoundManager.set_sound_volume(0)
76+
sound_volume_slider.set_value(0)
77+
else:
78+
SoundManager.set_sound_volume(old_sound_volume)
79+
sound_volume_slider.set_value(old_sound_volume)
80+
81+
func mute_music():
82+
DJ.toggle_mute_music()
83+
update_music_volume()
84+
render()
85+
86+
func mute_sound():
87+
DJ.toggle_mute_sound()
88+
update_sound_volume()
89+
render()
90+
91+
func mute_all():
92+
DJ.mute_all(true)
93+
update_music_volume()
94+
update_sound_volume()
95+
render()
96+
97+
func unmute_all():
98+
DJ.mute_all(false)
99+
update_music_volume()
100+
update_sound_volume()
101+
render()

‎addons/dj/MuteButtonList.tscn

+82-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
[gd_scene load_steps=3 format=3 uid="uid://cpxj4aeuv006l"]
1+
[gd_scene load_steps=4 format=3 uid="uid://cpxj4aeuv006l"]
22

33
[ext_resource type="Script" path="res://addons/dj/MuteButtonList.gd" id="1_hivsh"]
44
[ext_resource type="Theme" uid="uid://cul4fuxbykddp" path="res://src/ui/BlueButtonTheme.tres" id="2_k4kf0"]
5+
[ext_resource type="FontFile" uid="uid://b1c3fty8httvk" path="res://addons/core/assets/fonts/at01.ttf" id="3_xcll7"]
56

67
[node name="MuteButtonList" type="VBoxContainer"]
78
anchors_preset = 15
@@ -11,6 +12,86 @@ grow_horizontal = 2
1112
grow_vertical = 2
1213
script = ExtResource("1_hivsh")
1314

15+
[node name="HBoxContainer" type="HBoxContainer" parent="."]
16+
custom_minimum_size = Vector2(0, 40)
17+
layout_mode = 2
18+
alignment = 1
19+
20+
[node name="CenterContainer" type="CenterContainer" parent="HBoxContainer"]
21+
layout_mode = 2
22+
23+
[node name="RichTextLabel" type="RichTextLabel" parent="HBoxContainer/CenterContainer"]
24+
clip_contents = false
25+
custom_minimum_size = Vector2(160, 0)
26+
layout_mode = 2
27+
theme_override_fonts/normal_font = ExtResource("3_xcll7")
28+
theme_override_font_sizes/normal_font_size = 32
29+
bbcode_enabled = true
30+
text = "[center]Music Volume"
31+
fit_content = true
32+
scroll_active = false
33+
34+
[node name="MarginContainer2" type="MarginContainer" parent="HBoxContainer"]
35+
visible = false
36+
custom_minimum_size = Vector2(20, 0)
37+
layout_mode = 2
38+
39+
[node name="CenterContainer2" type="CenterContainer" parent="HBoxContainer"]
40+
layout_mode = 2
41+
42+
[node name="MusicVolumeSlider" type="HSlider" parent="HBoxContainer/CenterContainer2"]
43+
unique_name_in_owner = true
44+
custom_minimum_size = Vector2(160, 0)
45+
layout_mode = 2
46+
size_flags_horizontal = 3
47+
max_value = 1.0
48+
step = 0.05
49+
50+
[node name="MarginContainer" type="MarginContainer" parent="HBoxContainer"]
51+
visible = false
52+
custom_minimum_size = Vector2(20, 0)
53+
layout_mode = 2
54+
55+
[node name="HBoxContainer2" type="HBoxContainer" parent="."]
56+
custom_minimum_size = Vector2(0, 40)
57+
layout_mode = 2
58+
alignment = 1
59+
60+
[node name="CenterContainer" type="CenterContainer" parent="HBoxContainer2"]
61+
layout_mode = 2
62+
63+
[node name="RichTextLabel" type="RichTextLabel" parent="HBoxContainer2/CenterContainer"]
64+
clip_contents = false
65+
custom_minimum_size = Vector2(160, 0)
66+
layout_mode = 2
67+
theme_override_fonts/normal_font = ExtResource("3_xcll7")
68+
theme_override_font_sizes/normal_font_size = 32
69+
bbcode_enabled = true
70+
text = "[center]Sound Volume"
71+
fit_content = true
72+
scroll_active = false
73+
74+
[node name="MarginContainer2" type="MarginContainer" parent="HBoxContainer2"]
75+
visible = false
76+
custom_minimum_size = Vector2(20, 0)
77+
layout_mode = 2
78+
79+
[node name="CenterContainer2" type="CenterContainer" parent="HBoxContainer2"]
80+
layout_mode = 2
81+
82+
[node name="SoundVolumeSlider" type="HSlider" parent="HBoxContainer2/CenterContainer2"]
83+
unique_name_in_owner = true
84+
custom_minimum_size = Vector2(160, 0)
85+
layout_mode = 2
86+
size_flags_horizontal = 3
87+
max_value = 1.0
88+
step = 0.05
89+
90+
[node name="MarginContainer" type="MarginContainer" parent="HBoxContainer2"]
91+
visible = false
92+
custom_minimum_size = Vector2(20, 0)
93+
layout_mode = 2
94+
1495
[node name="MuteMusicButton" type="Button" parent="."]
1596
unique_name_in_owner = true
1697
layout_mode = 2

‎default_bus_layout.tres

+12
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ bus/1/mute = false
77
bus/1/bypass_fx = false
88
bus/1/volume_db = -12.8786
99
bus/1/send = &"Master"
10+
bus/2/name = &"Sound"
11+
bus/2/solo = false
12+
bus/2/mute = false
13+
bus/2/bypass_fx = false
14+
bus/2/volume_db = -12.0527
15+
bus/2/send = &"Master"
16+
bus/3/name = &"UI Sound"
17+
bus/3/solo = false
18+
bus/3/mute = false
19+
bus/3/bypass_fx = false
20+
bus/3/volume_db = -12.0583
21+
bus/3/send = &"Master"

‎src/Sounds.gd

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ enum S {
7878
],
7979
}
8080

81+
func _ready():
82+
if not Engine.is_editor_hint():
83+
SoundManager.set_default_sound_bus("Sound")
84+
SoundManager.set_default_ui_sound_bus("UI Sound")
85+
SoundManager.set_sound_volume(0.3)
86+
87+
super._ready()
8188

8289
####################################################################
8390

‎src/menus/OptionsPanel.tscn

+8-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ layout_mode = 2
6464
theme = ExtResource("3_io088")
6565

6666
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/VBoxContainer/HBoxContainer/CenterContainer2/VBoxContainer2/PanelContainer"]
67-
custom_minimum_size = Vector2(244, 384)
6867
layout_mode = 2
6968
size_flags_horizontal = 3
7069
size_flags_vertical = 0
@@ -74,6 +73,8 @@ anim_duration = 0.5
7473

7574
[node name="ResetSaveDataButton" type="Button" parent="PanelContainer/VBoxContainer/HBoxContainer/CenterContainer2/VBoxContainer2/PanelContainer/VBoxContainer"]
7675
unique_name_in_owner = true
76+
visible = false
77+
modulate = Color(1, 1, 1, 0)
7778
custom_minimum_size = Vector2(200, 0)
7879
layout_mode = 2
7980
focus_neighbor_top = NodePath("../../../../../../../../MainMenuButton")
@@ -82,16 +83,22 @@ text = "Reset Save Data"
8283

8384
[node name="UnlockAllButton" type="Button" parent="PanelContainer/VBoxContainer/HBoxContainer/CenterContainer2/VBoxContainer2/PanelContainer/VBoxContainer"]
8485
unique_name_in_owner = true
86+
visible = false
87+
modulate = Color(1, 1, 1, 0)
8588
custom_minimum_size = Vector2(200, 0)
8689
layout_mode = 2
8790
theme = ExtResource("4_06n2g")
8891
text = "Unlock All Puzzles"
8992

9093
[node name="MarginContainer2" type="MarginContainer" parent="PanelContainer/VBoxContainer/HBoxContainer/CenterContainer2/VBoxContainer2/PanelContainer/VBoxContainer"]
94+
visible = false
95+
modulate = Color(1, 1, 1, 0)
9196
custom_minimum_size = Vector2(20, 20)
9297
layout_mode = 2
9398

9499
[node name="MuteButtonList" parent="PanelContainer/VBoxContainer/HBoxContainer/CenterContainer2/VBoxContainer2/PanelContainer/VBoxContainer" instance=ExtResource("5_bdku5")]
100+
visible = false
101+
modulate = Color(1, 1, 1, 0)
95102
layout_mode = 2
96103

97104
[node name="CenterContainer" type="CenterContainer" parent="PanelContainer/VBoxContainer/HBoxContainer"]

0 commit comments

Comments
 (0)
Please sign in to comment.