using Amphora;
using Azimuth;
using Blacklight.Core;
using Blacklight.Core.Drawables;
using System;
using Prism;
using OrientedBox = Annulus.OrientedBox;
using Rect = Annulus.Rect;
namespace Glass.Blacklight
{
///
/// A drawing made of a few primitives, placed in a box that runs from -1 to 1 up and down and from minus to plus
/// across, with y pointing up. It draws into whatever rect it is given at one scale on both
/// axes, so the picture is never stretched. Line widths are in pixels, so they hold when the picture grows.
///
public class Picture
{
///
/// A circle drawn as an outline. An Ellipse is always the unit circle, so the radius is a transform.
///
public struct Ring
{
public Ellipse Ellipse;
public Vector Center;
public Scalar Radius;
}
public static readonly Color Clear = Colors.Transparent;
private const int CurvePieces = 12;
public readonly DynamicArray Lines = new DynamicArray();
public readonly DynamicArray Rings = new DynamicArray();
public readonly DynamicArray Polygons = new DynamicArray();
///
/// The box's half width, relative to its half height.
///
public readonly double Aspect;
private double halfWidthInPixels = 1;
private double halfHeightInPixels = 1;
public Picture(double aspect)
{
if (aspect <= 0)
{
throw new ArgumentOutOfRangeException(nameof(aspect), aspect, "A picture needs a positive width.");
}
Aspect = aspect;
}
///
/// Makes a picture that lands exactly when drawn into a rect of this many pixels, if it is placed with Pixel
/// and PixelRadius.
///
public static Picture InPixels(double width, double height)
{
if (!(width > 0) || !(height > 0))
{
throw new ArgumentOutOfRangeException(nameof(width), width, "A picture needs a positive size.");
}
return new Picture(width / height) { halfWidthInPixels = width / 2, halfHeightInPixels = height / 2 };
}
///
/// Gets the point at a pixel position, measured from the top left with y pointing down.
///
public Vector Pixel(double x, double y)
{
return new Vector(
(x - halfWidthInPixels) / halfWidthInPixels,
(halfHeightInPixels - y) / halfHeightInPixels);
}
///
/// Gets a radius of this many pixels, as the circle and arc methods take it.
///
public double PixelRadius(double pixels)
{
return pixels / halfWidthInPixels;
}
///
/// Gets how many entities the picture puts in a Cel.
///
public int PartCount => Lines.Count + Rings.Count + Polygons.Count;
// Stored with y pointing down, the way Glass lays out, so the picture never needs a mirrored transform.
private Vector Place(Vector point)
{
return new Vector(point.X * Aspect, -point.Y);
}
public Picture AddLine(Vector start, Vector end, Color color, double pixelWidth, bool roundCaps = false)
{
LineCapStyle cap = roundCaps ? LineCapStyle.Round : LineCapStyle.None;
Lines.Add(new LineSegment
{
Start = Place(start),
End = Place(end),
PixelWidth = pixelWidth,
InsideColor = color,
StartCap = cap,
EndCap = cap,
});
return this;
}
///
/// Adds a circle outline. Its radius is a portion of the box's half width, as the original's was.
///
public Picture AddCircle(Vector center, double radius, Color color, double pixelWidth)
{
Rings.Add(new Ring
{
Ellipse = new Ellipse { ShellColor = color, InsideColor = Clear, ShellPixelThickness = pixelWidth },
Center = Place(center),
Radius = radius * Aspect,
});
return this;
}
///
/// Adds a filled circle with no outline. Additiveness 1 adds the fill's light to what is beneath it, as a glow
/// does, where 0 covers it.
///
public Picture AddDisc(Vector center, double radius, Color fill, double additiveness = 0)
{
Rings.Add(new Ring
{
Ellipse = new Ellipse { ShellColor = Clear, InsideColor = fill, Additiveness = additiveness },
Center = Place(center),
Radius = radius * Aspect,
});
return this;
}
///
/// Adds a filled polygon.
///
public Picture AddPolygon(Vector[] corners, Color fill)
{
var placed = new Vector[corners.Length];
for (int i = 0; i < corners.Length; ++i)
{
placed[i] = Place(corners[i]);
}
Polygons.Add(new Polygon(placed) { InsideColor = fill });
return this;
}
///
/// Adds the outline of a polygon as lines whose round caps fill in the corners. A polygon's own shell has no
/// width in pixels.
///
public Picture AddOutline(Vector[] corners, Color color, double pixelWidth)
{
for (int i = 0; i < corners.Length; ++i)
{
AddLine(corners[i], corners[(i + 1) % corners.Length], color, pixelWidth, roundCaps: true);
}
return this;
}
private static Vector[] BarCorners(Vector start, Vector end, double halfWidth)
{
Vector along = (end - start).NormalizedCopy();
Vector across = new Vector(-along.Y, along.X) * halfWidth;
return new[] { start + across, end + across, end - across, start - across };
}
///
/// Adds a straight bar of the given half width.
///
public Picture AddBar(Vector start, Vector end, double halfWidth, Color fill)
{
return AddPolygon(BarCorners(start, end, halfWidth), fill);
}
///
/// Adds the outline of a straight bar of the given half width.
///
public Picture AddBarOutline(Vector start, Vector end, double halfWidth, Color color, double pixelWidth)
{
return AddOutline(BarCorners(start, end, halfWidth), color, pixelWidth);
}
///
/// Adds a cubic Bezier curve, which has no drawable of its own, as a run of short lines.
///
public Picture AddCurve(Vector start, Vector control1, Vector control2, Vector end, Color color,
double pixelWidth)
{
Vector previous = start;
for (int i = 1; i <= CurvePieces; ++i)
{
double t = (double)i / CurvePieces;
double u = 1 - t;
Vector next = start * (u * u * u) + control1 * (3 * u * u * t) + control2 * (3 * u * t * t)
+ end * (t * t * t);
AddLine(previous, next, color, pixelWidth);
previous = next;
}
return this;
}
// Pictures are stored with y pointing down, so a turn that looks clockwise is counterclockwise there.
private Ring Sector(Vector center, double radius, Vector from, Vector to, Ellipse ellipse)
{
ellipse.SetSector(new Vector(from.X, -from.Y), new Vector(to.X, -to.Y));
return new Ring { Ellipse = ellipse, Center = Place(center), Radius = radius * Aspect };
}
///
/// Adds a filled slice of a circle that runs from the center out to an arc, turning as 's
/// does.
///
public Picture AddPie(Vector center, double radius, Vector from, Vector to, Color fill)
{
Rings.Add(Sector(center, radius, from, to, new Ellipse { ShellColor = Clear, InsideColor = fill }));
return this;
}
///
/// Adds an arc of a circle, turning clockwise from to , with y up
/// as the picture's points have it. Its stroke runs just inside the radius, as a circle's does.
///
public Picture AddArc(Vector center, double radius, Vector from, Vector to, Color color, double pixelWidth)
{
Rings.Add(Sector(center, radius, from, to,
new Ellipse { ShellColor = color, InsideColor = Clear, ShellPixelThickness = pixelWidth }));
return this;
}
///
/// Draws the picture into the Cel, centered in and as big as fits.
///
public void Draw(Cel cel, Rect rect, in DrawState drawState)
{
Draw(cel, rect, in drawState, AffineMatrix.Identity);
}
///
/// Draws the picture into the Cel, centered in and as big as fits, then moved by
/// within the space the rect is in.
///
public void Draw(Cel cel, Rect rect, in DrawState drawState, in AffineMatrix placement)
{
Scalar scale = Scalar.Min(rect.Width / (2 * Aspect), rect.Height / 2);
AffineMatrix pictureToControl = placement
* AffineMatrix.BuildFromTranslation(rect.TopLeft + new Vector(rect.Width / 2, rect.Height / 2))
* AffineMatrix.BuildFromScale(scale);
for (int i = 0; i < Polygons.Count; ++i)
{
cel.AddEntity(Polygons[i], in drawState, in pictureToControl);
}
for (int i = 0; i < Rings.Count; ++i)
{
Ring ring = Rings[i];
cel.AddEntity(ring.Ellipse, in drawState,
pictureToControl * AffineMatrix.BuildFromTranslation(ring.Center)
* AffineMatrix.BuildFromScale(ring.Radius));
}
for (int i = 0; i < Lines.Count; ++i)
{
cel.AddEntity(Lines[i], in drawState, in pictureToControl);
}
}
}
}