Merge #35: interface scale, safe area, and the mobile test kit
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
ad08f83389
14 changed files with 868 additions and 3 deletions
244
game/tests/mobile/scale_test.gd
Normal file
244
game/tests/mobile/scale_test.gd
Normal file
|
|
@ -0,0 +1,244 @@
|
|||
extends RefCounted
|
||||
## The interface scale and the safe area (MobilePort, GameSettings, #35):
|
||||
## * scale_for: phones about 1.6 (clamped to 1.8 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.8, 1.8],
|
||||
["iPhone SE", Vector2(1334, 750), 326.0, 2.0, 1.8, 1.8],
|
||||
["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")
|
||||
1
game/tests/mobile/scale_test.gd.uid
Normal file
1
game/tests/mobile/scale_test.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cmrhytpkiqesd
|
||||
187
game/tests/mobile/sticks_test.gd
Normal file
187
game/tests/mobile/sticks_test.gd
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
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()
|
||||
1
game/tests/mobile/sticks_test.gd.uid
Normal file
1
game/tests/mobile/sticks_test.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ccpmbww2ehqxx
|
||||
49
game/tests/mobile/touch.gd
Normal file
49
game/tests/mobile/touch.gd
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
extends RefCounted
|
||||
## Fingers for the phone tests (game/tests/mobile/), sent the way a phone sends them: through
|
||||
## Input.parse_input_event, so Godot's touch-to-click emulation runs too (finger 0 is also the
|
||||
## left mouse button), and delivered at once (Input.flush_buffered_events), so a check right
|
||||
## after a call sees its effect on the GUI and on _unhandled_input.
|
||||
## Points are canvas coordinates of the root viewport (what a full-rect HUD Control sees, at any
|
||||
## interface scale); they're turned into window pixels through the root's final transform, as
|
||||
## a phone's touches arrive.
|
||||
## const Touch := preload("res://game/tests/mobile/touch.gd")
|
||||
## Touch.touch(0, at, true) # finger 0 down at `at`
|
||||
## Touch.drag(0, at, at + Vector2(150, 0)) # it moves (one drag event; the finger stays down)
|
||||
## Touch.touch(0, at + Vector2(150, 0), false) # and lifts
|
||||
## Touch.tap(1, at) # finger 1 down and up at once
|
||||
|
||||
|
||||
## Finger i goes down (pressed) or lifts at this canvas point.
|
||||
static func touch(i: int, canvas_pos: Vector2, pressed: bool) -> void:
|
||||
var e := InputEventScreenTouch.new()
|
||||
e.index = i
|
||||
e.pressed = pressed
|
||||
e.position = window_pos(canvas_pos)
|
||||
_send(e)
|
||||
|
||||
|
||||
## Finger i, already down, moves from one canvas point to another (one drag event).
|
||||
static func drag(i: int, from: Vector2, to: Vector2) -> void:
|
||||
var e := InputEventScreenDrag.new()
|
||||
e.index = i
|
||||
e.position = window_pos(to)
|
||||
e.relative = window_pos(to) - window_pos(from)
|
||||
e.screen_relative = e.relative
|
||||
_send(e)
|
||||
|
||||
|
||||
## Finger i taps this canvas point: down, then up.
|
||||
static func tap(i: int, canvas_pos: Vector2) -> void:
|
||||
touch(i, canvas_pos, true)
|
||||
touch(i, canvas_pos, false)
|
||||
|
||||
|
||||
## A canvas point of the root viewport in window pixels (where a phone reports a touch there).
|
||||
static func window_pos(canvas_pos: Vector2) -> Vector2:
|
||||
var tree := Engine.get_main_loop() as SceneTree
|
||||
return tree.root.get_final_transform() * canvas_pos
|
||||
|
||||
|
||||
static func _send(e: InputEvent) -> void:
|
||||
Input.parse_input_event(e)
|
||||
Input.flush_buffered_events()
|
||||
1
game/tests/mobile/touch.gd.uid
Normal file
1
game/tests/mobile/touch.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://33srlb8fgmxj
|
||||
Loading…
Add table
Add a link
Reference in a new issue