Merge #35: interface scale, safe area, and the mobile test kit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-14 14:41:49 -04:00
commit ad08f83389
14 changed files with 868 additions and 3 deletions

View file

@ -11,6 +11,8 @@ extends RefCounted
## resolution scale, used when the pixel size is 1 (the HUD stays sharp either way).
## * Gameplay: whether the tips show on the first day and night (Tutorial).
## * Co-op: your player name, and whether your microphone is sent in voice chat.
## * Touch (phones): the interface size, times the scale the screen's size calls for
## (MobilePort.ui_scale; it doesn't change desktop).
## Values missing from the file or out of range fall back to the defaults, so an old or
## hand-edited file never breaks the game.
@ -33,6 +35,8 @@ var show_tips := true
## Co-op: your name for the lobby and the name tag, and whether your microphone is sent.
var player_name := ""
var microphone := true
## Phones: the Interface size, 0.8-1.3 times the scale the screen calls for (MobilePort.ui_scale).
var interface_size := 1.0
static func load_from(path: String = PATH) -> GameSettings:
@ -52,6 +56,7 @@ static func load_from(path: String = PATH) -> GameSettings:
var n: Variant = f.get_value("coop", "player_name", "")
g.player_name = String(n).strip_edges().left(20) if n is String else ""
g.microphone = _flag(f.get_value("coop", "microphone", g.microphone), g.microphone)
g.interface_size = _number(f.get_value("touch", "interface_size", g.interface_size), 0.8, 1.3, g.interface_size)
return g
@ -68,11 +73,12 @@ func save_to(path: String = PATH) -> Error:
f.set_value("gameplay", "show_tips", show_tips)
f.set_value("coop", "player_name", player_name)
f.set_value("coop", "microphone", microphone)
f.set_value("touch", "interface_size", interface_size)
return f.save(path)
## Applies everything: bus volumes, dead zones (into config too, when given), and the display
## (skipped without a real display, e.g. headless).
## Applies everything: bus volumes, dead zones (into config too, when given), the interface
## scale, and the display (skipped without a real display, e.g. headless).
func apply(viewport: Viewport, config: GameConfig = null) -> void:
for bus in BUSES:
var i := AudioServer.get_bus_index(bus)
@ -88,6 +94,7 @@ func apply(viewport: Viewport, config: GameConfig = null) -> void:
config.aim_dead_zone = aim_dead_zone
if viewport != null:
apply_pixels(viewport)
apply_scale(viewport)
if DisplayServer.get_name() == "headless":
return
var mode := DisplayServer.window_get_mode()
@ -112,6 +119,15 @@ func apply_pixels(viewport: Viewport) -> void:
viewport.msaa_3d = ProjectSettings.get_setting("rendering/anti_aliasing/quality/msaa_3d", Viewport.MSAA_DISABLED)
## The interface scale (MobilePort.ui_scale: on a phone, the screen's size times
## interface_size; always 1 on desktop) as the window's content_scale_factor, which scales every
## CanvasLayer (the HUD, the menus, the sticks) and leaves the 3D view alone.
func apply_scale(viewport: Viewport) -> void:
var window := viewport.get_window()
if window != null:
window.content_scale_factor = MobilePort.ui_scale(interface_size)
static func _number(v: Variant, lo: float, hi: float, fallback: float) -> float:
if not (v is float or v is int):
return fallback

View file

