From 40cc28be19b2279f9d723ab8e685e5d76637ba81 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 9 May 2026 01:35:00 +0100 Subject: [PATCH] Fix Direct2D overlay rectangle coordinates --- .../Render/DirectWriteOverlay.cs | 13 +++++++--- src/Aquarium.Engine/Render/Ui/DebugUi.cs | 25 +++++++++++-------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Aquarium.Engine/Render/DirectWriteOverlay.cs b/src/Aquarium.Engine/Render/DirectWriteOverlay.cs index 21d3a4b..1c2a13e 100644 --- a/src/Aquarium.Engine/Render/DirectWriteOverlay.cs +++ b/src/Aquarium.Engine/Render/DirectWriteOverlay.cs @@ -76,12 +76,12 @@ internal sealed class DirectWriteOverlay : IDisposable DrawHeader( "directwrite overlay", titleFormat, - new Rect(18, 14, Math.Min(width - 18, 340), 42), + RectFromEdges(18, 14, Math.Min(width - 18, 340), 42), primaryTextBrush); renderTarget.DrawText( $"grid r {frame.Grid.Radius:0.00} cam z {frame.CameraPosition.Z:0.00}", smallFormat, - new Rect(18, 38, Math.Min(width - 18, 420), 62), + RectFromEdges(18, 38, Math.Min(width - 18, 420), 62), quietTextBrush, DrawTextOptions.Clip); if (renderDebugMode > 0) @@ -89,14 +89,14 @@ internal sealed class DirectWriteOverlay : IDisposable renderTarget.DrawText( $"render debug {renderDebugMode} F1 cycle 0-8 select", smallFormat, - new Rect(18, 56, Math.Min(width - 18, 420), 80), + RectFromEdges(18, 56, Math.Min(width - 18, 420), 80), quietTextBrush, DrawTextOptions.Clip); } renderTarget.DrawText( "crisp text belongs after tonemapping", smallFormat, - new Rect(18, Math.Max(64, height - 36), Math.Min(width - 18, 420), height - 12), + RectFromEdges(18, Math.Max(64, height - 36), Math.Min(width - 18, 420), height - 12), quietTextBrush, DrawTextOptions.Clip); debugUi?.Draw( @@ -166,4 +166,9 @@ internal sealed class DirectWriteOverlay : IDisposable { return Path.Combine(AppContext.BaseDirectory, "Assets", "Fonts", fileName); } + + private static Rect RectFromEdges(float left, float top, float right, float bottom) + { + return new Rect(left, top, Math.Max(0.0f, right - left), Math.Max(0.0f, bottom - top)); + } } diff --git a/src/Aquarium.Engine/Render/Ui/DebugUi.cs b/src/Aquarium.Engine/Render/Ui/DebugUi.cs index a31b00a..c89c826 100644 --- a/src/Aquarium.Engine/Render/Ui/DebugUi.cs +++ b/src/Aquarium.Engine/Render/Ui/DebugUi.cs @@ -129,7 +129,7 @@ internal sealed class DebugUi target.DrawText( Title, titleFormat, - new Rect(panelBounds.Left + 12.0f, panelBounds.Top + 8.0f, panelBounds.Right - 12.0f, panelBounds.Top + HeaderHeight - 4.0f), + RectFromEdges(panelBounds.Left + 12.0f, panelBounds.Top + 8.0f, panelBounds.Right - 12.0f, panelBounds.Top + HeaderHeight - 4.0f), primaryBrush, DrawTextOptions.Clip); @@ -141,7 +141,7 @@ internal sealed class DebugUi target.DrawText( "F2 hide", smallFormat, - new Rect(panelBounds.Left + 12.0f, panelBounds.Bottom - 26.0f, panelBounds.Right - 12.0f, panelBounds.Bottom - 8.0f), + RectFromEdges(panelBounds.Left + 12.0f, panelBounds.Bottom - 26.0f, panelBounds.Right - 12.0f, panelBounds.Bottom - 8.0f), quietBrush, DrawTextOptions.Clip); } @@ -152,11 +152,11 @@ internal sealed class DebugUi foreach (var control in controls) { var height = control is SectionControl ? SectionHeight : RowHeight; - control.Bounds = new Rect(PanelLeft + 10.0f, y, PanelLeft + PanelWidth - 10.0f, y + height); + control.Bounds = RectFromEdges(PanelLeft + 10.0f, y, PanelLeft + PanelWidth - 10.0f, y + height); y += height + (control is SectionControl ? 2.0f : RowGap); } - panelBounds = new Rect(PanelLeft, PanelTop, PanelLeft + PanelWidth, y + 30.0f); + panelBounds = RectFromEdges(PanelLeft, PanelTop, PanelLeft + PanelWidth, y + 30.0f); } private static bool Contains(Rect bounds, Vector2 point) @@ -167,6 +167,11 @@ internal sealed class DebugUi && point.Y <= bounds.Bottom; } + private static Rect RectFromEdges(float left, float top, float right, float bottom) + { + return new Rect(left, top, Math.Max(0.0f, right - left), Math.Max(0.0f, bottom - top)); + } + public sealed class DebugUiPanel { private readonly List controls; @@ -245,12 +250,12 @@ internal sealed class DebugUi protected Rect LabelBounds() { - return new Rect(Bounds.Left + 8.0f, Bounds.Top + 6.0f, Bounds.Left + LabelWidth, Bounds.Bottom); + return RectFromEdges(Bounds.Left + 8.0f, Bounds.Top + 6.0f, Bounds.Left + LabelWidth, Bounds.Bottom); } protected Rect ValueBounds() { - return new Rect(Bounds.Left + LabelWidth + 10.0f, Bounds.Top + 6.0f, Bounds.Right - TrackWidth - 20.0f, Bounds.Bottom); + return RectFromEdges(Bounds.Left + LabelWidth + 10.0f, Bounds.Top + 6.0f, Bounds.Right - TrackWidth - 20.0f, Bounds.Bottom); } } @@ -314,7 +319,7 @@ internal sealed class DebugUi DrawRow(target, rowBrush, outlineBrush); target.DrawText(Label.ToUpperInvariant(), format, LabelBounds(), accentBrush, DrawTextOptions.Clip); - var box = new Rect(Bounds.Right - 28.0f, Bounds.Top + 8.0f, Bounds.Right - 13.0f, Bounds.Bottom - 8.0f); + var box = RectFromEdges(Bounds.Right - 28.0f, Bounds.Top + 8.0f, Bounds.Right - 13.0f, Bounds.Bottom - 8.0f); target.DrawRectangle(box, read() ? accentBrush : outlineBrush, 1.0f); if (read()) { @@ -365,11 +370,11 @@ internal sealed class DebugUi var track = TrackBounds(); var t = ReadNormalized(); - var fill = new Rect(track.Left, track.Top, track.Left + (track.Right - track.Left) * t, track.Bottom); + var fill = RectFromEdges(track.Left, track.Top, track.Left + (track.Right - track.Left) * t, track.Bottom); target.FillRectangle(track, dimAccentBrush); target.FillRectangle(fill, accentBrush); var thumbX = fill.Right; - target.FillRectangle(new Rect(thumbX - 4.0f, track.Top - 4.0f, thumbX + 4.0f, track.Bottom + 4.0f), accentBrush); + target.FillRectangle(RectFromEdges(thumbX - 4.0f, track.Top - 4.0f, thumbX + 4.0f, track.Bottom + 4.0f), accentBrush); } private Rect TrackBounds() @@ -377,7 +382,7 @@ internal sealed class DebugUi var right = Bounds.Right - 12.0f; var left = right - TrackWidth; var centerY = (Bounds.Top + Bounds.Bottom) * 0.5f; - return new Rect(left, centerY - TrackHeight * 0.5f, right, centerY + TrackHeight * 0.5f); + return RectFromEdges(left, centerY - TrackHeight * 0.5f, right, centerY + TrackHeight * 0.5f); } }