harvest/game/scripts/settings_menu.gd
2026-10-04 12:07:36 -04:00

304 lines
10 KiB
GDScript

class_name SettingsMenu
extends CenterContainer
## Settings, from the main menu or the pause menu (Main): one scrolling column so the
## keyboard and gamepad can reach everything in order.
## Audio a volume slider per bus (GameSettings.BUSES)
## Controls move / aim stick dead zones, then every binding (read from the InputMap, so
## it always shows what the game really uses; remapping isn't offered)
## Display fullscreen, VSync, 3D resolution scale
## Gameplay tips on the first day and night
## Co-op your name (the lobby and your name tag), and your microphone for voice chat
## Every change is applied and saved at once (changed); Back or Escape / B closes it.
signal changed
signal closed
## (label, actions) shown in the bindings list.
const BINDINGS := [
["Move", []],
["Aim", []],
["Shoot", ["fire"]],
["Fear", ["fear"]],
["Patch up (hold)", ["patch"]],
["Gather / repair / workbench", ["interact"]],
["Build mode", ["build_mode"]],
["Pick a buildable", ["build_1", "build_2", "build_3", "build_4", "build_next"]],
["Rotate", ["rotate_left", "rotate_right"]],
["Dismantle / leave build mode", ["cancel"]],
["Pause", ["pause"]],
["Hide tips", ["hide_tips"]],
["Microphone on / off (co-op)", ["toggle_mic"]],
]
const PAD_BUTTONS := {
JOY_BUTTON_A: "A", JOY_BUTTON_B: "B", JOY_BUTTON_X: "X", JOY_BUTTON_Y: "Y", JOY_BUTTON_BACK: "Back",
JOY_BUTTON_START: "Start", JOY_BUTTON_LEFT_SHOULDER: "LB", JOY_BUTTON_RIGHT_SHOULDER: "RB",
JOY_BUTTON_DPAD_UP: "D-pad up", JOY_BUTTON_DPAD_DOWN: "D-pad down", JOY_BUTTON_DPAD_LEFT: "D-pad left",
JOY_BUTTON_DPAD_RIGHT: "D-pad right", JOY_BUTTON_LEFT_STICK: "L3", JOY_BUTTON_RIGHT_STICK: "R3",
}
var settings: GameSettings
var is_open := false
var back_button: Button
## Controls by setting, for tests: "volume/Master", "move_dead_zone", "aim_dead_zone",
## "fullscreen", "vsync", "resolution_scale", "show_tips".
var controls := {}
var _box: VBoxContainer
var _scroll: ScrollContainer
var _values := {} # setting -> value Label
var _loading := false
func _ready() -> void:
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
var panel := PanelContainer.new()
add_child(panel)
var margin := MarginContainer.new()
for side in ["left", "right", "top", "bottom"]:
margin.add_theme_constant_override("margin_" + side, 28)
panel.add_child(margin)
var outer := VBoxContainer.new()
outer.add_theme_constant_override("separation", 14)
margin.add_child(outer)
var title := Label.new()
title.text = "Settings"
title.add_theme_font_size_override("font_size", 44)
title.add_theme_color_override("font_color", UiTheme.GOLD)
outer.add_child(title)
_scroll = ScrollContainer.new()
_scroll.custom_minimum_size = Vector2(980.0, 640.0)
_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
_scroll.follow_focus = true
outer.add_child(_scroll)
_box = VBoxContainer.new()
_box.size_flags_horizontal = Control.SIZE_EXPAND_FILL
_box.add_theme_constant_override("separation", 10)
_scroll.add_child(_box)
_head("Audio")
for bus in GameSettings.BUSES:
_slider("volume/" + bus, GameSettings.BUS_NAMES[bus], 0.0, 1.0, 0.05)
_head("Controls")
_slider("move_dead_zone", "Move stick dead zone", 0.05, 0.9, 0.05)
_slider("aim_dead_zone", "Aim stick dead zone", 0.05, 0.9, 0.05)
_note("Mouse aim follows the cursor directly, so it has no sensitivity to set.")
_bindings()
_head("Display")
_check("fullscreen", "Fullscreen")
_check("vsync", "VSync")
_slider("resolution_scale", "3D resolution scale", 0.5, 1.0, 0.05)
_head("Gameplay")
_check("show_tips", "Tips on the first day and night")
_head("Co-op")
_text("player_name", "Your name")
_check("microphone", "Send my microphone in voice chat")
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 20)
outer.add_child(row)
back_button = Button.new()
back_button.text = "Back"
back_button.custom_minimum_size = Vector2(220.0, 54.0)
back_button.pressed.connect(close)
row.add_child(back_button)
var reset := Button.new()
reset.text = "Reset to defaults"
reset.custom_minimum_size = Vector2(280.0, 54.0)
reset.pressed.connect(func() -> void:
var fresh := GameSettings.new()
for key in ["volume", "move_dead_zone", "aim_dead_zone", "fullscreen", "vsync", "resolution_scale", "show_tips"]:
settings.set(key, fresh.get(key))
_load()
changed.emit())
row.add_child(reset)
visible = false
mouse_filter = Control.MOUSE_FILTER_IGNORE
func open(p_settings: GameSettings) -> void:
settings = p_settings
_load()
is_open = true
visible = true
mouse_filter = Control.MOUSE_FILTER_STOP
_scroll.scroll_vertical = 0
(controls["volume/Master"] as Control).grab_focus.call_deferred()
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color(0.0, 0.0, 0.0, 0.4)) # dims what's behind
func close() -> void:
if not is_open:
return
is_open = false
visible = false
mouse_filter = Control.MOUSE_FILTER_IGNORE
closed.emit()
## "W, A, S, D, Up, Left, Down, Right" and "Left stick" for an action list.
static func describe_keys(actions: Array) -> String:
var out := PackedStringArray()
for a: String in actions:
if not InputMap.has_action(a):
continue
for e in InputMap.action_get_events(a):
var text := ""
if e is InputEventKey:
var k := e as InputEventKey
text = OS.get_keycode_string(k.physical_keycode if k.physical_keycode != KEY_NONE else k.keycode)
elif e is InputEventMouseButton:
text = {MOUSE_BUTTON_LEFT: "Left mouse", MOUSE_BUTTON_RIGHT: "Right mouse", MOUSE_BUTTON_MIDDLE: "Middle mouse"}.get(
(e as InputEventMouseButton).button_index, "Mouse")
if text != "" and not out.has(text):
out.append(text)
return ", ".join(out)
static func describe_pad(actions: Array) -> String:
var out := PackedStringArray()
for a: String in actions:
if not InputMap.has_action(a):
continue
for e in InputMap.action_get_events(a):
var text := ""
if e is InputEventJoypadButton:
text = PAD_BUTTONS.get((e as InputEventJoypadButton).button_index, "Button %d" % (e as InputEventJoypadButton).button_index)
elif e is InputEventJoypadMotion:
var m := e as InputEventJoypadMotion
text = {JOY_AXIS_LEFT_X: "Left stick", JOY_AXIS_LEFT_Y: "Left stick", JOY_AXIS_RIGHT_X: "Right stick",
JOY_AXIS_RIGHT_Y: "Right stick", JOY_AXIS_TRIGGER_LEFT: "LT", JOY_AXIS_TRIGGER_RIGHT: "RT"}.get(m.axis, "Axis")
if text != "" and not out.has(text):
out.append(text)
return ", ".join(out)
func _load() -> void:
_loading = true
for key: String in controls:
var c: Control = controls[key]
var v: Variant = settings.volume[key.trim_prefix("volume/")] if key.begins_with("volume/") else settings.get(key)
if c is HSlider:
(c as HSlider).value = float(v)
_show_value(key, float(v))
elif c is CheckBox:
(c as CheckBox).button_pressed = bool(v)
elif c is LineEdit:
(c as LineEdit).text = String(v)
_loading = false
func _apply_value(key: String, v: Variant) -> void:
if _loading or settings == null:
return
if key.begins_with("volume/"):
settings.volume[key.trim_prefix("volume/")] = float(v)
else:
settings.set(key, v)
if v is float:
_show_value(key, v)
changed.emit()
func _show_value(key: String, v: float) -> void:
if _values.has(key):
(_values[key] as Label).text = "%d%%" % roundi(v * 100.0)
func _head(text: String) -> void:
var l := Label.new()
l.text = text
l.add_theme_font_size_override("font_size", 28)
l.add_theme_color_override("font_color", UiTheme.GOLD)
_box.add_child(l)
func _note(text: String) -> void:
var l := Label.new()
l.text = text
l.add_theme_font_size_override("font_size", 18)
l.add_theme_color_override("font_color", Color(0.72, 0.74, 0.68))
_box.add_child(l)
func _slider(key: String, label: String, lo: float, hi: float, step: float) -> void:
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 18)
_box.add_child(row)
var l := Label.new()
l.text = label
l.custom_minimum_size = Vector2(340.0, 0.0)
row.add_child(l)
var s := HSlider.new()
s.min_value = lo
s.max_value = hi
s.step = step
s.custom_minimum_size = Vector2(440.0, 32.0)
s.size_flags_vertical = Control.SIZE_SHRINK_CENTER
s.value_changed.connect(func(v: float) -> void: _apply_value(key, v))
row.add_child(s)
var value := Label.new()
value.custom_minimum_size = Vector2(80.0, 0.0)
row.add_child(value)
controls[key] = s
_values[key] = value
func _text(key: String, label: String) -> void:
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 18)
_box.add_child(row)
var l := Label.new()
l.text = label
l.custom_minimum_size = Vector2(340.0, 0.0)
row.add_child(l)
var e := LineEdit.new()
e.max_length = 20
e.custom_minimum_size = Vector2(440.0, 40.0)
e.placeholder_text = "Shown over your scarecrow in co-op"
e.text_changed.connect(func(t: String) -> void: _apply_value(key, t.strip_edges()))
row.add_child(e)
controls[key] = e
func _check(key: String, label: String) -> void:
var c := CheckBox.new()
c.text = label
c.toggled.connect(func(on: bool) -> void: _apply_value(key, on))
_box.add_child(c)
controls[key] = c
func _bindings() -> void:
var grid := GridContainer.new()
grid.columns = 3
grid.add_theme_constant_override("h_separation", 30)
grid.add_theme_constant_override("v_separation", 4)
_box.add_child(grid)
for head in ["Action", "Keyboard / mouse", "Gamepad"]:
var l := Label.new()
l.text = head
l.add_theme_font_size_override("font_size", 18)
l.add_theme_color_override("font_color", Color(0.72, 0.74, 0.68))
grid.add_child(l)
for row: Array in BINDINGS:
var keys := describe_keys(row[1])
var pad := describe_pad(row[1])
match row[0]:
"Move":
keys = "W A S D, arrow keys"
pad = describe_pad(["move_left", "move_right", "move_forward", "move_back"])
"Aim":
keys = "Mouse cursor"
pad = describe_pad(["aim_left", "aim_right", "aim_up", "aim_down"])
for text in [row[0], keys, pad]:
var l := Label.new()
l.text = text
l.add_theme_font_size_override("font_size", 19)
grid.add_child(l)
func _unhandled_input(event: InputEvent) -> void:
if is_open and event.is_action_pressed("ui_cancel"):
close()
get_viewport().set_input_as_handled()