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 ## (not on phones: MobilePort) ## Every change is applied and saved at once (changed); Back or Escape / B closes it. ## On a phone (MobilePort) the list is shorter and made for fingers: ## Audio as on desktop ## Touch Interface size, Swap sticks, Camera distance, Haptics, then how the touch ## controls work (touch_guide, in place of the bindings) ## Display 3D resolution scale ## Gameplay tips on the first day and night ## (no dead zones, Fullscreen, VSync, pixel size, mouse note, or co-op; Reset to defaults leaves ## those alone). The panel fills the safe area (fit), its rows are at least the minimum touch ## target tall, and the list scrolls by dragging anywhere on it. A slider only takes a finger that ## moves mostly sideways (TouchSlider), so scrolling never nudges a value; the Interface size ## applies when its finger lifts (the whole menu rescales with it). signal changed signal closed const PANEL_MARGIN := 28 const BUTTON_HEIGHT := 54.0 const SCROLL_SIZE := Vector2(980.0, 640.0) ## Phones: a safe area shorter than this (a phone's; not an iPad's) gets the compact panel. const COMPACT_HEIGHT := 800.0 ## Phones: the panel keeps this far inside the safe area. const EDGE := 8.0 ## Phones: how far a finger moves (points) before it scrolls the list or takes a slider. const DRAG_SLOP_PT := 10.0 ## Phones: the setting that applies when its slider's finger lifts, not while it moves. const ON_RELEASE := "interface_size" ## Shown here, but Reset to defaults leaves them alone (co-op's). const KEEP_ON_RESET: Array[String] = ["player_name", "microphone"] ## (label, actions) shown in the bindings list. const BINDINGS := [ ["Move", []], ["Aim", []], ["Attack (main weapon)", ["fire"]], ["Throw a pumpkin bomb (off-hand)", ["offhand"]], ["Switch main weapon", ["weapon_1", "weapon_2", "weapon_3", "weapon_4", "weapon_5", "weapon_6", "weapon_next"]], ["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"]], ["Pouch (what you carry, the shed's stock)", ["pouch"]], ["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 var reset_button: Button ## Controls by setting, for tests: "volume/Master", "move_dead_zone", "aim_dead_zone", ## "fullscreen", "vsync", "resolution_scale", "show_tips"; on phones "interface_size", ## "swap_sticks", "camera_distance", "haptics" instead of the dead zones, Fullscreen, VSync, and ## the pixel size. var controls := {} var _box: VBoxContainer var _scroll: ScrollContainer var _values := {} # setting -> value Label var _loading := false var _phone := false var _panel: PanelContainer var _margin: MarginContainer var _outer: VBoxContainer var _title: Label var _tall: Array[Control] = [] # phones: the rows, at least the minimum touch target tall var _guide: Array[Label] = [] ## Phones: the finger dragging the list (drag_begin), and whether it has started scrolling it. var _drag_on := false var _drag_scrolling := false var _drag_from := Vector2.ZERO var _drag_start := 0 func _ready() -> void: set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) _phone = MobilePort.active() var panel := PanelContainer.new() add_child(panel) _panel = panel var margin := MarginContainer.new() for side in ["left", "right", "top", "bottom"]: margin.add_theme_constant_override("margin_" + side, PANEL_MARGIN) panel.add_child(margin) _margin = margin var outer := VBoxContainer.new() outer.add_theme_constant_override("separation", 14) margin.add_child(outer) _outer = outer var title := Label.new() _title = title 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 = SCROLL_SIZE _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) if _phone: _head("Touch") _slider("interface_size", "Interface size", GameSettings.INTERFACE_SIZE_MIN, GameSettings.INTERFACE_SIZE_MAX, 0.05) _check("swap_sticks", "Swap the sticks (move on the %s)" % ("left" if TouchSticks.MOVE_ON_RIGHT else "right")) _slider("camera_distance", "Camera distance", GameSettings.CAMERA_DISTANCE_MIN, GameSettings.CAMERA_DISTANCE_MAX, 1.0) _check("haptics", "Buzz when the scarecrow is hit") _touch_guide() _head("Display") _slider("resolution_scale", "3D resolution scale", 0.5, 1.0, 0.05) else: _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("pixel_size", "Pixel size", 1.0, GameSettings.MAX_PIXEL_SIZE, 1.0) _slider("resolution_scale", "3D resolution scale", 0.5, 1.0, 0.05) _note("Pixel size 1 turns the pixel look off; the resolution scale is used only then.") _head("Gameplay") _check("show_tips", "Tips on the first day and night") if not _phone: # no co-op on phones _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, BUTTON_HEIGHT) back_button.pressed.connect(close) row.add_child(back_button) reset_button = Button.new() reset_button.text = "Reset to defaults" reset_button.custom_minimum_size = Vector2(280.0, BUTTON_HEIGHT) reset_button.pressed.connect(reset_to_defaults) row.add_child(reset_button) visible = false mouse_filter = Control.MOUSE_FILTER_IGNORE if _phone: _scroll.gui_input.connect(_on_list_input) get_viewport().size_changed.connect(fit) fit() ## Reset to defaults: every setting shown here but co-op's. What isn't shown (on a phone the dead ## zones, Fullscreen, VSync, and the pixel size; on desktop the touch settings) stays as it is. func reset_to_defaults() -> void: var fresh := GameSettings.new() for key: String in controls: if key in KEEP_ON_RESET: continue if key.begins_with("volume/"): var bus := key.trim_prefix("volume/") settings.volume[bus] = fresh.volume[bus] else: settings.set(key, fresh.get(key)) _load() changed.emit() _refresh_guide() ## A phone: the panel fills the safe area (up to the desktop's size), the rows and buttons at ## least the minimum touch target tall, with a smaller title and margins on a phone's short ## canvas. (Nothing on desktop.) func fit() -> void: if not _phone or not is_inside_tree(): return UiTheme.inset_to_safe_area(self) var safe := MobilePort.safe_rect(self) var compact := safe.size.y < COMPACT_HEIGHT var m := 14 if compact else PANEL_MARGIN for side in ["left", "right", "top", "bottom"]: _margin.add_theme_constant_override("margin_" + side, m) _title.add_theme_font_size_override("font_size", 32 if compact else 44) _outer.add_theme_constant_override("separation", 8 if compact else 14) var target := UiTheme.touch_height(self, 0.0) for c in _tall: c.custom_minimum_size.y = target for b: Button in [back_button, reset_button]: b.custom_minimum_size.y = UiTheme.touch_height(self, BUTTON_HEIGHT) _scroll.custom_minimum_size = Vector2.ZERO var chrome := Vector2(2.0 * m + _panel.get_theme_stylebox(&"panel").get_minimum_size().x, _panel.get_combined_minimum_size().y - _scroll.get_combined_minimum_size().y) _scroll.custom_minimum_size = Vector2(minf(SCROLL_SIZE.x, safe.size.x - chrome.x - 2.0 * EDGE), minf(SCROLL_SIZE.y, safe.size.y - chrome.y - 2.0 * EDGE)).max(Vector2(160.0, 120.0)) queue_redraw() # the dim covers the screen from wherever the inset put this ## How the touch controls work (the guide on a phone, in place of the bindings): the sticks' ## sides as Prompts says (they follow Swap sticks), then the buttons, the hotbar, and the backpack. static func touch_guide() -> PackedStringArray: return PackedStringArray([ "Move: touch the %s half of the screen and slide your thumb." % Prompts.move_side(), "Attack: touch the %s half and slide toward an alien to aim and attack." % Prompts.attack_side(), "Use: hold it to gather, repair, or upgrade; tap it for the workbench or the shed. Build (by day) starts building.", "Fear scares off everything nearby. Bomb throws a pumpkin bomb. Hold Patch to patch up with straw.", "Pause is at the top of the screen. Tap the hotbar to switch weapons, and the backpack to see your pouch.", ]) func open(p_settings: GameSettings) -> void: settings = p_settings _load() is_open = true visible = true mouse_filter = Control.MOUSE_FILTER_STOP fit() _scroll.scroll_vertical = 0 (controls["volume/Master"] as Control).grab_focus.call_deferred() func _draw() -> void: draw_rect(UiTheme.overlay_rect(self), Color(0.0, 0.0, 0.0, 0.4)) # dims what's behind func _has_point(point: Vector2) -> bool: return UiTheme.overlay_rect(self).has_point(point) # all of the screen, also outside the safe area func close() -> void: if not is_open: return drag_end() 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 _refresh_guide() 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, roundi(v) if key == "pixel_size" else v) if v is float: _show_value(key, v) changed.emit() _refresh_guide() # (after Main applied it: Swap sticks moves the sticks, and the guide's sides) func _show_value(key: String, v: float) -> void: if not _values.has(key): return if key == "pixel_size": (_values[key] as Label).text = "Off" if roundi(v) <= 1 else "%d×" % roundi(v) elif key == "camera_distance": (_values[key] as Label).text = "%d m" % roundi(v) else: (_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 = TouchSlider.new(self) if _phone else 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: if key == ON_RELEASE and s is TouchSlider and (s as TouchSlider).sliding(): _show_value(key, v) # applied when the finger lifts else: _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 if _phone: # the row shares its width, and all of its (touch-tall) height is the slider's l.custom_minimum_size.x = 0.0 l.size_flags_horizontal = Control.SIZE_EXPAND_FILL l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART l.vertical_alignment = VERTICAL_ALIGNMENT_CENTER s.custom_minimum_size.x = 200.0 s.size_flags_horizontal = Control.SIZE_EXPAND_FILL s.size_flags_stretch_ratio = 1.4 s.size_flags_vertical = Control.SIZE_FILL value.vertical_alignment = VERTICAL_ALIGNMENT_CENTER if key == ON_RELEASE: s.drag_ended.connect(func(_changed: bool) -> void: _apply_value(key, s.value)) _tall.append(row) 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 if _phone: c.mouse_filter = Control.MOUSE_FILTER_PASS # a drag from it reaches the list, which scrolls _tall.append(c) ## Phones: the touch guide's lines (touch_guide), under a caption. func _touch_guide() -> void: _note("Playing by touch") for i in touch_guide().size(): var l := Label.new() l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART l.add_theme_font_size_override("font_size", 19) _box.add_child(l) _guide.append(l) func _refresh_guide() -> void: if _guide.is_empty(): return var lines := touch_guide() for i in _guide.size(): _guide[i].text = lines[i] 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() # --- Phones: the list scrolls by dragging (see the class notes) --- ## How far a finger moves (canvas pixels) before it scrolls the list or takes a slider. func drag_slop() -> float: return MobilePort.minimum_target(self) * DRAG_SLOP_PT / MobilePort.TARGET_PT ## Every touch the rows pass on (the check boxes pass theirs; a TouchSlider hands over its own): ## a finger that goes up or down by more than drag_slop scrolls the list. Godot's own touch ## scrolling never sees them (it would scroll too). func _on_list_input(e: InputEvent) -> void: var mb := e as InputEventMouseButton if mb != null and mb.button_index == MOUSE_BUTTON_LEFT: if mb.pressed: drag_begin(mb.global_position) else: drag_end() _scroll.accept_event() elif e is InputEventMouseMotion: if _drag_on: drag_move((e as InputEventMouseMotion).global_position) _scroll.accept_event() elif e is InputEventScreenTouch or e is InputEventScreenDrag: _scroll.accept_event() ## A finger down on the list at this canvas point. func drag_begin(at: Vector2) -> void: _drag_on = true _drag_scrolling = false _drag_from = at ## The finger at this point: once it's gone far enough up or down (or now, when forced: a slider ## handing it over), the list follows it. func drag_move(at: Vector2, force := false) -> void: if not _drag_on: return if not _drag_scrolling: if not force and absf(at.y - _drag_from.y) <= drag_slop(): return _drag_scrolling = true _drag_from = at _drag_start = _scroll.scroll_vertical _box.propagate_notification(NOTIFICATION_SCROLL_BEGIN) # a check box under the finger lets go return _scroll.scroll_vertical = roundi(_drag_start - (at.y - _drag_from.y)) func drag_end() -> void: _drag_on = false _drag_scrolling = false ## A slider in the phone's scrolling list. A finger on it waits to see which way it goes: past the ## menu's drag_slop, mostly sideways takes the slider (the value follows the finger), and mostly up ## or down hands the finger to the list, which scrolls. A tap sets the value where it lands, as a ## click does. Keys and the gamepad work as on any slider. class TouchSlider extends HSlider: enum Finger { NONE, WAITING, SLIDING, SCROLLING } var menu: SettingsMenu var finger := Finger.NONE var _from := Vector2.ZERO func _init(p_menu: SettingsMenu) -> void: menu = p_menu ## A finger is moving the value now. func sliding() -> bool: return finger == Finger.SLIDING func _notification(what: int) -> void: if what == NOTIFICATION_VISIBILITY_CHANGED and not is_visible_in_tree(): finger = Finger.NONE func _gui_input(e: InputEvent) -> void: var mb := e as InputEventMouseButton if mb != null and mb.button_index == MOUSE_BUTTON_LEFT: var was := finger finger = Finger.NONE if mb.pressed: finger = Finger.WAITING _from = mb.global_position elif was == Finger.WAITING: _slide_to(mb.position) # a tap elif was == Finger.SLIDING: drag_ended.emit(true) elif was == Finger.SCROLLING: menu.drag_end() accept_event() return var mm := e as InputEventMouseMotion if mm == null or finger == Finger.NONE: return if not (mm.button_mask & MOUSE_BUTTON_MASK_LEFT): # its release went elsewhere if finger == Finger.SCROLLING: menu.drag_end() finger = Finger.NONE return match finger: Finger.WAITING: var d := mm.global_position - _from if d.length() > menu.drag_slop(): if absf(d.x) > absf(d.y): finger = Finger.SLIDING drag_started.emit() _slide_to(mm.position) else: finger = Finger.SCROLLING menu.drag_begin(_from) menu.drag_move(mm.global_position, true) Finger.SLIDING: _slide_to(mm.position) Finger.SCROLLING: menu.drag_move(mm.global_position) accept_event() ## The value under this point (local), as a click on a slider sets it. func _slide_to(at: Vector2) -> void: var grab := float(get_theme_icon(&"grabber").get_width()) var room := size.x - grab if room > 0.0: set_as_ratio(clampf((at.x - grab * 0.5) / room, 0.0, 1.0))