#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
This commit is contained in:
Claude 2026-09-12 18:04:00 +02:00
commit f1292db40a
7 changed files with 279 additions and 14 deletions

View file

@ -63,6 +63,7 @@ var context_actions_expanded: bool = false
func _ready() -> void:
get_viewport().size_changed.connect(_on_viewport_resized)
_seat_hud_columns()
build_button.pressed.connect(_on_build_pressed)
action_button.pressed.connect(_on_context_action_pressed)
@ -97,6 +98,22 @@ func _ready() -> void:
_layout_context_buttons()
# HudScale reapplies each control's authored offsets whenever the window
# changes, which wipes the two layouts this controller writes at runtime.
# Re-running them after a resize is what keeps a desktop window that gets
# dragged to another size from leaving the context row where it was.
func _on_viewport_resized() -> void:
# Deferred: HudScale reapplies on the same signal, and which of two
# siblings connected first is not a thing to depend on. By the end of the
# frame its offsets are in, and these two can seat against them.
_relayout_hud.call_deferred()
func _relayout_hud() -> void:
_seat_hud_columns()
_layout_context_buttons()
func _seat_hud_columns() -> void:
var readout: ResourceReadout = get_node_or_null("../UI/ResourceReadout")
if readout == null:
@ -109,7 +126,10 @@ func _seat_hud_columns() -> void:
highest = minf(highest, button.position.y)
if is_inf(highest):
return
var shift: float = readout.content_bottom() + HUD_COLUMN_MARGIN - highest
# The margin is authored in the same canvas units as everything else, so
# it scales with the HUD (#228) or it would be the one gap that stayed
# phone-sized on a desktop.
var shift: float = readout.content_bottom() + HUD_COLUMN_MARGIN * readout.scale.y - highest
if shift <= 0.0:
return
for button in buttons:
@ -222,7 +242,14 @@ func _clear_detected_context() -> void:
func _layout_context_buttons() -> void:
var row_top: float = build_button.position.y + build_button.size.y + HUD_ROW_GAP
# position is in the UI layer's space and size is in the button's own,
# and since #228 those differ by the HUD scale on a short canvas. Every
# gap and stride below is therefore scaled, while the RECTS keep their
# authored size - HudScale renders them down rather than shrinking them.
# Mixing the two is what this function got wrong twice before (see
# test_hud_layout), and a desktop canvas makes it wrong again.
var k: float = build_button.scale.y
var row_top: float = build_button.position.y + build_button.size.y * k + HUD_ROW_GAP * k
var row_height: float = build_button.size.y
action_button.offset_top = row_top
action_button.offset_bottom = row_top + row_height
@ -234,8 +261,8 @@ func _layout_context_buttons() -> void:
for i in range(buttons.size()):
var button: Control = buttons[i]
button.visible = true
button.offset_left = 188 + i * 164
button.offset_right = button.offset_left + 148
button.offset_left = (188.0 + float(i) * 164.0) * k
button.offset_right = button.offset_left + 148.0
button.offset_top = row_top
button.offset_bottom = row_top + row_height