Add preview hover guides and editor hotkeys

This commit is contained in:
Meta Vens 2026-06-01 14:26:33 +01:00
commit fc38dd0d69
7 changed files with 112 additions and 13 deletions

View file

@ -128,8 +128,13 @@ public enum KeyCode
Delete,
Enter,
LeftArrow,
UpArrow,
RightArrow,
DownArrow,
Home,
End,
PageUp,
PageDown,
Shift,
Control,
}

View file

@ -100,6 +100,17 @@ public readonly record struct AquariumUiPreviewInteraction(
float DeltaX,
float DeltaY);
public readonly record struct AquariumUiPreviewState(
string HoverItemId = "",
string HoverHandle = "",
string ActiveItemId = "",
string ActiveHandle = "");
public readonly record struct AquariumUiPreviewGuide(
string Axis,
float Position,
string Tone = "neutral");
public sealed record AquariumUiElement(
string Id,
string Kind,
@ -120,6 +131,8 @@ public sealed record AquariumUiElement(
Action<int>? WriteOption = null,
IReadOnlyList<AquariumUiOption>? Options = null,
Func<IReadOnlyList<AquariumUiPreviewItem>>? ReadPreviewItems = null,
Func<AquariumUiPreviewState>? ReadPreviewState = null,
Func<IReadOnlyList<AquariumUiPreviewGuide>>? ReadPreviewGuides = null,
bool PreviewBlitsOutputBuffer = false,
Action<AquariumUiPreviewInteraction>? HandlePreviewInteraction = null,
Action? Invoke = null,
@ -217,13 +230,17 @@ public sealed class AquariumUiSurfaceBuilder(List<AquariumUiElement> children)
Func<IReadOnlyList<AquariumUiPreviewItem>> readItems,
float weight = 1.0f,
bool blitOutputBuffer = false,
Action<AquariumUiPreviewInteraction>? handleInteraction = null)
Action<AquariumUiPreviewInteraction>? handleInteraction = null,
Func<AquariumUiPreviewState>? readState = null,
Func<IReadOnlyList<AquariumUiPreviewGuide>>? readGuides = null)
{
children.Add(new AquariumUiElement(
id,
"preview",
label,
ReadPreviewItems: readItems,
ReadPreviewState: readState,
ReadPreviewGuides: readGuides,
PreviewBlitsOutputBuffer: blitOutputBuffer,
HandlePreviewInteraction: handleInteraction,
Weight: Math.Max(0.001f, weight)));

View file

@ -69,10 +69,15 @@ public sealed class Win32Window : IDisposable
private const int VK_DELETE = 0x2E;
private const int VK_RETURN = 0x0D;
private const int VK_LEFT = 0x25;
private const int VK_UP = 0x26;
private const int VK_RIGHT = 0x27;
private const int VK_DOWN = 0x28;
private const int VK_HOME = 0x24;
private const int VK_END = 0x23;
private const int VK_PRIOR = 0x21;
private const int VK_NEXT = 0x22;
private const int VK_SHIFT = 0x10;
private const int VK_CONTROL = 0x11;
private const int WHEEL_DELTA = 120;
private readonly WndProc windowProcedure;
@ -824,18 +829,33 @@ public sealed class Win32Window : IDisposable
case VK_LEFT:
input.SetKey(KeyCode.LeftArrow, isDown);
break;
case VK_UP:
input.SetKey(KeyCode.UpArrow, isDown);
break;
case VK_RIGHT:
input.SetKey(KeyCode.RightArrow, isDown);
break;
case VK_DOWN:
input.SetKey(KeyCode.DownArrow, isDown);
break;
case VK_HOME:
input.SetKey(KeyCode.Home, isDown);
break;
case VK_END:
input.SetKey(KeyCode.End, isDown);
break;
case VK_PRIOR:
input.SetKey(KeyCode.PageUp, isDown);
break;
case VK_NEXT:
input.SetKey(KeyCode.PageDown, isDown);
break;
case VK_SHIFT:
input.SetKey(KeyCode.Shift, isDown);
break;
case VK_CONTROL:
input.SetKey(KeyCode.Control, isDown);
break;
}
}

View file

@ -613,6 +613,14 @@ public sealed class D3D12Renderer : IAquariumRenderer
clientUiSurfaceWantsMouse = true;
if (!input.LeftMouseDown)
{
active.Element.HandlePreviewInteraction?.Invoke(new AquariumUiPreviewInteraction(
active.ItemId,
active.Handle,
"end",
Math.Clamp((input.MousePosition.X - active.Canvas.Left) / Math.Max(1.0f, active.Canvas.Width), 0.0f, 1.0f),
Math.Clamp((input.MousePosition.Y - active.Canvas.Top) / Math.Max(1.0f, active.Canvas.Height), 0.0f, 1.0f),
0.0f,
0.0f));
activePreviewDrag = null;
return;
}
@ -633,6 +641,14 @@ public sealed class D3D12Renderer : IAquariumRenderer
if (TryHitPreview(input.MousePosition, out var hit))
{
clientUiSurfaceWantsMouse = true;
hit.Element.HandlePreviewInteraction?.Invoke(new AquariumUiPreviewInteraction(
hit.ItemId,
hit.Handle,
"hover",
hit.X,
hit.Y,
0.0f,
0.0f));
if (input.IsMousePressed(MouseButton.Left))
{
activePreviewDrag = new AquariumUiPreviewDrag(hit.Element, hit.ItemId, hit.Handle, hit.Canvas);
@ -766,7 +782,14 @@ public sealed class D3D12Renderer : IAquariumRenderer
return true;
}
return false;
hit = new AquariumUiPreviewHit(
element,
"",
"canvas",
canvas,
Math.Clamp((point.X - canvas.Left) / Math.Max(1.0f, canvas.Width), 0.0f, 1.0f),
Math.Clamp((point.Y - canvas.Top) / Math.Max(1.0f, canvas.Height), 0.0f, 1.0f));
return true;
}
private static bool TryPreviewCanvas(Rect bounds, bool hasLabel, out Rect canvas)

