Skip to content

fix: Keep a HUD component at the left or top edge in place on resize - #4284

Open
Yasser-Ameur wants to merge 1 commit into
flame-engine:mainfrom
Yasser-Ameur:fix/hud-margin-edge-position
Open

Yasser-Ameur wants to merge 1 commit into
flame-engine:mainfrom
Yasser-Ameur:fix/hud-margin-edge-position

Conversation

@Yasser-Ameur

Copy link
Copy Markdown
Contributor

Description

A HUD component that is placed by position at the left or top edge moves when the game is resized, and leaves the screen when the game gets smaller:

final bar = HudMarginComponent(position: Vector2.zero(), size: Vector2(50, 20));
await game.camera.viewport.add(bar);   // game is 800x600
// position after resizing to 1000x700, then to 400x300
// main:   [200, 100], then [-400, -300]
// branch: [0, 0], then [0, 0]

A component created with a position and no margin turns that position into a margin on load. When the component touches the left or top edge, that margin has a zero left or top, and the resize code reads a zero as "not set, measure from the right or bottom edge". A resize is needed to see it, such as a desktop or web window resize or a phone rotation.

A margin that was derived from the position is now remembered and always measured from the left and top edges. HudMarginComponent and the ComponentViewportMargin mixin (used by HudButtonComponent and JoystickComponent) had the same code, and both get the same change. A margin that is passed explicitly behaves as before, also when it has a zero left or top.

New tests in hud_margin_component_test.dart and hud_button_component_test.dart place a component at the edge and resize the game; they fail on main.

Locally on Windows, from packages/flame: dart format --output=none --set-exit-if-changed on the four changed files reports 0 changed, flutter analyze reports no issues, and test/components gives 657 passed. The melos gates, cspell and the markdown check were not run.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • [-] I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

A HudMarginComponent, or a component using ComponentViewportMargin such
as HudButtonComponent, created with a position turns that position into
a margin, but a zero left or top margin was read as "measure from the
right or bottom edge". A component placed at x = 0 or y = 0 therefore
moved away from that edge when the game was resized. Margins derived
from the position are now always measured from the left and top edges.
@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review December 1, 2026 09:09

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant