A10: Menus and settings

The game now opens on a main menu when it has a window: practice
against bots (count and skill), join a server (by address, or from a
LAN search list), settings, and quit. In a match, Escape brings up a
pause menu (the match goes on) with settings and leave; when a match
the player was in ends, a results screen shows their place,
eliminations and the last of the feed.

- KeyBindings: every action rebindable to a key or mouse button.
  bind() refuses an input another action uses and names it; Escape is
  reserved. ClientInput reads keys through the bindings.
- GameSettings: bindings, mouse, graphics (quality preset and the
  print pass), audio (master, music and effects buses), display
  (window mode, vsync, frame cap), name, last server and practice
  choices, saved in user://settings.cfg. Missing, unreadable or
  corrupt files and bad values fall back to defaults, value by value;
  hand-edited key clashes are repaired on load.
- Menus (src/ui): paper-and-ink panels, brass focus edge, keyboard
  navigable; a drawn sea-and-tide-post backdrop, or an ink wash over
  a match. AppShell runs menus and the session.
- LocalServer takes a bot difficulty.
- make menu-captures renders every screen.

Closes #50

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 11:30:09 -05:00
co-authored by Claude Opus 5.5
parent 386653f782
commit c504551b28
39 changed files with 2022 additions and 50 deletions
+594
View File
@@ -0,0 +1,594 @@
## The game's menus (A10), in the paper-and-ink motifs of docs/STYLE.md: the main menu,
## practice against bots, joining a server (with the LAN and list-server browser),
## settings (controls, mouse, graphics, audio, display), the pause menu and the results.
##
## Menus only edit `settings` and say what the player asked for through signals; the
## program (Main) starts and leaves matches. Every screen can be driven from the
## keyboard: each grabs focus on its first control when shown, and Escape goes back.
## show_screen() and the buttons' `pressed` signals are what the UI test drives.
class_name Menus
extends Control
signal practice_requested(bots: int, difficulty: int)
signal join_requested(host: String, port: int, player_name: String)
signal resume_requested
signal leave_requested
signal quit_requested
signal settings_changed
const SCREENS: Array[String] = ["main", "practice", "join", "settings", "pause", "results"]
const PANEL_WIDTH: float = 560.0
const FONT_TITLE: int = 40
const FONT_HEADING: int = 24
const FONT_BODY: int = 17
const FONT_SMALL: int = 14
## Seconds a server search listens for answers.
const SEARCH_SECONDS: float = 2.0
var settings: GameSettings = GameSettings.new()
## Where settings are saved; tests point it somewhere harmless.
var settings_path: String = GameSettings.DEFAULT_PATH
## Screen name -> its panel.
var screens: Dictionary = {}
## The screen shown, and the ones Back returns through.
var current: String = ""
var _history: Array[String] = []
## Controls tab: the action waiting for a key, or "".
var listening_for: String = ""
var _binding_buttons: Dictionary = {}
var _binding_note: Label
## Practice.
var _bots: SpinBox
var _difficulty: OptionButton
## Join.
var _name: LineEdit
var _address: LineEdit
var _servers: ItemList
var _found: Array[Dictionary] = []
var _join_note: Label
var _browser: ServerBrowser = null
var _search_left: float = 0.0
## Settings.
var _tabs: TabContainer
var _sensitivity: HSlider
var _aim_scale: HSlider
var _invert: CheckButton
var _quality: OptionButton
var _print_pass: CheckButton
var _volumes: Dictionary = {}
var _window_mode: OptionButton
var _vsync: CheckButton
var _max_fps: OptionButton
## Results.
var _results_title: Label
var _results_body: Label
## The backdrop drawn behind the menus when no match is showing.
var backdrop: MenuBackdrop
## Over a match, a wash of ink instead, so the match shows through.
var shade: ColorRect
## True while a match is being played under the menus (AppShell sets it).
var over_match: bool = false:
set(value):
over_match = value
backdrop.visible = not value
shade.visible = value
func _init() -> void:
name = "Menus"
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
theme = MenuTheme.make()
backdrop = MenuBackdrop.new()
backdrop.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
add_child(backdrop)
shade = ColorRect.new()
shade.color = Color(Palette.INK, 0.45)
shade.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
shade.mouse_filter = Control.MOUSE_FILTER_IGNORE
shade.visible = false
add_child(shade)
_build_main()
_build_practice()
_build_join()
_build_settings()
_build_pause()
_build_results()
for screen: String in screens:
(screens[screen] as Control).visible = false
## Puts `with` in the menus and refreshes every control from it.
func use_settings(with: GameSettings, path: String = GameSettings.DEFAULT_PATH) -> void:
settings = with
settings_path = path
_refresh_controls()
## Shows `screen`, remembering the one before for Back. `remember` false replaces
## the history (the main, pause and results screens start afresh).
func show_screen(screen: String, remember: bool = true) -> void:
assert(screens.has(screen), "no screen called %s" % screen)
if remember and not current.is_empty() and current != screen:
_history.append(current)
elif not remember:
_history.clear()
if not current.is_empty():
(screens[current] as Control).visible = false
current = screen
listening_for = ""
visible = true
var panel: Control = screens[screen]
panel.visible = true
if screen == "settings" or screen == "practice" or screen == "join":
_refresh_controls()
if is_inside_tree():
first_focus(screen).grab_focus()
## Back a screen; from the first one, the pause menu resumes and others stay.
func back() -> void:
if not listening_for.is_empty():
listening_for = ""
_binding_note.text = "Left as it was."
_refresh_bindings()
return
if current == "settings":
save_settings()
if _history.is_empty():
if current == "pause":
resume_requested.emit()
return
var previous: String = _history.pop_back()
(screens[current] as Control).visible = false
current = ""
show_screen(previous)
## Hides every screen (a match is being played).
func close() -> void:
if not current.is_empty():
(screens[current] as Control).visible = false
current = ""
_history.clear()
listening_for = ""
visible = false
## The control a screen focuses first.
func first_focus(screen: String) -> Control:
var panel: Control = screens[screen]
return panel.get_meta("first") as Control
## The buttons on a screen, by their text, for tests and for keyboard focus order.
func buttons_of(screen: String) -> Dictionary:
var found: Dictionary = {}
_collect_buttons(screens[screen] as Node, found)
return found
## Shows the results of a match: `place` (0 when unknown), out of `total`, with
## `eliminations`, and the last lines of the feed.
func show_results(place: int, total: int, eliminations: int, lines: Array[String]) -> void:
if place == 1:
_results_title.text = "Last on dry ground"
elif place > 0:
_results_title.text = "Placed %d of %d" % [place, total]
else:
_results_title.text = "The match is over"
var body: Array[String] = ["Eliminations: %d" % eliminations]
if not lines.is_empty():
body.append("")
body.append_array(lines)
_results_body.text = "\n".join(body)
show_screen("results", false)
func save_settings() -> Error:
var error: Error = settings.save(settings_path)
settings_changed.emit()
return error
func _process(delta: float) -> void:
if _browser == null:
return
_browser.poll()
_search_left -= delta
if _search_left <= 0.0:
_finish_search()
func _unhandled_input(event: InputEvent) -> void:
if not visible or current.is_empty():
return
if not listening_for.is_empty():
var code: int = KeyBindings.code_of_event(event)
if code == KEY_ESCAPE:
back()
get_viewport().set_input_as_handled()
elif code != 0:
try_bind(listening_for, code)
get_viewport().set_input_as_handled()
return
if event.is_action_pressed("ui_cancel"):
back()
get_viewport().set_input_as_handled()
## Starts waiting for the next key or mouse button for `action`.
func listen_for(action: String) -> void:
listening_for = action
(_binding_buttons[action] as Button).text = "Press a key..."
_binding_note.text = "Press the new key or mouse button for %s, or Escape to leave it." % KeyBindings.label_of(action)
## Binds `action` to `code`, saying why not when it can't. Returns bind()'s answer.
func try_bind(action: String, code: int) -> String:
var answer: String = settings.bindings.bind(action, code)
listening_for = ""
if answer.is_empty():
_binding_note.text = "%s: %s." % [KeyBindings.label_of(action), KeyBindings.describe(code)]
save_settings()
elif answer == "reserved":
_binding_note.text = "%s is kept for the menus." % KeyBindings.describe(code)
elif KeyBindings.has_action(answer):
_binding_note.text = "%s is already used for %s. Change that first." % [KeyBindings.describe(code), KeyBindings.label_of(answer)]
else:
_binding_note.text = "That can't be bound."
_refresh_bindings()
return answer
# --- Building the screens ------------------------------------------------------------
func _panel(screen: String, title: String) -> VBoxContainer:
var panel: PanelContainer = PanelContainer.new()
panel.name = screen.capitalize()
panel.set_anchors_and_offsets_preset(Control.PRESET_CENTER)
panel.custom_minimum_size = Vector2(PANEL_WIDTH, 0.0)
panel.grow_horizontal = Control.GROW_DIRECTION_BOTH
panel.grow_vertical = Control.GROW_DIRECTION_BOTH
add_child(panel)
var column: VBoxContainer = VBoxContainer.new()
column.add_theme_constant_override("separation", 10)
panel.add_child(column)
var heading: Label = Label.new()
heading.text = title
heading.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
heading.add_theme_font_size_override("font_size", FONT_TITLE if screen == "main" else FONT_HEADING)
column.add_child(heading)
column.add_child(MenuTheme.rope())
screens[screen] = panel
return column
func _button(column: Container, text: String, action: Callable) -> Button:
var button: Button = Button.new()
button.text = text
button.pressed.connect(action)
column.add_child(button)
var panel: Control = column
while panel != null and not screens.values().has(panel):
panel = panel.get_parent() as Control
if panel != null and not panel.has_meta("first"):
panel.set_meta("first", button)
return button
func _row(column: Container, label_text: String, control: Control) -> void:
var row: HBoxContainer = HBoxContainer.new()
row.add_theme_constant_override("separation", 12)
var label: Label = Label.new()
label.text = label_text
label.custom_minimum_size = Vector2(200.0, 0.0)
row.add_child(label)
control.size_flags_horizontal = Control.SIZE_EXPAND_FILL
row.add_child(control)
column.add_child(row)
## A row with a slider and its value beside it, as `format` % value (or a percentage
## when `percent`).
func _slider_row(column: Container, label_text: String, slider: HSlider, percent: bool = false) -> void:
var holder: HBoxContainer = HBoxContainer.new()
holder.add_theme_constant_override("separation", 10)
slider.size_flags_horizontal = Control.SIZE_EXPAND_FILL
slider.size_flags_vertical = Control.SIZE_SHRINK_CENTER
holder.add_child(slider)
var readout: Label = Label.new()
readout.custom_minimum_size = Vector2(52.0, 0.0)
readout.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
holder.add_child(readout)
var show: Callable = func(value: float) -> void:
readout.text = ("%d%%" % roundi(value * 100.0)) if percent else ("%.2f" % value)
slider.value_changed.connect(show)
slider.set_meta("readout", show)
_row(column, label_text, holder)
func _note(column: Container) -> Label:
var label: Label = Label.new()
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
label.add_theme_font_size_override("font_size", FONT_SMALL)
label.custom_minimum_size = Vector2(PANEL_WIDTH - 60.0, 0.0)
column.add_child(label)
return label
func _build_main() -> void:
var column: VBoxContainer = _panel("main", "TIDEWALL")
var motto: Label = Label.new()
motto.text = "The sea is rising. Build high, hold your ground."
motto.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
motto.add_theme_font_size_override("font_size", FONT_SMALL)
column.add_child(motto)
_button(column, "Practice against bots", func() -> void: show_screen("practice"))
_button(column, "Join a server", func() -> void: show_screen("join"))
_button(column, "Settings", func() -> void: show_screen("settings"))
_button(column, "Quit", func() -> void: quit_requested.emit())
func _build_practice() -> void:
var column: VBoxContainer = _panel("practice", "Practice")
_bots = SpinBox.new()
_bots.min_value = 1
_bots.max_value = Tuning.SOLO_PLAYERS - 1
_bots.step = 1
_row(column, "Bots", _bots)
_difficulty = OptionButton.new()
_difficulty.add_item("Mixed", 0)
for index: int in Tuning.BOT_DIFFICULTIES.size():
_difficulty.add_item(String(Tuning.BOT_DIFFICULTIES[index]["name"]).capitalize(), index + 1)
_row(column, "Bot skill", _difficulty)
_button(column, "Set out", _start_practice)
_button(column, "Back", back)
func _start_practice() -> void:
settings.practice_bots = int(_bots.value)
settings.practice_difficulty = _difficulty.selected - 1
save_settings()
practice_requested.emit(settings.practice_bots, settings.practice_difficulty)
func _build_join() -> void:
var column: VBoxContainer = _panel("join", "Join a server")
_name = LineEdit.new()
_name.max_length = GameSettings.MAX_NAME_LENGTH
_name.placeholder_text = ClientConfig.DEFAULT_NAME
_row(column, "Your name", _name)
_address = LineEdit.new()
_address.placeholder_text = "host or host:port"
_address.text_submitted.connect(func(_text: String) -> void: _join_address())
_row(column, "Address", _address)
_button(column, "Join this address", _join_address)
screens["join"].set_meta("first", _address)
_servers = ItemList.new()
_servers.custom_minimum_size = Vector2(0.0, 150.0)
_servers.item_activated.connect(_join_listed)
column.add_child(_servers)
_button(column, "Search the LAN", _search)
_button(column, "Join the chosen server", func() -> void:
var chosen: PackedInt32Array = _servers.get_selected_items()
if chosen.is_empty():
_join_note.text = "Choose a server from the list first."
else:
_join_listed(chosen[0]))
_join_note = _note(column)
_button(column, "Back", back)
## Looks for servers on the LAN for a moment, then lists them.
func _search() -> void:
if _browser != null:
return
_browser = ServerBrowser.new()
if _browser.open() != OK:
_browser = null
_join_note.text = "Couldn't open a network socket to search."
return
_browser.scan_lan()
_search_left = SEARCH_SECONDS
_join_note.text = "Searching..."
func _finish_search() -> void:
show_servers(_browser.sorted())
_browser.close()
_browser = null
## Lists `found` (ServerBrowser.sorted()).
func show_servers(found: Array[Dictionary]) -> void:
_found = found
_servers.clear()
for server: Dictionary in found:
var line: String = "%s %d/%d %s" % [server.get("name", "?"), int(server.get("players", 0)), int(server.get("max_players", 0)), server.get("source", "")]
if not server.get("compatible", false):
line += " (another version)"
_servers.add_item(line)
_servers.set_item_disabled(_servers.item_count - 1, not server.get("compatible", false))
_join_note.text = "%d found." % found.size() if not found.is_empty() else "No servers answered. Try an address."
func _join_listed(index: int) -> void:
if index < 0 or index >= _found.size() or not _found[index].get("compatible", false):
return
var server: Dictionary = _found[index]
_join(String(server["address"]), int(server["port"]))
func _join_address() -> void:
var text: String = _address.text.strip_edges()
if text.is_empty():
_join_note.text = "Type an address first."
return
var target: Dictionary = ServerConfig.parse_host_port(text, ServerConfig.DEFAULT_PORT)
if target.is_empty():
_join_note.text = "That isn't an address: use host or host:port."
return
settings.last_server = text
_join(String(target["host"]), int(target["port"]))
func _join(host: String, port: int) -> void:
settings.player_name = _name.text
save_settings()
join_requested.emit(host, port, settings.player_name)
func _build_settings() -> void:
var column: VBoxContainer = _panel("settings", "Settings")
_tabs = TabContainer.new()
_tabs.custom_minimum_size = Vector2(PANEL_WIDTH + 140.0, 420.0)
column.add_child(_tabs)
var controls_scroll: ScrollContainer = ScrollContainer.new()
controls_scroll.name = "Controls"
controls_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
_tabs.add_child(controls_scroll)
var controls: VBoxContainer = VBoxContainer.new()
controls.size_flags_horizontal = Control.SIZE_EXPAND_FILL
controls_scroll.add_child(controls)
for action: Dictionary in KeyBindings.ACTIONS:
var id: String = action["id"]
var button: Button = Button.new()
button.pressed.connect(listen_for.bind(id))
_binding_buttons[id] = button
_row(controls, String(action["label"]), button)
var reset: Button = Button.new()
reset.text = "Put every key back"
reset.pressed.connect(func() -> void:
settings.bindings.reset()
_binding_note.text = "Every key is back to its default."
_refresh_bindings()
save_settings())
controls.add_child(reset)
var mouse: VBoxContainer = _tab("Mouse")
_sensitivity = _slider(InputSettings.MIN_SENSITIVITY, 4.0, 0.05)
_sensitivity.value_changed.connect(func(value: float) -> void: settings.mouse.mouse_sensitivity = value)
_slider_row(mouse, "Look speed", _sensitivity)
_aim_scale = _slider(0.1, 2.0, 0.05)
_aim_scale.value_changed.connect(func(value: float) -> void: settings.mouse.aim_sensitivity_scale = value)
_slider_row(mouse, "Speed while aiming", _aim_scale)
_invert = CheckButton.new()
_invert.toggled.connect(func(on: bool) -> void: settings.mouse.invert_y = on)
_row(mouse, "Invert up and down", _invert)
var graphics: VBoxContainer = _tab("Graphics")
_quality = OptionButton.new()
for index: int in GameSettings.QUALITY_NAMES.size():
_quality.add_item(GameSettings.QUALITY_NAMES[index], index)
_quality.item_selected.connect(func(index: int) -> void: settings.quality = index)
_row(graphics, "Quality", _quality)
_print_pass = CheckButton.new()
_print_pass.toggled.connect(func(on: bool) -> void: settings.print_pass = on)
_row(graphics, "Woodcut ink lines", _print_pass)
var audio: VBoxContainer = _tab("Audio")
for bus: Array in [["master", "Everything"], ["music", "Music"], ["effects", "Effects"]]:
var slider: HSlider = _slider(0.0, 1.0, 0.05)
var key: String = bus[0]
slider.value_changed.connect(func(value: float) -> void: settings.set(key + "_volume", value))
_volumes[key] = slider
_slider_row(audio, bus[1], slider, true)
var display: VBoxContainer = _tab("Display")
_window_mode = OptionButton.new()
for index: int in GameSettings.WINDOW_MODES.size():
_window_mode.add_item(GameSettings.WINDOW_MODES[index], index)
_window_mode.item_selected.connect(func(index: int) -> void: settings.window_mode = index)
_row(display, "Window", _window_mode)
_vsync = CheckButton.new()
_vsync.toggled.connect(func(on: bool) -> void: settings.vsync = on)
_row(display, "Vertical sync", _vsync)
_max_fps = OptionButton.new()
for cap: int in GameSettings.FRAME_CAPS:
_max_fps.add_item("No limit" if cap == 0 else "%d" % cap, cap)
_max_fps.item_selected.connect(func(index: int) -> void: settings.max_fps = GameSettings.FRAME_CAPS[index])
_row(display, "Frame limit", _max_fps)
_binding_note = _note(column)
_binding_note.text = "Choose an action to change its key."
_tabs.tab_changed.connect(func(tab: int) -> void: _binding_note.visible = tab == 0)
var done: Button = _button(column, "Back", back)
screens["settings"].set_meta("first", _tabs.get_tab_bar())
done.name = "Back"
func _tab(title: String) -> VBoxContainer:
var column: VBoxContainer = VBoxContainer.new()
column.name = title
column.add_theme_constant_override("separation", 10)
_tabs.add_child(column)
return column
func _slider(low: float, high: float, step: float) -> HSlider:
var slider: HSlider = HSlider.new()
slider.min_value = low
slider.max_value = high
slider.step = step
slider.custom_minimum_size = Vector2(220.0, 24.0)
return slider
func _build_pause() -> void:
var column: VBoxContainer = _panel("pause", "Paused")
var note: Label = _note(column)
note.text = "The match goes on while you're here."
note.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_button(column, "Back to the match", func() -> void: resume_requested.emit())
_button(column, "Settings", func() -> void: show_screen("settings"))
_button(column, "Leave the match", func() -> void: leave_requested.emit())
func _build_results() -> void:
var column: VBoxContainer = _panel("results", "Results")
_results_title = Label.new()
_results_title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_results_title.add_theme_font_size_override("font_size", FONT_HEADING)
column.add_child(_results_title)
_results_body = _note(column)
_button(column, "Stay and watch", func() -> void: resume_requested.emit())
_button(column, "Back to the main menu", func() -> void: leave_requested.emit())
# --- Keeping controls in step with the settings ------------------------------------
func _refresh_controls() -> void:
_bots.value = settings.practice_bots
_difficulty.selected = settings.practice_difficulty + 1
_name.text = settings.player_name
if _address.text.is_empty():
_address.text = settings.last_server
_sensitivity.set_value_no_signal(settings.mouse.mouse_sensitivity)
_aim_scale.set_value_no_signal(settings.mouse.aim_sensitivity_scale)
_invert.set_pressed_no_signal(settings.mouse.invert_y)
_quality.selected = settings.quality
_print_pass.set_pressed_no_signal(settings.print_pass)
(_volumes["master"] as HSlider).set_value_no_signal(settings.master_volume)
(_volumes["music"] as HSlider).set_value_no_signal(settings.music_volume)
(_volumes["effects"] as HSlider).set_value_no_signal(settings.effects_volume)
_window_mode.selected = settings.window_mode
_vsync.set_pressed_no_signal(settings.vsync)
_max_fps.selected = maxi(GameSettings.FRAME_CAPS.find(settings.max_fps), 0)
for slider: HSlider in [_sensitivity, _aim_scale, _volumes["master"], _volumes["music"], _volumes["effects"]]:
(slider.get_meta("readout") as Callable).call(slider.value)
_refresh_bindings()
func _refresh_bindings() -> void:
for action: String in _binding_buttons:
(_binding_buttons[action] as Button).text = KeyBindings.describe(settings.bindings.code_of(action))
func _collect_buttons(node: Node, found: Dictionary) -> void:
for child: Node in node.get_children():
var button: Button = child as Button
if button != null and not (button is OptionButton) and not (button is CheckButton):
found[button.text] = button
_collect_buttons(child, found)