2024-12-13 19:57:07 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
|
|
|
|
|
namespace Dashboard.Drawing
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interface for all drawing extensions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public interface IDrawExtension
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Name of this extension.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
|
|
|
|
|
public IReadOnlyList<IDrawExtension> Requires { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The list of commands this extension defines, if any.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyList<IDrawCommand> Commands { get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A simple draw extension.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DrawExtension : IDrawExtension
|
|
|
|
|
{
|
|
|
|
|
private readonly List<IDrawCommand> _drawCommands = new List<IDrawCommand>();
|
|
|
|
|
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
|
|
|
|
|
public IReadOnlyList<IDrawCommand> Commands { get; }
|
|
|
|
|
|
|
|
|
|
public IReadOnlyList<IDrawExtension> Requires { get; }
|
|
|
|
|
|
|
|
|
|
public DrawExtension(string name, IEnumerable<IDrawExtension>? requires = null)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
Commands = _drawCommands.AsReadOnly();
|
|
|
|
|
Requires = (requires ?? Enumerable.Empty<IDrawExtension>()).ToImmutableList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void AddCommand(IDrawCommand command)
|
|
|
|
|
{
|
|
|
|
|
_drawCommands.Add(command);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class DrawExtensionClass
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the draw controller for the given queue.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="extension">The extension instance.</param>
|
|
|
|
|
/// <param name="queue">The draw queue.</param>
|
|
|
|
|
/// <returns>The draw controller for this queue.</returns>
|
|
|
|
|
public static IDrawController GetController(this IDrawExtension extension, DrawQueue queue)
|
|
|
|
|
{
|
|
|
|
|
return queue.GetController(extension);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Point(this DrawQueue queue, Vector3 position, float size, IBrush brush)
|
|
|
|
|
{
|
2024-12-14 14:27:40 +01:00
|
|
|
|
Vector3 radius = new Vector3(size / 2f);
|
|
|
|
|
Box3d bounds = new Box3d(position - radius, position + radius);
|
|
|
|
|
|
2024-12-14 17:46:56 +01:00
|
|
|
|
IDrawController controller = queue.GetController(DbBaseCommands.Instance);
|
2024-12-14 14:27:40 +01:00
|
|
|
|
controller.EnsureBounds(bounds);
|
2024-12-13 19:57:07 +01:00
|
|
|
|
controller.Write(DbBaseCommands.Instance.DrawPoint, new PointCommandArgs(position, size, brush));
|
|
|
|
|
}
|
2024-12-14 17:46:56 +01:00
|
|
|
|
|
|
|
|
|
public static void Line(this DrawQueue queue, Vector3 start, Vector3 end, float size, IBrush brush)
|
|
|
|
|
{
|
|
|
|
|
Vector3 radius = new Vector3(size / 2f);
|
|
|
|
|
Vector3 min = Vector3.Min(start, end) - radius;
|
|
|
|
|
Vector3 max = Vector3.Max(start, end) + radius;
|
|
|
|
|
Box3d bounds = new Box3d(min, max);
|
|
|
|
|
|
|
|
|
|
IDrawController controller = queue.GetController(DbBaseCommands.Instance);
|
|
|
|
|
controller.EnsureBounds(bounds);
|
|
|
|
|
controller.Write(DbBaseCommands.Instance.DrawLine, new LineCommandArgs(start, end, size, brush));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Rect(this DrawQueue queue, Vector3 start, Vector3 end, IBrush fillBrush)
|
|
|
|
|
{
|
|
|
|
|
IDrawController controller = queue.GetController(DbBaseCommands.Instance);
|
|
|
|
|
Vector3 min = Vector3.Min(start, end);
|
|
|
|
|
Vector3 max = Vector3.Max(start, end);
|
|
|
|
|
controller.EnsureBounds(new Box3d(min, max));
|
|
|
|
|
controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, fillBrush));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Rect(this DrawQueue queue, Vector3 start, Vector3 end, IBrush strikeBrush, float strikeSize,
|
2024-12-24 21:20:43 +01:00
|
|
|
|
BorderKind kind = BorderKind.Center)
|
2024-12-14 17:46:56 +01:00
|
|
|
|
{
|
|
|
|
|
IDrawController controller = queue.GetController(DbBaseCommands.Instance);
|
|
|
|
|
Vector3 min = Vector3.Min(start, end);
|
|
|
|
|
Vector3 max = Vector3.Max(start, end);
|
|
|
|
|
controller.EnsureBounds(new Box3d(min, max));
|
|
|
|
|
controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, strikeBrush, strikeSize, kind));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Rect(this DrawQueue queue, Vector3 start, Vector3 end, IBrush fillBrush, IBrush strikeBrush,
|
2024-12-24 21:20:43 +01:00
|
|
|
|
float strikeSize, BorderKind kind = BorderKind.Center)
|
2024-12-14 17:46:56 +01:00
|
|
|
|
{
|
|
|
|
|
IDrawController controller = queue.GetController(DbBaseCommands.Instance);
|
|
|
|
|
Vector3 min = Vector3.Min(start, end);
|
|
|
|
|
Vector3 max = Vector3.Max(start, end);
|
|
|
|
|
controller.EnsureBounds(new Box3d(min, max));
|
|
|
|
|
controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, fillBrush, strikeBrush, strikeSize, kind));
|
|
|
|
|
}
|
2024-12-24 21:20:43 +01:00
|
|
|
|
|
|
|
|
|
public static void Text(this DrawQueue queue, Vector3 position, IBrush brush, string text, IFont font,
|
|
|
|
|
Anchor anchor = Anchor.Left)
|
|
|
|
|
{
|
|
|
|
|
IDrawController controller = queue.GetController(DbBaseCommands.Instance);
|
|
|
|
|
controller.EnsureBounds(new Box3d(position, position));
|
|
|
|
|
controller.Write(TextExtension.Instance.TextCommand, new TextCommandArgs(font, brush, anchor, position, text));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Text(this DrawQueue queue, Vector3 position, IBrush textBrush, IBrush borderBrush,
|
|
|
|
|
float borderRadius, string text, IFont font, Anchor anchor = Anchor.Left, BorderKind borderKind = BorderKind.Outset)
|
|
|
|
|
{
|
|
|
|
|
IDrawController controller = queue.GetController(DbBaseCommands.Instance);
|
|
|
|
|
controller.EnsureBounds(new Box3d(position, position));
|
|
|
|
|
controller.Write(TextExtension.Instance.TextCommand, new TextCommandArgs(font, textBrush, anchor, position, text)
|
|
|
|
|
{
|
|
|
|
|
BorderBrush = borderBrush,
|
|
|
|
|
BorderRadius = borderRadius,
|
|
|
|
|
BorderKind = borderKind,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-12-13 19:57:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|