Make all commands be parallel to the XY plane because i can't be bothered to fix them all.

This commit is contained in:
H. Utku Maden 2025-01-17 21:34:15 +03:00
parent cde0fe2901
commit 8ce1329dfc
3 changed files with 78 additions and 67 deletions

@ -23,9 +23,9 @@ namespace Dashboard.Drawing
{ {
AddCommand(DrawPoint = new DrawCommand<PointCommandArgs>("Point", this, PointCommandArgs.CommandSize)); AddCommand(DrawPoint = new DrawCommand<PointCommandArgs>("Point", this, PointCommandArgs.CommandSize));
AddCommand(DrawLine = new DrawCommand<LineCommandArgs>("Line", this, LineCommandArgs.CommandSize)); AddCommand(DrawLine = new DrawCommand<LineCommandArgs>("Line", this, LineCommandArgs.CommandSize));
AddCommand(DrawRectF = new RectCommand(RectCommand.Mode.Fill)); AddCommand(DrawRectF = new RectCommand(this, RectCommand.Mode.Fill));
AddCommand(DrawRectS = new RectCommand(RectCommand.Mode.Strike)); AddCommand(DrawRectS = new RectCommand(this, RectCommand.Mode.Strike));
AddCommand(DrawRectFS = new RectCommand(RectCommand.Mode.FillStrike)); AddCommand(DrawRectFS = new RectCommand(this, RectCommand.Mode.FillStrike));
} }
public static readonly DbBaseCommands Instance = new DbBaseCommands(); public static readonly DbBaseCommands Instance = new DbBaseCommands();
@ -33,13 +33,15 @@ namespace Dashboard.Drawing
public struct PointCommandArgs : IParameterSerializer<PointCommandArgs> public struct PointCommandArgs : IParameterSerializer<PointCommandArgs>
{ {
public Vector3 Position { get; private set; } public Vector2 Position { get; private set; }
public float Depth { get; private set; }
public float Size { get; private set; } public float Size { get; private set; }
public IBrush? Brush { get; private set; } public IBrush? Brush { get; private set; }
public PointCommandArgs(Vector3 position, float size, IBrush brush) public PointCommandArgs(Vector2 position, float depth, float size, IBrush brush)
{ {
Position = position; Position = position;
Depth = depth;
Brush = brush; Brush = brush;
Size = size; Size = size;
} }
@ -51,7 +53,7 @@ namespace Dashboard.Drawing
Span<Value> value = stackalloc Value[] Span<Value> value = stackalloc Value[]
{ {
new Value(Position, Size, queue.RequireResource(Brush!)) new Value(Position, Depth, Size, queue.RequireResource(Brush!))
}; };
MemoryMarshal.AsBytes(value).CopyTo(bytes); MemoryMarshal.AsBytes(value).CopyTo(bytes);
@ -67,28 +69,31 @@ namespace Dashboard.Drawing
Value value = MemoryMarshal.AsRef<Value>(bytes); Value value = MemoryMarshal.AsRef<Value>(bytes);
Position = value.Position; Position = value.Position;
Depth = value.Depth;
Size = value.Size; Size = value.Size;
Brush = (IBrush)queue.Resources[value.BrushIndex]; Brush = (IBrush)queue.Resources[value.BrushIndex];
} }
private record struct Value(Vector3 Position, float Size, int BrushIndex); private record struct Value(Vector2 Position, float Depth, float Size, int BrushIndex);
public static readonly int CommandSize = Unsafe.SizeOf<Value>(); public static readonly int CommandSize = Unsafe.SizeOf<Value>();
} }
public struct LineCommandArgs : IParameterSerializer<LineCommandArgs> public struct LineCommandArgs : IParameterSerializer<LineCommandArgs>
{ {
public Vector3 Start { get; private set; } public Vector2 Start { get; private set; }
public Vector3 End { get; private set; } public Vector2 End { get; private set; }
public float Depth { get; private set; }
public float Size { get; private set; } public float Size { get; private set; }
public IBrush? Brush { get; private set; } public IBrush? Brush { get; private set; }
public LineCommandArgs(Vector3 start, Vector3 end, float size, IBrush brush) public LineCommandArgs(Vector2 start, Vector2 end, float depth, float size, IBrush brush)
{ {
Start = start; Start = start;
End = end; End = end;
Brush = brush; Depth = depth;
Size = size; Size = size;
Brush = brush;
} }
public int Serialize(DrawQueue queue, Span<byte> bytes) public int Serialize(DrawQueue queue, Span<byte> bytes)
@ -98,7 +103,7 @@ namespace Dashboard.Drawing
Span<Value> value = stackalloc Value[] Span<Value> value = stackalloc Value[]
{ {
new Value(Start, End, Size, queue.RequireResource(Brush!)) new Value(Start, End, Depth, Size, queue.RequireResource(Brush!))
}; };
MemoryMarshal.AsBytes(value).CopyTo(bytes); MemoryMarshal.AsBytes(value).CopyTo(bytes);
@ -114,21 +119,17 @@ namespace Dashboard.Drawing
Start = value.Start; Start = value.Start;
End = value.End; End = value.End;
Depth = value.Depth;
Size = value.Size; Size = value.Size;
Brush = (IBrush)queue.Resources[value.BrushIndex]; Brush = (IBrush)queue.Resources[value.BrushIndex];
} }
private record struct Value(Vector3 Start, Vector3 End, float Size, int BrushIndex); private record struct Value(Vector2 Start, Vector2 End, float Depth, float Size, int BrushIndex);
public static readonly int CommandSize = Unsafe.SizeOf<Value>(); public static readonly int CommandSize = Unsafe.SizeOf<Value>();
} }
public enum BorderKind
{
Inset = -1,
Center = 0,
Outset = 1,
}
public class RectCommand : IDrawCommand<RectCommandArgs> public class RectCommand : IDrawCommand<RectCommandArgs>
{ {
@ -137,9 +138,9 @@ namespace Dashboard.Drawing
public IDrawExtension Extension { get; } public IDrawExtension Extension { get; }
public int Length { get; } public int Length { get; }
public RectCommand(Mode mode) public RectCommand(IDrawExtension extension, Mode mode)
{ {
Extension = DbBaseCommands.Instance; Extension = extension;
_mode = mode; _mode = mode;
switch (mode) switch (mode)
@ -175,15 +176,15 @@ namespace Dashboard.Drawing
{ {
case Mode.Fill: case Mode.Fill:
ref readonly RectF f = ref MemoryMarshal.AsRef<RectF>(param); ref readonly RectF f = ref MemoryMarshal.AsRef<RectF>(param);
args = new RectCommandArgs(f.Start, f.End, (IBrush)queue.Resources[f.FillBrushIndex]); args = new RectCommandArgs(f.Start, f.End, f.Depth, (IBrush)queue.Resources[f.FillBrushIndex]);
break; break;
case Mode.Strike: case Mode.Strike:
ref readonly RectS s = ref MemoryMarshal.AsRef<RectS>(param); ref readonly RectS s = ref MemoryMarshal.AsRef<RectS>(param);
args = new RectCommandArgs(s.Start, s.End, (IBrush)queue.Resources[s.StrikeBrushIndex], s.StrikeSize, s.BorderKind); args = new RectCommandArgs(s.Start, s.End, s.Depth, (IBrush)queue.Resources[s.StrikeBrushIndex], s.StrikeSize, s.BorderKind);
break; break;
default: default:
ref readonly RectFS fs = ref MemoryMarshal.AsRef<RectFS>(param); ref readonly RectFS fs = ref MemoryMarshal.AsRef<RectFS>(param);
args = new RectCommandArgs(fs.Start, fs.End, (IBrush)queue.Resources[fs.FillBrushIndex], args = new RectCommandArgs(fs.Start, fs.End, fs.Depth, (IBrush)queue.Resources[fs.FillBrushIndex],
(IBrush)queue.Resources[fs.StrikeBrushIndex], fs.StrikeSize, fs.BorderKind); (IBrush)queue.Resources[fs.StrikeBrushIndex], fs.StrikeSize, fs.BorderKind);
break; break;
} }
@ -207,12 +208,14 @@ namespace Dashboard.Drawing
ref RectF f = ref MemoryMarshal.AsRef<RectF>(param); ref RectF f = ref MemoryMarshal.AsRef<RectF>(param);
f.Start = obj.Start; f.Start = obj.Start;
f.End = obj.End; f.End = obj.End;
f.Depth = obj.Depth;
f.FillBrushIndex = queue.RequireResource(obj.FillBrush!); f.FillBrushIndex = queue.RequireResource(obj.FillBrush!);
break; break;
case Mode.Strike: case Mode.Strike:
ref RectS s = ref MemoryMarshal.AsRef<RectS>(param); ref RectS s = ref MemoryMarshal.AsRef<RectS>(param);
s.Start = obj.Start; s.Start = obj.Start;
s.End = obj.End; s.End = obj.End;
s.Depth = obj.Depth;
s.StrikeBrushIndex = queue.RequireResource(obj.StrikeBrush!); s.StrikeBrushIndex = queue.RequireResource(obj.StrikeBrush!);
s.StrikeSize = obj.StrikeSize; s.StrikeSize = obj.StrikeSize;
s.BorderKind = obj.BorderKind; s.BorderKind = obj.BorderKind;
@ -221,6 +224,7 @@ namespace Dashboard.Drawing
ref RectFS fs = ref MemoryMarshal.AsRef<RectFS>(param); ref RectFS fs = ref MemoryMarshal.AsRef<RectFS>(param);
fs.Start = obj.Start; fs.Start = obj.Start;
fs.End = obj.End; fs.End = obj.End;
fs.Depth = obj.Depth;
fs.FillBrushIndex = queue.RequireResource(obj.FillBrush!); fs.FillBrushIndex = queue.RequireResource(obj.FillBrush!);
fs.StrikeBrushIndex = queue.RequireResource(obj.StrikeBrush!); fs.StrikeBrushIndex = queue.RequireResource(obj.StrikeBrush!);
fs.StrikeSize = obj.StrikeSize; fs.StrikeSize = obj.StrikeSize;
@ -239,42 +243,46 @@ namespace Dashboard.Drawing
FillStrike = Fill | Strike, FillStrike = Fill | Strike,
} }
private record struct RectF(Vector3 Start, Vector3 End, int FillBrushIndex); private record struct RectF(Vector2 Start, Vector2 End, float Depth, int FillBrushIndex);
private record struct RectS(Vector3 Start, Vector3 End, int StrikeBrushIndex, float StrikeSize, BorderKind BorderKind); private record struct RectS(Vector2 Start, Vector2 End, float Depth, int StrikeBrushIndex, float StrikeSize, BorderKind BorderKind);
private record struct RectFS(Vector3 Start, Vector3 End, int FillBrushIndex, int StrikeBrushIndex, float StrikeSize, BorderKind BorderKind); private record struct RectFS(Vector2 Start, Vector2 End, float Depth, int FillBrushIndex, int StrikeBrushIndex, float StrikeSize, BorderKind BorderKind);
} }
public struct RectCommandArgs public struct RectCommandArgs
{ {
public Vector3 Start { get; private set; } public Vector2 Start { get; private set; }
public Vector3 End { get; private set; } public Vector2 End { get; private set; }
public BorderKind BorderKind { get; private set; } = BorderKind.Center; public float Depth { get; private set; }
public float StrikeSize { get; private set; } = 0f; public float StrikeSize { get; private set; } = 0f;
public BorderKind BorderKind { get; private set; } = BorderKind.Center;
public IBrush? FillBrush { get; private set; } = null; public IBrush? FillBrush { get; private set; } = null;
public IBrush? StrikeBrush { get; private set; } = null; public IBrush? StrikeBrush { get; private set; } = null;
public bool IsStruck => StrikeSize != 0; public bool IsStruck => StrikeSize != 0;
public RectCommandArgs(Vector3 start, Vector3 end, IBrush fillBrush) public RectCommandArgs(Vector2 start, Vector2 end, float depth, IBrush fillBrush)
{ {
Start = start; Start = start;
End = end; End = end;
Depth = depth;
FillBrush = fillBrush; FillBrush = fillBrush;
} }
public RectCommandArgs(Vector3 start, Vector3 end, IBrush strikeBrush, float strikeSize, BorderKind borderKind) public RectCommandArgs(Vector2 start, Vector2 end, float depth, IBrush strikeBrush, float strikeSize, BorderKind borderKind)
{ {
Start = start; Start = start;
End = end; End = end;
Depth = depth;
StrikeBrush = strikeBrush; StrikeBrush = strikeBrush;
StrikeSize = strikeSize; StrikeSize = strikeSize;
BorderKind = borderKind; BorderKind = borderKind;
} }
public RectCommandArgs(Vector3 start, Vector3 end, IBrush fillBrush, IBrush strikeBrush, float strikeSize, public RectCommandArgs(Vector2 start, Vector2 end, float depth, IBrush fillBrush, IBrush strikeBrush, float strikeSize,
BorderKind borderKind) BorderKind borderKind)
{ {
Start = start; Start = start;
End = end; End = end;
Depth = depth;
FillBrush = fillBrush; FillBrush = fillBrush;
StrikeBrush = strikeBrush; StrikeBrush = strikeBrush;
StrikeSize = strikeSize; StrikeSize = strikeSize;

@ -1,7 +1,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Drawing;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Runtime.InteropServices;
namespace Dashboard.Drawing namespace Dashboard.Drawing
{ {
@ -62,62 +64,62 @@ namespace Dashboard.Drawing
return queue.GetController(extension); return queue.GetController(extension);
} }
public static void Point(this DrawQueue queue, Vector3 position, float size, IBrush brush) public static void Point(this DrawQueue queue, Vector2 position, float depth, float size, IBrush brush)
{ {
Vector3 radius = new Vector3(size / 2f); Vector2 radius = new Vector2(0.5f * size);
Box3d bounds = new Box3d(position - radius, position + radius); Box2d bounds = new Box2d(position - radius, position + radius);
IDrawController controller = queue.GetController(DbBaseCommands.Instance); IDrawController controller = queue.GetController(DbBaseCommands.Instance);
controller.EnsureBounds(bounds); controller.EnsureBounds(bounds, depth);
controller.Write(DbBaseCommands.Instance.DrawPoint, new PointCommandArgs(position, size, brush)); controller.Write(DbBaseCommands.Instance.DrawPoint, new PointCommandArgs(position, depth, size, brush));
} }
public static void Line(this DrawQueue queue, Vector3 start, Vector3 end, float size, IBrush brush) public static void Line(this DrawQueue queue, Vector2 start, Vector2 end, float depth, float size, IBrush brush)
{ {
Vector3 radius = new Vector3(size / 2f); Vector2 radius = new Vector2(size / 2f);
Vector3 min = Vector3.Min(start, end) - radius; Vector2 min = Vector2.Min(start, end) - radius;
Vector3 max = Vector3.Max(start, end) + radius; Vector2 max = Vector2.Max(start, end) + radius;
Box3d bounds = new Box3d(min, max); Box2d bounds = new Box2d(min, max);
IDrawController controller = queue.GetController(DbBaseCommands.Instance); IDrawController controller = queue.GetController(DbBaseCommands.Instance);
controller.EnsureBounds(bounds); controller.EnsureBounds(bounds, depth);
controller.Write(DbBaseCommands.Instance.DrawLine, new LineCommandArgs(start, end, size, brush)); controller.Write(DbBaseCommands.Instance.DrawLine, new LineCommandArgs(start, end, depth, size, brush));
} }
public static void Rect(this DrawQueue queue, Vector3 start, Vector3 end, IBrush fillBrush) public static void Rect(this DrawQueue queue, Vector2 start, Vector2 end, float depth, IBrush fillBrush)
{ {
IDrawController controller = queue.GetController(DbBaseCommands.Instance); IDrawController controller = queue.GetController(DbBaseCommands.Instance);
Vector3 min = Vector3.Min(start, end); Vector2 min = Vector2.Min(start, end);
Vector3 max = Vector3.Max(start, end); Vector2 max = Vector2.Max(start, end);
controller.EnsureBounds(new Box3d(min, max)); controller.EnsureBounds(new Box2d(min, max), depth);
controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, fillBrush)); controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, depth, fillBrush));
} }
public static void Rect(this DrawQueue queue, Vector3 start, Vector3 end, IBrush strikeBrush, float strikeSize, public static void Rect(this DrawQueue queue, Vector2 start, Vector2 end, float depth, IBrush strikeBrush, float strikeSize,
BorderKind kind = BorderKind.Center) BorderKind kind = BorderKind.Center)
{ {
IDrawController controller = queue.GetController(DbBaseCommands.Instance); IDrawController controller = queue.GetController(DbBaseCommands.Instance);
Vector3 min = Vector3.Min(start, end); Vector2 min = Vector2.Min(start, end);
Vector3 max = Vector3.Max(start, end); Vector2 max = Vector2.Max(start, end);
controller.EnsureBounds(new Box3d(min, max)); controller.EnsureBounds(new Box2d(min, max), depth);
controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, strikeBrush, strikeSize, kind)); controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, depth, strikeBrush, strikeSize, kind));
} }
public static void Rect(this DrawQueue queue, Vector3 start, Vector3 end, IBrush fillBrush, IBrush strikeBrush, public static void Rect(this DrawQueue queue, Vector2 start, Vector2 end, float depth, IBrush fillBrush, IBrush strikeBrush,
float strikeSize, BorderKind kind = BorderKind.Center) float strikeSize, BorderKind kind = BorderKind.Center)
{ {
IDrawController controller = queue.GetController(DbBaseCommands.Instance); IDrawController controller = queue.GetController(DbBaseCommands.Instance);
Vector3 min = Vector3.Min(start, end); Vector2 min = Vector2.Min(start, end);
Vector3 max = Vector3.Max(start, end); Vector2 max = Vector2.Max(start, end);
controller.EnsureBounds(new Box3d(min, max)); controller.EnsureBounds(new Box2d(min, max), depth);
controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, fillBrush, strikeBrush, strikeSize, kind)); controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, depth, fillBrush, strikeBrush, strikeSize, kind));
} }
public static void Text(this DrawQueue queue, Vector3 position, IBrush brush, string text, IFont font, public static void Text(this DrawQueue queue, Vector3 position, IBrush brush, string text, IFont font,
Anchor anchor = Anchor.Left) Anchor anchor = Anchor.Left)
{ {
IDrawController controller = queue.GetController(DbBaseCommands.Instance); IDrawController controller = queue.GetController(DbBaseCommands.Instance);
controller.EnsureBounds(new Box3d(position, position)); controller.EnsureBounds(new Box2d(position.X, position.Y, position.X, position.Y), position.Z);
controller.Write(TextExtension.Instance.TextCommand, new TextCommandArgs(font, brush, anchor, position, text)); controller.Write(TextExtension.Instance.TextCommand, new TextCommandArgs(font, brush, anchor, position, text));
} }
@ -125,7 +127,7 @@ namespace Dashboard.Drawing
float borderRadius, string text, IFont font, Anchor anchor = Anchor.Left, BorderKind borderKind = BorderKind.Outset) float borderRadius, string text, IFont font, Anchor anchor = Anchor.Left, BorderKind borderKind = BorderKind.Outset)
{ {
IDrawController controller = queue.GetController(DbBaseCommands.Instance); IDrawController controller = queue.GetController(DbBaseCommands.Instance);
controller.EnsureBounds(new Box3d(position, position)); controller.EnsureBounds(new Box2d(position.X, position.Y, position.X, position.Y), position.Z);
controller.Write(TextExtension.Instance.TextCommand, new TextCommandArgs(font, textBrush, anchor, position, text) controller.Write(TextExtension.Instance.TextCommand, new TextCommandArgs(font, textBrush, anchor, position, text)
{ {
BorderBrush = borderBrush, BorderBrush = borderBrush,

@ -178,9 +178,9 @@ namespace Dashboard.Drawing
private class DrawController(DrawQueue Queue) : IDrawController private class DrawController(DrawQueue Queue) : IDrawController
{ {
public void EnsureBounds(Box3d bounds) public void EnsureBounds(Box2d bounds, float depth)
{ {
Queue.Bounds = Box3d.Union(Queue.Bounds, bounds); Queue.Bounds = Box3d.Union(Queue.Bounds, bounds, depth);
} }
public void Write(IDrawCommand command) public void Write(IDrawCommand command)
@ -239,11 +239,12 @@ namespace Dashboard.Drawing
public bool MoveNext() public bool MoveNext()
{ {
if (_index == -1)
_index = 0;
if (_index >= _length) if (_index >= _length)
return false; return false;
if (_index == -1)
_index = 0;
_index += FromVlq(_stream[_index .. (_index + 5)], out int command); _index += FromVlq(_stream[_index .. (_index + 5)], out int command);
_current = _queue.Command[command]; _current = _queue.Command[command];
@ -336,7 +337,7 @@ namespace Dashboard.Drawing
/// Ensures that the canvas is at least a certain size. /// Ensures that the canvas is at least a certain size.
/// </summary> /// </summary>
/// <param name="bounds">The bounding box.</param> /// <param name="bounds">The bounding box.</param>
void EnsureBounds(Box3d bounds); void EnsureBounds(Box2d bounds, float depth);
/// <summary> /// <summary>
/// Write into the command stream. /// Write into the command stream.