@ -7,6 +7,8 @@ extends RefCounted
## main menu's button and the settings' Co-op section), two thumbsticks move and attack
## (TouchSticks), and leaving the app pauses the game.
## The microphone is never opened (audio/driver/enable_input.mobile in project.godot).
## The interface is scaled for the screen's physical size (ui_scale) and kept inside its safe
## area (safe_rect), with touch targets of at least minimum_target(); see below.
## * perf_test(): the iOS preset's "perf_test" feature. Main adds PerfTest (the frame-time
## readout and test buttons), and CampaignDebug's jumps work in that release build.
## On desktop, `-- --mobile` and `-- --perf-test` after the usual arguments turn the same switches
@ -25,3 +27,213 @@ static func active() -> bool:
static func perf_test() -> bool:
return OS.has_feature("perf_test") or "--perf-test" in OS.get_cmdline_user_args()
## --- The interface on a phone: its scale, the safe area, and the touch target ---
## ui_scale() is the root window's content_scale_factor on a phone (GameSettings.apply). It
## scales every CanvasLayer (the HUD, the menus, the sticks) and leaves the 3D view alone. It's
## worked out from the screen's physical size: clampf(4.3 / the shorter side in inches, 1, 1.8),
## about 1.6 on a phone and 1 on an iPad, times the Interface size setting (0.8-1.3). At 1.6 a
## 19.5:9 phone's canvas is about 1460x675 instead of 2340x1080. Desktop is always 1.
## The density (screen_get_dpi) isn't trusted blindly: when it's 0 or impossible for a phone or a
## tablet (DPI_RANGE, INCH_RANGE), the size class decides: a shorter side below 500 points is a
## phone (1.6), anything bigger a tablet (1). PerfTest shows what a device reports (describe()).
## The desktop preview (-- --mobile, or tests' forced = 1) plays the phone in the window:
## * --dpi=N (or preview_dpi) gives the window that density. Without it, a window at least
## 1.7:1 (16:9 and wider) is a phone whose shorter side is 2.7 in (about 1.6x), a squarer one
## an iPad's 6 in (1x).
## * --safe-area=left,top,right,bottom (or preview_insets) insets the safe area by that many
## window pixels on each side, to stand in for a notch and the home indicator.
## Apple's minimum touch target, in points (an iPhone's pixels over its screen scale).
const TARGET_PT := 44.0
## The scale from the screen's size: this many inches across the shorter side is 1x.
const REFERENCE_INCHES := 4.3
const MIN_SCALE := 1.0
const MAX_SCALE := 1.8
## The size class, when the density can't be trusted: a phone's shorter side is below this many
## points, and gets PHONE_SCALE; anything bigger is a tablet (TABLET_SCALE).
const PHONE_POINTS := 500.0
const PHONE_SCALE := 1.6
const TABLET_SCALE := 1.0
## The densities (dots per inch) and shorter sides (inches) a phone or a tablet can have. Outside
## them the reported density is wrong.
const DPI_RANGE := Vector2(120.0, 800.0)
const INCH_RANGE := Vector2(1.5, 13.0)
## An original iPhone's density: one point per pixel. Pixels per point are the density over it
## where the screen doesn't say (the desktop preview).
const POINT_DPI := 163.0
## The desktop preview without --dpi: a window at least this wide for its height is a phone with
## PREVIEW_PHONE_INCHES across its shorter side, a squarer one an iPad with PREVIEW_TABLET_INCHES.
const PREVIEW_PHONE_ASPECT := 1.7
const PREVIEW_PHONE_INCHES := 2.7
const PREVIEW_TABLET_INCHES := 6.0
## The desktop preview's density (dots per inch). 0: the --dpi=N user argument, or by the
## window's shape (see above).
static var preview_dpi := 0.0
## The desktop preview's safe-area insets in window pixels: [left, top, right, bottom]. Empty:
## the --safe-area=l,t,r,b user argument, or none.
static var preview_insets: Array = []
## A phone in the desktop preview (active() without the "mobile" feature: -- --mobile, or a test).
static func previewing() -> bool:
return active() and not OS.has_feature("mobile")
## The interface scale (the window's content_scale_factor): screen_scale() times interface_size
## (the Interface size setting, 0.8-1.3) on a phone; 1 on desktop.
static func ui_scale(interface_size: float = 1.0) -> float:
if not active():
return 1.0
return screen_scale() * clampf(interface_size, 0.8, 1.3)
## The scale this screen's physical size calls for (scale_for), before the Interface size.
static func screen_scale() -> float:
return scale_for(screen_pixels(), screen_dpi(), pixels_per_point())
## The scale for a screen this many pixels across, at this density (dots per inch), with this
## many pixels per point: clampf(4.3 / the shorter side in inches, 1, 1.8). When the density
## can't be right (dpi_plausible), the size class: below PHONE_POINTS points across the shorter
## side is a phone (1.6), else a tablet (1).
static func scale_for(pixels: Vector2, dpi: float, per_point: float) -> float:
var short := minf(pixels.x, pixels.y)
if dpi_plausible(pixels, dpi):
return clampf(REFERENCE_INCHES / (short / dpi), MIN_SCALE, MAX_SCALE)
return PHONE_SCALE if short / maxf(per_point, 0.001) < PHONE_POINTS else TABLET_SCALE
## Whether a screen this many pixels across could have this density: one inside DPI_RANGE, which
## makes the shorter side a length inside INCH_RANGE.
static func dpi_plausible(pixels: Vector2, dpi: float) -> bool:
if dpi < DPI_RANGE.x or dpi > DPI_RANGE.y:
return false
var inches := minf(pixels.x, pixels.y) / dpi
return inches >= INCH_RANGE.x and inches <= INCH_RANGE.y
## The phone's screen in pixels: the screen on a phone, the window in the desktop preview.
static func screen_pixels() -> Vector2:
if previewing():
var root := _root()
return Vector2(root.size) if root != null else Vector2.ZERO
return Vector2(DisplayServer.screen_get_size())
## The screen's density (dots per inch): the screen's own on a phone; in the desktop preview
## preview_dpi, --dpi=N, or the phone's or the iPad's by the window's shape.
static func screen_dpi() -> float:
if not previewing():
return float(DisplayServer.screen_get_dpi())
if preview_dpi > 0.0:
return preview_dpi
var arg := _arg("--dpi=")
if arg.is_valid_float() and float(arg) > 0.0:
return float(arg)
var px := screen_pixels()
var short := minf(px.x, px.y)
var phone := maxf(px.x, px.y) >= PREVIEW_PHONE_ASPECT * short
return short / (PREVIEW_PHONE_INCHES if phone else PREVIEW_TABLET_INCHES)
## Pixels per point: the screen's scale on a phone (2 or 3 on an iPhone; Android's density
## over 160), else the density over POINT_DPI (the desktop preview, or a phone that says 1 and
## has a plausible density), else 1.
static func pixels_per_point() -> float:
var dpi := screen_dpi()
if previewing():
return maxf(dpi / POINT_DPI, 0.001)
var s := DisplayServer.screen_get_scale()
if s > 1.0:
return s
return dpi / POINT_DPI if dpi_plausible(screen_pixels(), dpi) else 1.0
## The safe area in window pixels: the display's (DisplayServer.get_display_safe_area: clear of
## the notch, the rounded corners and the home indicator) on a phone; the window less the
## preview's insets in the desktop preview. The whole window when there's none.
static func safe_area_pixels(window: Window) -> Rect2:
var full := Rect2(Vector2.ZERO, Vector2(window.size))
var safe := full
if previewing():
var inset := insets()
safe = Rect2(full.position + Vector2(inset[0], inset[1]),
(full.size - Vector2(inset[0] + inset[2], inset[1] + inset[3])).max(Vector2.ZERO))
elif active():
safe = Rect2(DisplayServer.get_display_safe_area()).abs()
safe.position -= Vector2(DisplayServer.window_get_position()) # screen to window (0 when full screen)
safe = safe.intersection(full)
return safe if safe.has_area() else full
## The preview's safe-area insets in window pixels, [left, top, right, bottom]: preview_insets,
## or --safe-area=l,t,r,b, or none.
static func insets() -> Array:
var from: Array = preview_insets
if from.size() != 4:
from = Array(_arg("--safe-area=").split(","))
var out: Array = []
for v: Variant in from:
var x := float(v) if v is float or v is int or (v is String and (v as String).is_valid_float()) else NAN
if is_nan(x):
return [0.0, 0.0, 0.0, 0.0]
out.append(maxf(x, 0.0))
return out if out.size() == 4 else [0.0, 0.0, 0.0, 0.0]
## The safe area in ci's canvas coordinates (the window's pixels through the viewport's final
## transform and ci's canvas layer, so with the interface scale): where the HUD's widgets and
## the menus go on a phone. On desktop, the whole visible rect.
static func safe_rect(ci: CanvasItem) -> Rect2:
if not active():
return ci.get_viewport_rect()
return _to_window(ci).affine_inverse() * safe_area_pixels(ci.get_window())
## Apple's minimum touch target (44 points) in canvas pixels at the current scale, for ci's
## canvas (the root window's without one): about 70 on a phone or an iPad. 44 on desktop.
static func minimum_target(ci: CanvasItem = null) -> float:
if not active():
return TARGET_PT
var s := absf(_to_window(ci).get_scale().y)
return TARGET_PT * pixels_per_point() / s if s > 0.0 else TARGET_PT
## Two lines for PerfTest's overlay, to check on a device what it reports: the screen (pixels,
## density, pixels per point) and the display's safe area as reported (the simulated one in the
## desktop preview); then the scale and how it was worked out, the window's factor, and the
## canvas and the touch target that gives.
static func describe() -> String:
var px := screen_pixels()
var dpi := screen_dpi()
var root := _root()
var safe := Rect2i(safe_area_pixels(root)) if previewing() and root != null else DisplayServer.get_display_safe_area()
var canvas := root.get_visible_rect().size if root != null else Vector2.ZERO
return "Screen %d×%d · %d dpi · %.2f px/pt · safe area %d,%d %d×%d\nScale ×%.2f (%s) · window ×%.2f · canvas %d×%d · 44 pt = %d px" % [
roundi(px.x), roundi(px.y), roundi(dpi), pixels_per_point(), safe.position.x, safe.position.y, safe.size.x, safe.size.y,
screen_scale(), "dpi" if dpi_plausible(px, dpi) else "size class", root.content_scale_factor if root != null else 1.0,
roundi(canvas.x), roundi(canvas.y), roundi(minimum_target())]
## ci's canvas coordinates to window pixels (the root window's canvas without ci).
static func _to_window(ci: CanvasItem) -> Transform2D:
if ci != null:
return ci.get_viewport().get_final_transform() * ci.get_canvas_transform()
var root := _root()
return root.get_final_transform() if root != null else Transform2D.IDENTITY
static func _root() -> Window:
var tree := Engine.get_main_loop() as SceneTree
return tree.root if tree != null else null
## The value of a `--name=value` user argument ("" without one).
static func _arg(prefix: String) -> String:
for a: String in OS.get_cmdline_user_args():
if a.begins_with(prefix):
return a.trim_prefix(prefix)
return ""

