Skip to content

Commit

Permalink
feat: save audio settings
Browse files Browse the repository at this point in the history
  • Loading branch information
myin142 committed Sep 1, 2023
1 parent d905c38 commit d008eb3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
5 changes: 4 additions & 1 deletion godot/addons/menu-system/AudioSlider.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ var vol_offset = 15
func _ready():
master_id = AudioServer.get_bus_index(bus_name)
value_changed.connect(_volume_changed)
value = get_volume_percentage()
update()

if _has_button():
button.toggled.connect(func(mute):
AudioServer.set_bus_mute(master_id, mute)
_update_states()
)

func update():
value = get_volume_percentage()

func _has_button():
return button and button.toggle_mode

Expand Down
7 changes: 4 additions & 3 deletions godot/src/game_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ func _ready():

SceneManager.scene_loaded.connect(func():
points.add_scored_points()
_save_game()
#_save_game()
)

func _exit_tree():
points.add_scored_points()
_save_game()

func unlock_piece(p):
Expand All @@ -68,12 +69,12 @@ func unlock_piece(p):
if unlocked_pieces.size() == 0:
first_unlocked.emit()
unlocked_pieces.append(p)
_save_game()
#_save_game()

func _set_selected_piece(piece):
selected_piece = piece
selected_piece_changed.emit(piece)
_save_game()
#_save_game()

func get_piece_profile(piece):
if not piece in PIECE_PROFILE:
Expand Down
3 changes: 3 additions & 0 deletions godot/src/grid/slot.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,18 @@ script = ExtResource("2_arkt7")

[node name="ClickSound" type="AudioStreamPlayer" parent="."]
stream = ExtResource("3_iqce0")
bus = &"SFX"

[node name="SwipeSound" type="AudioStreamPlayer" parent="."]
stream = ExtResource("4_bo2sh")
volume_db = -20.0
pitch_scale = 0.7
bus = &"SFX"

[node name="WrongSound" type="AudioStreamPlayer" parent="."]
stream = ExtResource("5_002jb")
volume_db = -10.0
bus = &"SFX"

[connection signal="press_click" from="SwipeControl" to="." method="_on_swipe_control_press_click"]
[connection signal="press_released" from="SwipeControl" to="." method="_on_swipe_control_press_released"]
Expand Down
34 changes: 34 additions & 0 deletions godot/src/menu/settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,39 @@ extends CenterContainer

signal close()

const CONFIG_FILE = "user://settings.cfg"

@export var bgm: AudioSlider
@export var sfx: AudioSlider

@onready var _audio := $Audio

var _logger = Logger.new("Settings")
var _config = ConfigFile.new()

func _ready():
_load_settings()
bgm.update()
sfx.update()

func _load_settings():
var error = _config.load(CONFIG_FILE)
if error != OK:
_logger.error("Failed to load settings: %s" % error)
return

_logger.debug("Loading settings")
_audio.load_settings(_config)

func _exit_tree():
_save_config()

func _save_config():
if Env.is_web(): return

_logger.debug("Saving settings")
_audio.save_settings(_config)
_config.save(CONFIG_FILE)

func _on_texture_button_pressed():
close.emit()
34 changes: 19 additions & 15 deletions godot/src/menu/settings.tscn
Original file line number Diff line number Diff line change
@@ -1,58 +1,62 @@
[gd_scene load_steps=10 format=3 uid="uid://dc230wcy1bgpy"]
[gd_scene load_steps=11 format=3 uid="uid://dc230wcy1bgpy"]

[ext_resource type="Script" path="res://addons/menu-system/AudioSlider.gd" id="2_5nebq"]
[ext_resource type="Script" path="res://src/menu/settings.gd" id="2_n4qpr"]
[ext_resource type="StyleBox" uid="uid://bd0c76q5d7ocf" path="res://theme/dialog.tres" id="2_xd0ep"]
[ext_resource type="Texture2D" uid="uid://bdotu11c1jmm" path="res://assets/ui/close.svg" id="3_0f6sa"]
[ext_resource type="Script" path="res://src/menu/AudioSettings.gd" id="3_fgf21"]
[ext_resource type="Texture2D" uid="uid://dpmrhvpwm2x18" path="res://assets/ui/sound_music.svg" id="3_sr4w4"]
[ext_resource type="Texture2D" uid="uid://2ovcuoh360df" path="res://assets/ui/sound_music_mute.svg" id="4_d6t0m"]
[ext_resource type="Texture2D" uid="uid://lut7tqw0cix8" path="res://assets/ui/sound_sfx.svg" id="4_lna1p"]
[ext_resource type="Script" path="res://src/menu/UIButton.gd" id="4_lwv1e"]
[ext_resource type="Texture2D" uid="uid://dekt1q13mwogl" path="res://assets/ui/sound_sfx_mute.svg" id="7_feu51"]

