LineCanvas.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. #nullable enable
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// Defines the style of lines for a <see cref="LineCanvas"/>.
  10. /// </summary>
  11. public enum LineStyle {
  12. /// <summary>
  13. /// No border is drawn.
  14. /// </summary>
  15. None,
  16. /// <summary>
  17. /// The border is drawn using thin line CM.Glyphs.
  18. /// </summary>
  19. Single,
  20. /// <summary>
  21. /// The border is drawn using thin line glyphs with dashed (double and triple) straight lines.
  22. /// </summary>
  23. Dashed,
  24. /// <summary>
  25. /// The border is drawn using thin line glyphs with short dashed (triple and quadruple) straight lines.
  26. /// </summary>
  27. Dotted,
  28. /// <summary>
  29. /// The border is drawn using thin double line CM.Glyphs.
  30. /// </summary>
  31. Double,
  32. /// <summary>
  33. /// The border is drawn using heavy line CM.Glyphs.
  34. /// </summary>
  35. Heavy,
  36. /// <summary>
  37. /// The border is drawn using heavy line glyphs with dashed (double and triple) straight lines.
  38. /// </summary>
  39. HeavyDashed,
  40. /// <summary>
  41. /// The border is drawn using heavy line glyphs with short dashed (triple and quadruple) straight lines.
  42. /// </summary>
  43. HeavyDotted,
  44. /// <summary>
  45. /// The border is drawn using thin line glyphs with rounded corners.
  46. /// </summary>
  47. Rounded,
  48. /// <summary>
  49. /// The border is drawn using thin line glyphs with rounded corners and dashed (double and triple) straight lines.
  50. /// </summary>
  51. RoundedDashed,
  52. /// <summary>
  53. /// The border is drawn using thin line glyphs with rounded corners and short dashed (triple and quadruple) straight lines.
  54. /// </summary>
  55. RoundedDotted,
  56. // TODO: Support Ruler
  57. ///// <summary>
  58. ///// The border is drawn as a diagnostic ruler ("|123456789...").
  59. ///// </summary>
  60. //Ruler
  61. }
  62. /// <summary>
  63. /// Facilitates box drawing and line intersection detection
  64. /// and rendering. Does not support diagonal lines.
  65. /// </summary>
  66. public class LineCanvas : IDisposable {
  67. /// <summary>
  68. /// Creates a new instance.
  69. /// </summary>
  70. public LineCanvas()
  71. {
  72. // TODO: Refactor ConfigurationManager to not use an event handler for this.
  73. // Instead, have it call a method on any class appropriately attributed
  74. // to update the cached values. See Issue #2871
  75. ConfigurationManager.Applied += ConfigurationManager_Applied;
  76. }
  77. /// <summary>
  78. /// Creates a new instance with the given <paramref name="lines"/>.
  79. /// </summary>
  80. /// <param name="lines">Initial lines for the canvas.</param>
  81. public LineCanvas (IEnumerable<StraightLine> lines) : this()
  82. {
  83. _lines = lines.ToList();
  84. }
  85. private void ConfigurationManager_Applied (object? sender, ConfigurationManagerEventArgs e)
  86. {
  87. foreach (var irr in runeResolvers) {
  88. irr.Value.SetGlyphs ();
  89. }
  90. }
  91. private List<StraightLine> _lines = new List<StraightLine> ();
  92. /// <summary>
  93. /// Gets the lines in the canvas.
  94. /// </summary>
  95. public IReadOnlyCollection<StraightLine> Lines { get { return _lines.AsReadOnly(); } }
  96. Dictionary<IntersectionRuneType, IntersectionRuneResolver> runeResolvers = new Dictionary<IntersectionRuneType, IntersectionRuneResolver> {
  97. {IntersectionRuneType.ULCorner,new ULIntersectionRuneResolver()},
  98. {IntersectionRuneType.URCorner,new URIntersectionRuneResolver()},
  99. {IntersectionRuneType.LLCorner,new LLIntersectionRuneResolver()},
  100. {IntersectionRuneType.LRCorner,new LRIntersectionRuneResolver()},
  101. {IntersectionRuneType.TopTee,new TopTeeIntersectionRuneResolver()},
  102. {IntersectionRuneType.LeftTee,new LeftTeeIntersectionRuneResolver()},
  103. {IntersectionRuneType.RightTee,new RightTeeIntersectionRuneResolver()},
  104. {IntersectionRuneType.BottomTee,new BottomTeeIntersectionRuneResolver()},
  105. {IntersectionRuneType.Cross,new CrossIntersectionRuneResolver()},
  106. // TODO: Add other resolvers
  107. };
  108. /// <summary>
  109. /// <para>
  110. /// Adds a new <paramref name="length"/> long line to the canvas starting at <paramref name="start"/>.
  111. /// </para>
  112. /// <para>
  113. /// Use positive <paramref name="length"/> for the line to extend Right and negative for Left
  114. /// when <see cref="Orientation"/> is <see cref="Orientation.Horizontal"/>.
  115. /// </para>
  116. /// <para>
  117. /// Use positive <paramref name="length"/> for the line to extend Down and negative for Up
  118. /// when <see cref="Orientation"/> is <see cref="Orientation.Vertical"/>.
  119. /// </para>
  120. /// </summary>
  121. /// <param name="start">Starting point.</param>
  122. /// <param name="length">The length of line. 0 for an intersection (cross or T). Positive for Down/Right. Negative for Up/Left.</param>
  123. /// <param name="orientation">The direction of the line.</param>
  124. /// <param name="style">The style of line to use</param>
  125. /// <param name="attribute"></param>
  126. public void AddLine (Point start, int length, Orientation orientation, LineStyle style, Attribute? attribute = default)
  127. {
  128. _cachedBounds = Rect.Empty;
  129. _lines.Add (new StraightLine (start, length, orientation, style, attribute));
  130. }
  131. /// <summary>
  132. /// Adds a new line to the canvas
  133. /// </summary>
  134. /// <param name="line"></param>
  135. public void AddLine (StraightLine line)
  136. {
  137. _cachedBounds = Rect.Empty;
  138. _lines.Add (line);
  139. }
  140. /// <summary>
  141. /// Removes the last line added to the canvas
  142. /// </summary>
  143. /// <returns></returns>
  144. public StraightLine RemoveLastLine()
  145. {
  146. var l = _lines.LastOrDefault ();
  147. if(l != null) {
  148. _lines.Remove(l);
  149. }
  150. return l!;
  151. }
  152. /// <summary>
  153. /// Clears all lines from the LineCanvas.
  154. /// </summary>
  155. public void Clear ()
  156. {
  157. _cachedBounds = Rect.Empty;
  158. _lines.Clear ();
  159. }
  160. /// <summary>
  161. /// Clears any cached states from the canvas
  162. /// Call this method if you make changes to lines
  163. /// that have already been added.
  164. /// </summary>
  165. public void ClearCache ()
  166. {
  167. _cachedBounds = Rect.Empty;
  168. }
  169. private Rect _cachedBounds;
  170. /// <summary>
  171. /// Gets the rectangle that describes the bounds of the canvas. Location is the coordinates of the
  172. /// line that is furthest left/top and Size is defined by the line that extends the furthest
  173. /// right/bottom.
  174. /// </summary>
  175. public Rect Bounds {
  176. get {
  177. if (_cachedBounds.IsEmpty) {
  178. if (_lines.Count == 0) {
  179. return _cachedBounds;
  180. }
  181. Rect bounds = _lines [0].Bounds;
  182. for (var i = 1; i < _lines.Count; i++) {
  183. var line = _lines [i];
  184. var lineBounds = line.Bounds;
  185. bounds = Rect.Union (bounds, lineBounds);
  186. }
  187. if (bounds.Width == 0) {
  188. bounds.Width = 1;
  189. }
  190. if (bounds.Height == 0) {
  191. bounds.Height = 1;
  192. }
  193. _cachedBounds = new Rect (bounds.X, bounds.Y, bounds.Width, bounds.Height);
  194. }
  195. return _cachedBounds;
  196. }
  197. }
  198. // TODO: Unless there's an obvious use case for this API we should delete it in favor of the
  199. // simpler version that doensn't take an area.
  200. /// <summary>
  201. /// Evaluates the lines that have been added to the canvas and returns a map containing
  202. /// the glyphs and their locations. The glyphs are the characters that should be rendered
  203. /// so that all lines connect up with the appropriate intersection symbols.
  204. /// </summary>
  205. /// <param name="inArea">A rectangle to constrain the search by.</param>
  206. /// <returns>A map of the points within the canvas that intersect with <paramref name="inArea"/>.</returns>
  207. public Dictionary<Point, Rune> GetMap (Rect inArea)
  208. {
  209. var map = new Dictionary<Point, Rune> ();
  210. // walk through each pixel of the bitmap
  211. for (int y = inArea.Y; y < inArea.Y + inArea.Height; y++) {
  212. for (int x = inArea.X; x < inArea.X + inArea.Width; x++) {
  213. var intersects = _lines
  214. .Select (l => l.Intersects (x, y))
  215. .Where (i => i != null)
  216. .ToArray ();
  217. var rune = GetRuneForIntersects (Application.Driver, intersects);
  218. if (rune != null) {
  219. map.Add (new Point (x, y), rune.Value);
  220. }
  221. }
  222. }
  223. return map;
  224. }
  225. /// <summary>
  226. /// Evaluates the lines that have been added to the canvas and returns a map containing
  227. /// the glyphs and their locations. The glyphs are the characters that should be rendered
  228. /// so that all lines connect up with the appropriate intersection symbols.
  229. /// </summary>
  230. /// <returns>A map of all the points within the canvas.</returns>
  231. public Dictionary<Point, Cell> GetCellMap ()
  232. {
  233. var map = new Dictionary<Point, Cell> ();
  234. // walk through each pixel of the bitmap
  235. for (int y = Bounds.Y; y < Bounds.Y + Bounds.Height; y++) {
  236. for (int x = Bounds.X; x < Bounds.X + Bounds.Width; x++) {
  237. var intersects = _lines
  238. .Select (l => l.Intersects (x, y))
  239. .Where (i => i != null)
  240. .ToArray ();
  241. var cell = GetCellForIntersects (Application.Driver, intersects);
  242. if (cell != null) {
  243. map.Add (new Point (x, y), cell);
  244. }
  245. }
  246. }
  247. return map;
  248. }
  249. /// <summary>
  250. /// Evaluates the lines that have been added to the canvas and returns a map containing
  251. /// the glyphs and their locations. The glyphs are the characters that should be rendered
  252. /// so that all lines connect up with the appropriate intersection symbols.
  253. /// </summary>
  254. /// <returns>A map of all the points within the canvas.</returns>
  255. public Dictionary<Point, Rune> GetMap () => GetMap (Bounds);
  256. /// <summary>
  257. /// Returns the contents of the line canvas rendered to a string. The string
  258. /// will include all columns and rows, even if <see cref="Bounds"/> has negative coordinates.
  259. /// For example, if the canvas contains a single line that starts at (-1,-1) with a length of 2, the
  260. /// rendered string will have a length of 2.
  261. /// </summary>
  262. /// <returns>The canvas rendered to a string.</returns>
  263. public override string ToString ()
  264. {
  265. if (Bounds.IsEmpty) {
  266. return string.Empty;
  267. }
  268. // Generate the rune map for the entire canvas
  269. var runeMap = GetMap ();
  270. // Create the rune canvas
  271. Rune [,] canvas = new Rune [Bounds.Height, Bounds.Width];
  272. // Copy the rune map to the canvas, adjusting for any negative coordinates
  273. foreach (var kvp in runeMap) {
  274. int x = kvp.Key.X - Bounds.X;
  275. int y = kvp.Key.Y - Bounds.Y;
  276. canvas [y, x] = kvp.Value;
  277. }
  278. // Convert the canvas to a string
  279. StringBuilder sb = new StringBuilder ();
  280. for (int y = 0; y < canvas.GetLength (0); y++) {
  281. for (int x = 0; x < canvas.GetLength (1); x++) {
  282. Rune r = canvas [y, x];
  283. sb.Append (r.Value == 0 ? ' ' : r.ToString ());
  284. }
  285. if (y < canvas.GetLength (0) - 1) {
  286. sb.AppendLine ();
  287. }
  288. }
  289. return sb.ToString ();
  290. }
  291. private abstract class IntersectionRuneResolver {
  292. internal Rune _round;
  293. internal Rune _doubleH;
  294. internal Rune _doubleV;
  295. internal Rune _doubleBoth;
  296. internal Rune _thickH;
  297. internal Rune _thickV;
  298. internal Rune _thickBoth;
  299. internal Rune _normal;
  300. public IntersectionRuneResolver()
  301. {
  302. SetGlyphs ();
  303. }
  304. /// <summary>
  305. /// Sets the glyphs used. Call this method after construction and any time
  306. /// ConfigurationManager has updated the settings.
  307. /// </summary>
  308. public abstract void SetGlyphs ();
  309. public Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition? [] intersects)
  310. {
  311. var useRounded = intersects.Any (i => i?.Line.Length != 0 && (
  312. i?.Line.Style == LineStyle.Rounded || i?.Line.Style == LineStyle.RoundedDashed || i?.Line.Style == LineStyle.RoundedDotted));
  313. // Note that there aren't any glyphs for intersections of double lines with heavy lines
  314. bool doubleHorizontal = intersects.Any (l => l?.Line.Orientation == Orientation.Horizontal && l.Line.Style == LineStyle.Double);
  315. bool doubleVertical = intersects.Any (l => l?.Line.Orientation == Orientation.Vertical && l.Line.Style == LineStyle.Double);
  316. bool thickHorizontal = intersects.Any (l => l?.Line.Orientation == Orientation.Horizontal && (
  317. l.Line.Style == LineStyle.Heavy || l.Line.Style == LineStyle.HeavyDashed || l.Line.Style == LineStyle.HeavyDotted));
  318. bool thickVertical = intersects.Any (l => l?.Line.Orientation == Orientation.Vertical && (
  319. l.Line.Style == LineStyle.Heavy || l.Line.Style == LineStyle.HeavyDashed || l.Line.Style == LineStyle.HeavyDotted));
  320. if (doubleHorizontal) {
  321. return doubleVertical ? _doubleBoth : _doubleH;
  322. }
  323. if (doubleVertical) {
  324. return _doubleV;
  325. }
  326. if (thickHorizontal) {
  327. return thickVertical ? _thickBoth : _thickH;
  328. }
  329. if (thickVertical) {
  330. return _thickV;
  331. }
  332. return useRounded ? _round : _normal;
  333. }
  334. }
  335. private class ULIntersectionRuneResolver : IntersectionRuneResolver {
  336. public override void SetGlyphs ()
  337. {
  338. _round = CM.Glyphs.ULCornerR;
  339. _doubleH = CM.Glyphs.ULCornerSingleDbl;
  340. _doubleV = CM.Glyphs.ULCornerDblSingle;
  341. _doubleBoth = CM.Glyphs.ULCornerDbl;
  342. _thickH = CM.Glyphs.ULCornerLtHv;
  343. _thickV = CM.Glyphs.ULCornerHvLt;
  344. _thickBoth = CM.Glyphs.ULCornerHv;
  345. _normal = CM.Glyphs.ULCorner;
  346. }
  347. }
  348. private class URIntersectionRuneResolver : IntersectionRuneResolver {
  349. public override void SetGlyphs ()
  350. {
  351. _round = CM.Glyphs.URCornerR;
  352. _doubleH = CM.Glyphs.URCornerSingleDbl;
  353. _doubleV = CM.Glyphs.URCornerDblSingle;
  354. _doubleBoth = CM.Glyphs.URCornerDbl;
  355. _thickH = CM.Glyphs.URCornerHvLt;
  356. _thickV = CM.Glyphs.URCornerLtHv;
  357. _thickBoth = CM.Glyphs.URCornerHv;
  358. _normal = CM.Glyphs.URCorner;
  359. }
  360. }
  361. private class LLIntersectionRuneResolver : IntersectionRuneResolver {
  362. public override void SetGlyphs ()
  363. {
  364. _round = CM.Glyphs.LLCornerR;
  365. _doubleH = CM.Glyphs.LLCornerSingleDbl;
  366. _doubleV = CM.Glyphs.LLCornerDblSingle;
  367. _doubleBoth = CM.Glyphs.LLCornerDbl;
  368. _thickH = CM.Glyphs.LLCornerLtHv;
  369. _thickV = CM.Glyphs.LLCornerHvLt;
  370. _thickBoth = CM.Glyphs.LLCornerHv;
  371. _normal = CM.Glyphs.LLCorner;
  372. }
  373. }
  374. private class LRIntersectionRuneResolver : IntersectionRuneResolver {
  375. public override void SetGlyphs ()
  376. {
  377. _round = CM.Glyphs.LRCornerR;
  378. _doubleH = CM.Glyphs.LRCornerSingleDbl;
  379. _doubleV = CM.Glyphs.LRCornerDblSingle;
  380. _doubleBoth = CM.Glyphs.LRCornerDbl;
  381. _thickH = CM.Glyphs.LRCornerLtHv;
  382. _thickV = CM.Glyphs.LRCornerHvLt;
  383. _thickBoth = CM.Glyphs.LRCornerHv;
  384. _normal = CM.Glyphs.LRCorner;
  385. }
  386. }
  387. private class TopTeeIntersectionRuneResolver : IntersectionRuneResolver {
  388. public override void SetGlyphs ()
  389. {
  390. _round = CM.Glyphs.TopTee;
  391. _doubleH = CM.Glyphs.TopTeeDblH;
  392. _doubleV = CM.Glyphs.TopTeeDblV;
  393. _doubleBoth = CM.Glyphs.TopTeeDbl;
  394. _thickH = CM.Glyphs.TopTeeHvH;
  395. _thickV = CM.Glyphs.TopTeeHvV;
  396. _thickBoth = CM.Glyphs.TopTeeHvDblH;
  397. _normal = CM.Glyphs.TopTee;
  398. }
  399. }
  400. private class LeftTeeIntersectionRuneResolver : IntersectionRuneResolver {
  401. public override void SetGlyphs ()
  402. {
  403. _round = CM.Glyphs.LeftTee;
  404. _doubleH = CM.Glyphs.LeftTeeDblH;
  405. _doubleV = CM.Glyphs.LeftTeeDblV;
  406. _doubleBoth = CM.Glyphs.LeftTeeDbl;
  407. _thickH = CM.Glyphs.LeftTeeHvH;
  408. _thickV = CM.Glyphs.LeftTeeHvV;
  409. _thickBoth = CM.Glyphs.LeftTeeHvDblH;
  410. _normal = CM.Glyphs.LeftTee;
  411. }
  412. }
  413. private class RightTeeIntersectionRuneResolver : IntersectionRuneResolver {
  414. public override void SetGlyphs ()
  415. {
  416. _round = CM.Glyphs.RightTee;
  417. _doubleH = CM.Glyphs.RightTeeDblH;
  418. _doubleV = CM.Glyphs.RightTeeDblV;
  419. _doubleBoth = CM.Glyphs.RightTeeDbl;
  420. _thickH = CM.Glyphs.RightTeeHvH;
  421. _thickV = CM.Glyphs.RightTeeHvV;
  422. _thickBoth = CM.Glyphs.RightTeeHvDblH;
  423. _normal = CM.Glyphs.RightTee;
  424. }
  425. }
  426. private class BottomTeeIntersectionRuneResolver : IntersectionRuneResolver {
  427. public override void SetGlyphs ()
  428. {
  429. _round = CM.Glyphs.BottomTee;
  430. _doubleH = CM.Glyphs.BottomTeeDblH;
  431. _doubleV = CM.Glyphs.BottomTeeDblV;
  432. _doubleBoth = CM.Glyphs.BottomTeeDbl;
  433. _thickH = CM.Glyphs.BottomTeeHvH;
  434. _thickV = CM.Glyphs.BottomTeeHvV;
  435. _thickBoth = CM.Glyphs.BottomTeeHvDblH;
  436. _normal = CM.Glyphs.BottomTee;
  437. }
  438. }
  439. private class CrossIntersectionRuneResolver : IntersectionRuneResolver {
  440. public override void SetGlyphs ()
  441. {
  442. _round = CM.Glyphs.Cross;
  443. _doubleH = CM.Glyphs.CrossDblH;
  444. _doubleV = CM.Glyphs.CrossDblV;
  445. _doubleBoth = CM.Glyphs.CrossDbl;
  446. _thickH = CM.Glyphs.CrossHvH;
  447. _thickV = CM.Glyphs.CrossHvV;
  448. _thickBoth = CM.Glyphs.CrossHv;
  449. _normal = CM.Glyphs.Cross;
  450. }
  451. }
  452. private Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition? [] intersects)
  453. {
  454. if (!intersects.Any ()) {
  455. return null;
  456. }
  457. var runeType = GetRuneTypeForIntersects (intersects);
  458. if (runeResolvers.TryGetValue (runeType, out var resolver)) {
  459. return resolver.GetRuneForIntersects (driver, intersects);
  460. }
  461. // TODO: Remove these once we have all of the below ported to IntersectionRuneResolvers
  462. var useDouble = intersects.Any (i => i?.Line.Style == LineStyle.Double);
  463. var useDashed = intersects.Any (i => i?.Line.Style == LineStyle.Dashed || i?.Line.Style == LineStyle.RoundedDashed);
  464. var useDotted = intersects.Any (i => i?.Line.Style == LineStyle.Dotted || i?.Line.Style == LineStyle.RoundedDotted);
  465. // horiz and vert lines same as Single for Rounded
  466. var useThick = intersects.Any (i => i?.Line.Style == LineStyle.Heavy);
  467. var useThickDashed = intersects.Any (i => i?.Line.Style == LineStyle.HeavyDashed);
  468. var useThickDotted = intersects.Any (i => i?.Line.Style == LineStyle.HeavyDotted);
  469. // TODO: Support ruler
  470. //var useRuler = intersects.Any (i => i.Line.Style == LineStyle.Ruler && i.Line.Length != 0);
  471. // TODO: maybe make these resolvers too for simplicity?
  472. switch (runeType) {
  473. case IntersectionRuneType.None:
  474. return null;
  475. case IntersectionRuneType.Dot:
  476. return (Rune)CM.Glyphs.Dot;
  477. case IntersectionRuneType.HLine:
  478. if (useDouble) {
  479. return CM.Glyphs.HLineDbl;
  480. }
  481. if (useDashed) {
  482. return CM.Glyphs.HLineDa2;
  483. }
  484. if (useDotted) {
  485. return CM.Glyphs.HLineDa3;
  486. }
  487. return useThick ? CM.Glyphs.HLineHv : (useThickDashed ? CM.Glyphs.HLineHvDa2 : (useThickDotted ? CM.Glyphs.HLineHvDa3 : CM.Glyphs.HLine));
  488. case IntersectionRuneType.VLine:
  489. if (useDouble) {
  490. return CM.Glyphs.VLineDbl;
  491. }
  492. if (useDashed) {
  493. return CM.Glyphs.VLineDa3;
  494. }
  495. if (useDotted) {
  496. return CM.Glyphs.VLineDa4;
  497. }
  498. return useThick ? CM.Glyphs.VLineHv : (useThickDashed ? CM.Glyphs.VLineHvDa3 : (useThickDotted ? CM.Glyphs.VLineHvDa4 : CM.Glyphs.VLine));
  499. default: throw new Exception ("Could not find resolver or switch case for " + nameof (runeType) + ":" + runeType);
  500. }
  501. }
  502. private Attribute? GetAttributeForIntersects (IntersectionDefinition? [] intersects) =>intersects [0]!.Line.Attribute;
  503. private Cell? GetCellForIntersects (ConsoleDriver driver, IntersectionDefinition? [] intersects)
  504. {
  505. if (!intersects.Any ()) {
  506. return null;
  507. }
  508. var cell = new Cell ();
  509. var rune = GetRuneForIntersects (driver, intersects);
  510. if (rune.HasValue) {
  511. cell.Rune = rune.Value;
  512. }
  513. cell.Attribute = GetAttributeForIntersects (intersects);
  514. return cell;
  515. }
  516. private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition? [] intersects)
  517. {
  518. var set = new HashSet<IntersectionType> (intersects.Select (i => i!.Type));
  519. #region Cross Conditions
  520. if (Has (set,
  521. IntersectionType.PassOverHorizontal,
  522. IntersectionType.PassOverVertical
  523. )) {
  524. return IntersectionRuneType.Cross;
  525. }
  526. if (Has (set,
  527. IntersectionType.PassOverVertical,
  528. IntersectionType.StartLeft,
  529. IntersectionType.StartRight
  530. )) {
  531. return IntersectionRuneType.Cross;
  532. }
  533. if (Has (set,
  534. IntersectionType.PassOverHorizontal,
  535. IntersectionType.StartUp,
  536. IntersectionType.StartDown
  537. )) {
  538. return IntersectionRuneType.Cross;
  539. }
  540. if (Has (set,
  541. IntersectionType.StartLeft,
  542. IntersectionType.StartRight,
  543. IntersectionType.StartUp,
  544. IntersectionType.StartDown)) {
  545. return IntersectionRuneType.Cross;
  546. }
  547. #endregion
  548. #region Corner Conditions
  549. if (Exactly (set,
  550. IntersectionType.StartRight,
  551. IntersectionType.StartDown)) {
  552. return IntersectionRuneType.ULCorner;
  553. }
  554. if (Exactly (set,
  555. IntersectionType.StartLeft,
  556. IntersectionType.StartDown)) {
  557. return IntersectionRuneType.URCorner;
  558. }
  559. if (Exactly (set,
  560. IntersectionType.StartUp,
  561. IntersectionType.StartLeft)) {
  562. return IntersectionRuneType.LRCorner;
  563. }
  564. if (Exactly (set,
  565. IntersectionType.StartUp,
  566. IntersectionType.StartRight)) {
  567. return IntersectionRuneType.LLCorner;
  568. }
  569. #endregion Corner Conditions
  570. #region T Conditions
  571. if (Has (set,
  572. IntersectionType.PassOverHorizontal,
  573. IntersectionType.StartDown)) {
  574. return IntersectionRuneType.TopTee;
  575. }
  576. if (Has (set,
  577. IntersectionType.StartRight,
  578. IntersectionType.StartLeft,
  579. IntersectionType.StartDown)) {
  580. return IntersectionRuneType.TopTee;
  581. }
  582. if (Has (set,
  583. IntersectionType.PassOverHorizontal,
  584. IntersectionType.StartUp)) {
  585. return IntersectionRuneType.BottomTee;
  586. }
  587. if (Has (set,
  588. IntersectionType.StartRight,
  589. IntersectionType.StartLeft,
  590. IntersectionType.StartUp)) {
  591. return IntersectionRuneType.BottomTee;
  592. }
  593. if (Has (set,
  594. IntersectionType.PassOverVertical,
  595. IntersectionType.StartRight)) {
  596. return IntersectionRuneType.LeftTee;
  597. }
  598. if (Has (set,
  599. IntersectionType.StartRight,
  600. IntersectionType.StartDown,
  601. IntersectionType.StartUp)) {
  602. return IntersectionRuneType.LeftTee;
  603. }
  604. if (Has (set,
  605. IntersectionType.PassOverVertical,
  606. IntersectionType.StartLeft)) {
  607. return IntersectionRuneType.RightTee;
  608. }
  609. if (Has (set,
  610. IntersectionType.StartLeft,
  611. IntersectionType.StartDown,
  612. IntersectionType.StartUp)) {
  613. return IntersectionRuneType.RightTee;
  614. }
  615. #endregion
  616. if (All (intersects, Orientation.Horizontal)) {
  617. return IntersectionRuneType.HLine;
  618. }
  619. if (All (intersects, Orientation.Vertical)) {
  620. return IntersectionRuneType.VLine;
  621. }
  622. return IntersectionRuneType.Dot;
  623. }
  624. private bool All (IntersectionDefinition? [] intersects, Orientation orientation)
  625. {
  626. return intersects.All (i => i!.Line.Orientation == orientation);
  627. }
  628. /// <summary>
  629. /// Returns true if the <paramref name="intersects"/> collection has all the <paramref name="types"/>
  630. /// specified (i.e. AND).
  631. /// </summary>
  632. /// <param name="intersects"></param>
  633. /// <param name="types"></param>
  634. /// <returns></returns>
  635. private bool Has (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  636. {
  637. return types.All (t => intersects.Contains (t));
  638. }
  639. /// <summary>
  640. /// Returns true if all requested <paramref name="types"/> appear in <paramref name="intersects"/>
  641. /// and there are no additional <see cref="IntersectionRuneType"/>
  642. /// </summary>
  643. /// <param name="intersects"></param>
  644. /// <param name="types"></param>
  645. /// <returns></returns>
  646. private bool Exactly (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  647. {
  648. return intersects.SetEquals (types);
  649. }
  650. /// <summary>
  651. /// Merges one line canvas into this one.
  652. /// </summary>
  653. /// <param name="lineCanvas"></param>
  654. public void Merge (LineCanvas lineCanvas)
  655. {
  656. foreach (var line in lineCanvas._lines) {
  657. AddLine (line);
  658. }
  659. }
  660. /// <inheritdoc />
  661. public void Dispose ()
  662. {
  663. ConfigurationManager.Applied -= ConfigurationManager_Applied;
  664. }
  665. }
  666. internal class IntersectionDefinition {
  667. /// <summary>
  668. /// The point at which the intersection happens
  669. /// </summary>
  670. internal Point Point { get; }
  671. /// <summary>
  672. /// Defines how <see cref="Line"/> position relates
  673. /// to <see cref="Point"/>.
  674. /// </summary>
  675. internal IntersectionType Type { get; }
  676. /// <summary>
  677. /// The line that intersects <see cref="Point"/>
  678. /// </summary>
  679. internal StraightLine Line { get; }
  680. internal IntersectionDefinition (Point point, IntersectionType type, StraightLine line)
  681. {
  682. Point = point;
  683. Type = type;
  684. Line = line;
  685. }
  686. }
  687. /// <summary>
  688. /// The type of Rune that we will use before considering
  689. /// double width, curved borders etc
  690. /// </summary>
  691. internal enum IntersectionRuneType {
  692. None,
  693. Dot,
  694. ULCorner,
  695. URCorner,
  696. LLCorner,
  697. LRCorner,
  698. TopTee,
  699. BottomTee,
  700. RightTee,
  701. LeftTee,
  702. Cross,
  703. HLine,
  704. VLine,
  705. }
  706. internal enum IntersectionType {
  707. /// <summary>
  708. /// There is no intersection
  709. /// </summary>
  710. None,
  711. /// <summary>
  712. /// A line passes directly over this point traveling along
  713. /// the horizontal axis
  714. /// </summary>
  715. PassOverHorizontal,
  716. /// <summary>
  717. /// A line passes directly over this point traveling along
  718. /// the vertical axis
  719. /// </summary>
  720. PassOverVertical,
  721. /// <summary>
  722. /// A line starts at this point and is traveling up
  723. /// </summary>
  724. StartUp,
  725. /// <summary>
  726. /// A line starts at this point and is traveling right
  727. /// </summary>
  728. StartRight,
  729. /// <summary>
  730. /// A line starts at this point and is traveling down
  731. /// </summary>
  732. StartDown,
  733. /// <summary>
  734. /// A line starts at this point and is traveling left
  735. /// </summary>
  736. StartLeft,
  737. /// <summary>
  738. /// A line exists at this point who has 0 length
  739. /// </summary>
  740. Dot
  741. }
  742. }