View file

@ -1,3 +1,4 @@
using System.Numerics;
using Vortice.DCommon;
using Vortice.Direct2D1;
using Vortice.DirectWrite;
@ -396,8 +397,10 @@ internal sealed class DirectWriteOverlay : IDisposable
}
renderTarget.DrawRectangle(canvas, outlineBrush, 1.0f);
DrawPreviewGuides(element, canvas);
var items = element.ReadPreviewItems?.Invoke() ?? [];
var state = element.ReadPreviewState?.Invoke() ?? default;
foreach (var item in items)
{
var left = canvas.Left + Math.Clamp(item.X, 0.0f, 1.0f) * canvas.Width;
@ -410,24 +413,51 @@ internal sealed class DirectWriteOverlay : IDisposable
continue;
}
var brush = item.Selected ? accentBrush : ToneBrush(item.Tone);
renderTarget.FillRectangle(itemBounds, item.Selected ? hoverRowBrush : rowBrush);
renderTarget.DrawRectangle(itemBounds, brush, item.Selected ? 2.0f : 1.0f);
var hovered = string.Equals(state.HoverItemId, item.Id, StringComparison.Ordinal);
var active = string.Equals(state.ActiveItemId, item.Id, StringComparison.Ordinal);
var brush = active || item.Selected ? accentBrush : ToneBrush(item.Tone);
renderTarget.FillRectangle(itemBounds, active ? activeRowBrush : hovered ? hoverRowBrush : rowBrush);
renderTarget.DrawRectangle(itemBounds, brush, active || item.Selected ? 2.0f : hovered ? 1.5f : 1.0f);
renderTarget.DrawText(item.Label, smallFormat, RectFromEdges(itemBounds.Left + 6.0f, itemBounds.Top + 3.0f, itemBounds.Right - 6.0f, itemBounds.Bottom), primaryTextBrush, DrawTextOptions.Clip);
if (item.Selected && element.HandlePreviewInteraction is not null)
{
DrawPreviewHandle(itemBounds.Left, itemBounds.Top);
DrawPreviewHandle(itemBounds.Right, itemBounds.Top);
DrawPreviewHandle(itemBounds.Left, itemBounds.Bottom);
DrawPreviewHandle(itemBounds.Right, itemBounds.Bottom);
DrawPreviewHandle(itemBounds.Left, itemBounds.Top, state, item.Id, "nw");
DrawPreviewHandle(itemBounds.Right, itemBounds.Top, state, item.Id, "ne");
DrawPreviewHandle(itemBounds.Left, itemBounds.Bottom, state, item.Id, "sw");
DrawPreviewHandle(itemBounds.Right, itemBounds.Bottom, state, item.Id, "se");
}
}
}
private void DrawPreviewHandle(float x, float y)
private void DrawPreviewGuides(AquariumUiElement element, Rect canvas)
{
var handle = RectFromEdges(x - 4.0f, y - 4.0f, x + 4.0f, y + 4.0f);
renderTarget.FillRectangle(handle, accentActiveBrush);
var guides = element.ReadPreviewGuides?.Invoke() ?? [];
foreach (var guide in guides)
{
var position = Math.Clamp(guide.Position, 0.0f, 1.0f);
var brush = ToneBrush(guide.Tone);
if (string.Equals(guide.Axis, "x", StringComparison.Ordinal))
{
var x = canvas.Left + position * canvas.Width;
renderTarget.DrawLine(new Vector2(x, canvas.Top), new Vector2(x, canvas.Bottom), brush, 1.25f);
}
else if (string.Equals(guide.Axis, "y", StringComparison.Ordinal))
{
var y = canvas.Top + position * canvas.Height;
renderTarget.DrawLine(new Vector2(canvas.Left, y), new Vector2(canvas.Right, y), brush, 1.25f);
}
}
}
private void DrawPreviewHandle(float x, float y, AquariumUiPreviewState state, string itemId, string handleId)
{
var active = string.Equals(state.ActiveItemId, itemId, StringComparison.Ordinal) &&
string.Equals(state.ActiveHandle, handleId, StringComparison.Ordinal);
var hovered = string.Equals(state.HoverItemId, itemId, StringComparison.Ordinal) &&
string.Equals(state.HoverHandle, handleId, StringComparison.Ordinal);
var radius = active ? 6.0f : hovered ? 5.0f : 4.0f;
var handle = RectFromEdges(x - radius, y - radius, x + radius, y + radius);
renderTarget.FillRectangle(handle, active ? accentActiveBrush : hovered ? accentHoverBrush : accentBrush);
renderTarget.DrawRectangle(handle, panelBrush, 1.0f);
}