[node name="Settings" type="CenterContainer"]
[node name="Settings" type="CenterContainer" node_paths=PackedStringArray("bgm", "sfx")]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("2_n4qpr")
bgm = NodePath("Audio/MarginContainer/VBoxContainer/GridContainer/HSlider2")
sfx = NodePath("Audio/MarginContainer/VBoxContainer/GridContainer/HSlider3")

[node name="PanelContainer" type="PanelContainer" parent="."]
[node name="Audio" type="PanelContainer" parent="."]
layout_mode = 2
theme_override_styles/panel = ExtResource("2_xd0ep")
script = ExtResource("3_fgf21")

[node name="MarginContainer" type="MarginContainer" parent="PanelContainer"]
[node name="MarginContainer" type="MarginContainer" parent="Audio"]
layout_mode = 2
theme_override_constants/margin_left = 50
theme_override_constants/margin_top = 50
theme_override_constants/margin_right = 50
theme_override_constants/margin_bottom = 50

[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer"]
[node name="VBoxContainer" type="VBoxContainer" parent="Audio/MarginContainer"]
layout_mode = 2
theme_override_constants/separation = 20

[node name="GridContainer" type="GridContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
[node name="GridContainer" type="GridContainer" parent="Audio/MarginContainer/VBoxContainer"]
custom_minimum_size = Vector2(500, 0)
layout_mode = 2
theme_override_constants/h_separation = 20
theme_override_constants/v_separation = 20
columns = 2

[node name="Master" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer/GridContainer"]
[node name="Master" type="Label" parent="Audio/MarginContainer/VBoxContainer/GridContainer"]
visible = false
layout_mode = 2
text = "Master "

[node name="HSlider" type="HSlider" parent="PanelContainer/MarginContainer/VBoxContainer/GridContainer"]
[node name="HSlider" type="HSlider" parent="Audio/MarginContainer/VBoxContainer/GridContainer"]
visible = false
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 4
script = ExtResource("2_5nebq")

[node name="BGM" type="TextureButton" parent="PanelContainer/MarginContainer/VBoxContainer/GridContainer"]
[node name="BGM" type="TextureButton" parent="Audio/MarginContainer/VBoxContainer/GridContainer"]
custom_minimum_size = Vector2(64, 64)
layout_mode = 2
toggle_mode = true
Expand All @@ -62,15 +66,15 @@ ignore_texture_size = true
stretch_mode = 5
script = ExtResource("4_lwv1e")

[node name="HSlider2" type="HSlider" parent="PanelContainer/MarginContainer/VBoxContainer/GridContainer" node_paths=PackedStringArray("button")]
[node name="HSlider2" type="HSlider" parent="Audio/MarginContainer/VBoxContainer/GridContainer" node_paths=PackedStringArray("button")]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 4
script = ExtResource("2_5nebq")
button = NodePath("../BGM")
bus_name = "BGM"

[node name="SFX" type="TextureButton" parent="PanelContainer/MarginContainer/VBoxContainer/GridContainer"]
[node name="SFX" type="TextureButton" parent="Audio/MarginContainer/VBoxContainer/GridContainer"]
custom_minimum_size = Vector2(64, 64)
layout_mode = 2
toggle_mode = true
Expand All @@ -80,27 +84,27 @@ ignore_texture_size = true
stretch_mode = 5
script = ExtResource("4_lwv1e")

[node name="HSlider3" type="HSlider" parent="PanelContainer/MarginContainer/VBoxContainer/GridContainer" node_paths=PackedStringArray("button")]
[node name="HSlider3" type="HSlider" parent="Audio/MarginContainer/VBoxContainer/GridContainer" node_paths=PackedStringArray("button")]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 4
script = ExtResource("2_5nebq")
button = NodePath("../SFX")
bus_name = "SFX"

[node name="MarginContainer2" type="MarginContainer" parent="PanelContainer"]
[node name="MarginContainer2" type="MarginContainer" parent="Audio"]
layout_mode = 2
size_flags_horizontal = 8
size_flags_vertical = 0
theme_override_constants/margin_top = -25
theme_override_constants/margin_right = -25

[node name="TextureButton" type="TextureButton" parent="PanelContainer/MarginContainer2"]
[node name="TextureButton" type="TextureButton" parent="Audio/MarginContainer2"]
custom_minimum_size = Vector2(50, 50)
layout_mode = 2
texture_normal = ExtResource("3_0f6sa")
ignore_texture_size = true
stretch_mode = 5
script = ExtResource("4_lwv1e")

[connection signal="pressed" from="PanelContainer/MarginContainer2/TextureButton" to="." method="_on_texture_button_pressed"]
[connection signal="pressed" from="Audio/MarginContainer2/TextureButton" to="." method="_on_texture_button_pressed"]

0 comments on commit d008eb3

Please sign in to comment.