Add debug text editing capture

This commit is contained in:
Meta Vens 2026-05-12 14:04:00 +01:00
commit b82d8bbdd1
8 changed files with 344 additions and 24 deletions

View file

@ -90,6 +90,13 @@ public sealed class InputState
{
textInput.Add(value);
}
public InputState WithoutInteractiveInput()
{
var snapshot = new InputState();
snapshot.SetMousePosition(MousePosition);
return snapshot;
}
}
public enum MouseButton
@ -118,5 +125,11 @@ public enum KeyCode
RenderDebugCycle,
DebugUiToggle,
Backspace,
Delete,
Enter,
LeftArrow,
RightArrow,
Home,
End,
Shift,
}

View file

@ -50,9 +50,14 @@ public static class AquariumHost
lastFrame = now;
renderer.UpdateUi(input, runtimeLoader.Runtime.Ui);
ApplyRendererDebugInput(renderer, input);
var runtimeInput = renderer.CapturesInput ? input.WithoutInteractiveInput() : input;
if (!renderer.CapturesInput)
{
ApplyRendererDebugInput(renderer, input);
}
SyncRendererSettingsToRuntime(renderer, runtimeLoader.Runtime);
runtimeLoader.Update(deltaSeconds, input);
runtimeLoader.Update(deltaSeconds, runtimeInput);
synthHost.Update(AquariumSynthDocument.Combine(runtimeLoader.Runtime.Synth, renderer.DebugSynth), deltaSeconds);
if (!ReferenceEquals(settingsRuntime, runtimeLoader.Runtime))
{

View file

@ -64,7 +64,13 @@ public sealed class Win32Window : IDisposable
private const int VK_F1 = 0x70;
private const int VK_F2 = 0x71;
private const int VK_BACK = 0x08;
private const int VK_DELETE = 0x2E;
private const int VK_RETURN = 0x0D;
private const int VK_LEFT = 0x25;
private const int VK_RIGHT = 0x27;
private const int VK_HOME = 0x24;
private const int VK_END = 0x23;
private const int VK_SHIFT = 0x10;
private const int WHEEL_DELTA = 120;
private readonly WndProc windowProcedure;
@ -694,9 +700,27 @@ public sealed class Win32Window : IDisposable
case VK_BACK:
input.SetKey(KeyCode.Backspace, isDown);
break;
case VK_DELETE:
input.SetKey(KeyCode.Delete, isDown);
break;
case VK_RETURN:
input.SetKey(KeyCode.Enter, isDown);
break;
case VK_LEFT:
input.SetKey(KeyCode.LeftArrow, isDown);
break;
case VK_RIGHT:
input.SetKey(KeyCode.RightArrow, isDown);
break;
case VK_HOME:
input.SetKey(KeyCode.Home, isDown);
break;
case VK_END:
input.SetKey(KeyCode.End, isDown);
break;
case VK_SHIFT:
input.SetKey(KeyCode.Shift, isDown);
break;
}
}

View file

