diff --git a/Changelog/2026-10-05-floating-window-placement.md b/Changelog/2026-10-05-floating-window-placement.md new file mode 100644 index 0000000..b53319c --- /dev/null +++ b/Changelog/2026-10-05-floating-window-placement.md @@ -0,0 +1,63 @@ +# Floating windows open where they can be seen (RynthAi 0.5.5-legacy-ui) + +Date: 2026-10-05 +Branches (local): RynthCore `merge/aelrynth-2026.10.5.1`; RynthSuite `fix/rynthai-launcher-remote-commands` +Previous plugin version: 0.5.4-legacy-ui + +## Problem + +New windows with no saved position opened in the top-left corner of the game view: + +- the Pets window at ImGui's default (60,60); +- the Item HUD at (20,220); +- the Mini Remote at (20,120); +- the Dungeon Map in the panel cascade (100,100 and onwards). + +Popped-out panels are separate Windows windows above the game window. They often sit in that +corner, and nothing drawn inside the game frame (ImGui windows included) can appear over them. So +the new windows opened hidden underneath. The draw-order fix (`2026-10-05-imgui-over-avalonia-port.md`) +only covers the Avalonia layer painted inside the game frame. + +## Changes + +### RynthAi (plugin) + +- New `UiPlacement` helper: + - `CenterFirstUse(offset)`: before `Begin`, sets a `FirstUseEver` position centred in the game + view (pivot 0.5,0.5). It only applies when imgui.ini has nothing saved for the window. + - `RescueOncePerShow(ref checked)`: once per show, moves the window to the middle if less than + 40 px of it is on screen. The test is the ILT Hub's existing off-screen check, now shared as + `UiPlacement.IsOnScreen`. +- First-use spots (centre plus offset): + + | Window | First-use spot | + | --- | --- | + | ILT Hub | centre | + | Pets | centre | + | Quests (popped out) | centre + (40,40) | + | Item HUD | centre + (220,-160) | + | Mini Remote | centre + (-220,-160) | + + All five also get the off-screen check each time they're shown. +- The ILT Hub's off-screen rescue now moves the window to the middle of the game view instead of + (40,40), which is where popped-out panels usually sit. + +### Engine + +- `PanelSpec.OpenCentered`: a panel with nothing saved and no `DefaultPos` opens centred in the game + view instead of the cascade. The Dungeon Map uses it. +- Docked panels open clear of popped-out windows. + - When an engine panel opens docked, `ImGuiPanelHost` checks the visible popped-out windows + (`LayeredWindow.VisibleContentRects`: every Avalonia and ImGui pop-out and the floating bar). + - If more than half of the panel would be hidden, it opens at the nearest spot on a 9x9 grid over + the game view that's hidden the least. The open's normal save records that spot. + - Each move is logged as `ImGuiPanelHost: would open under a popped-out window; moved from + (x,y) to (x,y).` + +## Limits + +- Plugin windows (Pets, Quests, Item HUD, Mini Remote, Hub) are centred on first use and pulled back + if off-screen. They don't yet avoid popped-out panels: the plugin can't see where those are. That + would need a new PluginSdk host call. +- ImGui windows still can't draw above popped-out panels. To keep both visible, dock the panel, or + move it away from where the windows open. diff --git a/src/RynthCore.Engine/ImGui/ImGuiPanelHost.cs b/src/RynthCore.Engine/ImGui/ImGuiPanelHost.cs index f153763..f55b35d 100644 --- a/src/RynthCore.Engine/ImGui/ImGuiPanelHost.cs +++ b/src/RynthCore.Engine/ImGui/ImGuiPanelHost.cs @@ -66,7 +66,11 @@ internal sealed record PanelSpec( ImGuiWindowFlags BodyFlags = ImGuiWindowFlags.None, // Where the panel first opens (logical units) when nothing is saved for its // key; null = the cascade. (Script windows: a FirstUseEver position.) - Vector2? DefaultPos = null); + Vector2? DefaultPos = null, + // With nothing saved and no DefaultPos: open centred in the game view rather + // than in the top-left cascade, where popped-out panels (separate windows + // above the game frame) usually sit and would hide it. + bool OpenCentered = false); /// <summary>An ImGui panel face. One instance per open; dropped on close.</summary> internal interface IImGuiPanel @@ -648,6 +652,10 @@ internal static class ImGuiPanelHost { pos = defaultPos; // the face's own first spot (a script window's FirstUseEver position) } + else if (entry.Spec.OpenCentered && CenteredLogicalPos(size, placeScale) is Vector2 centered) + { + pos = centered; + } else { int shown = 0; @@ -667,6 +675,104 @@ internal static class ImGuiPanelHost entry.NeedsPlacement = true; entry.DesiredPos = entry.Pos != pos * placeScale ? pos * placeScale : null; entry.DesiredForDisplay = ImGuiNET.ImGui.GetIO().DisplaySize; + + // Popped-out windows sit above the game frame, so a docked panel opening under + // one would be invisible. The new spot is saved by the open's Persist, so the + // panel comes back where it could be seen. + if (ClearOfPopOuts(entry.Pos, entry.Size) is Vector2 clear) + { + RynthLog.UI($"ImGuiPanelHost: {entry.Title} would open under a popped-out window; " + + $"moved from ({entry.Pos.X:0},{entry.Pos.Y:0}) to ({clear.X:0},{clear.Y:0})."); + entry.Pos = clear; + entry.PersistedPos = clear; + entry.DesiredPos = null; // don't pull it back under the pop-out as the display grows + } + } + + /// <summary>Above this share of a docked panel hidden by popped-out windows, it opens elsewhere.</summary> + private const float MaxCoveredFraction = 0.5f; + /// <summary>Candidate spots per axis when looking for a clear place (a grid over the game view).</summary> + private const int ClearSpotSteps = 8; + + /// <summary> + /// A spot (client px) for a docked window of <paramref name="size"/> at <paramref name="pos"/> + /// that popped-out windows hide less, nearest the original among the least-covered; null when + /// at most half of it is covered or no spot is better. + /// </summary> + private static Vector2? ClearOfPopOuts(Vector2 pos, Vector2 size) + { + float area = size.X * size.Y; + if (area <= 0) return null; + List<(Vector2 Min, Vector2 Max)> covers = PopOutRectsInClient(); + if (covers.Count == 0) return null; + float covered = CoveredArea(pos, size, covers); + if (covered <= area * MaxCoveredFraction) return null; + + Vector2 display = ImGuiNET.ImGui.GetIO().DisplaySize; + if (display.X <= 1 || display.Y <= 1) return null; + Vector2 room = Vector2.Max(Vector2.Zero, display - size); + + // Pass 1: the least coverage any grid spot reaches. Pass 2: the nearest spot within + // 2% of the panel's area of that, so a nearly-as-clear nearby spot beats a far one. + float least = float.MaxValue; + for (int i = 0; i <= ClearSpotSteps; i++) + for (int j = 0; j <= ClearSpotSteps; j++) + least = Math.Min(least, CoveredArea(GridSpot(room, i, j), size, covers)); + if (least >= covered) return null; + + float tolerance = least + area * 0.02f; + Vector2? best = null; + float bestDist = float.MaxValue; + for (int i = 0; i <= ClearSpotSteps; i++) + for (int j = 0; j <= ClearSpotSteps; j++) + { + Vector2 spot = GridSpot(room, i, j); + if (CoveredArea(spot, size, covers) > tolerance) continue; + float dist = Vector2.DistanceSquared(spot, pos); + if (dist < bestDist) { bestDist = dist; best = spot; } + } + return best; + } + + private static Vector2 GridSpot(Vector2 room, int i, int j) => + new(MathF.Round(room.X * i / ClearSpotSteps), MathF.Round(room.Y * j / ClearSpotSteps)); + + /// <summary>Visible popped-out windows' rects in the game window's client px (ImGui coordinates).</summary> + private static List<(Vector2 Min, Vector2 Max)> PopOutRectsInClient() + { + Vector2 origin = ImGuiPopOuts.ClientOriginOnScreen(); + var rects = new List<(Vector2, Vector2)>(); + foreach (var (left, top, width, height) in UI.LayeredWindow.VisibleContentRects()) + { + var min = new Vector2(left, top) - origin; + rects.Add((min, min + new Vector2(width, height))); + } + return rects; + } + + /// <summary>Area of the window hidden by <paramref name="covers"/> (overlaps between covers count twice).</summary> + private static float CoveredArea(Vector2 pos, Vector2 size, List<(Vector2 Min, Vector2 Max)> covers) + { + Vector2 max = pos + size; + float total = 0; + foreach (var (cMin, cMax) in covers) + { + float w = MathF.Min(max.X, cMax.X) - MathF.Max(pos.X, cMin.X); + float h = MathF.Min(max.Y, cMax.Y) - MathF.Max(pos.Y, cMin.Y); + if (w > 0 && h > 0) total += w * h; + } + return total; + } + + /// <summary> + /// Top-left (logical units) that centres a window of logical <paramref name="size"/> in the + /// game view; null while the display size isn't known yet. + /// </summary> + private static Vector2? CenteredLogicalPos(Vector2 size, float placeScale) + { + Vector2 display = ImGuiNET.ImGui.GetIO().DisplaySize; + if (display.X <= 1 || display.Y <= 1 || placeScale <= 0) return null; + return Vector2.Max(Vector2.Zero, (display / placeScale - size) * 0.5f); } private static Vector2 ClampToDisplay(Vector2 pos, Vector2 size) diff --git a/src/RynthCore.Engine/ImGui/Panels/DungeonMapFace.cs b/src/RynthCore.Engine/ImGui/Panels/DungeonMapFace.cs index 42b1e9f..558e47b 100644 --- a/src/RynthCore.Engine/ImGui/Panels/DungeonMapFace.cs +++ b/src/RynthCore.Engine/ImGui/Panels/DungeonMapFace.cs @@ -44,7 +44,7 @@ internal sealed class DungeonMapFace : IImGuiPanel { DungeonMapSettingsStore.Load(); // engine init thread: the file read stays off AC's thread ImGuiPanelHost.Register(Title, - new PanelSpec(new Vector2(480, 520), new Vector2(240, 200), Background: 0, EdgeToEdge: true), + new PanelSpec(new Vector2(480, 520), new Vector2(240, 200), Background: 0, EdgeToEdge: true, OpenCentered: true), () => new DungeonMapFace()); } diff --git a/src/RynthCore.Engine/UI/LayeredWindow.cs b/src/RynthCore.Engine/UI/LayeredWindow.cs index 1bad6aa..ed1a32d 100644 --- a/src/RynthCore.Engine/UI/LayeredWindow.cs +++ b/src/RynthCore.Engine/UI/LayeredWindow.cs @@ -371,6 +371,25 @@ internal sealed unsafe class LayeredWindow : IDisposable } } + /// <summary> + /// The content rects (screen px: left, top, width, height) of every visible panel window. + /// They sit above the game window, so nothing drawn in the game frame can show over them. + /// </summary> + internal static List<(int Left, int Top, int Width, int Height)> VisibleContentRects() + { + LayeredWindow[] windows; + lock (_instancesLock) + { + windows = new LayeredWindow[_instances.Count]; + _instances.Values.CopyTo(windows, 0); + } + var rects = new List<(int, int, int, int)>(windows.Length); + foreach (LayeredWindow w in windows) + if (w.IsVisible && w.Width > 0 && w.Height > 0) + rects.Add((w.ScreenLeft, w.ScreenTop, w.Width, w.Height)); + return rects; + } + private static readonly HashSet<IntPtr> _sweptLogged = new(); [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]