Skip to content

Commit

Permalink
Merge pull request #121 from Maaack/audio-input-option
Browse files Browse the repository at this point in the history
Audio input option
  • Loading branch information
Maaack authored Jul 11, 2024
2 parents 2211e86 + 176a82f commit 7a1140b
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
class_name ListOptionControl
extends OptionControl

## Locks Option Titles from auto-updating when editing Option Values.
## Intentionally put first for initialization.
@export var lock_titles : bool = false
## Defines the list of possible values for the variable
## this option stores in the config file.
@export var option_values : Array :
set(value) :
option_values = value
_on_option_values_changed()

## Defines the list of options displayed to the user.
## Length should match with Option Values.
@export var option_titles : Array[String] :
set(value):
option_titles = value
if is_inside_tree():
_set_option_list(option_titles)

@export var lock_titles : bool = false
var custom_option_values : Array

func _on_option_values_changed():
Expand Down Expand Up @@ -67,6 +73,7 @@ func disable_option(option_index : int, disabled : bool = true):
%OptionButton.set_item_disabled(option_index, disabled)

func _ready():
lock_titles = lock_titles
option_titles = option_titles
option_values = option_values
super._ready()
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

[node name="OptionControl" instance=ExtResource("1_blo3b")]
script = ExtResource("2_kt4vl")
lock_titles = false
option_values = []
option_titles = []
lock_titles = false

[node name="OptionButton" type="OptionButton" parent="." index="1"]
unique_name_in_owner = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,41 @@ const OptionSectionNames : Dictionary = {
OptionSections.CUSTOM : AppSettings.CUSTOM_SECTION,
}

## Locks config names in case of issues with inherited scenes.
## Intentionally put first for initialization.
@export var lock_config_names : bool = false
## Defines text displayed to the user.
@export var option_name : String :
set(value):
var _update_config : bool = option_name.to_pascal_case() == key
var _update_config : bool = option_name.to_pascal_case() == key and not lock_config_names
option_name = value
if is_inside_tree():
%OptionLabel.text = "%s%s" % [option_name, label_suffix]
if _update_config:
key = option_name.to_pascal_case()

## Defines what section in the config file this option belongs under.
@export var option_section : OptionSections :
set(value):
var _update_config : bool = OptionSectionNames[option_section] == section
var _update_config : bool = OptionSectionNames[option_section] == section and not lock_config_names
option_section = value
if _update_config:
section = OptionSectionNames[option_section]

@export_group("Config Names")
## Defines the key for this option variable in the config file.
@export var key : String
## Defines the section for this option variable in the config file.
@export var section : String
@export_group("Format")
@export var label_suffix : String = " :"
@export_group("Properties")
## Defines whether the option is editable, or only visible by the user.
@export var editable : bool = true : set = set_editable
## Defines what kind of variable this option stores in the config file.
@export var property_type : Variant.Type = TYPE_BOOL

## It is advised to use an external editor to set the default value in the scene file.
## Godot can experience a bug (caching issue?) that may undo changes.
var default_value
var _connected_nodes : Array

Expand Down Expand Up @@ -101,8 +111,11 @@ func set_editable(value : bool = true):
node.editable = editable

func _ready():
lock_config_names = lock_config_names
option_section = option_section
option_name = option_name
property_type = property_type
default_value = default_value
set_value(_get_setting(default_value))
for child in get_children():
_connect_option_inputs(child)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
extends ListOptionControl

func _set_input_device():
var current_setting = _get_setting(default_value)
if current_setting is bool:
current_setting = &"Default"
AudioServer.input_device = _get_setting(default_value)

func _add_microphone_audio_stream() -> void:
var instance = AudioStreamPlayer.new()
instance.stream = AudioStreamMicrophone.new()
instance.autoplay = true
add_child.call_deferred(instance)
instance.ready.connect(_set_input_device)

func _ready():
if ProjectSettings.get_setting("audio/driver/enable_input", false):
if AudioServer.input_device.is_empty():
_add_microphone_audio_stream()
else:
_set_input_device()
if not Engine.is_editor_hint():
option_values = AudioServer.get_input_device_list()
else:
hide()
super._ready()

func _on_setting_changed(value):
if value >= option_values.size(): return
AudioServer.input_device = option_values[value]
super._on_setting_changed(value)

func _value_title_map(value : Variant) -> String:
if value is String:
return value
else:
return super._value_title_map(value)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[gd_scene load_steps=3 format=3 uid="uid://dxj1gsxtlp6v0"]

[ext_resource type="PackedScene" uid="uid://b6bl3n5mp3m1e" path="res://addons/maaacks_game_template/base/scenes/Menus/OptionsMenu/OptionControl/ListOptionControl.tscn" id="1_0vgeo"]
[ext_resource type="Script" path="res://addons/maaacks_game_template/examples/scenes/Menus/OptionsMenu/Audio/AudioInputOptionControl.gd" id="2_6qeue"]

[node name="AudioInputOptionControl" instance=ExtResource("1_0vgeo")]
script = ExtResource("2_6qeue")
option_name = "Input Device"
option_section = 2
key = "InputDevice"
section = "AudioSettings"
property_type = 4
default_value = "Default"

