using System.Collections.Generic;
using Annulus;
using Azimuth;
using Blacklight.Core;
using Blacklight.Core.Drawables;
using Prism;
namespace Glass.Blacklight
{
///
/// Draws the testbed's materials as Blacklight vector art, with no sprite sheet behind any of it.
///
public class VectorShapes
{
// A shape's colors are baked into its drawable, so each distinct look is one cache entry. The testbed's few
// fixed sizes keep the cache small.
private readonly record struct RoundedKey(
double Width, double Height, double Radius, double OutlineWidth, int Fill, int Outline);
private readonly record struct TickKey(double Side, int Color);
private readonly record struct ChevronKey(double Width, double Height, int Color, bool PointsUp);
private readonly Dictionary chevrons = new Dictionary();
private readonly Dictionary roundedRects =
new Dictionary();
private readonly Dictionary ticks = new Dictionary();
private static int Pack(Color color)
{
return (color.Alpha << 24) | (color.Red << 16) | (color.Green << 8) | color.Blue;
}
// Clipped art would need cutting along a curve, so only whole shapes are drawn.
private static bool IsWhole(Rect rect, in DrawState drawState)
{
if (!(drawState.VisibleWithin(rect) is OrientedBox visible))
{
return false;
}
return visible.TryGetRect(out Rect upright)
&& upright.Left == rect.Left && upright.Top == rect.Top
&& upright.Width == rect.Width && upright.Height == rect.Height;
}
// Each corner is one bowing piece whose control point is the square corner. The sides are pieces whose control
// point lies on them.
private static ClosedQuadraticBezierCurve BuildRoundedRect(in RoundedKey key, Color fill, Color outline)
{
double w = key.Width;
double h = key.Height;
double r = key.Radius;
var curve = new ClosedQuadraticBezierCurve(new[]
{
new Vector(r, 0), new Vector(w / 2, 0),
new Vector(w - r, 0), new Vector(w, 0),
new Vector(w, r), new Vector(w, h / 2),
new Vector(w, h - r), new Vector(w, h),
new Vector(w - r, h), new Vector(w / 2, h),
new Vector(r, h), new Vector(0, h),
new Vector(0, h - r), new Vector(0, h / 2),
new Vector(0, r), new Vector(0, 0),
});
curve.InsideColor = fill;
curve.ShellColor = outline;
curve.ShellModelSpaceThickness = key.OutlineWidth;
return curve;
}
private void DrawRoundedRect(Cel context, Rect rect, Scalar radius, Scalar outlineWidth, Color fill,
Color outline, in DrawState drawState)
{
if (rect.Width == 0 || rect.Height == 0)
{
return;
}
if (!IsWhole(rect, in drawState))
{
SolidRect.Draw(context, fill, rect, in drawState);
return;
}
// Two opposite corners would otherwise meet in a point, which the curve can't build.
double limit = 0.45 * System.Math.Min(rect.Width, rect.Height);
var key = new RoundedKey(
rect.Width, rect.Height, System.Math.Min(radius, limit), outlineWidth, Pack(fill), Pack(outline));
if (!roundedRects.TryGetValue(key, out ClosedQuadraticBezierCurve curve))
{
curve = BuildRoundedRect(in key, fill, outline);
roundedRects.Add(key, curve);
}
context.AddEntity(curve, drawState.ModelToWorld * AffineMatrix.BuildFromTranslation(rect.TopLeft));
}
private void RenderRoundedRect(Renderer renderer, Cel context, RoundedRectMaterial material, Rect rect,
in DrawState drawState)
{
DrawRoundedRect(context, rect, material.CornerRadius, material.OutlineWidth, material.FillColor,
material.OutlineColor, in drawState);
}
private static void DrawCircle(Cel context, Vector center, Scalar diameter, Color fill, Color outline,
Scalar outlineWidth, in DrawState drawState)
{
var ellipse = new Ellipse
{
InsideColor = fill,
ShellColor = outline,
ShellPixelThickness = outlineWidth,
};
context.AddEntity(ellipse,
drawState.ModelToWorld * Ellipse.ModelToWorld(new Vector(diameter, diameter), position: center));
}
private void RenderRadio(Renderer renderer, Cel context, RadioIndicatorMaterial material, Rect rect,
in DrawState drawState)
{
if (!IsWhole(rect, in drawState))
{
return;
}
Vector center = rect.TopLeft + new Vector(rect.Width / 2, rect.Height / 2);
Scalar diameter = Scalar.Min(rect.Width, rect.Height);
DrawCircle(context, center, diameter, material.FillColor, material.OutlineColor, material.OutlineWidth,
in drawState);
if (material.IsChecked)
{
DrawCircle(context, center, diameter * 0.5, material.DotColor, material.DotColor, 0, in drawState);
}
}
private Polyline TickFor(double side, Color color)
{
var key = new TickKey(side, Pack(color));
if (!ticks.TryGetValue(key, out Polyline tick))
{
tick = new Polyline(new[]
{
new Vector(0.24 * side, 0.54 * side),
new Vector(0.43 * side, 0.72 * side),
new Vector(0.78 * side, 0.30 * side),
})
{
InsideColor = color,
InsidePixelThickness = 0.09 * side,
};
ticks.Add(key, tick);
}
return tick;
}
private void RenderCheck(Renderer renderer, Cel context, CheckIndicatorMaterial material, Rect rect,
in DrawState drawState)
{
if (!IsWhole(rect, in drawState))
{
return;
}
DrawRoundedRect(context, rect, radius: 3, material.OutlineWidth, material.FillColor,
material.OutlineColor, in drawState);
if (material.IsChecked)
{
context.AddEntity(TickFor(rect.Width, material.TickColor),
drawState.ModelToWorld * AffineMatrix.BuildFromTranslation(rect.TopLeft));
}
}
private Polyline ChevronFor(double width, double height, Color color, bool pointsUp)
{
var key = new ChevronKey(width, height, Pack(color), pointsUp);
if (!chevrons.TryGetValue(key, out Polyline chevron))
{
double tipY = pointsUp ? 0.35 * height : 0.65 * height;
double footY = pointsUp ? 0.65 * height : 0.35 * height;
chevron = new Polyline(new[]
{
new Vector(0.3 * width, footY),
new Vector(0.5 * width, tipY),
new Vector(0.7 * width, footY),
})
{
InsideColor = color,
InsidePixelThickness = 0.16 * height,
};
chevrons.Add(key, chevron);
}
return chevron;
}
private void RenderArrow(Renderer renderer, Cel context, ArrowIndicatorMaterial material, Rect rect,
in DrawState drawState)
{
if (!IsWhole(rect, in drawState))
{
return;
}
DrawRoundedRect(context, rect, radius: 3, outlineWidth: 1, material.FillColor, material.OutlineColor,
in drawState);
context.AddEntity(ChevronFor(rect.Width, rect.Height, material.ArrowColor, material.PointsUp),
drawState.ModelToWorld * AffineMatrix.BuildFromTranslation(rect.TopLeft));
}
///
/// Teaches to draw the testbed's vector materials.
///
public void Register(Renderer renderer)
{
renderer.RegisterRenderCallback(RenderArrow);
renderer.RegisterRenderCallback(RenderRoundedRect);
renderer.RegisterRenderCallback(RenderRadio);
renderer.RegisterRenderCallback(RenderCheck);
}
}
}