From 4454721c9dcc0ec897eea70e08373f69c6de7472 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 12 May 2026 05:32:51 +0100 Subject: [PATCH] Expose client UI composition API --- .../AquariumRuntimeContracts.cs | 3 + .../Ui/AquariumUi.cs | 94 +++++++++++++++++++ src/Aquarium.Engine/AquariumHost.cs | 2 +- src/Aquarium.Engine/Render/D3D12Renderer.cs | 17 +++- .../Render/DirectWriteOverlay.cs | 24 ++++- .../Render/IAquariumRenderer.cs | 3 +- src/Aquarium.Engine/Render/Ui/DebugUi.cs | 66 +++++++++++-- src/Aquarium.Epiphany/AquariumRuntime.cs | 16 +++- src/Aquarium.Sample.Minimal/MinimalRuntime.cs | 3 + state/evidence.jsonl | 1 + state/memory.json | 3 +- 11 files changed, 219 insertions(+), 13 deletions(-) create mode 100644 src/Aquarium.Engine.Contracts/Ui/AquariumUi.cs diff --git a/src/Aquarium.Engine.Contracts/AquariumRuntimeContracts.cs b/src/Aquarium.Engine.Contracts/AquariumRuntimeContracts.cs index 3b5aa46..187ef78 100644 --- a/src/Aquarium.Engine.Contracts/AquariumRuntimeContracts.cs +++ b/src/Aquarium.Engine.Contracts/AquariumRuntimeContracts.cs @@ -1,5 +1,6 @@ using Aquarium.Engine.Input; using Aquarium.Engine.Render; +using Aquarium.Engine.Ui; namespace Aquarium.Engine; @@ -13,6 +14,8 @@ public interface IAquariumRuntime : IDisposable AquariumRenderPlan RenderPlan { get; } + AquariumUiDocument Ui { get; } + void Start(); void Update(float deltaSeconds, InputState input); diff --git a/src/Aquarium.Engine.Contracts/Ui/AquariumUi.cs b/src/Aquarium.Engine.Contracts/Ui/AquariumUi.cs new file mode 100644 index 0000000..ede01af --- /dev/null +++ b/src/Aquarium.Engine.Contracts/Ui/AquariumUi.cs @@ -0,0 +1,94 @@ +namespace Aquarium.Engine.Ui; + +public sealed class AquariumUiDocument +{ + private readonly List panels = []; + + public static AquariumUiDocument Empty { get; } = new(); + + public IReadOnlyList Panels => panels; + + public AquariumUiDocument Panel(string title, Action compose) + { + return Panel(title, 18.0f, 82.0f, 360.0f, compose); + } + + public AquariumUiDocument Panel(string title, float left, float top, float width, Action compose) + { + var controls = new List(); + compose(new AquariumUiPanelBuilder(controls)); + panels.Add(new AquariumUiPanel(title, left, top, width, controls)); + return this; + } +} + +public sealed record AquariumUiPanel( + string Title, + float Left, + float Top, + float Width, + IReadOnlyList Controls); + +public sealed class AquariumUiPanelBuilder(List controls) +{ + public AquariumUiPanelBuilder Section(string title, Func? isVisible = null) + { + controls.Add(new AquariumUiSection(title, isVisible)); + return this; + } + + public AquariumUiPanelBuilder Button(string label, Action action, string? tooltip = null, Func? isVisible = null) + { + controls.Add(new AquariumUiButton(label, action, tooltip, isVisible)); + return this; + } + + public AquariumUiPanelBuilder Toggle(string label, Func read, Action write, string? tooltip = null, Func? isVisible = null) + { + controls.Add(new AquariumUiToggle(label, read, write, tooltip, isVisible)); + return this; + } + + public AquariumUiPanelBuilder Slider(string label, Func read, Action write, float min, float max, string format = "0.###", string? tooltip = null, Func? isVisible = null) + { + controls.Add(new AquariumUiFloatSlider(label, read, write, min, max, format, tooltip, isVisible)); + return this; + } + + public AquariumUiPanelBuilder Slider(string label, Func read, Action write, int min, int max, string? tooltip = null, Func? isVisible = null) + { + controls.Add(new AquariumUiIntSlider(label, read, write, min, max, tooltip, isVisible)); + return this; + } + + public AquariumUiPanelBuilder Options(string label, Func read, Action write, IReadOnlyList options, string? tooltip = null, Func? isVisible = null) + { + controls.Add(new AquariumUiOptions(label, read, write, options, tooltip, isVisible)); + return this; + } +} + +public readonly record struct AquariumUiOption(int Value, string Label); + +public abstract record AquariumUiControl(string Label, string? Tooltip, Func? IsVisible) +{ + public bool Visible => IsVisible?.Invoke() ?? true; +} + +public sealed record AquariumUiSection(string Label, Func? IsVisible = null) + : AquariumUiControl(Label, null, IsVisible); + +public sealed record AquariumUiButton(string Label, Action Action, string? Tooltip = null, Func? IsVisible = null) + : AquariumUiControl(Label, Tooltip, IsVisible); + +public sealed record AquariumUiToggle(string Label, Func Read, Action Write, string? Tooltip = null, Func? IsVisible = null) + : AquariumUiControl(Label, Tooltip, IsVisible); + +public sealed record AquariumUiFloatSlider(string Label, Func Read, Action Write, float Min, float Max, string Format = "0.###", string? Tooltip = null, Func? IsVisible = null) + : AquariumUiControl(Label, Tooltip, IsVisible); + +public sealed record AquariumUiIntSlider(string Label, Func Read, Action Write, int Min, int Max, string? Tooltip = null, Func? IsVisible = null) + : AquariumUiControl(Label, Tooltip, IsVisible); + +public sealed record AquariumUiOptions(string Label, Func Read, Action Write, IReadOnlyList Options, string? Tooltip = null, Func? IsVisible = null) + : AquariumUiControl(Label, Tooltip, IsVisible); diff --git a/src/Aquarium.Engine/AquariumHost.cs b/src/Aquarium.Engine/AquariumHost.cs index 43d6746..91c0b13 100644 --- a/src/Aquarium.Engine/AquariumHost.cs +++ b/src/Aquarium.Engine/AquariumHost.cs @@ -47,7 +47,7 @@ public static class AquariumHost var deltaSeconds = (float)(now - lastFrame).TotalSeconds; lastFrame = now; - renderer.UpdateDebugUi(input); + renderer.UpdateUi(input, runtimeLoader.Runtime.Ui); ApplyRendererDebugInput(renderer, input); SyncRendererSettingsToRuntime(renderer, runtimeLoader.Runtime); runtimeLoader.Update(deltaSeconds, input); diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index c1d7dc5..2adbd13 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -3,6 +3,7 @@ using Aquarium.Engine.Render.Features; using Aquarium.Engine.Render.Graph; using SharpGen.Runtime; using Aquarium.Engine.Render.Ui; +using Aquarium.Engine.Ui; using Vortice; using Vortice.D3DCompiler; using Vortice.Direct3D; @@ -89,6 +90,8 @@ public sealed class D3D12Renderer : IAquariumRenderer private readonly ID3D12Fence fence; private readonly AutoResetEvent fenceEvent = new(false); private readonly DebugUi debugUi; + private IReadOnlyList clientUiPanels = []; + private AquariumUiDocument? currentClientUi; private ID3D11Resource[] overlayWrappedBackBuffers = []; private DirectWriteOverlay[] overlays = []; private D3D12RenderTarget heightFieldRenderTarget; @@ -244,9 +247,19 @@ public sealed class D3D12Renderer : IAquariumRenderer public bool HasPresentedReadyFrame => hasPresentedReadyFrame; - public void UpdateDebugUi(InputState input) + public void UpdateUi(InputState input, AquariumUiDocument clientUi) { debugUi.Update(input); + if (!ReferenceEquals(currentClientUi, clientUi)) + { + currentClientUi = clientUi; + clientUiPanels = clientUi.Panels.Select(DebugUi.FromContract).ToArray(); + } + + foreach (var panel in clientUiPanels) + { + panel.Update(input); + } } public void CycleRenderDebugMode() @@ -561,7 +574,7 @@ public sealed class D3D12Renderer : IAquariumRenderer { var wrappedBackBuffer = overlayWrappedBackBuffers[frameIndex]; overlayOn12Device.AcquireWrappedResources([wrappedBackBuffer]); - overlays[frameIndex].Render(frame, RenderDebugMode, debugUi); + overlays[frameIndex].Render(frame, RenderDebugMode, debugUi, clientUiPanels); overlayOn12Device.ReleaseWrappedResources([wrappedBackBuffer]); overlayContext.Flush(); frameResources.BackBuffer.MarkState(ResourceStates.Present); diff --git a/src/Aquarium.Engine/Render/DirectWriteOverlay.cs b/src/Aquarium.Engine/Render/DirectWriteOverlay.cs index c14f295..fd621ba 100644 --- a/src/Aquarium.Engine/Render/DirectWriteOverlay.cs +++ b/src/Aquarium.Engine/Render/DirectWriteOverlay.cs @@ -82,7 +82,7 @@ internal sealed class DirectWriteOverlay : IDisposable smallFormat = CreateTextFormat("Ubuntu Sans", 11.0f, FontWeight.Regular); } - public void Render(AquariumFrame frame, int renderDebugMode, DebugUi? debugUi) + public void Render(AquariumFrame frame, int renderDebugMode, DebugUi? debugUi, IReadOnlyList clientUiPanels) { renderTarget.BeginDraw(); DrawHeader( @@ -130,6 +130,28 @@ internal sealed class DirectWriteOverlay : IDisposable trackActiveBrush, width, height); + foreach (var panel in clientUiPanels) + { + panel.Draw( + renderTarget, + titleFormat, + smallFormat, + panelBrush, + rowBrush, + hoverRowBrush, + activeRowBrush, + outlineBrush, + primaryTextBrush, + quietTextBrush, + accentBrush, + accentHoverBrush, + accentActiveBrush, + dimAccentBrush, + trackHoverBrush, + trackActiveBrush, + width, + height); + } renderTarget.EndDraw(); } diff --git a/src/Aquarium.Engine/Render/IAquariumRenderer.cs b/src/Aquarium.Engine/Render/IAquariumRenderer.cs index ba82f68..39debd6 100644 --- a/src/Aquarium.Engine/Render/IAquariumRenderer.cs +++ b/src/Aquarium.Engine/Render/IAquariumRenderer.cs @@ -1,4 +1,5 @@ using Aquarium.Engine.Input; +using Aquarium.Engine.Ui; namespace Aquarium.Engine.Render; @@ -10,7 +11,7 @@ public interface IAquariumRenderer : IDisposable bool HasPresentedReadyFrame { get; } - void UpdateDebugUi(InputState input); + void UpdateUi(InputState input, AquariumUiDocument clientUi); void CycleRenderDebugMode(); diff --git a/src/Aquarium.Engine/Render/Ui/DebugUi.cs b/src/Aquarium.Engine/Render/Ui/DebugUi.cs index da75594..ddbb902 100644 --- a/src/Aquarium.Engine/Render/Ui/DebugUi.cs +++ b/src/Aquarium.Engine/Render/Ui/DebugUi.cs @@ -1,6 +1,7 @@ using System.Globalization; using System.Numerics; using Aquarium.Engine.Input; +using Aquarium.Engine.Ui; using Vortice.Direct2D1; using Vortice.DirectWrite; using Vortice.Mathematics; @@ -9,9 +10,9 @@ namespace Aquarium.Engine.Render.Ui; internal sealed class DebugUi { - private const float PanelLeft = 18.0f; - private const float PanelTop = 82.0f; - private const float PanelWidth = 360.0f; + private const float DefaultPanelLeft = 18.0f; + private const float DefaultPanelTop = 82.0f; + private const float DefaultPanelWidth = 360.0f; private const float HeaderHeight = 42.0f; private const float RowHeight = 31.0f; private const float RowGap = 6.0f; @@ -25,6 +26,9 @@ internal sealed class DebugUi private const float TooltipHeight = 34.0f; private readonly List controls = []; + private readonly float panelLeft; + private readonly float panelTop; + private readonly float panelWidth; private int? activeSliderId; private int? activeControlId; private bool closeButtonHovered; @@ -33,8 +37,16 @@ internal sealed class DebugUi private Vector2 mousePosition; public DebugUi(string title) + : this(title, DefaultPanelLeft, DefaultPanelTop, DefaultPanelWidth) + { + } + + public DebugUi(string title, float left, float top, float width) { Title = title; + panelLeft = left; + panelTop = top; + panelWidth = width; } public string Title { get; } @@ -43,6 +55,17 @@ internal sealed class DebugUi public bool WantsMouse { get; private set; } + public static DebugUi FromContract(AquariumUiPanel source) + { + var ui = new DebugUi(source.Title, source.Left, source.Top, source.Width); + foreach (var control in source.Controls) + { + ui.AddContractControl(control); + } + + return ui; + } + public DebugUi Panel(Action compose) { var panel = new DebugUiPanel(controls); @@ -50,6 +73,37 @@ internal sealed class DebugUi return this; } + private void AddContractControl(AquariumUiControl control) + { + switch (control) + { + case AquariumUiSection section: + controls.Add(new SectionControl(section.Label, () => section.Visible)); + break; + case AquariumUiButton button: + controls.Add(new ButtonControl(button.Label, button.Action, button.Tooltip, () => button.Visible)); + break; + case AquariumUiToggle toggle: + controls.Add(new ToggleControl(toggle.Label, toggle.Read, toggle.Write, toggle.Tooltip, () => toggle.Visible)); + break; + case AquariumUiFloatSlider slider: + controls.Add(new FloatSliderControl(slider.Label, slider.Read, slider.Write, slider.Min, slider.Max, slider.Format, slider.Tooltip, () => slider.Visible)); + break; + case AquariumUiIntSlider slider: + controls.Add(new IntSliderControl(slider.Label, slider.Read, slider.Write, slider.Min, slider.Max, slider.Tooltip, () => slider.Visible)); + break; + case AquariumUiOptions options: + controls.Add(new OptionControl( + options.Label, + options.Read, + options.Write, + options.Options.Select(option => new DebugUiOption(option.Value, option.Label)).ToArray(), + options.Tooltip, + () => options.Visible)); + break; + } + } + public void Update(InputState input) { if (input.IsKeyPressed(KeyCode.DebugUiToggle)) @@ -223,15 +277,15 @@ internal sealed class DebugUi private void Layout() { - var y = PanelTop + HeaderHeight + 10.0f; + var y = panelTop + HeaderHeight + 10.0f; foreach (var control in controls.Where(control => control.IsVisible)) { var height = control.LayoutHeight; - control.Bounds = RectFromEdges(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 = RectFromEdges(PanelLeft, PanelTop, PanelLeft + PanelWidth, y + 30.0f); + panelBounds = RectFromEdges(panelLeft, panelTop, panelLeft + panelWidth, y + 30.0f); } private Rect CloseButtonBounds() diff --git a/src/Aquarium.Epiphany/AquariumRuntime.cs b/src/Aquarium.Epiphany/AquariumRuntime.cs index e2082aa..0fcc491 100644 --- a/src/Aquarium.Epiphany/AquariumRuntime.cs +++ b/src/Aquarium.Epiphany/AquariumRuntime.cs @@ -2,6 +2,7 @@ using System.Numerics; using Aquarium.Engine; using Aquarium.Engine.Input; using Aquarium.Engine.Render; +using Aquarium.Engine.Ui; using Aquarium.Epiphany.State; namespace Aquarium.Epiphany; @@ -20,6 +21,7 @@ public sealed class AquariumRuntime : IAquariumRuntime private Vector2 previousCursorWorld; private float secondsSinceStateSave; private bool graphicsSettingsDirty; + private bool timePaused; public AquariumRuntime(AquariumRuntimeOptions options) { @@ -41,12 +43,20 @@ public sealed class AquariumRuntime : IAquariumRuntime ViewFrame = ViewFrame.FromCamera(cameraRig.Target, cameraRig.Distance); previousFrameTimeSeconds = timeSeconds; previousCursorWorld = ViewFrame.Center; + Ui = new AquariumUiDocument() + .Panel("Epiphany", 396.0f, 82.0f, 360.0f, panel => panel + .Section("Runtime") + .Toggle("Pause Time", () => timePaused, value => timePaused = value, "Stops Epiphany simulation time while leaving the renderer alive.") + .Slider("Time", () => timeSeconds, value => timeSeconds = MathF.Max(0.0f, value), 0.0f, 720.0f, "0.0", "Scrubs Epiphany simulation time.") + .Button("Flush State", FlushState, "Writes current Epiphany runtime state to CultCache.")); } public AquariumRuntimeOptions Options { get; } public AquariumRenderPlan RenderPlan { get; } = EpiphanyRenderPlan.Create(); + public AquariumUiDocument Ui { get; } + public AquariumFrame Frame => new(ViewFrame, cameraRig.Position, timeSeconds); public GraphicsSettings GraphicsSettings @@ -94,7 +104,11 @@ public sealed class AquariumRuntime : IAquariumRuntime public void Update(float deltaSeconds, InputState input) { - timeSeconds += Math.Max(deltaSeconds, 0.0f); + if (!timePaused) + { + timeSeconds += Math.Max(deltaSeconds, 0.0f); + } + cameraRig.ApplyInput(input, deltaSeconds); ViewFrame = ViewFrame.FromCamera(cameraRig.Target, cameraRig.Distance); secondsSinceStateSave += Math.Max(deltaSeconds, 0.0f); diff --git a/src/Aquarium.Sample.Minimal/MinimalRuntime.cs b/src/Aquarium.Sample.Minimal/MinimalRuntime.cs index 346a6ee..aa6e171 100644 --- a/src/Aquarium.Sample.Minimal/MinimalRuntime.cs +++ b/src/Aquarium.Sample.Minimal/MinimalRuntime.cs @@ -2,6 +2,7 @@ using System.Numerics; using Aquarium.Engine; using Aquarium.Engine.Input; using Aquarium.Engine.Render; +using Aquarium.Engine.Ui; namespace Aquarium.Sample.Minimal; @@ -15,6 +16,8 @@ public sealed class MinimalRuntime : IAquariumRuntime public AquariumRenderPlan RenderPlan { get; } = CreateRenderPlan(); + public AquariumUiDocument Ui { get; } = AquariumUiDocument.Empty; + public AquariumFrame Frame => new( new ViewFrame(Vector2.Zero, 24.0f), new Vector3(0.0f, -10.0f, 7.0f), diff --git a/state/evidence.jsonl b/state/evidence.jsonl index ac113b1..6f2bb53 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -53,3 +53,4 @@ {"timestamp":"2026-05-12T01:04:00+01:00","topic":"body-surface-packet-minimalism","lesson":"Do not let diagnostic identity fields squat in BodySurface. The hot surface packet now carries only PBR material values. Distance belongs to bodyDistance and the marcher; field identity is emitted by the proxy for temporal ownership; role/material/LOD/cost ids and the common shadeRoleAgentBody router were cut because per-role shader files should own their own material decisions directly.","files":["src/Aquarium.Engine/Render/Shaders/D3D12BodyCommon.hlsli","src/Aquarium.Engine/Render/Shaders/D3D12BodyProxy.hlsli","src/Aquarium.Engine/Render/Shaders/D3D12AgentCharacters.hlsli","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","src/Aquarium.Engine/Render/D3D12Renderer.cs","state/memory.json","state/map.yaml"],"source":"user architecture correction"} {"timestamp":"2026-05-12T01:58:00+01:00","topic":"temporal-event-lane-cut","lesson":"Dormant full-frame lanes are architectural debt, not preparedness. Aquarium had scene event color/metadata targets plus event history targets even though every producer wrote zero coverage. The event lane, dead AgentVisual LOD/index vector, dead reactive control channel, and one-field AgentSurface wrapper were cut; temporal resolve now carries only scene color, metadata, and coverage/step control history.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12BodyCommon.hlsli","src/Aquarium.Engine/Render/Shaders/D3D12BodyProxy.hlsli","src/Aquarium.Engine/Render/Shaders/D3D12AgentCharacters.hlsli","docs/hlsl-renderer.md","state/memory.json","state/map.yaml"],"source":"purification pass"} {"timestamp":"2026-05-12T04:30:00+01:00","topic":"engine-client-boundary-vocabulary","lesson":"Aquarium.Engine and Aquarium.Engine.Contracts must speak renderer vocabulary, not Epiphany policy vocabulary. Engine contracts expose ViewFrame, height-field brushes, SDF objects, SDF lights, generic shader manifests, and generic proxy passes. Epiphany may call its own things grid, agents, bodies, or roles, but the engine only consumes declared rendering data. Enforce this with scripts/verify-boundaries.ps1 before every architectural pass.","files":["scripts/verify-boundaries.ps1","src/Aquarium.Engine.Contracts","src/Aquarium.Engine","src/Aquarium.Epiphany","state/memory.json","state/map.yaml"],"source":"engine-client extraction correction"} +{"timestamp":"2026-05-12T05:30:00+01:00","topic":"client-ui-composition-contract","lesson":"Keep the engine debug panel as engine-owned UI, but expose a separate AquariumUiDocument contract for client overlay panels. Clients compose controls through Vortice-free panel builders; Aquarium.Engine translates those declarations into its DirectWrite/Direct2D overlay and owns input dispatch. Do not let Epiphany depend on DebugUi internals.","files":["src/Aquarium.Engine.Contracts/Ui/AquariumUi.cs","src/Aquarium.Engine/Render/Ui/DebugUi.cs","src/Aquarium.Engine/Render/DirectWriteOverlay.cs","src/Aquarium.Epiphany/AquariumRuntime.cs","state/memory.json"],"source":"client UI abstraction pass"} diff --git a/state/memory.json b/state/memory.json index 8aebb25..04cecbd 100644 --- a/state/memory.json +++ b/state/memory.json @@ -67,7 +67,8 @@ ], "text_and_ui": [ "Overlay text uses DirectWrite/Direct2D after scene rendering. It is crisp final-pixel UI for debug surfaces, terminals, inspectors, and splash/status text.", - "Aquarium debug controls follow the CultLib code-first binding style without Unity prefab assumptions: panels, sections, sliders, toggles, buttons, and option rows bound by explicit read/write delegates.", + "Aquarium UI controls follow the CultLib code-first binding style without Unity prefab assumptions: panels, sections, sliders, toggles, buttons, and option rows bound by explicit read/write delegates.", + "Aquarium.Engine.Contracts exposes AquariumUiDocument as the client-facing overlay composition surface. Clients declare panels and controls; Aquarium.Engine owns DirectWrite/Direct2D rendering, input dispatch, hover/active feedback, and panel presentation. Engine debug UI remains engine-owned and separate from client UI.", "Vortice.Mathematics.Rect uses x, y, width, height. UI code should use explicit RectFromEdges helpers when reasoning in edges.", "Debug UI feedback needs honest element-specific states: base, hover, and active. Sliders respond on track/fill and handle separately, keep geometry stable, and brighten by lerping accent colors toward white.", "Scene/diegetic labels should use renderer-owned MSDF/SDF billboards when they live in world space. Overlay text should not be shoved into HDR for convenience."