spaceship/ui/HudScale.gd

78 lines
3.2 KiB
GDScript3
Raw Permalink Normal View History

#228: shrink the HUD on a screen the canvas is short on The layout is authored in CANVAS units. With stretch "canvas_items" and aspect "expand" over a 1920x1080 base, a 720x1280 phone scales by 0.375 and gets 1920x3413 canvas units - while ANY 16:9-or-wider screen lands on 1920x1080 units however many pixels it really has. The canvas is three times shorter, so a 148-unit button went from 4.3% of the screen's height to 13.7%. Measured at 3.16x, and confirmed twice: once by hand off the scene file, once by the test failing with the rule disabled. Nothing collided. That was checked pair by pair on both canvases and the same six pairs overlap on each, all of them same-slot alternatives. The fault was only that everything was enormous. HudScale captures each HUD control's authored offsets once, then moves it toward the edge it hangs off and scales the NODE. Scaling the rect alone was tried first and was wrong: half the HUD draws its own contents at fixed sizes - ResourceReadout lays its rows out in code, Labels carry their own font size - so shrunken boxes kept full-size content and the build button slid up underneath the resource list. Scaling the node takes content and children with it. The cost is that position (parent space) and size (local space) now differ by that scale, and a lot of code here assumed they did not. Five places: ResourceReadout.content_bottom() added a scaled position to an unscaled height, overstating itself by more than a button - so _seat_hud_columns pushed the whole column down to clear space nothing occupied. A live bug, not just a bad measurement. HUD_COLUMN_MARGIN would have been the one gap still phone-sized. _layout_context_buttons wrote its stride and pitch unscaled, landing the action row on the building-action row - which is the second of the two regressions test_hud_layout was written for, in a new disguise. test_hud_layout and test_hangar_management compared authored rects. And HudScale reapplies on resize, which wiped the two layouts BuildController writes at runtime - so dragging a desktop window left the context row where it was. Both re-run on size_changed now, deferred, because which of two siblings connected first is not a thing to depend on. test_hud_layout ran at one window shape, which happened to be the desktop canvas. It now runs every check at both, so this cannot hide at whichever shape was not being exercised. Rejected: scaling the UI CanvasLayer does not work at all - Control anchors resolve against the viewport rect, so bottom-anchored elements land mid-screen. A single scaled wrapper Control with the HUD reparented under it is the genuinely clean shape and would need no scale-aware arithmetic anywhere, but it changes every node path and would break every get_node in the controllers and tests. MIN_SCALE floors the shrink at 0.45 rather than the proportional 0.316: a landscape phone gets the same short canvas as a desktop, and there the sticks are still thumb targets. That leaves the HUD at 1.42x its phone share on a PC rather than 1.0x. 177/177 at JOBS=3. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019AJMdB7iuHuLFZM1aBu3GD
2026-09-12 18:04:00 +02:00
extends Node
# Forgejo #228: the HUD is proportionally three times too big on a PC.
#
# The layout is authored in CANVAS units against a portrait phone. With
# stretch "canvas_items"/"expand" and a 1920x1080 base, a 720x1280 phone
# scales by 0.375 and so gets a canvas 1920x3413 units tall - while ANY
# 16:9-or-wider screen lands on 1920x1080 units whatever its pixel count.
# The canvas is three times shorter, so a 148-unit button goes from 4.3% of
# the screen's height to 13.7% of it. Nothing collides; everything is just
# enormous, and the thumb sticks in particular.
#
# Each HUD element's offsets are measured FROM ITS OWN ANCHOR, so scaling
# them scales the element's size and its distance from the edge it hangs off
# together. That is the whole trick: one multiplier per control, no layout
# rules to re-derive, and anchoring keeps working untouched.
# The canvas the HUD was drawn for. Not a magic number: 1280 / (720 / 1920).
const AUTHORED_CANVAS_HEIGHT: float = 3413.0
# Below this the controls stop being worth pressing. A landscape phone gets
# the same short canvas as a desktop, and there the sticks are still thumb
# targets - so the shrink is bounded rather than purely proportional.
const MIN_SCALE: float = 0.45
# Authored offsets and sizes, captured before anything is touched.
# Re-reading them after a resize would measure a previous scaling and let the
# HUD ratchet away to nothing - the same trap NeonPanel documents.
var _authored: Dictionary = {}
func _ready() -> void:
var layer: Node = get_parent()
for child in layer.get_children():
if child == self or not (child is Control):
continue
var c: Control = child
# Full-bleed things - the modal menus, the breach overlay - hang off
# every edge at once and have nothing to shrink.
if is_equal_approx(c.anchor_left, 0.0) and is_equal_approx(c.anchor_right, 1.0) \
and is_equal_approx(c.anchor_top, 0.0) and is_equal_approx(c.anchor_bottom, 1.0):
continue
_authored[c] = [c.offset_left, c.offset_top,
c.offset_right - c.offset_left, c.offset_bottom - c.offset_top]
get_viewport().size_changed.connect(_apply)
_apply()
func hud_scale() -> float:
var height: float = get_viewport().get_visible_rect().size.y
if height <= 0.0:
return 1.0
# Only ever shrinks. A canvas TALLER than the phone's is not a reason to
# inflate the HUD over the game.
return clampf(height / AUTHORED_CANVAS_HEIGHT, MIN_SCALE, 1.0)
func _apply() -> void:
var k: float = hud_scale()
for control in _authored:
if not is_instance_valid(control):
continue
var a: Array = _authored[control]
var c: Control = control
# Position moves toward the edge the element hangs off; the RECT keeps
# its authored size and the node is scaled instead.
#
# Scaling the rect alone was wrong: half the HUD draws its own
# contents at fixed canvas sizes - ResourceReadout lays its rows out
# in code - so a shrunken rect kept full-size content inside it, and
# the build button slid up underneath the resource list. Scaling the
# node takes the drawn content with it.
c.pivot_offset = Vector2.ZERO
c.offset_left = float(a[0]) * k
c.offset_top = float(a[1]) * k
c.offset_right = c.offset_left + float(a[2])
c.offset_bottom = c.offset_top + float(a[3])
c.scale = Vector2(k, k)