- Added options menu with Claude. Complete with keybindings and volume sliders.
This commit is contained in:
parent
8cb47464dd
commit
46cac1e3c7
9 changed files with 293 additions and 3 deletions
77
Autoloads/settings.gd
Executable file
77
Autoloads/settings.gd
Executable file
|
|
@ -0,0 +1,77 @@
|
|||
extends Node
|
||||
|
||||
# Autoloaded as "Settings", so any script can call e.g. Settings.save_settings().
|
||||
# Keeps player settings (key bindings and volume) and saves them to a file so
|
||||
# they're remembered between play sessions.
|
||||
|
||||
# user:// is a per-user folder Godot picks for save data. On Linux it's
|
||||
# ~/.local/share/godot/app_userdata/slime-platformer/
|
||||
const SAVE_PATH = "user://settings.cfg"
|
||||
|
||||
# Which input actions show up in the Controls tab. "pause" is left out on
|
||||
# purpose so the player can't lose the key that opens the menu.
|
||||
const REBINDABLE_ACTIONS = ["moveLeft", "moveRight", "moveUp", "moveDown", "jump"]
|
||||
|
||||
# Audio bus names, as set up in the Audio tab at the bottom of the editor.
|
||||
const VOLUME_BUSES = ["Master", "Music", "SFX"]
|
||||
|
||||
func _ready() -> void:
|
||||
load_settings()
|
||||
|
||||
func save_settings() -> void:
|
||||
var config := ConfigFile.new()
|
||||
|
||||
for action in REBINDABLE_ACTIONS:
|
||||
var keys := []
|
||||
for event in InputMap.action_get_events(action):
|
||||
if event is InputEventKey: keys.append(event.physical_keycode)
|
||||
config.set_value("controls", action, keys)
|
||||
|
||||
for bus in VOLUME_BUSES:
|
||||
config.set_value("audio", bus, get_volume(bus))
|
||||
|
||||
config.save(SAVE_PATH)
|
||||
|
||||
func load_settings() -> void:
|
||||
var config := ConfigFile.new()
|
||||
# No save file yet (first launch), so just keep the defaults.
|
||||
if config.load(SAVE_PATH) != OK: return
|
||||
|
||||
for action in REBINDABLE_ACTIONS:
|
||||
if config.has_section_key("controls", action):
|
||||
InputMap.action_erase_events(action)
|
||||
for keycode in config.get_value("controls", action):
|
||||
InputMap.action_add_event(action, make_key_event(keycode))
|
||||
|
||||
for bus in VOLUME_BUSES:
|
||||
set_volume(bus, config.get_value("audio", bus, 1.0))
|
||||
|
||||
# Replace one of an action's keys. index is which key it is in the action's list,
|
||||
# since actions can have more than one (like Left Arrow and A for moveLeft).
|
||||
# An index past the end of the list adds a new key instead.
|
||||
func rebind(action: String, index: int, keycode: Key) -> void:
|
||||
var events := InputMap.action_get_events(action)
|
||||
if index < events.size(): events[index] = make_key_event(keycode)
|
||||
else: events.append(make_key_event(keycode))
|
||||
# Re-add everything so the keys stay in the same order.
|
||||
InputMap.action_erase_events(action)
|
||||
for event in events:
|
||||
InputMap.action_add_event(action, event)
|
||||
|
||||
# Put every binding back to what's set in Project Settings > Input Map.
|
||||
func reset_controls() -> void:
|
||||
InputMap.load_from_project_settings()
|
||||
|
||||
func make_key_event(keycode: Key) -> InputEventKey:
|
||||
var event := InputEventKey.new()
|
||||
# Physical keycodes go by key position, so WASD stays in the same spot on
|
||||
# non-QWERTY keyboards (like AZERTY).
|
||||
event.physical_keycode = keycode
|
||||
return event
|
||||
|
||||
# Volume is linear: 0.0 is silent, 1.0 is full volume.
|
||||
func get_volume(bus: String) -> float:
|
||||
return AudioServer.get_bus_volume_linear(AudioServer.get_bus_index(bus))
|
||||
|
||||
func set_volume(bus: String, volume: float) -> void:
|
||||
AudioServer.set_bus_volume_linear(AudioServer.get_bus_index(bus), volume)
|
||||
1
Autoloads/settings.gd.uid
Executable file
1
Autoloads/settings.gd.uid
Executable file
|
|
@ -0,0 +1 @@
|
|||
uid://dvw2smqe1l8jr
|
||||
114
UI/options_menu.gd
Executable file
114
UI/options_menu.gd
Executable file
|
|
@ -0,0 +1,114 @@
|
|||
extends Control
|
||||
|
||||
# Options screen with a Controls tab (key rebinding) and a Sound tab (volume).
|
||||
# The rows in both tabs are built in code from the lists in Settings, so adding
|
||||
# a new action or audio bus there makes it show up here automatically.
|
||||
|
||||
signal closed
|
||||
|
||||
@onready var tabs = $CenterContainer/PanelContainer/VBoxContainer/TabContainer
|
||||
@onready var bindingList = $CenterContainer/PanelContainer/VBoxContainer/TabContainer/Controls/BindingList
|
||||
@onready var volumeList = $CenterContainer/PanelContainer/VBoxContainer/TabContainer/Sound/VolumeList
|
||||
|
||||
# The binding button waiting for a key press, or null if we're not waiting.
|
||||
var listeningButton: Button = null
|
||||
|
||||
func _ready() -> void:
|
||||
hide()
|
||||
build_binding_rows()
|
||||
build_volume_rows()
|
||||
|
||||
func open() -> void:
|
||||
show()
|
||||
tabs.current_tab = 0
|
||||
tabs.get_tab_bar().grab_focus()
|
||||
|
||||
func close() -> void:
|
||||
stop_listening()
|
||||
hide()
|
||||
Settings.save_settings()
|
||||
closed.emit()
|
||||
|
||||
func build_binding_rows() -> void:
|
||||
for child in bindingList.get_children():
|
||||
child.queue_free()
|
||||
|
||||
for action in Settings.REBINDABLE_ACTIONS:
|
||||
var label := Label.new()
|
||||
label.text = action.capitalize() # "moveLeft" -> "Move Left"
|
||||
label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||||
bindingList.add_child(label)
|
||||
|
||||
# Two key slots per action (the other grid column is the label).
|
||||
# An empty slot shows "-" and can be clicked to add a second key.
|
||||
for index in range(bindingList.columns - 1):
|
||||
var button := Button.new()
|
||||
button.custom_minimum_size.x = 90
|
||||
button.text = key_text(action, index)
|
||||
button.pressed.connect(start_listening.bind(button, action, index))
|
||||
bindingList.add_child(button)
|
||||
|
||||
func key_text(action: String, index: int) -> String:
|
||||
var events = InputMap.action_get_events(action)
|
||||
if index < events.size() and events[index] is InputEventKey:
|
||||
return events[index].as_text_physical_keycode()
|
||||
return "-"
|
||||
|
||||
func build_volume_rows() -> void:
|
||||
for bus in Settings.VOLUME_BUSES:
|
||||
var label := Label.new()
|
||||
label.text = bus
|
||||
volumeList.add_child(label)
|
||||
|
||||
var slider := HSlider.new()
|
||||
slider.min_value = 0
|
||||
slider.max_value = 100
|
||||
slider.step = 5
|
||||
slider.value = Settings.get_volume(bus) * 100
|
||||
slider.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||||
slider.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||||
volumeList.add_child(slider)
|
||||
|
||||
var percentLabel := Label.new()
|
||||
percentLabel.custom_minimum_size.x = 40
|
||||
percentLabel.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
|
||||
percentLabel.text = "%d%%" % slider.value
|
||||
volumeList.add_child(percentLabel)
|
||||
|
||||
slider.value_changed.connect(_on_volume_changed.bind(bus, percentLabel))
|
||||
|
||||
func _on_volume_changed(value: float, bus: String, percentLabel: Label) -> void:
|
||||
Settings.set_volume(bus, value / 100)
|
||||
percentLabel.text = "%d%%" % value
|
||||
|
||||
func start_listening(button: Button, action: String, index: int) -> void:
|
||||
stop_listening()
|
||||
listeningButton = button
|
||||
listeningButton.set_meta("action", action)
|
||||
listeningButton.set_meta("index", index)
|
||||
listeningButton.text = "Press a key..."
|
||||
|
||||
func stop_listening() -> void:
|
||||
if listeningButton == null: return
|
||||
listeningButton.text = key_text(listeningButton.get_meta("action"), listeningButton.get_meta("index"))
|
||||
listeningButton = null
|
||||
|
||||
# _input runs before the buttons and the pause menu see the key, so while we're
|
||||
# waiting for a new binding, the key gets used here and nowhere else.
|
||||
func _input(event: InputEvent) -> void:
|
||||
if listeningButton == null: return
|
||||
if not (event is InputEventKey and event.pressed and not event.echo): return
|
||||
|
||||
get_viewport().set_input_as_handled()
|
||||
# Escape cancels instead of being bound.
|
||||
if event.physical_keycode != KEY_ESCAPE:
|
||||
Settings.rebind(listeningButton.get_meta("action"), listeningButton.get_meta("index"), event.physical_keycode)
|
||||
stop_listening()
|
||||
|
||||
func _on_reset_controls_button_pressed() -> void:
|
||||
stop_listening()
|
||||
Settings.reset_controls()
|
||||
build_binding_rows()
|
||||
|
||||
func _on_back_button_pressed() -> void:
|
||||
close()
|
||||
1
UI/options_menu.gd.uid
Executable file
1
UI/options_menu.gd.uid
Executable file
|
|
@ -0,0 +1 @@
|
|||
uid://b535wkmy72axq
|
||||
65
UI/options_menu.tscn
Executable file
65
UI/options_menu.tscn
Executable file
|
|
@ -0,0 +1,65 @@
|
|||
[gd_scene format=3]
|
||||
|
||||
[ext_resource type="Script" path="res://UI/options_menu.gd" id="1_options"]
|
||||
|
||||
[node name="OptionsMenu" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_options")
|
||||
|
||||
[node name="CenterContainer" type="CenterContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="PanelContainer" type="PanelContainer" parent="CenterContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer/PanelContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Title" type="Label" parent="CenterContainer/PanelContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Options"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="TabContainer" type="TabContainer" parent="CenterContainer/PanelContainer/VBoxContainer"]
|
||||
custom_minimum_size = Vector2(360, 220)
|
||||
layout_mode = 2
|
||||
current_tab = 0
|
||||
|
||||
[node name="Controls" type="VBoxContainer" parent="CenterContainer/PanelContainer/VBoxContainer/TabContainer"]
|
||||
layout_mode = 2
|
||||
metadata/_tab_index = 0
|
||||
|
||||
[node name="BindingList" type="GridContainer" parent="CenterContainer/PanelContainer/VBoxContainer/TabContainer/Controls"]
|
||||
layout_mode = 2
|
||||
columns = 3
|
||||
|
||||
[node name="ResetControlsButton" type="Button" parent="CenterContainer/PanelContainer/VBoxContainer/TabContainer/Controls"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 8
|
||||
text = "Reset to Defaults"
|
||||
|
||||
[node name="Sound" type="VBoxContainer" parent="CenterContainer/PanelContainer/VBoxContainer/TabContainer"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
metadata/_tab_index = 1
|
||||
|
||||
[node name="VolumeList" type="GridContainer" parent="CenterContainer/PanelContainer/VBoxContainer/TabContainer/Sound"]
|
||||
layout_mode = 2
|
||||
columns = 3
|
||||
|
||||
[node name="BackButton" type="Button" parent="CenterContainer/PanelContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Back"
|
||||
|
||||
[connection signal="pressed" from="CenterContainer/PanelContainer/VBoxContainer/TabContainer/Controls/ResetControlsButton" to="." method="_on_reset_controls_button_pressed"]
|
||||
[connection signal="pressed" from="CenterContainer/PanelContainer/VBoxContainer/BackButton" to="." method="_on_back_button_pressed"]
|
||||
|
|
@ -5,14 +5,19 @@ extends CanvasLayer
|
|||
# running while the rest of the game is paused. Without that, the menu would
|
||||
# freeze along with everything else and you could never unpause.
|
||||
|
||||
@onready var mainPanel = $CenterContainer
|
||||
@onready var resumeButton = $CenterContainer/PanelContainer/VBoxContainer/ResumeButton
|
||||
@onready var optionsButton = $CenterContainer/PanelContainer/VBoxContainer/OptionsButton
|
||||
@onready var optionsMenu = $OptionsMenu
|
||||
|
||||
func _ready() -> void:
|
||||
hide()
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("pause"):
|
||||
if visible: resume()
|
||||
# From the options screen, Escape goes back a step instead of unpausing.
|
||||
if optionsMenu.visible: optionsMenu.close()
|
||||
elif visible: resume()
|
||||
else: pause()
|
||||
# Stop anything else from also reacting to this key press.
|
||||
get_viewport().set_input_as_handled()
|
||||
|
|
@ -31,8 +36,12 @@ func _on_resume_button_pressed() -> void:
|
|||
resume()
|
||||
|
||||
func _on_options_button_pressed() -> void:
|
||||
# TODO: Open an options screen (volume, fullscreen, controls, etc).
|
||||
print("Options menu not made yet!")
|
||||
mainPanel.hide()
|
||||
optionsMenu.open()
|
||||
|
||||
func _on_options_menu_closed() -> void:
|
||||
mainPanel.show()
|
||||
optionsButton.grab_focus()
|
||||
|
||||
func _on_return_to_menu_button_pressed() -> void:
|
||||
# TODO: Once there's a main menu scene, unpause and switch to it, e.g.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
[gd_scene format=3]
|
||||
|
||||
[ext_resource type="Script" path="res://UI/pause_menu.gd" id="1_pause"]
|
||||
[ext_resource type="PackedScene" path="res://UI/options_menu.tscn" id="2_options"]
|
||||
|
||||
[node name="PauseMenu" type="CanvasLayer"]
|
||||
process_mode = 3
|
||||
|
|
@ -50,7 +51,10 @@ text = "Return to Menu"
|
|||
layout_mode = 2
|
||||
text = "Exit Game"
|
||||
|
||||
[node name="OptionsMenu" parent="." instance=ExtResource("2_options")]
|
||||
|
||||
[connection signal="pressed" from="CenterContainer/PanelContainer/VBoxContainer/ResumeButton" to="." method="_on_resume_button_pressed"]
|
||||
[connection signal="pressed" from="CenterContainer/PanelContainer/VBoxContainer/OptionsButton" to="." method="_on_options_button_pressed"]
|
||||
[connection signal="pressed" from="CenterContainer/PanelContainer/VBoxContainer/ReturnToMenuButton" to="." method="_on_return_to_menu_button_pressed"]
|
||||
[connection signal="pressed" from="CenterContainer/PanelContainer/VBoxContainer/ExitButton" to="." method="_on_exit_button_pressed"]
|
||||
[connection signal="closed" from="OptionsMenu" to="." method="_on_options_menu_closed"]
|
||||
|
|
|
|||
15
default_bus_layout.tres
Executable file
15
default_bus_layout.tres
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
[gd_resource type="AudioBusLayout" format=3]
|
||||
|
||||
[resource]
|
||||
bus/1/name = &"Music"
|
||||
bus/1/solo = false
|
||||
bus/1/mute = false
|
||||
bus/1/bypass_fx = false
|
||||
bus/1/volume_db = 0.0
|
||||
bus/1/send = &"Master"
|
||||
bus/2/name = &"SFX"
|
||||
bus/2/solo = false
|
||||
bus/2/mute = false
|
||||
bus/2/bypass_fx = false
|
||||
bus/2/volume_db = 0.0
|
||||
bus/2/send = &"Master"
|
||||
|
|
@ -15,6 +15,10 @@ run/main_scene="uid://co1lfi5fpj54e"
|
|||
config/features=PackedStringArray("4.7", "GL Compatibility")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[autoload]
|
||||
|
||||
Settings="*res://Autoloads/settings.gd"
|
||||
|
||||
[display]
|
||||
|
||||
window/size/viewport_width=640
|
||||
|
|
|
|||
Loading…
Reference in a new issue