harvest/game/tools/capture_mobile.gd
Forrest aee60b0ba2 #35: interface scale, safe area, and the mobile test kit
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>
2026-10-14 14:40:06 -04:00

110 lines
4.9 KiB
GDScript

extends SceneTree
## Captures the phone build's day HUD for review: runs the real game (main.tscn) in a window as a
## phone (MobilePort's preview: pass -- --mobile, so Main sets the farm up as on a phone and
## GameSettings.apply scales the interface), starts a New vacation without its intro, begins day
## 1, skips the day's conversation, and saves the screen; then the same with the safe area
## marked (MobilePort.safe_rect outlined, outside it shaded), and quits. It prints what the scale
## came to (MobilePort.describe). It needs a window (not --headless) that isn't fullscreen (the
## project's default), so pass --windowed and a resolution; keep it visible and pass
## --disable-vsync (a hidden window with VSync on crawls on some desktops). The desktop may
## resize a big window (one desktop here maximized a 2340x1080 one), so --size=WxH makes it
## borderless at exactly that size once it's up, and applies the scale for it again:
## godot --path . --rendering-method mobile --windowed --resolution 2340x1080 --screen 2 \
## --disable-vsync --script res://game/tools/capture_mobile.gd -- --mobile \
## --size=2340x1080 --safe-area=140,0,140,60 --out=/tmp/hs-mobile --prefix=phone
## Options (after --): --out=dir (default /tmp/hs-mobile/), --prefix=text (default "day"),
## --size=WxH, and MobilePort's own --safe-area=l,t,r,b and --dpi=N. It saves to its own
## user://capture_mobile_* files, so the player's save and settings are never touched.
const SAVE := "user://capture_mobile_save.json"
const SETTINGS := "user://capture_mobile_settings.cfg"
const SHADE := Color(1.0, 0.0, 0.6, 0.3)
const EDGE := Color(1.0, 0.0, 0.6, 0.9)
var _out := "/tmp/hs-mobile/"
var _prefix := "day"
var _size := Vector2i.ZERO
func _initialize() -> void:
for a in OS.get_cmdline_user_args():
var arg := String(a)
if arg.begins_with("--out="):
_out = arg.trim_prefix("--out=")
elif arg.begins_with("--prefix="):
_prefix = arg.trim_prefix("--prefix=")
elif arg.begins_with("--size="):
var wh := arg.trim_prefix("--size=").split("x")
if wh.size() == 2:
_size = Vector2i(int(wh[0]), int(wh[1]))
if not _out.ends_with("/"):
_out += "/"
_run.call_deferred()
func _run() -> void:
_clean()
DirAccess.make_dir_recursive_absolute(_out)
if not MobilePort.active():
push_warning("capture_mobile: not a phone; pass -- --mobile to see the phone build")
var main := (load("res://game/scenes/main.tscn") as PackedScene).instantiate() as Main
main.save_path = SAVE
main.settings_path = SETTINGS
root.add_child(main)
for i in 5:
await process_frame
if _size != Vector2i.ZERO and root.size != _size:
root.borderless = true # the desktop won't shrink or tile it then
root.size = _size
root.position = DisplayServer.screen_get_position(root.current_screen)
for i in 20:
await process_frame
main.settings.apply_scale(root) # the scale for the window's new shape
main.new_vacation(false)
await process_frame
main.sim.campaign.advance_phase() # the first morning's sign -> day 1
for i in 30:
await process_frame
if paused and main.screen == Main.Screen.PLAYING and main.sim.dialogue != null:
main.sim.dialogue.box.skip() # the day's conversation pauses the farm
await create_timer(1.5).timeout # the HUD settles (tips fade in, the camera arrives)
print("capture_mobile: window %s, canvas %s" % [root.size, root.get_visible_rect().size])
print("capture_mobile: ", MobilePort.describe())
await _shot("")
var marks := CanvasLayer.new()
marks.layer = 100
var c := Control.new()
c.mouse_filter = Control.MOUSE_FILTER_IGNORE
marks.add_child(c)
root.add_child(marks)
c.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
c.draw.connect(func() -> void:
var all := c.get_viewport_rect()
var safe := MobilePort.safe_rect(c)
c.draw_rect(Rect2(all.position, Vector2(all.size.x, safe.position.y - all.position.y)), SHADE)
c.draw_rect(Rect2(Vector2(all.position.x, safe.end.y), Vector2(all.size.x, all.end.y - safe.end.y)), SHADE)
c.draw_rect(Rect2(Vector2(all.position.x, safe.position.y), Vector2(safe.position.x - all.position.x, safe.size.y)), SHADE)
c.draw_rect(Rect2(Vector2(safe.end.x, safe.position.y), Vector2(all.end.x - safe.end.x, safe.size.y)), SHADE)
c.draw_rect(safe, EDGE, false, 3.0)
var t := MobilePort.minimum_target(c)
c.draw_rect(Rect2(safe.get_center() - Vector2(t, t) * 0.5, Vector2(t, t)), EDGE, false, 2.0)) # the minimum touch target
await _shot("_safe")
main.queue_free()
await process_frame
await create_timer(0.3).timeout
_clean()
quit()
func _shot(suffix: String) -> void:
await process_frame
await process_frame
var path := "%s%s%s.png" % [_out, _prefix, suffix]
var err := root.get_texture().get_image().save_png(path)
print("capture_mobile: %s (%s)" % [path, error_string(err)])
func _clean() -> void:
for p in [SAVE, SAVE + ".tmp", SETTINGS]:
if FileAccess.file_exists(p):
DirAccess.remove_absolute(ProjectSettings.globalize_path(p))