#41: menus and settings at phone scale

On phones the main menu, pause menu, question box, and settings fit the
scaled canvas inside the safe area, with buttons and rows at least 44 pt
tall (19.5:9 at 1.6x, the iPhone SE at the 1.7 cap, 4:3 iPads). The
pause menu puts its buttons in a column beside the rest.

Phone settings: Audio, Touch (Interface size, Swap sticks, Camera
distance, Haptics, and a short touch-controls guide), Display (3D
resolution scale), and Gameplay. The list scrolls by dragging, and a
slider only moves after a mostly sideways drag. GameSettings saves the
touch settings under [touch] and applies them live (&"touch_controls",
the follow camera on phones). A hit buzzes the phone with Haptics on.
Desktop menus are unchanged. Test: game/tests/mobile/menus_test.gd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-14 15:20:15 -04:00
parent 5392ae09af
commit 0c401171f3
10 changed files with 1139 additions and 46 deletions

View file

@ -10,10 +10,35 @@ extends CenterContainer
## 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", []],
@ -43,34 +68,54 @@ const PAD_BUTTONS := {
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".
## "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, 28)
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 = Vector2(980.0, 640.0)
_scroll.custom_minimum_size = SCROLL_SIZE
_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
_scroll.follow_focus = true
outer.add_child(_scroll)
@ -81,20 +126,30 @@ func _ready() -> void:
_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("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.")
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 MobilePort.active(): # no co-op on phones
if not _phone: # no co-op on phones
_head("Co-op")
_text("player_name", "Your name")
_check("microphone", "Send my microphone in voice chat")
@ -103,21 +158,76 @@ func _ready() -> void:
outer.add_child(row)
back_button = Button.new()
back_button.text = "Back"
back_button.custom_minimum_size = Vector2(220.0, 54.0)
back_button.custom_minimum_size = Vector2(220.0, BUTTON_HEIGHT)
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", "pixel_size", "resolution_scale", "show_tips"]:
settings.set(key, fresh.get(key))
_load()
changed.emit())
row.add_child(reset)
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:
@ -126,17 +236,23 @@ func open(p_settings: GameSettings) -> void:
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(Rect2(Vector2.ZERO, size), Color(0.0, 0.0, 0.0, 0.4)) # dims what's behind
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
@ -193,6 +309,7 @@ func _load() -> void:
elif c is LineEdit:
(c as LineEdit).text = String(v)
_loading = false
_refresh_guide()
func _apply_value(key: String, v: Variant) -> void:
@ -205,6 +322,7 @@ func _apply_value(key: String, v: Variant) -> void:
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:
@ -212,6 +330,8 @@ func _show_value(key: String, v: float) -> void:
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)
@ -240,19 +360,36 @@ func _slider(key: String, label: String, lo: float, hi: float, step: float) -> v
l.text = label
l.custom_minimum_size = Vector2(340.0, 0.0)
row.add_child(l)
var s := HSlider.new()
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: _apply_value(key, v))
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:
@ -278,6 +415,28 @@ func _check(key: String, label: String) -> void:
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:
@ -313,3 +472,129 @@ 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))