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:
@@ -0,0 +1,195 @@
|
||||
## Which key or mouse button does what. Each action has one input, held as a code: a
|
||||
## physical keycode (positions, so they sit the same on any layout), or a mouse button
|
||||
## as its negative index (-1 left, -2 right, ...).
|
||||
##
|
||||
## The number keys do double duty by design: slot N's key also picks build piece N in
|
||||
## build mode, so the builds have no actions of their own.
|
||||
##
|
||||
## bind() refuses an input another action already uses and names that action, so the
|
||||
## settings menu can say what clashes. Escape is kept for menus and can't be bound.
|
||||
class_name KeyBindings
|
||||
extends RefCounted
|
||||
|
||||
## Every action, in the order the settings menu lists them, with its default input.
|
||||
const ACTIONS: Array[Dictionary] = [
|
||||
{"id": "forward", "label": "Forward", "default": KEY_W},
|
||||
{"id": "back", "label": "Back", "default": KEY_S},
|
||||
{"id": "left", "label": "Left", "default": KEY_A},
|
||||
{"id": "right", "label": "Right", "default": KEY_D},
|
||||
{"id": "jump", "label": "Jump", "default": KEY_SPACE},
|
||||
{"id": "crouch", "label": "Crouch", "default": KEY_CTRL},
|
||||
{"id": "sprint", "label": "Sprint", "default": KEY_SHIFT},
|
||||
{"id": "fire", "label": "Use, fire or place", "default": -MOUSE_BUTTON_LEFT},
|
||||
{"id": "aim", "label": "Aim", "default": -MOUSE_BUTTON_RIGHT},
|
||||
{"id": "reload", "label": "Reload", "default": KEY_R},
|
||||
{"id": "interact", "label": "Pick up or open", "default": KEY_E},
|
||||
{"id": "drop", "label": "Drop held item", "default": KEY_G},
|
||||
{"id": "build", "label": "Build mode", "default": KEY_B},
|
||||
{"id": "material", "label": "Next material", "default": KEY_TAB},
|
||||
{"id": "slot_0", "label": "Boathook / wall", "default": KEY_1},
|
||||
{"id": "slot_1", "label": "Slot 1 / floor", "default": KEY_2},
|
||||
{"id": "slot_2", "label": "Slot 2 / ramp", "default": KEY_3},
|
||||
{"id": "slot_3", "label": "Slot 3 / roof", "default": KEY_4},
|
||||
{"id": "slot_4", "label": "Slot 4 / breakwater", "default": KEY_5},
|
||||
{"id": "slot_5", "label": "Slot 5", "default": KEY_6},
|
||||
]
|
||||
## Kept for opening and closing menus.
|
||||
const RESERVED: Array[int] = [KEY_ESCAPE]
|
||||
|
||||
## Action id -> code.
|
||||
var codes: Dictionary = {}
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
reset()
|
||||
|
||||
|
||||
## Every action back to its default.
|
||||
func reset() -> void:
|
||||
codes.clear()
|
||||
for action: Dictionary in ACTIONS:
|
||||
codes[action["id"]] = int(action["default"])
|
||||
|
||||
|
||||
static func has_action(action: String) -> bool:
|
||||
return _index_of(action) >= 0
|
||||
|
||||
|
||||
static func default_of(action: String) -> int:
|
||||
var index: int = _index_of(action)
|
||||
return int(ACTIONS[index]["default"]) if index >= 0 else 0
|
||||
|
||||
|
||||
static func label_of(action: String) -> String:
|
||||
var index: int = _index_of(action)
|
||||
return String(ACTIONS[index]["label"]) if index >= 0 else action
|
||||
|
||||
|
||||
func code_of(action: String) -> int:
|
||||
return int(codes.get(action, 0))
|
||||
|
||||
|
||||
## The action using `code`, other than `except`, or "" if none.
|
||||
func action_using(code: int, except: String = "") -> String:
|
||||
for action: String in codes:
|
||||
if action != except and int(codes[action]) == code:
|
||||
return action
|
||||
return ""
|
||||
|
||||
|
||||
## Binds `action` to `code`. Returns "" when done, or why not: "unknown action",
|
||||
## "reserved", "not an input", or the id of the action that already uses it (nothing
|
||||
## changes then).
|
||||
func bind(action: String, code: int) -> String:
|
||||
if not has_action(action):
|
||||
return "unknown action"
|
||||
if RESERVED.has(code):
|
||||
return "reserved"
|
||||
if not is_valid_code(code):
|
||||
return "not an input"
|
||||
var other: String = action_using(code, action)
|
||||
if not other.is_empty():
|
||||
return other
|
||||
codes[action] = code
|
||||
return ""
|
||||
|
||||
|
||||
## Every action whose input another action also uses (only possible in a hand-edited
|
||||
## settings file; load_from() repairs those).
|
||||
func conflicts() -> Array[String]:
|
||||
var found: Array[String] = []
|
||||
for action: String in codes:
|
||||
if not action_using(int(codes[action]), action).is_empty():
|
||||
found.append(action)
|
||||
found.sort()
|
||||
return found
|
||||
|
||||
|
||||
## True if the input for `action` is held now.
|
||||
func is_held(action: String) -> bool:
|
||||
var code: int = code_of(action)
|
||||
if code < 0:
|
||||
return Input.is_mouse_button_pressed(-code as MouseButton)
|
||||
if code > 0:
|
||||
return Input.is_physical_key_pressed(code as Key)
|
||||
return false
|
||||
|
||||
|
||||
## The action a press event is for, or "".
|
||||
func action_for_event(event: InputEvent) -> String:
|
||||
var code: int = code_of_event(event)
|
||||
return action_using(code) if code != 0 else ""
|
||||
|
||||
|
||||
## The code of a key or mouse button press, or 0 for anything else.
|
||||
static func code_of_event(event: InputEvent) -> int:
|
||||
var key: InputEventKey = event as InputEventKey
|
||||
if key != null and key.pressed and not key.echo:
|
||||
return int(key.physical_keycode) if key.physical_keycode != KEY_NONE else int(key.keycode)
|
||||
var button: InputEventMouseButton = event as InputEventMouseButton
|
||||
if button != null and button.pressed:
|
||||
return -int(button.button_index)
|
||||
return 0
|
||||
|
||||
|
||||
static func is_valid_code(code: int) -> bool:
|
||||
if code < 0:
|
||||
return -code >= MOUSE_BUTTON_LEFT and -code <= MOUSE_BUTTON_XBUTTON2 and -code != MOUSE_BUTTON_WHEEL_UP and -code != MOUSE_BUTTON_WHEEL_DOWN
|
||||
return code > 0 and not OS.get_keycode_string(code as Key).is_empty()
|
||||
|
||||
|
||||
## A name for a code, for the settings menu.
|
||||
static func describe(code: int) -> String:
|
||||
if code < 0:
|
||||
match -code:
|
||||
MOUSE_BUTTON_LEFT:
|
||||
return "Left mouse"
|
||||
MOUSE_BUTTON_RIGHT:
|
||||
return "Right mouse"
|
||||
MOUSE_BUTTON_MIDDLE:
|
||||
return "Middle mouse"
|
||||
_:
|
||||
return "Mouse %d" % -code
|
||||
if code == 0:
|
||||
return "None"
|
||||
return OS.get_keycode_string(code as Key)
|
||||
|
||||
|
||||
## Writes every binding into `config`'s "keys" section.
|
||||
func write(config: ConfigFile) -> void:
|
||||
for action: String in codes:
|
||||
config.set_value("keys", action, int(codes[action]))
|
||||
|
||||
|
||||
## Reads bindings from `config`. Anything missing, malformed or clashing with an
|
||||
## earlier action keeps its default, and a default taken by then is left unbound
|
||||
## rather than doubled.
|
||||
static func read(config: ConfigFile) -> KeyBindings:
|
||||
var bindings: KeyBindings = KeyBindings.new()
|
||||
for action: Dictionary in ACTIONS:
|
||||
var id: String = action["id"]
|
||||
if not config.has_section_key("keys", id):
|
||||
continue
|
||||
var value: Variant = config.get_value("keys", id)
|
||||
if value is int and is_valid_code(value) and not RESERVED.has(value):
|
||||
bindings.codes[id] = int(value)
|
||||
# Repair clashes in list order: the first action keeps its input.
|
||||
var taken: Dictionary = {}
|
||||
for action: Dictionary in ACTIONS:
|
||||
var id: String = action["id"]
|
||||
var code: int = int(bindings.codes[id])
|
||||
if taken.has(code):
|
||||
code = int(action["default"])
|
||||
if taken.has(code):
|
||||
code = 0
|
||||
bindings.codes[id] = code
|
||||
if code != 0:
|
||||
taken[code] = true
|
||||
return bindings
|
||||
|
||||
|
||||
static func _index_of(action: String) -> int:
|
||||
for index: int in ACTIONS.size():
|
||||
if ACTIONS[index]["id"] == action:
|
||||
return index
|
||||
return -1
|
||||
Reference in New Issue
Block a user