diff options
author | George L. Albany <Megacake1234@gmail.com> | 2023-06-04 02:17:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-04 02:17:28 +0200 |
commit | 3236f51d700674aee6ee6572109ffb3d5fa5fed2 (patch) | |
tree | b898150a8a45e7c0d814459e7f12f39429acefeb /game/src/Game/Menu/OptionMenu/VolumeGrid.gd | |
parent | 73e29d02e48739aba5ca5db1b9575c67e795400f (diff) | |
parent | b98166b28c47cccff731d30959b8250fb27ff408 (diff) |
Merge pull request #130 from Spartan322/organize/godot-project
Diffstat (limited to 'game/src/Game/Menu/OptionMenu/VolumeGrid.gd')
-rw-r--r-- | game/src/Game/Menu/OptionMenu/VolumeGrid.gd | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/game/src/Game/Menu/OptionMenu/VolumeGrid.gd b/game/src/Game/Menu/OptionMenu/VolumeGrid.gd new file mode 100644 index 0000000..46613b4 --- /dev/null +++ b/game/src/Game/Menu/OptionMenu/VolumeGrid.gd @@ -0,0 +1,70 @@ +extends GridContainer + +const RATIO_FOR_LINEAR : float = 100 + +var _slider_dictionary : Dictionary + +var initial_focus : Control + +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_row(bus_name : String, bus_index : int) -> HSlider: + var volume_label := Label.new() + if bus_name == "Master": + volume_label.text = "MASTER_BUS" + else: + volume_label.text = bus_name + 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 + if not initial_focus: initial_focus = volume_slider + return volume_slider + +# REQUIREMENTS +# * UI-22 +func _ready(): + for bus_index in AudioServer.bus_count: + add_volume_row(AudioServer.get_bus_name(bus_index), bus_index) + +func _notification(what : int) -> void: + match(what): + NOTIFICATION_VISIBILITY_CHANGED: + if visible and is_inside_tree() and initial_focus: initial_focus.grab_focus() + +# REQUIREMENTS +# * UIFUN-30 +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_slider in _slider_dictionary.values(): + volume_slider.load_setting(load_file) + +# REQUIREMENTS +# * UIFUN-23 +func _on_options_menu_save_settings(save_file : ConfigFile): + for volume_slider in _slider_dictionary.values(): + volume_slider.save_setting(save_file) + + +func _on_options_menu_reset_settings(): + for volume_slider in _slider_dictionary.values(): + volume_slider.reset_setting() |