harvest/game/tests/mobile/sticks_test.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

187 lines
7.9 KiB
GDScript

extends RefCounted
## The thumbsticks (TouchSticks) by touch, through the game's entry scene (main.tscn, with test
## save and settings paths) on a 19.5:9 phone at its interface scale: the 24 checks of the
## driver that came with them (8c81d81).
## * On a phone Main adds them, fire loses its mouse button, and combat aims by stick.
## * A finger on the right half is the move stick (it walks; its emulated click doesn't fire),
## one on the left half the attack stick (it aims screen-up and attacks while held, without
## switching combat to mouse aim); both work at once, and lifting a finger stops it. A tilt
## inside the aim dead zone doesn't fire.
## * A pause lets go of the sticks, and nothing walks on after it.
## * A button gets its touch first: no stick starts.
## * In the intro the sticks are off, and a held finger skips it.
## The InputMap's mouse buttons for fire and offhand (TouchSticks erases them) are put back.
const Touch := preload("res://game/tests/mobile/touch.gd")
const SAVE := "user://test_sticks_save.json"
const SETTINGS := "user://test_sticks_settings.cfg"
const PHONE := Vector2i(2340, 1080)
var t: GameTestRunner
var main: Main
var attacks: Array[int] = []
func run(p_t: GameTestRunner) -> void:
t = p_t
_clean()
var bindings := {}
for action: StringName in [&"fire", &"offhand"]:
bindings[action] = InputMap.action_get_events(action)
var size := t.root.size
var factor := t.root.content_scale_factor
MobilePort.forced = 1
t.root.size = PHONE
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.process_frame
await _sticks()
main.queue_free()
t.paused = false
await t.process_frame
for action: StringName in bindings:
InputMap.action_erase_events(action)
for e: InputEvent in bindings[action]:
InputMap.action_add_event(action, e)
for action in [&"fire", &"offhand", &"move_left", &"move_right", &"move_forward", &"move_back",
&"aim_left", &"aim_right", &"aim_up", &"aim_down"]:
Input.action_release(action)
MobilePort.forced = -1
t.root.size = size
t.root.content_scale_factor = factor
_clean()
func _clean() -> void:
for p in [SAVE, SAVE + ".tmp", SETTINGS]:
if FileAccess.file_exists(p):
DirAccess.remove_absolute(ProjectSettings.globalize_path(p))
func _physics(n: int) -> void:
for k in n:
await t.physics_frame
## Skips the first day's conversation, which pauses the farm.
func _skip_talk() -> void:
for k in 20:
await t.process_frame
if t.paused and main.screen == Main.Screen.PLAYING and main.sim.dialogue != null:
main.sim.dialogue.box.skip()
func _move() -> Vector2:
return Input.get_vector("move_left", "move_right", "move_forward", "move_back")
func _sticks() -> void:
var sticks := main.farm.get_node_or_null("Hud/TouchSticks") as TouchSticks
t.check(sticks != null, "TouchSticks added on a phone")
if sticks == null:
return
t.check(t.root.content_scale_factor > 1.5, "at the phone's interface scale (%.2f)" % t.root.content_scale_factor)
var mouse_fire := InputMap.action_get_events("fire").filter(func(e: InputEvent) -> bool: return e is InputEventMouseButton)
t.check(mouse_fire.is_empty(), "fire has no mouse button")
t.check(main.sim.combat.device == ScarecrowCombat.Device.GAMEPAD, "combat aims by stick")
main.sim.combat.attacked.connect(func(_w: int) -> void: attacks.append(0))
main.new_vacation(false)
await t.process_frame
main.sim.campaign.advance_phase() # morning -> day
await _skip_talk()
t.check(sticks.can_act(), "sticks can act in the day (phase %d)" % main.sim.campaign.state.phase)
var w := sticks.size
var right := Vector2(w.x * 0.75, w.y * 0.6)
var left := Vector2(w.x * 0.25, w.y * 0.6)
var player := main.sim.scarecrow.player
# Move: finger 0 on the right half, dragged to the rim, rightward.
var p0 := player.global_position
Touch.touch(0, right, true)
Touch.drag(0, right, right + Vector2(TouchSticks.RADIUS, 0.0))
await _physics(40)
t.check(_move().x > 0.9 and absf(_move().y) < 0.1, "move stick tilted right reads (1, 0): %s" % _move())
t.check(sticks.finger[TouchSticks.Stick.MOVE] == 0, "right half started the move stick")
t.check(player.global_position.distance_to(p0) > 1.0, "the scarecrow walked %.1f m" % player.global_position.distance_to(p0))
t.check(not Input.is_action_pressed("fire"), "moving doesn't fire (the tap's emulated click)")
# Attack: finger 1 on the left half, dragged up (forward on screen).
var a0 := attacks.size()
Touch.touch(1, left, true)
Touch.drag(1, left, left + Vector2(0.0, -TouchSticks.RADIUS))
await _physics(60)
t.check(Input.is_action_pressed("fire"), "attack stick holds fire")
t.check(attacks.size() - a0 >= 2, "attacks repeat while held (%d)" % (attacks.size() - a0))
var cam := t.root.get_camera_3d()
var fwd := Vector2(-cam.global_basis.z.x, -cam.global_basis.z.z).normalized()
t.check(main.sim.combat.aim.dot(fwd) > 0.9, "aim is screen-up (dot %.2f)" % main.sim.combat.aim.dot(fwd))
t.check(main.sim.combat.device == ScarecrowCombat.Device.GAMEPAD, "taps didn't switch combat to mouse aim")
t.check(_move().x > 0.9, "move holds while attacking")
# Let go of the attack stick.
Touch.touch(1, left + Vector2(0.0, -TouchSticks.RADIUS), false)
await _physics(5)
var a1 := attacks.size()
await _physics(40)
t.check(not Input.is_action_pressed("fire") and attacks.size() == a1, "releasing stops the attacks")
# Let go of the move stick.
Touch.touch(0, right + Vector2(TouchSticks.RADIUS, 0.0), false)
await _physics(5)
var p1 := player.global_position
await _physics(30)
t.check(_move() == Vector2.ZERO and player.global_position.distance_to(p1) < 0.2, "releasing stops the walk")
# A small tilt (inside the aim dead zone) aims nowhere and doesn't fire.
Touch.touch(1, left, true)
Touch.drag(1, left, left + Vector2(TouchSticks.RADIUS * 0.15, 0.0))
await _physics(10)
t.check(not Input.is_action_pressed("fire"), "a tilt inside the dead zone doesn't fire")
Touch.touch(1, left, false)
await _physics(3)
# A pause mid-walk lets go.
Touch.touch(0, right, true)
Touch.drag(0, right, right + Vector2(0.0, -TouchSticks.RADIUS))
await _physics(10)
t.check(_move().y < -0.9, "walking forward")
main.pause()
await t.process_frame
t.check(_move() == Vector2.ZERO and sticks.finger[TouchSticks.Stick.MOVE] == -1, "pause lets the sticks go")
main.resume()
await _physics(5)
var p2 := player.global_position
await _physics(30)
t.check(player.global_position.distance_to(p2) < 0.2, "no walking on after the pause")
Touch.touch(0, right, false) # the finger lifted after the pause: ignored
await t.process_frame
await _button(sticks, right)
# The intro: a held finger skips it; the sticks stay out of it.
main.new_vacation()
await t.process_frame
t.check(main.sim.intro.running and not sticks.can_act(), "intro running, sticks off")
Touch.touch(0, right, true)
await t.create_timer(OpeningIntro.SKIP_HOLD + 0.6).timeout
t.check(sticks.finger[TouchSticks.Stick.MOVE] == -1, "no stick during the intro")
t.check(main.sim.intro._skipping or not main.sim.intro.running, "holding a finger skips the intro")
Touch.touch(0, right, false)
await t.process_frame
## A button over the move half (as PerfTest's were, in the driver) takes its tap: no stick starts.
func _button(sticks: TouchSticks, at: Vector2) -> void:
var layer := CanvasLayer.new()
layer.layer = 20
var b := Button.new()
b.focus_mode = Control.FOCUS_NONE
b.size = Vector2(200.0, 100.0)
b.position = at - b.size * 0.5
layer.add_child(b)
main.add_child(layer)
await t.process_frame
var pressed: Array[int] = []
b.pressed.connect(func() -> void: pressed.append(0))
Touch.touch(0, at, true)
await t.process_frame
t.check(sticks.finger[TouchSticks.Stick.MOVE] == -1 and sticks.finger[TouchSticks.Stick.ATTACK] == -1, "a touch on a button starts no stick")
Touch.touch(0, at, false)
await t.process_frame
t.check(pressed.size() == 1, "the button got the tap")
layer.free()