[node name="OptionLabel" parent="." index="0"]
text = "Input Device :"

[node name="OptionButton" parent="." index="1"]
size_flags_horizontal = 3
text_overrun_behavior = 1
clip_text = true
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://dmgla7rq1g2cc"]
[gd_scene load_steps=4 format=3 uid="uid://dmgla7rq1g2cc"]

[ext_resource type="PackedScene" uid="uid://c8vnncjwqcpab" path="res://addons/maaacks_game_template/base/scenes/Menus/OptionsMenu/Audio/AudioOptionsMenu.tscn" id="1_ro573"]
[ext_resource type="Script" path="res://addons/maaacks_game_template/examples/scenes/Menus/OptionsMenu/Audio/AudioOptionsMenu.gd" id="2_g4nfi"]
[ext_resource type="PackedScene" uid="uid://dxj1gsxtlp6v0" path="res://scenes/Menus/OptionsMenu/Audio/AudioInputOptionControl.tscn" id="3_tfrh8"]

[node name="Audio" instance=ExtResource("1_ro573")]
script = ExtResource("2_g4nfi")

[node name="AudioInputOptionControl" parent="VBoxContainer" index="2" instance=ExtResource("3_tfrh8")]
layout_mode = 2
37 changes: 37 additions & 0 deletions scenes/Menus/OptionsMenu/Audio/AudioInputOptionControl.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
extends ListOptionControl

func _set_input_device():
var current_setting = _get_setting(default_value)
if current_setting is bool:
current_setting = &"Default"
AudioServer.input_device = _get_setting(default_value)

func _add_microphone_audio_stream() -> void:
var instance = AudioStreamPlayer.new()
instance.stream = AudioStreamMicrophone.new()
instance.autoplay = true
add_child.call_deferred(instance)
instance.ready.connect(_set_input_device)

func _ready():
if ProjectSettings.get_setting("audio/driver/enable_input", false):
if AudioServer.input_device.is_empty():
_add_microphone_audio_stream()
else:
_set_input_device()
if not Engine.is_editor_hint():
option_values = AudioServer.get_input_device_list()
else:
hide()
super._ready()

func _on_setting_changed(value):
if value >= option_values.size(): return
AudioServer.input_device = option_values[value]
super._on_setting_changed(value)

func _value_title_map(value : Variant) -> String:
if value is String:
return value
else:
return super._value_title_map(value)
21 changes: 21 additions & 0 deletions scenes/Menus/OptionsMenu/Audio/AudioInputOptionControl.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[gd_scene load_steps=3 format=3 uid="uid://dxj1gsxtlp6v0"]

[ext_resource type="PackedScene" uid="uid://b6bl3n5mp3m1e" path="res://addons/maaacks_game_template/base/scenes/Menus/OptionsMenu/OptionControl/ListOptionControl.tscn" id="1_0vgeo"]
[ext_resource type="Script" path="res://addons/maaacks_game_template/examples/scenes/Menus/OptionsMenu/Audio/AudioInputOptionControl.gd" id="2_6qeue"]

[node name="AudioInputOptionControl" instance=ExtResource("1_0vgeo")]
script = ExtResource("2_6qeue")
option_name = "Input Device"
option_section = 2
key = "InputDevice"
section = "AudioSettings"
property_type = 4
default_value = "Default"

[node name="OptionLabel" parent="." index="0"]
text = "Input Device :"

[node name="OptionButton" parent="." index="1"]
size_flags_horizontal = 3
text_overrun_behavior = 1
clip_text = true
12 changes: 8 additions & 4 deletions scenes/Menus/OptionsMenu/Audio/AudioOptionsMenu.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://c8cehdleoso6q"]

[ext_resource type="PackedScene" path="res://addons/maaacks_game_template/base/scenes/Menus/OptionsMenu/Audio/AudioOptionsMenu.tscn" id="1_7b5yq"]
[ext_resource type="Script" path="res://scenes/Menus/OptionsMenu/Audio/AudioOptionsMenu.gd" id="2_17u6b"]
[ext_resource type="PackedScene" uid="uid://c8vnncjwqcpab" path="res://addons/maaacks_game_template/base/scenes/Menus/OptionsMenu/Audio/AudioOptionsMenu.tscn" id="1_ro573"]
[ext_resource type="Script" path="res://addons/maaacks_game_template/examples/scenes/Menus/OptionsMenu/Audio/AudioOptionsMenu.gd" id="2_g4nfi"]
[ext_resource type="PackedScene" uid="uid://dxj1gsxtlp6v0" path="res://addons/maaacks_game_template/examples/scenes/Menus/OptionsMenu/Audio/AudioInputOptionControl.tscn" id="3_tfrh8"]

[node name="Audio" instance=ExtResource("1_7b5yq")]
script = ExtResource("2_17u6b")
[node name="Audio" instance=ExtResource("1_ro573")]
script = ExtResource("2_g4nfi")

[node name="AudioInputOptionControl" parent="VBoxContainer" index="2" instance=ExtResource("3_tfrh8")]
layout_mode = 2

0 comments on commit 7a1140b

Please sign in to comment.