using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rune = System.Rune;
namespace Terminal.Gui {
///
/// Defines the style of lines for a .
///
public enum LineStyle {
///
/// No border is drawn.
///
None,
///
/// The border is drawn using single-width line glyphs.
///
Single,
///
/// The border is drawn using double-width line glyphs.
///
Double,
///
/// The border is drawn using single-width line glyphs with rounded corners.
///
Rounded,
// TODO: Support Ruler
/////
///// The border is drawn as a diagnostic ruler ("|123456789...").
/////
//Ruler
}
///
/// Facilitates box drawing and line intersection detection
/// and rendering. Does not support diagonal lines.
///
public class LineCanvas {
private List _lines = new List ();
Dictionary runeResolvers = new Dictionary {
{IntersectionRuneType.ULCorner,new ULIntersectionRuneResolver()},
{IntersectionRuneType.URCorner,new URIntersectionRuneResolver()},
{IntersectionRuneType.LLCorner,new LLIntersectionRuneResolver()},
{IntersectionRuneType.LRCorner,new LRIntersectionRuneResolver()},
{IntersectionRuneType.TopTee,new TopTeeIntersectionRuneResolver()},
{IntersectionRuneType.LeftTee,new LeftTeeIntersectionRuneResolver()},
{IntersectionRuneType.RightTee,new RightTeeIntersectionRuneResolver()},
{IntersectionRuneType.BottomTee,new BottomTeeIntersectionRuneResolver()},
{IntersectionRuneType.Crosshair,new CrosshairIntersectionRuneResolver()},
// TODO: Add other resolvers
};
///
///
/// Adds a new long line to the canvas starting at .
///
///
/// Use positive for the line to extend Right and negative for Left
/// when is .
///
///
/// Use positive for the line to extend Down and negative for Up
/// when is .
///
///
/// Starting point.
/// The length of line. 0 for an intersection (cross or T). Positive for Down/Right. Negative for Up/Left.
/// The direction of the line.
/// The style of line to use
///
public void AddLine (Point start, int length, Orientation orientation, LineStyle style, Attribute? attribute = default)
{
_cachedBounds = Rect.Empty;
_lines.Add (new StraightLine (start, length, orientation, style, attribute));
}
private void AddLine (StraightLine line)
{
_cachedBounds = Rect.Empty;
_lines.Add (line);
}
///
/// Clears all lines from the LineCanvas.
///
public void Clear ()
{
_cachedBounds = Rect.Empty;
_lines.Clear ();
}
private Rect _cachedBounds;
///
/// Gets the rectangle that describes the bounds of the canvas. Location is the coordinates of the
/// line that is furthest left/top and Size is defined by the line that extends the furthest
/// right/bottom.
///
public Rect Bounds {
get {
if (_cachedBounds.IsEmpty) {
if (_lines.Count == 0) {
return _cachedBounds;
}
Rect bounds = _lines [0].Bounds;
for (var i = 1; i < _lines.Count; i++) {
var line = _lines [i];
var lineBounds = line.Bounds;
bounds = Rect.Union (bounds, lineBounds);
}
if (bounds.Width == 0) {
bounds.Width = 1;
}
if (bounds.Height == 0) {
bounds.Height = 1;
}
_cachedBounds = new Rect (bounds.X, bounds.Y, bounds.Width, bounds.Height);
}
return _cachedBounds;
}
}
// TODO: Unless there's an obvious use case for this API we should delete it in favor of the
// simpler version that doensn't take an area.
///
/// Evaluates the lines that have been added to the canvas and returns a map containing
/// the glyphs and their locations. The glyphs are the characters that should be rendered
/// so that all lines connect up with the appropriate intersection symbols.
///
/// A rectangle to constrain the search by.
/// A map of the points within the canvas that intersect with .
public Dictionary GetMap (Rect inArea)
{
var map = new Dictionary ();
// walk through each pixel of the bitmap
for (int y = inArea.Y; y < inArea.Y + inArea.Height; y++) {
for (int x = inArea.X; x < inArea.X + inArea.Width; x++) {
var intersects = _lines
.Select (l => l.Intersects (x, y))
.Where (i => i != null)
.ToArray ();
var rune = GetRuneForIntersects (Application.Driver, intersects);
if (rune != null) {
map.Add (new Point (x, y), rune.Value);
}
}
}
return map;
}
///
/// Evaluates the lines that have been added to the canvas and returns a map containing
/// the glyphs and their locations. The glyphs are the characters that should be rendered
/// so that all lines connect up with the appropriate intersection symbols.
///
/// A map of all the points within the canvas.
public Dictionary GetCellMap ()
{
var map = new Dictionary ();
// walk through each pixel of the bitmap
for (int y = Bounds.Y; y < Bounds.Y + Bounds.Height; y++) {
for (int x = Bounds.X; x < Bounds.X + Bounds.Width; x++) {
var intersects = _lines
.Select (l => l.Intersects (x, y))
.Where (i => i != null)
.ToArray ();
var cell = GetCellForIntersects (Application.Driver, intersects);
if (cell != null) {
map.Add (new Point (x, y), cell);
}
}
}
return map;
}
///
/// Evaluates the lines that have been added to the canvas and returns a map containing
/// the glyphs and their locations. The glyphs are the characters that should be rendered
/// so that all lines connect up with the appropriate intersection symbols.
///
/// A map of all the points within the canvas.
public Dictionary GetMap () => GetMap (Bounds);
///
/// Returns the contents of the line canvas rendered to a string. The string
/// will include all columns and rows, even if has negative coordinates.
/// For example, if the canvas contains a single line that starts at (-1,-1) with a length of 2, the
/// rendered string will have a length of 2.
///
/// The canvas rendered to a string.
public override string ToString ()
{
if (Bounds.IsEmpty) {
return string.Empty;
}
// Generate the rune map for the entire canvas
var runeMap = GetMap ();
// Create the rune canvas
Rune [,] canvas = new Rune [Bounds.Height, Bounds.Width];
// Copy the rune map to the canvas, adjusting for any negative coordinates
foreach (var kvp in runeMap) {
int x = kvp.Key.X - Bounds.X;
int y = kvp.Key.Y - Bounds.Y;
canvas [y, x] = kvp.Value;
}
// Convert the canvas to a string
StringBuilder sb = new StringBuilder ();
for (int y = 0; y < canvas.GetLength (0); y++) {
for (int x = 0; x < canvas.GetLength (1); x++) {
Rune r = canvas [y, x];
sb.Append (r.Value == 0 ? ' ' : r.ToString ());
}
if (y < canvas.GetLength (0) - 1) {
sb.AppendLine ();
}
}
return sb.ToString ();
}
private abstract class IntersectionRuneResolver {
readonly Rune round;
readonly Rune doubleH;
readonly Rune doubleV;
readonly Rune doubleBoth;
readonly Rune normal;
public IntersectionRuneResolver (Rune round, Rune doubleH, Rune doubleV, Rune doubleBoth, Rune normal)
{
this.round = round;
this.doubleH = doubleH;
this.doubleV = doubleV;
this.doubleBoth = doubleBoth;
this.normal = normal;
}
public Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
{
var useRounded = intersects.Any (i => i.Line.Style == LineStyle.Rounded && i.Line.Length != 0);
bool doubleHorizontal = intersects.Any (l => l.Line.Orientation == Orientation.Horizontal && l.Line.Style == LineStyle.Double);
bool doubleVertical = intersects.Any (l => l.Line.Orientation == Orientation.Vertical && l.Line.Style == LineStyle.Double);
if (doubleHorizontal) {
return doubleVertical ? doubleBoth : doubleH;
}
if (doubleVertical) {
return doubleV;
}
return useRounded ? round : normal;
}
}
private class ULIntersectionRuneResolver : IntersectionRuneResolver {
public ULIntersectionRuneResolver () :
base ('╭', '╒', '╓', '╔', '┌')
{
}
}
private class URIntersectionRuneResolver : IntersectionRuneResolver {
public URIntersectionRuneResolver () :
base ('╮', '╕', '╖', '╗', '┐')
{
}
}
private class LLIntersectionRuneResolver : IntersectionRuneResolver {
public LLIntersectionRuneResolver () :
base ('╰', '╘', '╙', '╚', '└')
{
}
}
private class LRIntersectionRuneResolver : IntersectionRuneResolver {
public LRIntersectionRuneResolver () :
base ('╯', '╛', '╜', '╝', '┘')
{
}
}
private class TopTeeIntersectionRuneResolver : IntersectionRuneResolver {
public TopTeeIntersectionRuneResolver () :
base ('┬', '╤', '╥', '╦', '┬')
{
}
}
private class LeftTeeIntersectionRuneResolver : IntersectionRuneResolver {
public LeftTeeIntersectionRuneResolver () :
base ('├', '╞', '╟', '╠', '├')
{
}
}
private class RightTeeIntersectionRuneResolver : IntersectionRuneResolver {
public RightTeeIntersectionRuneResolver () :
base ('┤', '╡', '╢', '╣', '┤')
{
}
}
private class BottomTeeIntersectionRuneResolver : IntersectionRuneResolver {
public BottomTeeIntersectionRuneResolver () :
base ('┴', '╧', '╨', '╩', '┴')
{
}
}
private class CrosshairIntersectionRuneResolver : IntersectionRuneResolver {
public CrosshairIntersectionRuneResolver () :
base ('┼', '╪', '╫', '╬', '┼')
{
}
}
private Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
{
if (!intersects.Any ()) {
return null;
}
var runeType = GetRuneTypeForIntersects (intersects);
if (runeResolvers.ContainsKey (runeType)) {
return runeResolvers [runeType].GetRuneForIntersects (driver, intersects);
}
// TODO: Remove these two once we have all of the below ported to IntersectionRuneResolvers
var useDouble = intersects.Any (i => i.Line.Style == LineStyle.Double);
var useRounded = intersects.Any (i => i.Line.Style == LineStyle.Rounded);
// TODO: Support ruler
//var useRuler = intersects.Any (i => i.Line.Style == LineStyle.Ruler && i.Line.Length != 0);
// TODO: maybe make these resolvers to for simplicity?
// or for dotted lines later on or that kind of thing?
switch (runeType) {
case IntersectionRuneType.None:
return null;
case IntersectionRuneType.Dot:
return (Rune)'.';
case IntersectionRuneType.HLine:
return useDouble ? driver.HDLine : driver.HLine;
case IntersectionRuneType.VLine:
return useDouble ? driver.VDLine : driver.VLine;
default: throw new Exception ("Could not find resolver or switch case for " + nameof (runeType) + ":" + runeType);
}
}
private Attribute? GetAttributeForIntersects (IntersectionDefinition [] intersects)
{
var set = new List (intersects.Where (i => i.Line.Attribute?.HasValidColors ?? false));
if (set.Count == 0) {
return null;
}
return set [0].Line.Attribute;
}
///
/// Represents a single row/column within the . Includes the glyph and the foreground/background colors.
///
public class Cell
{
///
/// The glyph to draw.
///
public Rune? Rune { get; set; }
///
/// The foreground color to draw the glyph with.
///
public Attribute? Attribute { get; set; }
}
private Cell GetCellForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
{
if (!intersects.Any ()) {
return null;
}
var cell = new Cell ();
cell.Rune = GetRuneForIntersects (driver, intersects);
cell.Attribute = GetAttributeForIntersects (intersects);
return cell;
}
private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition [] intersects)
{
var set = new HashSet (intersects.Select (i => i.Type));
#region Crosshair Conditions
if (Has (set,
IntersectionType.PassOverHorizontal,
IntersectionType.PassOverVertical
)) {
return IntersectionRuneType.Crosshair;
}
if (Has (set,
IntersectionType.PassOverVertical,
IntersectionType.StartLeft,
IntersectionType.StartRight
)) {
return IntersectionRuneType.Crosshair;
}
if (Has (set,
IntersectionType.PassOverHorizontal,
IntersectionType.StartUp,
IntersectionType.StartDown
)) {
return IntersectionRuneType.Crosshair;
}
if (Has (set,
IntersectionType.StartLeft,
IntersectionType.StartRight,
IntersectionType.StartUp,
IntersectionType.StartDown)) {
return IntersectionRuneType.Crosshair;
}
#endregion
#region Corner Conditions
if (Exactly (set,
IntersectionType.StartRight,
IntersectionType.StartDown)) {
return IntersectionRuneType.ULCorner;
}
if (Exactly (set,
IntersectionType.StartLeft,
IntersectionType.StartDown)) {
return IntersectionRuneType.URCorner;
}
if (Exactly (set,
IntersectionType.StartUp,
IntersectionType.StartLeft)) {
return IntersectionRuneType.LRCorner;
}
if (Exactly (set,
IntersectionType.StartUp,
IntersectionType.StartRight)) {
return IntersectionRuneType.LLCorner;
}
#endregion Corner Conditions
#region T Conditions
if (Has (set,
IntersectionType.PassOverHorizontal,
IntersectionType.StartDown)) {
return IntersectionRuneType.TopTee;
}
if (Has (set,
IntersectionType.StartRight,
IntersectionType.StartLeft,
IntersectionType.StartDown)) {
return IntersectionRuneType.TopTee;
}
if (Has (set,
IntersectionType.PassOverHorizontal,
IntersectionType.StartUp)) {
return IntersectionRuneType.BottomTee;
}
if (Has (set,
IntersectionType.StartRight,
IntersectionType.StartLeft,
IntersectionType.StartUp)) {
return IntersectionRuneType.BottomTee;
}
if (Has (set,
IntersectionType.PassOverVertical,
IntersectionType.StartRight)) {
return IntersectionRuneType.LeftTee;
}
if (Has (set,
IntersectionType.StartRight,
IntersectionType.StartDown,
IntersectionType.StartUp)) {
return IntersectionRuneType.LeftTee;
}
if (Has (set,
IntersectionType.PassOverVertical,
IntersectionType.StartLeft)) {
return IntersectionRuneType.RightTee;
}
if (Has (set,
IntersectionType.StartLeft,
IntersectionType.StartDown,
IntersectionType.StartUp)) {
return IntersectionRuneType.RightTee;
}
#endregion
if (All (intersects, Orientation.Horizontal)) {
return IntersectionRuneType.HLine;
}
if (All (intersects, Orientation.Vertical)) {
return IntersectionRuneType.VLine;
}
return IntersectionRuneType.Dot;
}
private bool All (IntersectionDefinition [] intersects, Orientation orientation)
{
return intersects.All (i => i.Line.Orientation == orientation);
}
///
/// Returns true if the collection has all the
/// specified (i.e. AND).
///
///
///
///
private bool Has (HashSet intersects, params IntersectionType [] types)
{
return types.All (t => intersects.Contains (t));
}
///
/// Returns true if all requested appear in
/// and there are no additional
///
///
///
///
private bool Exactly (HashSet intersects, params IntersectionType [] types)
{
return intersects.SetEquals (types);
}
///
/// Merges one line canvas into this one.
///
///
public void Merge (LineCanvas lineCanvas)
{
foreach (var line in lineCanvas._lines) {
AddLine (line);
}
}
internal class IntersectionDefinition {
///
/// The point at which the intersection happens
///
internal Point Point { get; }
///
/// Defines how position relates
/// to .
///
internal IntersectionType Type { get; }
///
/// The line that intersects
///
internal StraightLine Line { get; }
internal IntersectionDefinition (Point point, IntersectionType type, StraightLine line)
{
Point = point;
Type = type;
Line = line;
}
}
///
/// The type of Rune that we will use before considering
/// double width, curved borders etc
///
internal enum IntersectionRuneType {
None,
Dot,
ULCorner,
URCorner,
LLCorner,
LRCorner,
TopTee,
BottomTee,
RightTee,
LeftTee,
Crosshair,
HLine,
VLine,
}
internal enum IntersectionType {
///
/// There is no intersection
///
None,
///
/// A line passes directly over this point traveling along
/// the horizontal axis
///
PassOverHorizontal,
///
/// A line passes directly over this point traveling along
/// the vertical axis
///
PassOverVertical,
///
/// A line starts at this point and is traveling up
///
StartUp,
///
/// A line starts at this point and is traveling right
///
StartRight,
///
/// A line starts at this point and is traveling down
///
StartDown,
///
/// A line starts at this point and is traveling left
///
StartLeft,
///
/// A line exists at this point who has 0 length
///
Dot
}
// TODO: Add events that notify when StraightLine changes to enable dynamic layout
internal class StraightLine {
public Point Start { get; }
public int Length { get; }
public Orientation Orientation { get; }
public LineStyle Style { get; }
public Attribute? Attribute { get; set; }
internal StraightLine (Point start, int length, Orientation orientation, LineStyle style, Attribute? attribute = default)
{
this.Start = start;
this.Length = length;
this.Orientation = orientation;
this.Style = style;
this.Attribute = attribute;
}
internal IntersectionDefinition Intersects (int x, int y)
{
switch (Orientation) {
case Orientation.Horizontal: return IntersectsHorizontally (x, y);
case Orientation.Vertical: return IntersectsVertically (x, y);
default: throw new ArgumentOutOfRangeException (nameof (Orientation));
}
}
private IntersectionDefinition IntersectsHorizontally (int x, int y)
{
if (Start.Y != y) {
return null;
} else {
if (StartsAt (x, y)) {
return new IntersectionDefinition (
Start,
GetTypeByLength (IntersectionType.StartLeft, IntersectionType.PassOverHorizontal, IntersectionType.StartRight),
this
);
}
if (EndsAt (x, y)) {
return new IntersectionDefinition (
Start,
Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
this
);
} else {
var xmin = Math.Min (Start.X, Start.X + Length);
var xmax = Math.Max (Start.X, Start.X + Length);
if (xmin < x && xmax > x) {
return new IntersectionDefinition (
new Point (x, y),
IntersectionType.PassOverHorizontal,
this
);
}
}
return null;
}
}
private IntersectionDefinition IntersectsVertically (int x, int y)
{
if (Start.X != x) {
return null;
} else {
if (StartsAt (x, y)) {
return new IntersectionDefinition (
Start,
GetTypeByLength (IntersectionType.StartUp, IntersectionType.PassOverVertical, IntersectionType.StartDown),
this
);
}
if (EndsAt (x, y)) {
return new IntersectionDefinition (
Start,
Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
this
);
} else {
var ymin = Math.Min (Start.Y, Start.Y + Length);
var ymax = Math.Max (Start.Y, Start.Y + Length);
if (ymin < y && ymax > y) {
return new IntersectionDefinition (
new Point (x, y),
IntersectionType.PassOverVertical,
this
);
}
}
return null;
}
}
private IntersectionType GetTypeByLength (IntersectionType typeWhenNegative, IntersectionType typeWhenZero, IntersectionType typeWhenPositive)
{
if (Length == 0) {
return typeWhenZero;
}
return Length < 0 ? typeWhenNegative : typeWhenPositive;
}
private bool EndsAt (int x, int y)
{
var sub = (Length == 0) ? 0 : (Length > 0) ? 1 : -1;
if (Orientation == Orientation.Horizontal) {
return Start.X + Length - sub == x && Start.Y == y;
}
return Start.X == x && Start.Y + Length - sub == y;
}
private bool StartsAt (int x, int y)
{
return Start.X == x && Start.Y == y;
}
///
/// Gets the rectangle that describes the bounds of the canvas. Location is the coordinates of the
/// line that is furthest left/top and Size is defined by the line that extends the furthest
/// right/bottom.
///
internal Rect Bounds {
get {
// 0 and 1/-1 Length means a size (width or height) of 1
var size = Math.Max (1, Math.Abs (Length));
// How much to offset x or y to get the start of the line
var offset = Math.Abs (Length < 0 ? Length + 1 : 0);
var x = Start.X - (Orientation == Orientation.Horizontal ? offset : 0);
var y = Start.Y - (Orientation == Orientation.Vertical ? offset : 0);
var width = Orientation == Orientation.Horizontal ? size : 1;
var height = Orientation == Orientation.Vertical ? size : 1;
return new Rect (x, y, width, height);
}
}
///
/// Formats the Line as a string in (Start.X,Start.Y,Length,Orientation) notation.
///
public override string ToString ()
{
return $"({Start.X},{Start.Y},{Length},{Orientation})";
}
}
}
}