@ -262,6 +262,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
public bool HasPresentedReadyFrame => hasPresentedReadyFrame;
public bool CapturesInput => debugUi.WantsKeyboard;
public AquariumSynthDocument DebugSynth => new AquariumSynthDocument
{
MasterGain = 1.0f,

View file

@ -12,6 +12,8 @@ public interface IAquariumRenderer : IDisposable
bool HasPresentedReadyFrame { get; }
bool CapturesInput { get; }
AquariumSynthDocument DebugSynth { get; }
void UpdateUi(InputState input, AquariumUiDocument clientUi);

View file

@ -69,6 +69,8 @@ internal sealed class DebugUi
public bool WantsMouse { get; private set; }
public bool WantsKeyboard { get; private set; }
public static DebugUi FromContract(AquariumUiPanel source)
{
var ui = new DebugUi(source.Title, source.Left, source.Top, source.Width);
@ -137,6 +139,7 @@ internal sealed class DebugUi
if (!IsVisible)
{
WantsMouse = false;
WantsKeyboard = false;
activeSliderId = null;
focusedTextId = null;
return;
@ -144,6 +147,12 @@ internal sealed class DebugUi
Layout();
mousePosition = input.MousePosition;
if (focusedTextId is { } focusedId && controls.All(control => !control.IsVisible || control.Id != focusedId))
{
focusedTextId = null;
}
WantsKeyboard = focusedTextId is not null;
WantsMouse = Contains(panelBounds, mousePosition);
closeButtonHovered = Contains(CloseButtonBounds(), mousePosition);
closeButtonActive = closeButtonHovered && input.LeftMouseDown;
@ -170,6 +179,15 @@ internal sealed class DebugUi
return;
}
if (activeControlId is { } textDragId && input.LeftMouseDown)
{
if (controls.FirstOrDefault(control => control.IsVisible && control.Id == textDragId) is ITextInputControl textControl)
{
textControl.SelectToPoint(mousePosition);
return;
}
}
if (!input.IsMousePressed(MouseButton.Left))
{
ApplyTextInput(input);
@ -559,6 +577,8 @@ internal sealed class DebugUi
private interface ITextInputControl
{
void ApplyTextInput(InputState input);
void SelectToPoint(Vector2 point);
}
internal abstract class DebugUiControl(string label, string? tooltip = null, Func<bool>? isVisible = null)
@ -909,6 +929,10 @@ internal sealed class DebugUi
}
}
public void SelectToPoint(Vector2 point)
{
}
public override void Draw(
ID2D1RenderTarget target,
IDWriteFactory6 directWriteFactory,
@ -944,6 +968,12 @@ internal sealed class DebugUi
private const float LabelHeight = 20.0f;
private const float TextLineHeight = 18.0f;
private const float VerticalPadding = 9.0f;
private const float CharacterWidth = 7.0f;
private const float TextInsetX = 8.0f;
private const float TextInsetY = 6.0f;
private int caretIndex = -1;
private int selectionAnchor = -1;
private string lastValue = string.Empty;
public override float LayoutHeight => LabelLineHeight + VerticalPadding * 2.0f + Math.Max(1, lines) * TextLineHeight;
@ -951,49 +981,108 @@ internal sealed class DebugUi
public void ApplyTextInput(InputState input)
{
var value = read();
SyncEditingState();
var value = NormalizedValue;
var selecting = input.IsKeyDown(KeyCode.Shift);
if (input.IsKeyPressed(KeyCode.LeftArrow))
{
MoveCaret(Math.Max(0, caretIndex - 1), selecting);
}
if (input.IsKeyPressed(KeyCode.RightArrow))
{
MoveCaret(Math.Min(value.Length, caretIndex + 1), selecting);
}
if (input.IsKeyPressed(KeyCode.Home))
{
MoveCaret(LineStart(value, caretIndex), selecting);
}
if (input.IsKeyPressed(KeyCode.End))
{
MoveCaret(LineEnd(value, caretIndex), selecting);
}
if (input.IsKeyPressed(KeyCode.Backspace))
{
if (submit is not null && !acceptsReturn && IsEmptyPrompt(value))
{
return;
}
if (HasSelection)
{
value = DeleteSelection(value);
}
else if (caretIndex > 0)
{
value = value.Remove(caretIndex - 1, 1);
MoveCaret(caretIndex - 1, selecting: false);
}
}
if (input.IsKeyPressed(KeyCode.Delete))
{
if (HasSelection)
{
value = DeleteSelection(value);
}
else if (caretIndex < value.Length)
{
value = value.Remove(caretIndex, 1);
}
}
foreach (var ch in input.TextInput)
{
switch (ch)
{
case '\b':
if (submit is not null && !acceptsReturn && IsEmptyPrompt(value))
{
break;
}
if (value.Length > 0)
{
value = value[..^1];
}
break;
case '\r':
case '\n':
if (acceptsReturn)
{
value += "\n";
value = InsertText(value, "\n");
}
else
{
submit?.Invoke();
value = read();
caretIndex = -1;
selectionAnchor = -1;
value = NormalizedValue;
SyncEditingState(force: true);
}
break;
default:
if (!char.IsControl(ch))
{
value += ch;
value = InsertText(value, ch.ToString());
}
break;
}
}
if (value != read())
if (value != NormalizedValue)
{
write(value);
lastValue = value;
}
}
public void SelectToPoint(Vector2 point)
{
SyncEditingState();
MoveCaret(IndexFromPoint(point), selecting: true);
}
public override void Click(Vector2 mouse)
{
SyncEditingState();
MoveCaret(IndexFromPoint(mouse), selecting: false);
}
public override void Draw(
ID2D1RenderTarget target,
IDWriteFactory6 directWriteFactory,
@ -1022,8 +1111,9 @@ internal sealed class DebugUi
target.FillRectangle(box, IsActive ? activeRowBrush : IsHovered ? hoverRowBrush : rowBrush);
target.DrawRectangle(box, focused ? accentBrush : outlineBrush, focused ? 1.5f : 1.0f);
DrawSelection(target, activeRowBrush, box);
DrawBoxText(target, directWriteFactory, format, primaryBrush, box);
DrawCaret(target, accentBrush, box);
}
private string DisplayText()
@ -1033,7 +1123,7 @@ internal sealed class DebugUi
private Rect TextBounds(Rect box)
{
return RectFromEdges(box.Left + 8.0f, box.Top + 6.0f, box.Right - 8.0f, box.Bottom - 6.0f);
return RectFromEdges(box.Left + TextInsetX, box.Top + TextInsetY, box.Right - TextInsetX, box.Bottom - TextInsetY);
}
private void DrawBoxText(
@ -1045,9 +1135,7 @@ internal sealed class DebugUi
{
var bounds = TextBounds(box);
var displayLines = DisplayLines();
var top = alignBottom
? Math.Max(bounds.Top, bounds.Bottom - displayLines.Length * TextLineHeight)
: bounds.Top;
var top = TextTop(bounds, displayLines.Length);
for (var index = 0; index < displayLines.Length; index++)
{
using var layout = directWriteFactory.CreateTextLayout(displayLines[index], format, bounds.Width, TextLineHeight);
@ -1057,15 +1145,200 @@ internal sealed class DebugUi
private string[] DisplayLines()
{
var value = read().Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n');
var split = value.Split('\n');
var split = NormalizedValue.Split('\n');
return split.Length <= lines ? split : split.Skip(split.Length - lines).ToArray();
}
private int DisplayStartIndex()
{
var split = NormalizedValue.Split('\n');
if (split.Length <= lines)
{
return 0;
}
var skippedLines = split.Length - lines;
var index = 0;
for (var line = 0; line < skippedLines; line++)
{
index += split[line].Length + 1;
}
return index;
}
private void SyncEditingState(bool force = false)
{
var value = NormalizedValue;
if (!force && caretIndex >= 0 && string.Equals(value, lastValue, StringComparison.Ordinal))
{
return;
}
lastValue = value;
caretIndex = value.Length;
selectionAnchor = caretIndex;
}
private void MoveCaret(int index, bool selecting)
{
caretIndex = Math.Clamp(index, 0, NormalizedValue.Length);
if (!selecting)
{
selectionAnchor = caretIndex;
}
}
private string InsertText(string value, string text)
{
if (HasSelection)
{
value = DeleteSelection(value);
}
value = value.Insert(caretIndex, text);
MoveCaret(caretIndex + text.Length, selecting: false);
return value;
}
private string DeleteSelection(string value)
{
var start = SelectionStart;
var length = SelectionEnd - SelectionStart;
value = value.Remove(start, length);
MoveCaret(start, selecting: false);
return value;
}
private int IndexFromPoint(Vector2 point)
{
var bounds = TextBounds(BoxBounds);
var displayLines = DisplayLines();
var top = TextTop(bounds, displayLines.Length);
var line = Math.Clamp((int)MathF.Floor((point.Y - top) / TextLineHeight), 0, Math.Max(0, displayLines.Length - 1));
var column = Math.Max(0, (int)MathF.Round((point.X - bounds.Left) / CharacterWidth));
var index = DisplayStartIndex();
for (var lineIndex = 0; lineIndex < line; lineIndex++)
{
index += displayLines[lineIndex].Length + 1;
}
return Math.Min(index + Math.Min(column, displayLines[line].Length), NormalizedValue.Length);
}
private void DrawSelection(ID2D1RenderTarget target, ID2D1SolidColorBrush activeRowBrush, Rect box)
{
if (!HasSelection)
{
return;
}
var bounds = TextBounds(box);
var displayLines = DisplayLines();
var displayStart = DisplayStartIndex();
var displayEnd = displayStart + DisplayText().Length;
var start = Math.Max(SelectionStart, displayStart);
var end = Math.Min(SelectionEnd, displayEnd);
if (start >= end)
{
return;
}
var top = TextTop(bounds, displayLines.Length);
var lineStart = displayStart;
for (var line = 0; line < displayLines.Length; line++)
{
var lineEnd = lineStart + displayLines[line].Length;
var startColumn = Math.Max(0, start - lineStart);
var endColumn = Math.Min(displayLines[line].Length, end - lineStart);
if (endColumn > startColumn)
{
target.FillRectangle(
RectFromEdges(
bounds.Left + startColumn * CharacterWidth,
top + line * TextLineHeight + 2.0f,
bounds.Left + endColumn * CharacterWidth,
top + (line + 1) * TextLineHeight),
activeRowBrush);
}
lineStart = lineEnd + 1;
}
}
private void DrawCaret(ID2D1RenderTarget target, ID2D1SolidColorBrush accentBrush, Rect box)
{
if (!IsFocused)
{
return;
}
var bounds = TextBounds(box);
var displayLines = DisplayLines();
var displayStart = DisplayStartIndex();
var displayEnd = displayStart + DisplayText().Length;
if (caretIndex < displayStart || caretIndex > displayEnd)
{
return;
}
var top = TextTop(bounds, displayLines.Length);
var lineStart = displayStart;
for (var line = 0; line < displayLines.Length; line++)
{
var lineEnd = lineStart + displayLines[line].Length;
if (caretIndex <= lineEnd || line == displayLines.Length - 1)
{
var column = Math.Clamp(caretIndex - lineStart, 0, displayLines[line].Length);
var x = bounds.Left + column * CharacterWidth;
var y = top + line * TextLineHeight + 2.0f;
target.DrawLine(new Vector2(x, y), new Vector2(x, y + TextLineHeight - 2.0f), accentBrush, 1.25f);
return;
}
lineStart = lineEnd + 1;
}
}
private static int LineStart(string value, int index)
{
var clamped = Math.Clamp(index, 0, value.Length);
if (clamped == 0)
{
return 0;
}
var previousNewline = value.LastIndexOf('\n', Math.Max(0, clamped - 1));
return previousNewline < 0 ? 0 : previousNewline + 1;
}
private static int LineEnd(string value, int index)
{
var nextNewline = value.IndexOf('\n', Math.Clamp(index, 0, value.Length));
return nextNewline < 0 ? value.Length : nextNewline;
}
private float TextTop(Rect bounds, int lineCount)
{
return alignBottom
? Math.Max(bounds.Top, bounds.Bottom - lineCount * TextLineHeight)
: bounds.Top;
}
private bool HasLabel => !string.IsNullOrWhiteSpace(Label);
private float LabelLineHeight => HasLabel ? LabelHeight : 0.0f;
private Rect BoxBounds => RectFromEdges(Bounds.Left, Bounds.Top + LabelLineHeight, Bounds.Right, Bounds.Bottom);
private string NormalizedValue => read().Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n');
private bool HasSelection => caretIndex != selectionAnchor;
private int SelectionStart => Math.Min(caretIndex, selectionAnchor);
private int SelectionEnd => Math.Max(caretIndex, selectionAnchor);
private static bool IsEmptyPrompt(string value)
{
var normalized = value.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n');

View file

@ -64,3 +64,4 @@
{"timestamp":"2026-05-12T13:42:00+01:00","topic":"terminal-textbox-polish","lesson":"Terminal text boxes need terminal typography and prompt-safe editing. Bundle Ubuntu Sans Mono for DirectWrite monospace surfaces, let AquariumUiTextBox opt into monospace and bottom alignment, and guard empty trailing prompts so backspace cannot collapse the prompt sentinel and reclassify the log as command input.","files":["src/Aquarium.Engine/Assets/Fonts/UbuntuSansMono.ttf","src/Aquarium.Engine.Contracts/Ui/AquariumUi.cs","src/Aquarium.Engine/Render/DirectWriteOverlay.cs","src/Aquarium.Engine/Render/Ui/DebugUi.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","state/memory.json"],"source":"user terminal polish correction"}
{"timestamp":"2026-05-12T13:55:00+01:00","topic":"terminal-surface-layout","lesson":"Terminal surfaces should not display ordinary text-box labels or fake corner carets. A terminal prompt is already the affordance. Use label-less text boxes for terminal sessions, use a near-aligned monospace text format, and bottom-align by drawing the terminal line list against the box bottom rather than relying on centered DirectWrite paragraph layout.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/DirectWriteOverlay.cs","src/Aquarium.Engine/Render/Ui/DebugUi.cs","state/evidence.jsonl"],"source":"user visual correction"}
{"timestamp":"2026-05-12T14:05:00+01:00","topic":"terminal-bottom-line-layout","lesson":"DirectWrite multiline layout can still make a terminal body feel vertically floating even when the containing bounds are bottom-biased. For terminal-style text boxes, draw visible lines individually from the bottom of the text bounds using explicit line height; do not rely on padded blank lines or paragraph layout for bottom alignment.","files":["src/Aquarium.Engine/Render/Ui/DebugUi.cs","state/evidence.jsonl"],"source":"user screenshot correction"}
{"timestamp":"2026-05-12T14:25:00+01:00","topic":"debug-text-editing-capture","lesson":"Debug text boxes need real editing ownership: persistent caret index, selection anchor, mouse placement, drag selection, arrow/Home/End movement, Backspace/Delete, selected-text replacement, and input capture while focused. When a text box owns the keyboard, host-level debug hotkeys and client camera controls must receive a non-interactive input snapshot instead of the live key/mouse state.","files":["src/Aquarium.Engine.Contracts/Input/InputState.cs","src/Aquarium.Engine/AquariumHost.cs","src/Aquarium.Engine/Platform/Win32Window.cs","src/Aquarium.Engine/Render/IAquariumRenderer.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Ui/DebugUi.cs","state/memory.json"],"source":"user text editing requirement"}

View file

@ -68,7 +68,7 @@
"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 UI controls follow the CultLib code-first binding style without Unity prefab assumptions: panels, sections, sliders, toggles, buttons, option rows, and text boxes bound by explicit read/write delegates.",
"Aquarium.Engine.Contracts exposes AquariumUiDocument as the client-facing debug composition surface. Client panels are tabs inside the single Aquarium debug panel, clients can register terminal commands through AquariumUiDocument.Command, and longer editable strings should use AquariumUiTextBox instead of pretending to be row values. Text boxes may opt into bundled Ubuntu Sans Mono and bottom alignment when the surface is terminal-like.",
"Aquarium.Engine.Contracts exposes AquariumUiDocument as the client-facing debug composition surface. Client panels are tabs inside the single Aquarium debug panel, clients can register terminal commands through AquariumUiDocument.Command, and longer editable strings should use AquariumUiTextBox instead of pretending to be row values. Text boxes own caret/selection editing state, may opt into bundled Ubuntu Sans Mono and bottom alignment, and focused text boxes capture input so renderer debug hotkeys and client camera controls stay quiet while editing.",
"The unified F2 debug panel is titled Debug and owns engine controls, client tabs, a bottom-aligned multiline command terminal, and the synth playground. Tab navigation is panel chrome: a highlighted row of buttons below the title, not a content section made of ordinary controls. Do not reintroduce separate Aquarium/Epiphany debug panels fighting for the same hotkey.",
"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.",