Files
windroost/p2_godot_sample/scripts/battle_touch_target_test.gd
T

165 lines
5.5 KiB
GDScript

extends SceneTree
## Real-scene regression: every visible non-skill combat button keeps a
## >=48x48 global rect on all required viewports, locales and motion modes,
## without leaving the viewport. Skill buttons and bottom-tab semantics are
## intentionally not modified; TapButton/StormButton/UltimateButton are excluded
## from this target set, and the hidden in-battle bottom tab stays hidden.
const OUTPUT_PATH := "res://artifacts/battle_touch_target_test.json"
const BATTLE_SCENE := "res://scenes/battle_scene.tscn"
const SCENARIO := "P3A-N03-SEMI"
const SEED := 10001
const VIEWPORTS := [Vector2i(360, 800), Vector2i(390, 844), Vector2i(430, 932)]
const LOCALES := ["zh_CN", "en_US"]
const MODES := ["normal", "reduced_motion"]
const MIN_TARGET := 48.0
const EPSILON := 0.01
const SKILL_BUTTONS := ["TapButton", "StormButton", "UltimateButton"]
var _problems: Array = []
var _records: Array = []
var _checks := 0
func _init() -> void:
_run.call_deferred()
func _check(ok: bool, message: String) -> void:
_checks += 1
if not ok:
_problems.append(message)
func _wait_frames(count: int) -> void:
for _i in range(count):
await process_frame
func _run() -> void:
var viewport := get_root()
viewport.content_scale_mode = Window.CONTENT_SCALE_MODE_CANVAS_ITEMS
await _wait_frames(2)
for size in VIEWPORTS:
for locale in LOCALES:
for mode in MODES:
viewport.content_scale_size = size
viewport.size = size
await _wait_frames(3)
var node: Node = load(BATTLE_SCENE).instantiate()
viewport.add_child(node)
var battle := node as BattleScene
battle.set_process(false)
battle.setup(locale, SCENARIO, SEED)
if mode == "reduced_motion":
battle.set_reduced_motion(true)
await _wait_frames(4)
var vp_size := viewport.size
var key := "%sx%s_%s_%s" % [size.x, size.y, locale, mode]
var hud := battle.hud
var names := PackedStringArray()
# Required named non-skill targets, by the real Control objects.
var required: Dictionary = {
"SettingsButton": hud.get("_settings_button"),
"SpeedButton": hud.get("_speed_button"),
"PauseButton": hud.get("_pause_button"),
"ExitButton": hud.get("_exit_button"),
"GrowthButton": hud.get("_growth_button"),
}
for target_name in required.keys():
var control: Control = required[target_name]
_check(control != null, "%s missing required non-skill target %s" % [key, target_name])
if control == null:
continue
_check(control.visible and control.is_visible_in_tree(),
"%s %s must be visible" % [key, target_name])
names.append(str(target_name))
_check_target(key, str(target_name), control.get_global_rect(), vp_size)
# Every other visible non-skill combat button must also satisfy the target.
var buttons := _non_skill_buttons(hud)
for button in buttons:
var btn := button as Button
_check_target(key, str(btn.name), btn.get_global_rect(), vp_size)
names.append(str(btn.name))
_check(not battle.bottom_nav_visible_for_test(),
"%s in-battle bottom tab must stay hidden" % key)
_records.append({
"cell": key,
"viewport": [size.x, size.y],
"locale": locale,
"mode": mode,
"required_targets": required.keys(),
"non_skill_button_count": buttons.size(),
"bottom_nav_visible": battle.bottom_nav_visible_for_test(),
})
viewport.remove_child(node)
node.queue_free()
await _wait_frames(2)
var payload := {
"schema": 1,
"kind": "windroost_battle_touch_target",
"min_target": MIN_TARGET,
"viewports": [[360, 800], [390, 844], [430, 932]],
"locales": LOCALES,
"modes": MODES,
"excluded_skill_buttons": SKILL_BUTTONS,
"records": _records,
"checks": _checks,
"problems": _problems,
"overall": "PASS" if _problems.is_empty() else "FAIL",
}
var dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(dir)
var file := FileAccess.open(OUTPUT_PATH, FileAccess.WRITE)
if file != null:
file.store_string(JSON.stringify(payload, "\t", true))
file.close()
if _problems.is_empty():
print("BATTLE_TOUCH_TARGET_PASS cells=%d checks=%d" % [_records.size(), _checks])
quit(0)
else:
print("BATTLE_TOUCH_TARGET_FAIL %s" % "; ".join(PackedStringArray(_problems)))
quit(1)
func _non_skill_buttons(hud: Node) -> Array:
var result: Array = []
var buttons: Array = hud.find_children("*", "Button", true, false)
for button in buttons:
var btn := button as Button
if btn == null:
continue
if not btn.visible or not btn.is_visible_in_tree():
continue
if SKILL_BUTTONS.has(str(btn.name)):
continue
result.append(btn)
return result
func _check_target(cell: String, name: String, rect: Rect2, viewport: Vector2) -> void:
_check(rect.size.x >= MIN_TARGET - EPSILON,
"%s %s width %.3f < %.1f (rect=%s)" % [cell, name, rect.size.x, MIN_TARGET, str(rect)])
_check(rect.size.y >= MIN_TARGET - EPSILON,
"%s %s height %.3f < %.1f (rect=%s)" % [cell, name, rect.size.y, MIN_TARGET, str(rect)])
_check(rect.position.x >= -EPSILON,
"%s %s left %.3f < 0 (rect=%s)" % [cell, name, rect.position.x, str(rect)])
_check(rect.position.y >= -EPSILON,
"%s %s top %.3f < 0 (rect=%s)" % [cell, name, rect.position.y, str(rect)])
_check(rect.position.x + rect.size.x <= viewport.x + EPSILON,
"%s %s right %.3f > viewport %.3f (rect=%s)" % [cell, name, rect.position.x + rect.size.x, viewport.x, str(rect)])
_check(rect.position.y + rect.size.y <= viewport.y + EPSILON,
"%s %s bottom %.3f > viewport %.3f (rect=%s)" % [cell, name, rect.position.y + rect.size.y, viewport.y, str(rect)])