aboutsummaryrefslogtreecommitdiff
path: root/game/src/OptionMenu/VolumeGrid.gd
diff options
context:
space:
mode:
author Spartan322 <Megacake1234@gmail.com>2023-02-10 10:18:46 +0100
committer Spartan322 <Megacake1234@gmail.com>2023-02-10 10:31:28 +0100
commit3798205c740e7e2faf2594866cb497260012508c (patch)
tree4ca4a0835cb833fbba1983f0e8de5fa66227b86e /game/src/OptionMenu/VolumeGrid.gd
parent6525b89a37a31eaf88182b11410bd46b6658e297 (diff)
Implement a usable settings UI, should fulfill:
SS-58, SS-61, SS-6, SS-9, SS-10, SS-11, SS-13 UI-11, UI-12, UI-19, UI-44, UI-47, UI-22
Diffstat (limited to 'game/src/OptionMenu/VolumeGrid.gd')
-rw-r--r--game/src/OptionMenu/VolumeGrid.gd54
1 files changed, 54 insertions, 0 deletions
diff --git a/game/src/OptionMenu/VolumeGrid.gd b/game/src/OptionMenu/VolumeGrid.gd
new file mode 100644
index 0000000..fae5ff6
--- /dev/null
+++ b/game/src/OptionMenu/VolumeGrid.gd
@@ -0,0 +1,54 @@
+extends GridContainer
+
+const RATIO_FOR_LINEAR = 100
+
+var _slider_dictionary := {}
+
+func get_db_as_volume_value(db : float) -> float:
+ # db_to_linear produces a float between 0 and 1 from a db value
+ return db_to_linear(db) * RATIO_FOR_LINEAR
+
+func get_volume_value_as_db(value : float) -> float:
+ # linear_to_db consumes a float between 0 and 1 to produce the db value
+ return linear_to_db(value / RATIO_FOR_LINEAR)
+
+func add_volume_column(bus_name : StringName, bus_index : int) -> HSlider:
+ var volume_label := Label.new()
+ volume_label.text = bus_name + " Volume"
+ add_child(volume_label)
+
+ var volume_slider := SettingHSlider.new()
+ volume_slider.section_name = "Audio"
+ volume_slider.setting_name = volume_label.text
+ volume_slider.custom_minimum_size = Vector2(290, 0)
+ volume_slider.size_flags_vertical = Control.SIZE_FILL
+ volume_slider.min_value = 0
+ volume_slider.default_value = 100
+ volume_slider.max_value = 120 # 120 so volume can be boosted somewhat
+ volume_slider.value_changed.connect(_on_slider_value_changed.bind(bus_index))
+ add_child(volume_slider)
+
+ _slider_dictionary[volume_label.text] = volume_slider
+ return volume_slider
+
+func _ready():
+ for bus_index in AudioServer.bus_count:
+ add_volume_column(AudioServer.get_bus_name(bus_index), bus_index)
+
+func _on_slider_value_changed(value : float, bus_index : int) -> void:
+ AudioServer.set_bus_volume_db(bus_index, get_volume_value_as_db(value))
+
+
+func _on_options_menu_load_settings(load_file : ConfigFile):
+ for volume_label_text in _slider_dictionary:
+ _slider_dictionary[volume_label_text].load_setting(load_file)
+
+
+func _on_options_menu_save_settings(save_file : ConfigFile):
+ for volume_label_text in _slider_dictionary:
+ _slider_dictionary[volume_label_text].save_setting(save_file)
+
+
+func _on_options_menu_reset_settings():
+ for volume_label_text in _slider_dictionary:
+ _slider_dictionary[volume_label_text].reset_setting()