harvest/game/tests/mobile/platform_test.gd
Forrest c47e67891e #37: platform back button, no Quit on phones, flat labels, camera
- Android back: application/config/quit_on_go_back=false; Main.go_back()
  (from NOTIFICATION_WM_GO_BACK_REQUEST, phones only) closes the shop or
  pouch panel or leaves build mode, else pauses; paused it closes the
  question, then settings, then resumes (shared with the pause action);
  at the main menu it closes the question or settings, else quits.
- Phones hide the main menu's Quit and the pause menu's Quit the game.
- ScreenLabels.ensure() makes the one ScreenLabels (ToonLook's on
  desktop; Main's on phones, which watch()es the tree for Label3Ds).
  Label sizes grow with the window's content_scale_factor (1 on desktop).
- FollowCamera starts at PHONE_DISTANCE (12 m) on phones.
- MobilePort.forced for tests; game/tests/mobile/platform_test.gd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:36:26 -04:00

255 lines
11 KiB
GDScript

extends RefCounted
## #37, the phone's platform details, through the entry scene (main.tscn, test save and settings
## paths) with MobilePort.forced:
## * Android's back button (NOTIFICATION_WM_GO_BACK_REQUEST; quit_on_go_back is off, so the app
## doesn't just close): playing, it pauses (over the intro and the morning sign too); paused,
## it closes the question, then settings, then resumes; it closes the workbench's and the
## shed's panels, the pouch panel, and build mode before pausing; at the main menu it closes the
## question and settings, then quits (intercepted here, so the runner lives).
## * No Quit buttons on phones (focus moves past where they were); ScreenLabels, only one (a
## ToonLook added anyway shares it), draws the Label3Ds flat without ToonLook, growing with
## the interface scale; the camera starts at 12 m.
## * Desktop (forced 0) as before: both Quit buttons, back ignored, Escape / Start pauses and
## resumes, ToonLook's ScreenLabels, the camera at 16 m.
const SAVE := "user://test_platform_save.json"
const SETTINGS := "user://test_platform_settings.cfg"
const DT := 1.0 / 60.0
var t: GameTestRunner
func run(p_t) -> void:
t = p_t
_clean()
t.check(ProjectSettings.get_setting("application/config/quit_on_go_back") == false and not t.quit_on_go_back,
"back doesn't close the app by itself (quit_on_go_back off): Main handles it")
MobilePort.forced = 1
var main := await _start()
_phone_quit(main)
await _phone_back(main)
_camera(main, FollowCamera.PHONE_DISTANCE, "phones")
await _phone_labels(main)
await _phone_back_at_menu(main)
await _stop(main)
_clean()
MobilePort.forced = 0
main = await _start()
await _desktop(main)
_camera(main, 16.0, "desktop")
await _stop(main)
MobilePort.forced = -1
_clean()
func _clean() -> void:
for p in [SAVE, SAVE + ".tmp", SETTINGS]:
if FileAccess.file_exists(p):
DirAccess.remove_absolute(ProjectSettings.globalize_path(p))
func _start() -> Main:
var main := (load("res://game/scenes/main.tscn") as PackedScene).instantiate() as Main
main.save_path = SAVE
main.settings_path = SETTINGS
t.root.add_child(main)
await t.process_frame
await t.physics_frame
return main
func _stop(main: Main) -> void:
main.queue_free()
t.paused = false
await t.process_frame
## The back button as the platform sends it: to every node from the root.
func _back() -> void:
t.root.propagate_notification(Node.NOTIFICATION_WM_GO_BACK_REQUEST)
## The pause action as Escape / Start sends it.
func _press(action: String) -> void:
for pressed in [true, false]:
var e := InputEventAction.new()
e.action = StringName(action)
e.pressed = pressed
Input.parse_input_event(e)
await t.process_frame
await t.process_frame
func _skip_intro(sim: FarmSim) -> void:
sim.intro.skip()
var s := 0.0
while sim.intro.busy() and s < 10.0:
sim.intro.step(DT)
s += DT
func _screen_labels() -> Array:
return t.root.find_children("*", "ScreenLabels", true, false).filter(
func(n: Node) -> bool: return not n.is_queued_for_deletion())
func _paused_menu(main: Main) -> bool:
return main.screen == Main.Screen.PAUSED and t.paused and main.pause_menu.visible
func _playing(main: Main) -> bool:
return main.screen == Main.Screen.PLAYING and not t.paused and not main.pause_menu.visible
func _phone_quit(main: Main) -> void:
var mm := main.main_menu
var pm := main.pause_menu
t.check(not mm.quit_button.visible and not pm.quit_button.visible and mm.settings_button.visible and pm.menu_button.visible,
"phones: no Quit on the main menu or the pause menu (the rest are there)")
t.check(mm.settings_button.find_valid_focus_neighbor(SIDE_BOTTOM) != mm.quit_button
and mm.settings_button.find_next_valid_focus() != mm.quit_button
and pm.menu_button.find_valid_focus_neighbor(SIDE_BOTTOM) != pm.quit_button
and pm.menu_button.find_next_valid_focus() != pm.quit_button,
"and focus (arrows, Tab) never lands on the hidden ones")
func _phone_back(main: Main) -> void:
var sim := main.sim
var c := sim.campaign
main.main_menu.settings_button.pressed.emit()
t.check(main.settings_menu.is_open, "(settings open from the main menu)")
_back()
t.check(not main.settings_menu.is_open and main.screen == Main.Screen.MENU and main.main_menu.visible,
"back at the main menu's settings closes them, back to the menu")
main.main_menu.new_button.pressed.emit()
t.check(main.screen == Main.Screen.PLAYING and sim.intro.running, "(a new vacation, its intro playing)")
_back()
t.check(_paused_menu(main) and sim.intro.running, "back during the intro pauses (Restart the night skips it from there)")
_back()
t.check(_playing(main) and sim.intro.running, "and back again resumes it")
_skip_intro(sim)
t.check(c.state.phase == CampaignController.Phase.MORNING and sim.review_panel.visible, "(the first morning's sign)")
_back()
t.check(_paused_menu(main) and sim.review_panel.visible, "back over the morning sign pauses (the sign stays)")
main.pause_menu.settings_button.pressed.emit()
t.check(main.settings_menu.is_open and not main.pause_menu.visible, "(settings open from the pause menu)")
_back()
t.check(not main.settings_menu.is_open and _paused_menu(main), "back closes settings, back to the pause menu")
main.pause_menu.restart_button.pressed.emit()
t.check(main.confirm.is_open, "(Restart the night asks first)")
_back()
t.check(not main.confirm.is_open and _paused_menu(main) and c.state.phase == CampaignController.Phase.MORNING,
"back closes the question (nothing restarted), back to the pause menu")
_back()
t.check(_playing(main), "and back resumes")
c.begin_day()
await t.physics_frame
sim.upgrade_panel.open()
t.check(sim.upgrade_panel.is_open, "(the workbench's panel open)")
_back()
t.check(not sim.upgrade_panel.is_open and _playing(main), "back closes the workbench's panel and doesn't pause")
sim.shed_panel.open()
_back()
t.check(not sim.shed_panel.is_open and _playing(main), "and the shed's")
sim.resource_hud.toggle()
t.check(sim.resource_hud.open, "(the pouch panel open)")
_back()
t.check(not sim.resource_hud.open and _playing(main), "back closes the pouch panel")
sim.build.set_active(true)
_back()
t.check(not sim.build.active and _playing(main), "back leaves build mode")
_back()
t.check(_paused_menu(main), "and with nothing open, back pauses")
_back()
func _camera(main: Main, want: float, platform: String) -> void:
var cam := main.sim.camera as FollowCamera
t.check(cam != null and is_equal_approx(cam.distance, want), "%s: the camera starts %.0f m away (%.1f)" % [platform, want, cam.distance])
func _phone_labels(main: Main) -> void:
var sim := main.sim
var labels := _screen_labels()
t.check(main.get_node_or_null("ToonLook") == null and labels.size() == 1 and (labels[0] as Node).get_parent() is CanvasLayer
and ((labels[0] as Node).get_parent() as CanvasLayer).layer == -1,
"phones: no ToonLook, and one ScreenLabels on its layer under the HUD (%d)" % labels.size())
var drawer := labels[0] as ScreenLabels
var label3ds := main.farm.find_children("*", "Label3D", true, false)
t.check(label3ds.size() >= 5 and label3ds.all(func(l: Node) -> bool: return (l as Label3D).layers == 0),
"which draws the farm's %d Label3Ds flat (none render in 3D)" % label3ds.size())
var cam := t.root.get_camera_3d()
t.check(cam == sim.camera, "(playing: the follow camera)")
var sign := Label3D.new()
sign.text = "Gather"
sign.font_size = 40
sign.pixel_size = 0.0075
sign.outline_size = 10
main.farm.add_child(sign)
sign.global_position = cam.global_position - cam.global_basis.z * 10.0
sign.reset_physics_interpolation()
await t.process_frame
var drawn := drawer.entries().filter(func(e: Dictionary) -> bool: return e["label"] == sign)
t.check(sign.layers == 0 and drawn.size() == 1 and drawn[0]["text"] == "Gather"
and drawn[0]["font_size"] >= ScreenLabels.MIN_SIZE and t.root.get_visible_rect().encloses(drawn[0]["rect"]),
"and a label added later is drawn flat too, at a readable size (%s)" % [drawn[0]["font_size"] if drawn.size() == 1 else "not drawn"])
var was := t.root.content_scale_factor
t.root.content_scale_factor = 1.0
drawn = drawer.entries().filter(func(e: Dictionary) -> bool: return e["label"] == sign)
var at_1x: int = drawn[0]["font_size"] if drawn.size() == 1 else 0
t.root.content_scale_factor = 1.6 # the phone's interface scale (MobilePort.ui_scale, #35)
drawn = drawer.entries().filter(func(e: Dictionary) -> bool: return e["label"] == sign)
var at_16x: int = drawn[0]["font_size"] if drawn.size() == 1 else 0
t.root.content_scale_factor = was
t.check(at_1x > 0 and absi(at_16x - at_1x) <= 1,
"labels scale with the interface: at 1.6x the same canvas size (%d, was %d), so 1.6x as big on screen as in 3D" % [at_16x, at_1x])
sign.queue_free()
t.check(ScreenLabels.ensure(main) == drawer, "asking for ScreenLabels again gets the same one")
var look := ToonLook.new() # if both ever applied
main.add_child(look)
t.check(look.labels == drawer and _screen_labels().size() == 1, "a ToonLook added on a phone shares it: still one")
look.queue_free()
await t.process_frame
func _phone_back_at_menu(main: Main) -> void:
main.to_menu()
t.check(main.screen == Main.Screen.MENU and main.main_menu.visible, "(back at the main menu)")
main.main_menu.new_button.pressed.emit()
t.check(main.confirm.is_open, "(New vacation asks first: it would replace the save)")
_back()
t.check(not main.confirm.is_open and main.screen == Main.Screen.MENU and main.main_menu.visible,
"back at the main menu closes the question")
var quits := [0]
main.main_menu.quit_requested.disconnect(main.quit_game) # the runner must live
main.main_menu.quit_requested.connect(func() -> void: quits[0] += 1)
_back()
t.check(quits[0] == 1, "and back at the main menu itself quits (Android's way), as Quit would")
func _desktop(main: Main) -> void:
var mm := main.main_menu
var pm := main.pause_menu
t.check(mm.quit_button.visible and mm.quit_button.text == "Quit" and pm.quit_button.visible and pm.quit_button.text == "Quit the game",
"desktop: Quit on the main menu and Quit the game on the pause menu")
var look := main.get_node_or_null("ToonLook") as ToonLook
var labels := _screen_labels()
t.check(look != null and labels.size() == 1 and labels[0] == look.labels and look.labels.get_parent().get_parent() == look,
"desktop: ToonLook makes the one ScreenLabels, under itself (%d)" % labels.size())
var quits := [0]
mm.quit_requested.disconnect(main.quit_game)
mm.quit_requested.connect(func() -> void: quits[0] += 1)
_back()
t.check(quits[0] == 0 and main.screen == Main.Screen.MENU and mm.visible, "desktop: a back request does nothing at the main menu")
mm.new_button.pressed.emit()
_skip_intro(main.sim)
_back()
t.check(_playing(main), "or while playing")
await _press("pause")
t.check(_paused_menu(main), "desktop: Escape / Start still pauses")
pm.settings_button.pressed.emit()
await _press("pause")
t.check(not main.settings_menu.is_open and _paused_menu(main), "closes settings")
await _press("pause")
t.check(_playing(main), "and resumes")