MobilePort: ui_scale() (clampf(4.3 / the screen's shorter side in inches, 1, 1.8) times the
Interface size; 1 on desktop), a size-class fallback (below 500 pt = phone, 1.6) when the
density is 0 or implausible, safe_rect(ci) (the display's safe area mapped into ci's canvas
through the final transform), minimum_target() (44 pt in canvas pixels), and the `forced`
override for tests. The desktop preview (-- --mobile) plays the phone in the window:
--safe-area=l,t,r,b insets it, --dpi=N sets its density, and without --dpi a 16:9-or-wider
window is a 2.7 in phone (about 1.6x) and a squarer one a 6 in iPad (1x).
GameSettings: interface_size (0.8-1.3, saved under [touch]) and apply_scale(), which sets the
window's content_scale_factor (always 1 on desktop). PerfTest shows the screen, density, safe
area, and resulting scale, to check the density on a device.
Test kit: test_mobile (last in SUITES) runs game/tests/mobile/*_test.gd in name order and puts
the root window back between files; mobile/touch.gd sends touches through
Input.parse_input_event; scale_test covers phones, iPads, desktop, the safe rect, and the target;
sticks_test is the 8c81d81 driver's 24 checks. game/tools/capture_mobile.gd takes the phone
screenshots.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
239 lines
11 KiB
GDScript
239 lines
11 KiB
GDScript
class_name MobilePort
|
||
extends RefCounted
|
||
## The phone build's switches (the iOS preset in export_presets.cfg; docs/ios-test-port.md):
|
||
## * active(): a phone (the "mobile" feature). The farm keeps its standard materials (Main
|
||
## doesn't add ToonLook: the outline needs Forward+, phones run the Mobile renderer), the 3D
|
||
## view renders at GameSettings.resolution_scale instead of pixelated, co-op is hidden (the
|
||
## 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
|
||
## on, to see the phone build without a phone (add --rendering-method mobile for its renderer).
|
||
|
||
|
||
## Tests: -1 detects the platform, 0 forces desktop, 1 forces a phone.
|
||
static var forced := -1
|
||
|
||
|
||
static func active() -> bool:
|
||
if forced >= 0:
|
||
return forced == 1
|
||
return OS.has_feature("mobile") or "--mobile" in OS.get_cmdline_user_args()
|
||
|
||
|
||
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 ""
|