LineCanvas.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Terminal.Gui.Graphs {
  5. /// <summary>
  6. /// Facilitates box drawing and line intersection detection
  7. /// and rendering. Does not support diagonal lines.
  8. /// </summary>
  9. public class LineCanvas {
  10. private List<StraightLine> lines = new List<StraightLine> ();
  11. Dictionary<IntersectionRuneType, IntersectionRuneResolver> runeResolvers = new Dictionary<IntersectionRuneType, IntersectionRuneResolver> {
  12. {IntersectionRuneType.ULCorner,new ULIntersectionRuneResolver()},
  13. {IntersectionRuneType.URCorner,new URIntersectionRuneResolver()},
  14. {IntersectionRuneType.LLCorner,new LLIntersectionRuneResolver()},
  15. {IntersectionRuneType.LRCorner,new LRIntersectionRuneResolver()},
  16. {IntersectionRuneType.TopTee,new TopTeeIntersectionRuneResolver()},
  17. {IntersectionRuneType.LeftTee,new LeftTeeIntersectionRuneResolver()},
  18. {IntersectionRuneType.RightTee,new RightTeeIntersectionRuneResolver()},
  19. {IntersectionRuneType.BottomTee,new BottomTeeIntersectionRuneResolver()},
  20. {IntersectionRuneType.Crosshair,new CrosshairIntersectionRuneResolver()},
  21. // TODO: Add other resolvers
  22. };
  23. /// <summary>
  24. /// Add a new line to the canvas starting at <paramref name="from"/>.
  25. /// Use positive <paramref name="length"/> for Right and negative for Left
  26. /// when <see cref="Orientation"/> is <see cref="Orientation.Horizontal"/>.
  27. /// Use positive <paramref name="length"/> for Down and negative for Up
  28. /// when <see cref="Orientation"/> is <see cref="Orientation.Vertical"/>.
  29. /// </summary>
  30. /// <param name="from">Starting point.</param>
  31. /// <param name="length">Length of line. 0 for a dot.
  32. /// Positive for Down/Right. Negative for Up/Left.</param>
  33. /// <param name="orientation">Direction of the line.</param>
  34. /// <param name="style">The style of line to use</param>
  35. public void AddLine (Point from, int length, Orientation orientation, BorderStyle style)
  36. {
  37. lines.Add (new StraightLine (from, length, orientation, style));
  38. }
  39. /// <summary>
  40. /// Evaluate all currently defined lines that lie within
  41. /// <paramref name="inArea"/> and generate a 'bitmap' that
  42. /// shows what characters (if any) should be rendered at each
  43. /// point so that all lines connect up correctly with appropriate
  44. /// intersection symbols.
  45. /// <returns></returns>
  46. /// </summary>
  47. /// <param name="inArea"></param>
  48. /// <returns>Map as 2D array where first index is rows and second is column</returns>
  49. public Rune? [,] GenerateImage (Rect inArea)
  50. {
  51. Rune? [,] canvas = new Rune? [inArea.Height, inArea.Width];
  52. // walk through each pixel of the bitmap
  53. for (int y = 0; y < inArea.Height; y++) {
  54. for (int x = 0; x < inArea.Width; x++) {
  55. var intersects = lines
  56. .Select (l => l.Intersects (x, y))
  57. .Where (i => i != null)
  58. .ToArray ();
  59. // TODO: use Driver and LineStyle to map
  60. canvas [y, x] = GetRuneForIntersects (Application.Driver, intersects);
  61. }
  62. }
  63. return canvas;
  64. }
  65. /// <summary>
  66. /// Draws all the lines that lie within the <paramref name="bounds"/> onto
  67. /// the <paramref name="view"/> client area. This method should be called from
  68. /// <see cref="View.Redraw(Rect)"/>.
  69. /// </summary>
  70. /// <param name="view"></param>
  71. /// <param name="bounds"></param>
  72. public void Draw (View view, Rect bounds)
  73. {
  74. var runes = GenerateImage (bounds);
  75. for (int y = bounds.Y; y < bounds.Height; y++) {
  76. for (int x = bounds.X; x < bounds.Width; x++) {
  77. var rune = runes [y, x];
  78. if (rune.HasValue) {
  79. view.AddRune (x, y, rune.Value);
  80. }
  81. }
  82. }
  83. }
  84. private abstract class IntersectionRuneResolver
  85. {
  86. public Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
  87. {
  88. var useDouble = intersects.Any (i => i.Line.Style == BorderStyle.Double && i.Line.Length != 0);
  89. var useRounded = intersects.Any (i => i.Line.Style == BorderStyle.Rounded && i.Line.Length != 0);
  90. return GetRuneForIntersects (driver, intersects, useDouble, useRounded);
  91. }
  92. protected abstract Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded);
  93. }
  94. private class ULIntersectionRuneResolver : IntersectionRuneResolver
  95. {
  96. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  97. {
  98. // TODO: Handle all relevant permutations of double lines into single lines
  99. // to make F type borders instead.
  100. return useDouble ? driver.ULDCorner : useRounded ? driver.ULRCorner : driver.ULCorner;
  101. }
  102. }
  103. private class URIntersectionRuneResolver : IntersectionRuneResolver
  104. {
  105. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  106. {
  107. // TODO: Handle all relevant permutations of double lines into single lines
  108. // to make F type borders instead.
  109. return useDouble ? driver.URDCorner : useRounded ? driver.URRCorner : driver.URCorner;
  110. }
  111. }
  112. private class LLIntersectionRuneResolver : IntersectionRuneResolver
  113. {
  114. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  115. {
  116. // TODO: Handle all relevant permutations of double lines into single lines
  117. // to make F type borders instead.
  118. return useDouble ? driver.LLDCorner : useRounded ? driver.LLRCorner : driver.LLCorner;
  119. }
  120. }
  121. private class LRIntersectionRuneResolver : IntersectionRuneResolver
  122. {
  123. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  124. {
  125. // TODO: Handle all relevant permutations of double lines into single lines
  126. // to make F type borders instead.
  127. return useDouble ? driver.LRDCorner : useRounded ? driver.LRRCorner : driver.LRCorner;
  128. }
  129. }
  130. private class TopTeeIntersectionRuneResolver : IntersectionRuneResolver
  131. {
  132. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  133. {
  134. // TODO: Handle all relevant permutations of double lines into single lines
  135. // to make F type borders instead.
  136. return useDouble ? '╦' : driver.TopTee;
  137. }
  138. }
  139. private class LeftTeeIntersectionRuneResolver : IntersectionRuneResolver
  140. {
  141. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  142. {
  143. // TODO: Handle all relevant permutations of double lines into single lines
  144. // to make F type borders instead.
  145. return useDouble ? '╠' : driver.LeftTee;
  146. }
  147. }
  148. private class RightTeeIntersectionRuneResolver : IntersectionRuneResolver
  149. {
  150. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  151. {
  152. // TODO: Handle all relevant permutations of double lines into single lines
  153. // to make F type borders instead.
  154. return useDouble ? '╣' : driver.RightTee;
  155. }
  156. }
  157. private class BottomTeeIntersectionRuneResolver : IntersectionRuneResolver
  158. {
  159. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  160. {
  161. // TODO: Handle all relevant permutations of double lines into single lines
  162. // to make F type borders instead.
  163. return useDouble ? '╩' : driver.BottomTee;
  164. }
  165. }
  166. private class CrosshairIntersectionRuneResolver : IntersectionRuneResolver
  167. {
  168. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  169. {
  170. // TODO: Handle all relevant permutations of double lines into single lines
  171. // to make F type borders instead.
  172. return useDouble ? '╬' : '┼';
  173. }
  174. }
  175. private Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
  176. {
  177. if (!intersects.Any ())
  178. return null;
  179. var runeType = GetRuneTypeForIntersects (intersects);
  180. if(runeResolvers.ContainsKey (runeType)) {
  181. return runeResolvers [runeType].GetRuneForIntersects (driver, intersects);
  182. }
  183. // TODO: Remove these two once we have all of the below ported to IntersectionRuneResolvers
  184. var useDouble = intersects.Any (i => i.Line.Style == BorderStyle.Double && i.Line.Length != 0);
  185. var useRounded = intersects.Any (i => i.Line.Style == BorderStyle.Rounded && i.Line.Length != 0);
  186. // TODO: maybe make these resolvers to for simplicity?
  187. // or for dotted lines later on or that kind of thing?
  188. switch (runeType) {
  189. case IntersectionRuneType.None:
  190. return null;
  191. case IntersectionRuneType.Dot:
  192. return (Rune)'.';
  193. case IntersectionRuneType.HLine:
  194. return useDouble ? driver.HDLine : driver.HLine;
  195. case IntersectionRuneType.VLine:
  196. return useDouble ? driver.VDLine : driver.VLine;
  197. default: throw new Exception ("Could not find resolver or switch case for " + nameof (runeType) + ":" + runeType);
  198. }
  199. }
  200. private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition [] intersects)
  201. {
  202. if(intersects.All(i=>i.Line.Length == 0)) {
  203. return IntersectionRuneType.Dot;
  204. }
  205. // ignore dots
  206. intersects = intersects.Where (i => i.Type != IntersectionType.Dot).ToArray ();
  207. var set = new HashSet<IntersectionType> (intersects.Select (i => i.Type));
  208. #region Crosshair Conditions
  209. if (Has (set,
  210. IntersectionType.PassOverHorizontal,
  211. IntersectionType.PassOverVertical
  212. )) {
  213. return IntersectionRuneType.Crosshair;
  214. }
  215. if (Has (set,
  216. IntersectionType.PassOverVertical,
  217. IntersectionType.StartLeft,
  218. IntersectionType.StartRight
  219. )) {
  220. return IntersectionRuneType.Crosshair;
  221. }
  222. if (Has (set,
  223. IntersectionType.PassOverHorizontal,
  224. IntersectionType.StartUp,
  225. IntersectionType.StartDown
  226. )) {
  227. return IntersectionRuneType.Crosshair;
  228. }
  229. if (Has (set,
  230. IntersectionType.StartLeft,
  231. IntersectionType.StartRight,
  232. IntersectionType.StartUp,
  233. IntersectionType.StartDown)) {
  234. return IntersectionRuneType.Crosshair;
  235. }
  236. #endregion
  237. #region Corner Conditions
  238. if (Exactly (set,
  239. IntersectionType.StartRight,
  240. IntersectionType.StartDown)) {
  241. return IntersectionRuneType.ULCorner;
  242. }
  243. if (Exactly (set,
  244. IntersectionType.StartLeft,
  245. IntersectionType.StartDown)) {
  246. return IntersectionRuneType.URCorner;
  247. }
  248. if (Exactly (set,
  249. IntersectionType.StartUp,
  250. IntersectionType.StartLeft)) {
  251. return IntersectionRuneType.LRCorner;
  252. }
  253. if (Exactly (set,
  254. IntersectionType.StartUp,
  255. IntersectionType.StartRight)) {
  256. return IntersectionRuneType.LLCorner;
  257. }
  258. #endregion Corner Conditions
  259. #region T Conditions
  260. if (Has (set,
  261. IntersectionType.PassOverHorizontal,
  262. IntersectionType.StartDown)) {
  263. return IntersectionRuneType.TopTee;
  264. }
  265. if (Has (set,
  266. IntersectionType.StartRight,
  267. IntersectionType.StartLeft,
  268. IntersectionType.StartDown)) {
  269. return IntersectionRuneType.TopTee;
  270. }
  271. if (Has (set,
  272. IntersectionType.PassOverHorizontal,
  273. IntersectionType.StartUp)) {
  274. return IntersectionRuneType.BottomTee;
  275. }
  276. if (Has (set,
  277. IntersectionType.StartRight,
  278. IntersectionType.StartLeft,
  279. IntersectionType.StartUp)) {
  280. return IntersectionRuneType.BottomTee;
  281. }
  282. if (Has (set,
  283. IntersectionType.PassOverVertical,
  284. IntersectionType.StartRight)) {
  285. return IntersectionRuneType.LeftTee;
  286. }
  287. if (Has (set,
  288. IntersectionType.StartRight,
  289. IntersectionType.StartDown,
  290. IntersectionType.StartUp)) {
  291. return IntersectionRuneType.LeftTee;
  292. }
  293. if (Has (set,
  294. IntersectionType.PassOverVertical,
  295. IntersectionType.StartLeft)) {
  296. return IntersectionRuneType.RightTee;
  297. }
  298. if (Has (set,
  299. IntersectionType.StartLeft,
  300. IntersectionType.StartDown,
  301. IntersectionType.StartUp)) {
  302. return IntersectionRuneType.RightTee;
  303. }
  304. #endregion
  305. if (All (intersects, Orientation.Horizontal)) {
  306. return IntersectionRuneType.HLine;
  307. }
  308. if (All (intersects, Orientation.Vertical)) {
  309. return IntersectionRuneType.VLine;
  310. }
  311. return IntersectionRuneType.Dot;
  312. }
  313. private bool All (IntersectionDefinition [] intersects, Orientation orientation)
  314. {
  315. return intersects.All (i => i.Line.Orientation == orientation);
  316. }
  317. /// <summary>
  318. /// Returns true if the <paramref name="intersects"/> collection has all the <paramref name="types"/>
  319. /// specified (i.e. AND).
  320. /// </summary>
  321. /// <param name="intersects"></param>
  322. /// <param name="types"></param>
  323. /// <returns></returns>
  324. private bool Has (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  325. {
  326. return types.All (t => intersects.Contains (t));
  327. }
  328. /// <summary>
  329. /// Returns true if all requested <paramref name="types"/> appear in <paramref name="intersects"/>
  330. /// and there are no additional <see cref="IntersectionRuneType"/>
  331. /// </summary>
  332. /// <param name="intersects"></param>
  333. /// <param name="types"></param>
  334. /// <returns></returns>
  335. private bool Exactly (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  336. {
  337. return intersects.SetEquals (types);
  338. }
  339. class IntersectionDefinition {
  340. /// <summary>
  341. /// The point at which the intersection happens
  342. /// </summary>
  343. public Point Point { get; }
  344. /// <summary>
  345. /// Defines how <see cref="Line"/> position relates
  346. /// to <see cref="Point"/>.
  347. /// </summary>
  348. public IntersectionType Type { get; }
  349. /// <summary>
  350. /// The line that intersects <see cref="Point"/>
  351. /// </summary>
  352. public StraightLine Line { get; }
  353. public IntersectionDefinition (Point point, IntersectionType type, StraightLine line)
  354. {
  355. Point = point;
  356. Type = type;
  357. Line = line;
  358. }
  359. }
  360. /// <summary>
  361. /// The type of Rune that we will use before considering
  362. /// double width, curved borders etc
  363. /// </summary>
  364. enum IntersectionRuneType {
  365. None,
  366. Dot,
  367. ULCorner,
  368. URCorner,
  369. LLCorner,
  370. LRCorner,
  371. TopTee,
  372. BottomTee,
  373. RightTee,
  374. LeftTee,
  375. Crosshair,
  376. HLine,
  377. VLine,
  378. }
  379. enum IntersectionType {
  380. /// <summary>
  381. /// There is no intersection
  382. /// </summary>
  383. None,
  384. /// <summary>
  385. /// A line passes directly over this point traveling along
  386. /// the horizontal axis
  387. /// </summary>
  388. PassOverHorizontal,
  389. /// <summary>
  390. /// A line passes directly over this point traveling along
  391. /// the vertical axis
  392. /// </summary>
  393. PassOverVertical,
  394. /// <summary>
  395. /// A line starts at this point and is traveling up
  396. /// </summary>
  397. StartUp,
  398. /// <summary>
  399. /// A line starts at this point and is traveling right
  400. /// </summary>
  401. StartRight,
  402. /// <summary>
  403. /// A line starts at this point and is traveling down
  404. /// </summary>
  405. StartDown,
  406. /// <summary>
  407. /// A line starts at this point and is traveling left
  408. /// </summary>
  409. StartLeft,
  410. /// <summary>
  411. /// A line exists at this point who has 0 length
  412. /// </summary>
  413. Dot
  414. }
  415. class StraightLine {
  416. public Point Start { get; }
  417. public int Length { get; }
  418. public Orientation Orientation { get; }
  419. public BorderStyle Style { get; }
  420. public StraightLine (Point start, int length, Orientation orientation, BorderStyle style)
  421. {
  422. this.Start = start;
  423. this.Length = length;
  424. this.Orientation = orientation;
  425. this.Style = style;
  426. }
  427. internal IntersectionDefinition Intersects (int x, int y)
  428. {
  429. if (IsDot ()) {
  430. if (StartsAt (x, y)) {
  431. return new IntersectionDefinition (Start, IntersectionType.Dot, this);
  432. } else {
  433. return null;
  434. }
  435. }
  436. switch (Orientation) {
  437. case Orientation.Horizontal: return IntersectsHorizontally (x, y);
  438. case Orientation.Vertical: return IntersectsVertically (x, y);
  439. default: throw new ArgumentOutOfRangeException (nameof (Orientation));
  440. }
  441. }
  442. private IntersectionDefinition IntersectsHorizontally (int x, int y)
  443. {
  444. if (Start.Y != y) {
  445. return null;
  446. } else {
  447. if (StartsAt (x, y)) {
  448. return new IntersectionDefinition (
  449. Start,
  450. Length < 0 ? IntersectionType.StartLeft : IntersectionType.StartRight,
  451. this
  452. );
  453. }
  454. if (EndsAt (x, y)) {
  455. return new IntersectionDefinition (
  456. Start,
  457. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  458. this
  459. );
  460. } else {
  461. var xmin = Math.Min (Start.X, Start.X + Length);
  462. var xmax = Math.Max (Start.X, Start.X + Length);
  463. if (xmin < x && xmax > x) {
  464. return new IntersectionDefinition (
  465. new Point (x, y),
  466. IntersectionType.PassOverHorizontal,
  467. this
  468. );
  469. }
  470. }
  471. return null;
  472. }
  473. }
  474. private IntersectionDefinition IntersectsVertically (int x, int y)
  475. {
  476. if (Start.X != x) {
  477. return null;
  478. } else {
  479. if (StartsAt (x, y)) {
  480. return new IntersectionDefinition (
  481. Start,
  482. Length < 0 ? IntersectionType.StartUp : IntersectionType.StartDown,
  483. this
  484. );
  485. }
  486. if (EndsAt (x, y)) {
  487. return new IntersectionDefinition (
  488. Start,
  489. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  490. this
  491. );
  492. } else {
  493. var ymin = Math.Min (Start.Y, Start.Y + Length);
  494. var ymax = Math.Max (Start.Y, Start.Y + Length);
  495. if (ymin < y && ymax > y) {
  496. return new IntersectionDefinition (
  497. new Point (x, y),
  498. IntersectionType.PassOverVertical,
  499. this
  500. );
  501. }
  502. }
  503. return null;
  504. }
  505. }
  506. private bool EndsAt (int x, int y)
  507. {
  508. if (Orientation == Orientation.Horizontal) {
  509. return Start.X + Length == x && Start.Y == y;
  510. }
  511. return Start.X == x && Start.Y + Length == y;
  512. }
  513. private bool StartsAt (int x, int y)
  514. {
  515. return Start.X == x && Start.Y == y;
  516. }
  517. private bool IsDot ()
  518. {
  519. return Length == 0;
  520. }
  521. }
  522. }
  523. }