View file

@ -4,7 +4,9 @@ extends CanvasLayer
## late night on a phone (docs/ios-test-port.md). Over everything, top centre:
## * Now, over the last second: FPS, the frame time (average and worst), the CPU time in
## physics (the farm's tick, FarmSim.step, runs there) and in rendering, the GPU time to draw
## the view, the enemies alive, the 3D view's render size, and the renderer and device.
## the view, the enemies alive, the 3D view's render size, and the renderer and device. And
## the screen as the device reports it (size, density, safe area) with the interface scale
## that gives (MobilePort.describe), to check the density on a phone.
## * This night, from its start to its end (then held until the next night): the frame time's
## average, 99th percentile, and worst, the share of frames slower than 30 FPS, the peak
## enemies alive, and how long it ran. Frames while paused aren't counted. Printed as one
@ -145,6 +147,7 @@ func _refresh(now: int) -> void:
roundi(_second_frames * 1000.0 / span), span / n, _second_worst, _second_physics / n, _second_physics_worst,
_second_render_cpu / n, "%.1f ms" % gpu if gpu > 0.0 else "n/a", enemies, roundi(size_3d.x), roundi(size_3d.y),
RenderingServer.get_current_rendering_method(), RenderingServer.get_current_rendering_driver_name(), OS.get_model_name()]
_now.text += "\n" + MobilePort.describe() # the screen as the phone reports it, and the interface scale it gives
if _in_night:
_night.text = _describe_night("so far")
_second_usec = now