using System.Collections.Generic; using System.Collections.Immutable; using System.Drawing; using System.Linq; using System.Numerics; using System.Runtime.InteropServices; namespace Dashboard.Drawing { /// /// Interface for all drawing extensions. /// public interface IDrawExtension { /// /// Name of this extension. /// public string Name { get; } public IReadOnlyList Requires { get; } /// /// The list of commands this extension defines, if any. /// public IReadOnlyList Commands { get; } } /// /// A simple draw extension. /// public class DrawExtension : IDrawExtension { private readonly List _drawCommands = new List(); public string Name { get; } public IReadOnlyList Commands { get; } public IReadOnlyList Requires { get; } public DrawExtension(string name, IEnumerable? requires = null) { Name = name; Commands = _drawCommands.AsReadOnly(); Requires = (requires ?? Enumerable.Empty()).ToImmutableList(); } protected void AddCommand(IDrawCommand command) { _drawCommands.Add(command); } } public static class DrawExtensionClass { /// /// Get the draw controller for the given queue. /// /// The extension instance. /// The draw queue. /// The draw controller for this queue. public static IDrawController GetController(this IDrawExtension extension, DrawQueue queue) { return queue.GetController(extension); } public static void Point(this DrawQueue queue, Vector2 position, float depth, float size, IBrush brush) { Vector2 radius = new Vector2(0.5f * size); Box2d bounds = new Box2d(position - radius, position + radius); IDrawController controller = queue.GetController(DbBaseCommands.Instance); controller.EnsureBounds(bounds, depth); controller.Write(DbBaseCommands.Instance.DrawPoint, new PointCommandArgs(position, depth, size, brush)); } public static void Line(this DrawQueue queue, Vector2 start, Vector2 end, float depth, float size, IBrush brush) { Vector2 radius = new Vector2(size / 2f); Vector2 min = Vector2.Min(start, end) - radius; Vector2 max = Vector2.Max(start, end) + radius; Box2d bounds = new Box2d(min, max); IDrawController controller = queue.GetController(DbBaseCommands.Instance); controller.EnsureBounds(bounds, depth); controller.Write(DbBaseCommands.Instance.DrawLine, new LineCommandArgs(start, end, depth, size, brush)); } public static void Rect(this DrawQueue queue, Vector2 start, Vector2 end, float depth, IBrush fillBrush) { IDrawController controller = queue.GetController(DbBaseCommands.Instance); Vector2 min = Vector2.Min(start, end); Vector2 max = Vector2.Max(start, end); controller.EnsureBounds(new Box2d(min, max), depth); controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, depth, fillBrush)); } public static void Rect(this DrawQueue queue, Vector2 start, Vector2 end, float depth, IBrush strikeBrush, float strikeSize, BorderKind kind = BorderKind.Center) { IDrawController controller = queue.GetController(DbBaseCommands.Instance); Vector2 min = Vector2.Min(start, end); Vector2 max = Vector2.Max(start, end); controller.EnsureBounds(new Box2d(min, max), depth); controller.Write(DbBaseCommands.Instance.DrawRectF, new RectCommandArgs(start, end, depth, strikeBrush, strikeSize, kind)); } public static void Rect(this DrawQueue queue, Vector2 start, Vector2 end, float depth, IBrush fillBrush, IBrush strikeBrush, float strikeSize, BorderKind kind = BorderKind.Center) { IDrawController controller = queue.GetController(DbBaseCommands.Instance); Vector2 min = Vector2.Min(start, end); Vector2 max = Vector2.Max(start, end); controller.EnsureBounds(new Box2d(min, max), depth); 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, Anchor anchor = Anchor.Left) { IDrawController controller = queue.GetController(DbBaseCommands.Instance); 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)); } 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 Box2d(position.X, position.Y, position.X, position.Y), position.Z); controller.Write(TextExtension.Instance.TextCommand, new TextCommandArgs(font, textBrush, anchor, position, text) { BorderBrush = borderBrush, BorderRadius = borderRadius, BorderKind = borderKind, }); } } }