hy-farm/godot/FarmGodot/tools/verify_loading_screen.gd

96 lines
5.4 KiB
GDScript

extends SceneTree
const BootstrapScript := preload("res://scripts/bootstrap.gd")
const DESIGN_SIZE := Vector2(640.0, 960.0)
const BACKGROUND_SOURCE_SIZE := Vector2(941.0, 1672.0)
const BAR_SOURCE_SIZE := Vector2(1117.0, 223.0)
const BAR_WIDTH := 500.0
const BAR_SIZE := Vector2(BAR_WIDTH, BAR_WIDTH * BAR_SOURCE_SIZE.y / BAR_SOURCE_SIZE.x)
const BAR_POSITION := Vector2((DESIGN_SIZE.x - BAR_SIZE.x) * 0.5, 792.0)
const BAR_SLOT_REGION := Rect2(190.0, 83.0, 738.0, 68.0)
const FILL_SOURCE_REGION := Rect2(57.0, 55.0, 1003.0, 113.0)
var _failures: Array[String] = []
func _init() -> void:
_run.call_deferred()
func _run() -> void:
await process_frame
var bootstrap := BootstrapScript.new()
bootstrap.call("_build_loading_ui")
var layer := bootstrap.get_node_or_null("LoadingLayer") as Control
_expect(layer != null, "loading layer should be created")
if layer != null:
var background := layer.get_node_or_null("Background") as TextureRect
_expect(background != null and background.texture != null, "loading background should render texture")
var expected_bg_size := _cover_size(BACKGROUND_SOURCE_SIZE, DESIGN_SIZE)
_expect(background != null and background.stretch_mode == TextureRect.STRETCH_SCALE, "loading background should use computed source-size fit")
_expect(background != null and _near(background.size.x, expected_bg_size.x), "loading background width should match source-size fit: %.2f expected %.2f" % [background.size.x if background != null else -1.0, expected_bg_size.x])
_expect(background != null and _near(background.size.y, expected_bg_size.y), "loading background height should match source-size fit: %.2f expected %.2f" % [background.size.y if background != null else -1.0, expected_bg_size.y])
_expect(background != null and _near(background.position.y, (DESIGN_SIZE.y - expected_bg_size.y) * 0.5), "loading background should remain vertically centered")
var bar := layer.get_node_or_null("ProgressBar") as Control
_expect(bar != null, "loading progress bar should exist")
_expect(bar != null and _near(bar.position.x, BAR_POSITION.x), "loading progress bar should be centered")
_expect(bar != null and _near(bar.position.y, BAR_POSITION.y), "loading progress bar y should match loading layout")
_expect(bar != null and _near(bar.size.x, BAR_SIZE.x), "loading progress bar width should use adapted source ratio")
_expect(bar != null and _near(bar.size.y, BAR_SIZE.y), "loading progress bar height should use adapted source ratio")
var clip := layer.get_node_or_null("ProgressBar/FillClip") as Control
var fill := layer.get_node_or_null("ProgressBar/FillClip/Fill") as TextureRect
var percent := layer.get_node_or_null("ProgressBar/Percent") as Label
var fill_rect := _fill_rect()
_expect(clip != null and _near(clip.position.x, fill_rect.position.x), "loading fill should align to frame inner slot x")
_expect(clip != null and _near(clip.position.y, fill_rect.position.y), "loading fill should align to frame inner slot y")
_expect(fill != null and _near(fill.size.x, fill_rect.size.x), "loading fill visual width should fit frame inner slot")
_expect(fill != null and _near(fill.size.y, fill_rect.size.y), "loading fill visual height should fit frame inner slot")
_expect(fill != null and fill.texture is AtlasTexture, "loading fill should use cropped atlas region")
if fill != null and fill.texture is AtlasTexture:
var atlas := fill.texture as AtlasTexture
_expect(_near(atlas.region.position.x, FILL_SOURCE_REGION.position.x) and _near(atlas.region.position.y, FILL_SOURCE_REGION.position.y), "loading fill atlas should crop transparent source margin")
_expect(_near(atlas.region.size.x, FILL_SOURCE_REGION.size.x) and _near(atlas.region.size.y, FILL_SOURCE_REGION.size.y), "loading fill atlas should use source visual bounds")
_expect(percent != null and _near(percent.position.x, fill_rect.position.x), "loading percent text should align to fill x")
_expect(percent != null and _near(percent.position.y, fill_rect.position.y), "loading percent text should align to fill y")
bootstrap.call("_set_loading_progress", 0.5)
_expect(clip != null and _near(clip.size.x, fill_rect.size.x * 0.5), "loading fill width should match 50 percent")
_expect(percent != null and percent.text == "50%", "loading percent text should show 50%")
bootstrap.call("_set_loading_progress", 1.0)
_expect(clip != null and _near(clip.size.x, fill_rect.size.x), "loading fill width should match 100 percent")
_expect(percent != null and percent.text == "100%", "loading percent text should show 100%")
bootstrap.queue_free()
if not _failures.is_empty():
for failure in _failures:
push_error(failure)
quit(1)
return
print("Loading screen verification passed.")
quit()
func _expect(condition: bool, message: String) -> void:
if not condition:
_failures.append(message)
func _near(value: float, expected: float) -> bool:
return absf(value - expected) <= 0.5
func _cover_size(source_size: Vector2, target_size: Vector2) -> Vector2:
var scale := maxf(target_size.x / source_size.x, target_size.y / source_size.y)
return source_size * scale
func _fill_rect() -> Rect2:
var scale := BAR_SIZE.x / BAR_SOURCE_SIZE.x
var slot_position := BAR_SLOT_REGION.position * scale
var slot_size := BAR_SLOT_REGION.size * scale
var fill_size := Vector2(
slot_size.x,
slot_size.x * FILL_SOURCE_REGION.size.y / FILL_SOURCE_REGION.size.x
)
var fill_position := slot_position + Vector2(0.0, (slot_size.y - fill_size.y) * 0.5)
return Rect2(fill_position, fill_size)