extends RefCounted ## The interface scale and the safe area (MobilePort, GameSettings, #35): ## * scale_for: phones about 1.6 (clamped to 1.7 for the smallest), iPads and desktop 1, the ## same in portrait; a missing or impossible density falls back to the size class. ## * ui_scale: 1 on desktop whatever the Interface size; on a phone, the screen's scale times ## the Interface size (clamped to 0.8-1.3). The desktop preview plays a phone or an iPad by ## the window's shape, or at preview_dpi. ## * GameSettings: interface_size saves, loads, and falls back; apply() sets the window's ## content_scale_factor on a phone (the canvas shrinks by it) and leaves desktop at 1 with the ## same canvas. ## * safe_rect: the whole visible rect on desktop (insets ignored), the window less the ## simulated insets on a phone, mapped into the canvas with the scale; the insets parse. ## * minimum_target: 44 on desktop; on a phone 44 points (about 70 canvas pixels), whatever the ## window's size, and smaller in canvas pixels as the Interface size grows. const SETTINGS := "user://test_mobile_scale_settings.cfg" const PHONE := Vector2i(2340, 1080) # 19.5:9 const IPAD := Vector2i(1600, 1200) # 4:3 var t: GameTestRunner var _layer: CanvasLayer var _probe: Control func run(p_t: GameTestRunner) -> void: t = p_t var root := t.root var size := root.size var factor := root.content_scale_factor var pixels := [root.scaling_3d_mode, root.scaling_3d_scale, root.msaa_3d] _layer = CanvasLayer.new() _probe = Control.new() _layer.add_child(_probe) root.add_child(_layer) _probe.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) _devices() _settings_file() await _desktop() await _phone() await _ipad() await _min_target() _insets() MobilePort.forced = -1 MobilePort.preview_dpi = 0.0 MobilePort.preview_insets = [] GameSettings.new().apply(null) # the defaults for the audio and dead zones, as test_save does root.size = size root.content_scale_factor = factor root.scaling_3d_mode = pixels[0] root.scaling_3d_scale = pixels[1] root.msaa_3d = pixels[2] _layer.queue_free() await t.process_frame if FileAccess.file_exists(SETTINGS): DirAccess.remove_absolute(ProjectSettings.globalize_path(SETTINGS)) ## scale_for: real screens (pixels, density, pixels per point). func _devices() -> void: var cases := [ # name, pixels, dpi, px/pt, low, high ["iPhone 15", Vector2(2556, 1179), 460.0, 3.0, 1.6, 1.75], ["iPhone 15 Pro Max", Vector2(2796, 1290), 460.0, 3.0, 1.5, 1.6], ["iPhone 13 mini", Vector2(2340, 1080), 476.0, 3.0, 1.7, 1.7], ["iPhone SE", Vector2(1334, 750), 326.0, 2.0, 1.7, 1.7], ["iPad (10th)", Vector2(2360, 1640), 264.0, 2.0, 1.0, 1.0], ["iPad mini", Vector2(2266, 1488), 326.0, 2.0, 1.0, 1.0], ["iPad Pro 13\"", Vector2(2752, 2064), 264.0, 2.0, 1.0, 1.0], ["a 1080p desktop monitor", Vector2(1920, 1080), 96.0, 1.0, 1.0, 1.0], ] for c: Array in cases: var s := MobilePort.scale_for(c[1], c[2], c[3]) t.check(s >= c[4] - 0.005 and s <= c[5] + 0.005, "%s: scale %.2f (want %.2f-%.2f)" % [c[0], s, c[4], c[5]]) var portrait := MobilePort.scale_for(Vector2(c[1].y, c[1].x), c[2], c[3]) t.check(is_equal_approx(portrait, s), "%s: the same in portrait (%.2f)" % [c[0], portrait]) t.check(is_equal_approx(MobilePort.scale_for(Vector2(2556, 1179), 460.0, 3.0), 4.3 / (1179.0 / 460.0)), "the scale is 4.3 inches over the shorter side's") # The size class, for a density of 0 or one that can't be right. t.check(MobilePort.scale_for(Vector2(2556, 1179), 0.0, 3.0) == MobilePort.PHONE_SCALE, "no density: a phone (393 pt) gets 1.6") t.check(MobilePort.scale_for(Vector2(2556, 1179), 72.0, 3.0) == MobilePort.PHONE_SCALE, "72 dpi on a phone's pixels: the size class (1.6)") t.check(MobilePort.scale_for(Vector2(2556, 1179), 2000.0, 3.0) == MobilePort.PHONE_SCALE, "2000 dpi: the size class (1.6)") t.check(MobilePort.scale_for(Vector2(2360, 1640), 0.0, 2.0) == MobilePort.TABLET_SCALE, "no density: an iPad (820 pt) gets 1") t.check(MobilePort.scale_for(Vector2(1334, 750), 0.0, 2.0) == MobilePort.PHONE_SCALE, "no density: an iPhone SE (375 pt) gets 1.6") t.check(not MobilePort.dpi_plausible(Vector2(1179, 2556), 0.0) and MobilePort.dpi_plausible(Vector2(1179, 2556), 460.0), "0 dpi is never trusted; an iPhone's 460 is") ## interface_size in the settings file: saved, loaded, clamped, and a default when it's wrong. func _settings_file() -> void: var g := GameSettings.new() t.check(g.interface_size == 1.0, "Interface size defaults to 100%") g.interface_size = 1.25 g.save_to(SETTINGS) t.check(is_equal_approx(GameSettings.load_from(SETTINGS).interface_size, 1.25), "Interface size saves and loads") var f := ConfigFile.new() f.load(SETTINGS) f.set_value("touch", "interface_size", 5.0) f.save(SETTINGS) t.check(GameSettings.load_from(SETTINGS).interface_size == 1.3, "a size above 130% loads as 130%") f.set_value("touch", "interface_size", "big") f.save(SETTINGS) t.check(GameSettings.load_from(SETTINGS).interface_size == 1.0, "a size that isn't a number loads as 100%") ## Desktop: factor 1, the same canvas, whatever the Interface size, and insets don't apply. func _desktop() -> void: MobilePort.forced = 0 MobilePort.preview_insets = [140, 0, 140, 60] var root := t.root for size: Vector2i in [Vector2i(1920, 1080), PHONE]: root.size = size var g := GameSettings.new() g.interface_size = 1.3 g.apply(root) await t.process_frame t.check(root.content_scale_factor == 1.0 and MobilePort.ui_scale(1.3) == 1.0 and MobilePort.ui_scale(0.8) == 1.0, "desktop %s: the factor stays 1 (%.2f)" % [size, root.content_scale_factor]) var want := Vector2(size) * (1080.0 / size.y) # the 1080-tall canvas, as before t.check(root.get_visible_rect().size.is_equal_approx(want), "desktop %s: the canvas is %s (%s)" % [size, want, root.get_visible_rect().size]) t.check(MobilePort.safe_rect(_probe) == _probe.get_viewport_rect(), "desktop %s: the safe rect is the visible rect" % size) t.check(MobilePort.minimum_target() == 44.0 and MobilePort.minimum_target(_probe) == 44.0, "desktop: the minimum target is 44") t.check(Vector2(root.get_final_transform().get_scale()).is_equal_approx(Vector2.ONE), "desktop 2340x1080: no extra scale") MobilePort.preview_insets = [] ## A 19.5:9 phone in the preview: about 1.6, applied as the window's factor, and the safe rect ## inside the notch and home indicator insets. func _phone() -> void: MobilePort.forced = 1 var root := t.root root.size = PHONE t.check(MobilePort.previewing(), "forced to a phone on desktop: the preview") var s := MobilePort.screen_scale() t.check(s > 1.55 and s < 1.65, "a 19.5:9 window plays a phone: scale %.2f (about 1.6)" % s) t.check(is_equal_approx(MobilePort.ui_scale(), s) and is_equal_approx(MobilePort.ui_scale(1.25), s * 1.25) and is_equal_approx(MobilePort.ui_scale(5.0), s * 1.3) and is_equal_approx(MobilePort.ui_scale(0.1), s * 0.8), "ui_scale is that times the Interface size, clamped to 0.8-1.3") MobilePort.preview_dpi = 460.0 root.size = Vector2i(2556, 1179) t.check(is_equal_approx(MobilePort.screen_scale(), 4.3 / (1179.0 / 460.0)), "preview_dpi 460 at 2556x1179: an iPhone 15's %.2f" % MobilePort.screen_scale()) MobilePort.preview_dpi = 0.0 root.size = PHONE var g := GameSettings.new() g.apply(root) await t.process_frame t.check(is_equal_approx(root.content_scale_factor, s), "GameSettings.apply sets the factor to the scale (%.2f)" % root.content_scale_factor) var canvas := root.get_visible_rect().size t.check(canvas.is_equal_approx(Vector2(PHONE) / s), "the canvas shrinks by it: %s" % canvas) t.check(canvas.x > 1400.0 and canvas.x < 1520.0 and canvas.y > 650.0 and canvas.y < 700.0, "about 1460x675 (%s)" % canvas) t.check(_probe.size.is_equal_approx(canvas), "a full-rect HUD Control gets that canvas (%s)" % _probe.size) g.interface_size = 1.25 g.apply_scale(root) await t.process_frame t.check(is_equal_approx(root.content_scale_factor, s * 1.25), "Interface size 125%%: factor %.2f" % root.content_scale_factor) g.interface_size = 1.0 g.apply_scale(root) await t.process_frame # The safe rect, no inset: the whole canvas. var full := MobilePort.safe_rect(_probe) t.check(full.position.is_zero_approx() and full.size.is_equal_approx(canvas), "no inset: the safe rect is the canvas (%s)" % full) # A notch on both sides (the phone may turn either way) and the home indicator. MobilePort.preview_insets = [140, 0, 140, 60] var safe := MobilePort.safe_rect(_probe) var want := Rect2(Vector2(140.0, 0.0) / s, Vector2(PHONE.x - 280.0, PHONE.y - 60.0) / s) t.check(safe.position.is_equal_approx(want.position) and safe.size.is_equal_approx(want.size), "inset 140,0,140,60 px: the safe rect %s (want %s)" % [safe, want]) t.check(Rect2(Vector2.ZERO, canvas).encloses(safe), "it's inside the canvas") var corner := Vector2(root.get_final_transform() * safe.end) t.check(corner.is_equal_approx(Vector2(PHONE.x - 140.0, PHONE.y - 60.0)), "its far corner is %s window pixels (2200, 1020)" % corner) var line := MobilePort.describe() # PerfTest's readout t.check(line.contains("Screen 2340×1080 · 400 dpi") and line.contains("safe area 140,0 2060×1020") and line.contains("(dpi)") and line.contains("canvas 1469×678"), "describe(): %s" % line.replace("\n", " / ")) # A layer that's moved and scaled gets the same area in its own coordinates. var moved := CanvasLayer.new() moved.offset = Vector2(100.0, 50.0) moved.scale = Vector2(2.0, 2.0) var c := Control.new() moved.add_child(c) root.add_child(moved) var there := MobilePort.safe_rect(c) t.check(there.position.is_equal_approx((want.position - moved.offset) / 2.0) and there.size.is_equal_approx(want.size / 2.0), "in a moved, scaled layer: %s" % there) moved.free() # Too much inset: the whole window rather than nothing. MobilePort.preview_insets = [2000, 0, 2000, 0] t.check(MobilePort.safe_rect(_probe).size.is_equal_approx(canvas), "insets wider than the window: the whole canvas") MobilePort.preview_insets = [] ## A 4:3 window plays an iPad: 1x, the canvas 1920x1440 as before. func _ipad() -> void: MobilePort.forced = 1 var root := t.root root.size = IPAD var s := MobilePort.screen_scale() t.check(s == 1.0, "a 4:3 window plays an iPad: scale %.2f" % s) GameSettings.new().apply_scale(root) await t.process_frame t.check(root.content_scale_factor == 1.0 and root.get_visible_rect().size.is_equal_approx(Vector2(1920.0, 1440.0)), "4:3 at 1x: the canvas is 1920x1440 (%s)" % root.get_visible_rect().size) MobilePort.preview_insets = [0, 40, 0, 40] var safe := MobilePort.safe_rect(_probe) var k := 1920.0 / IPAD.x # canvas pixels per window pixel t.check(safe.position.is_equal_approx(Vector2(0.0, 40.0 * k)) and safe.size.is_equal_approx(Vector2(1920.0, (IPAD.y - 80.0) * k)), "4:3 inset 0,40,0,40: %s" % safe) MobilePort.preview_insets = [] ## The minimum touch target: 44 points, about 70 canvas pixels on a phone and on an iPad, the ## same at any window size, and smaller in canvas pixels as the Interface size grows. func _min_target() -> void: MobilePort.forced = 1 var root := t.root var targets := {} for size: Vector2i in [PHONE, Vector2i(1170, 540), IPAD]: root.size = size GameSettings.new().apply_scale(root) await t.process_frame var m := MobilePort.minimum_target(_probe) targets[size] = m t.check(m > 60.0 and m < 80.0, "%s: the minimum target is %.1f canvas px (about 70)" % [size, m]) t.check(is_equal_approx(m, MobilePort.minimum_target()), "%s: the same without a canvas item" % size) var points := m * root.get_final_transform().get_scale().y / MobilePort.pixels_per_point() t.check(is_equal_approx(points, 44.0), "%s: that's 44 points (%.2f)" % [size, points]) t.check(is_equal_approx(targets[PHONE], targets[Vector2i(1170, 540)]), "the phone's target doesn't depend on the window's pixels") root.size = PHONE var g := GameSettings.new() g.interface_size = 1.3 g.apply_scale(root) await t.process_frame t.check(is_equal_approx(MobilePort.minimum_target(_probe), targets[PHONE] / 1.3), "at 130%% it's 1.3 times fewer canvas pixels (%.1f)" % MobilePort.minimum_target(_probe)) ## The insets parse from numbers or text (as --safe-area=l,t,r,b), and anything else is none. func _insets() -> void: MobilePort.forced = 1 MobilePort.preview_insets = ["140", "0", "140.5", "60"] t.check(MobilePort.insets() == [140.0, 0.0, 140.5, 60.0], "text insets parse: %s" % [MobilePort.insets()]) MobilePort.preview_insets = [10, -5, 0, 0] t.check(MobilePort.insets() == [10.0, 0.0, 0.0, 0.0], "a negative inset is none") MobilePort.preview_insets = ["a", "0", "0", "0"] t.check(MobilePort.insets() == [0.0, 0.0, 0.0, 0.0], "a bad inset: none at all") MobilePort.preview_insets = [] t.check(MobilePort.insets() == [0.0, 0.0, 0.0, 0.0], "no --safe-area: no inset")