SlimePlatformer/UI/options_menu.gd

114 lines
3.8 KiB
GDScript
Executable file

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()