LineCanvas.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 abstract class CornerIntersectionRuneResolver : IntersectionRuneResolver {
  95. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  96. {
  97. bool doubleHorizontal = intersects.Any(l=>l.Line.Orientation == Orientation.Horizontal && l.Line.Style == BorderStyle.Double);
  98. bool doubleVertical = intersects.Any(l=>l.Line.Orientation == Orientation.Vertical && l.Line.Style == BorderStyle.Double);
  99. return GetRuneForIntersects(driver,useRounded,doubleHorizontal,doubleVertical);
  100. }
  101. protected abstract Rune? GetRuneForIntersects(ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical);
  102. }
  103. private class ULIntersectionRuneResolver : CornerIntersectionRuneResolver
  104. {
  105. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical)
  106. {
  107. if(doubleHorizontal)
  108. {
  109. return doubleVertical ? driver.ULDCorner : '╒';
  110. }
  111. if(doubleVertical)
  112. {
  113. return '╓';
  114. }
  115. return useRounded ? driver.ULRCorner : driver.ULCorner;
  116. }
  117. }
  118. private class URIntersectionRuneResolver : CornerIntersectionRuneResolver
  119. {
  120. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical)
  121. {
  122. if(doubleHorizontal)
  123. {
  124. return doubleVertical ? driver.URDCorner : '╕';
  125. }
  126. if(doubleVertical)
  127. {
  128. return '╖';
  129. }
  130. return useRounded ? driver.URRCorner : driver.URCorner;
  131. }
  132. }
  133. private class LLIntersectionRuneResolver : CornerIntersectionRuneResolver
  134. {
  135. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical)
  136. {
  137. if(doubleHorizontal)
  138. {
  139. return doubleVertical ? driver.LLDCorner : '╘';
  140. }
  141. if(doubleVertical)
  142. {
  143. return '╙';
  144. }
  145. return useRounded ? driver.LLRCorner : driver.LLCorner;
  146. }
  147. }
  148. private class LRIntersectionRuneResolver : CornerIntersectionRuneResolver
  149. {
  150. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, bool useRounded, bool doubleHorizontal, bool doubleVertical)
  151. {
  152. if(doubleHorizontal)
  153. {
  154. return doubleVertical ? driver.LRDCorner : '╛';
  155. }
  156. if(doubleVertical)
  157. {
  158. return '╜';
  159. }
  160. return useRounded ? driver.LRRCorner : driver.LRCorner;
  161. }
  162. }
  163. private class TopTeeIntersectionRuneResolver : IntersectionRuneResolver
  164. {
  165. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  166. {
  167. // TODO: Handle all relevant permutations of double lines into single lines
  168. // to make F type borders instead.
  169. return useDouble ? '╦' : driver.TopTee;
  170. }
  171. }
  172. private class LeftTeeIntersectionRuneResolver : IntersectionRuneResolver
  173. {
  174. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  175. {
  176. // TODO: Handle all relevant permutations of double lines into single lines
  177. // to make F type borders instead.
  178. return useDouble ? '╠' : driver.LeftTee;
  179. }
  180. }
  181. private class RightTeeIntersectionRuneResolver : IntersectionRuneResolver
  182. {
  183. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  184. {
  185. // TODO: Handle all relevant permutations of double lines into single lines
  186. // to make F type borders instead.
  187. return useDouble ? '╣' : driver.RightTee;
  188. }
  189. }
  190. private class BottomTeeIntersectionRuneResolver : IntersectionRuneResolver
  191. {
  192. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  193. {
  194. // TODO: Handle all relevant permutations of double lines into single lines
  195. // to make F type borders instead.
  196. return useDouble ? '╩' : driver.BottomTee;
  197. }
  198. }
  199. private class CrosshairIntersectionRuneResolver : IntersectionRuneResolver
  200. {
  201. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  202. {
  203. // TODO: Handle all relevant permutations of double lines into single lines
  204. // to make F type borders instead.
  205. return useDouble ? '╬' : '┼';
  206. }
  207. }
  208. private Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
  209. {
  210. if (!intersects.Any ())
  211. return null;
  212. var runeType = GetRuneTypeForIntersects (intersects);
  213. if(runeResolvers.ContainsKey (runeType)) {
  214. return runeResolvers [runeType].GetRuneForIntersects (driver, intersects);
  215. }
  216. // TODO: Remove these two once we have all of the below ported to IntersectionRuneResolvers
  217. var useDouble = intersects.Any (i => i.Line.Style == BorderStyle.Double && i.Line.Length != 0);
  218. var useRounded = intersects.Any (i => i.Line.Style == BorderStyle.Rounded && i.Line.Length != 0);
  219. // TODO: maybe make these resolvers to for simplicity?
  220. // or for dotted lines later on or that kind of thing?
  221. switch (runeType) {
  222. case IntersectionRuneType.None:
  223. return null;
  224. case IntersectionRuneType.Dot:
  225. return (Rune)'.';
  226. case IntersectionRuneType.HLine:
  227. return useDouble ? driver.HDLine : driver.HLine;
  228. case IntersectionRuneType.VLine:
  229. return useDouble ? driver.VDLine : driver.VLine;
  230. default: throw new Exception ("Could not find resolver or switch case for " + nameof (runeType) + ":" + runeType);
  231. }
  232. }
  233. private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition [] intersects)
  234. {
  235. if(intersects.All(i=>i.Line.Length == 0)) {
  236. return IntersectionRuneType.Dot;
  237. }
  238. // ignore dots
  239. intersects = intersects.Where (i => i.Type != IntersectionType.Dot).ToArray ();
  240. var set = new HashSet<IntersectionType> (intersects.Select (i => i.Type));
  241. #region Crosshair Conditions
  242. if (Has (set,
  243. IntersectionType.PassOverHorizontal,
  244. IntersectionType.PassOverVertical
  245. )) {
  246. return IntersectionRuneType.Crosshair;
  247. }
  248. if (Has (set,
  249. IntersectionType.PassOverVertical,
  250. IntersectionType.StartLeft,
  251. IntersectionType.StartRight
  252. )) {
  253. return IntersectionRuneType.Crosshair;
  254. }
  255. if (Has (set,
  256. IntersectionType.PassOverHorizontal,
  257. IntersectionType.StartUp,
  258. IntersectionType.StartDown
  259. )) {
  260. return IntersectionRuneType.Crosshair;
  261. }
  262. if (Has (set,
  263. IntersectionType.StartLeft,
  264. IntersectionType.StartRight,
  265. IntersectionType.StartUp,
  266. IntersectionType.StartDown)) {
  267. return IntersectionRuneType.Crosshair;
  268. }
  269. #endregion
  270. #region Corner Conditions
  271. if (Exactly (set,
  272. IntersectionType.StartRight,
  273. IntersectionType.StartDown)) {
  274. return IntersectionRuneType.ULCorner;
  275. }
  276. if (Exactly (set,
  277. IntersectionType.StartLeft,
  278. IntersectionType.StartDown)) {
  279. return IntersectionRuneType.URCorner;
  280. }
  281. if (Exactly (set,
  282. IntersectionType.StartUp,
  283. IntersectionType.StartLeft)) {
  284. return IntersectionRuneType.LRCorner;
  285. }
  286. if (Exactly (set,
  287. IntersectionType.StartUp,
  288. IntersectionType.StartRight)) {
  289. return IntersectionRuneType.LLCorner;
  290. }
  291. #endregion Corner Conditions
  292. #region T Conditions
  293. if (Has (set,
  294. IntersectionType.PassOverHorizontal,
  295. IntersectionType.StartDown)) {
  296. return IntersectionRuneType.TopTee;
  297. }
  298. if (Has (set,
  299. IntersectionType.StartRight,
  300. IntersectionType.StartLeft,
  301. IntersectionType.StartDown)) {
  302. return IntersectionRuneType.TopTee;
  303. }
  304. if (Has (set,
  305. IntersectionType.PassOverHorizontal,
  306. IntersectionType.StartUp)) {
  307. return IntersectionRuneType.BottomTee;
  308. }
  309. if (Has (set,
  310. IntersectionType.StartRight,
  311. IntersectionType.StartLeft,
  312. IntersectionType.StartUp)) {
  313. return IntersectionRuneType.BottomTee;
  314. }
  315. if (Has (set,
  316. IntersectionType.PassOverVertical,
  317. IntersectionType.StartRight)) {
  318. return IntersectionRuneType.LeftTee;
  319. }
  320. if (Has (set,
  321. IntersectionType.StartRight,
  322. IntersectionType.StartDown,
  323. IntersectionType.StartUp)) {
  324. return IntersectionRuneType.LeftTee;
  325. }
  326. if (Has (set,
  327. IntersectionType.PassOverVertical,
  328. IntersectionType.StartLeft)) {
  329. return IntersectionRuneType.RightTee;
  330. }
  331. if (Has (set,
  332. IntersectionType.StartLeft,
  333. IntersectionType.StartDown,
  334. IntersectionType.StartUp)) {
  335. return IntersectionRuneType.RightTee;
  336. }
  337. #endregion
  338. if (All (intersects, Orientation.Horizontal)) {
  339. return IntersectionRuneType.HLine;
  340. }
  341. if (All (intersects, Orientation.Vertical)) {
  342. return IntersectionRuneType.VLine;
  343. }
  344. return IntersectionRuneType.Dot;
  345. }
  346. private bool All (IntersectionDefinition [] intersects, Orientation orientation)
  347. {
  348. return intersects.All (i => i.Line.Orientation == orientation);
  349. }
  350. /// <summary>
  351. /// Returns true if the <paramref name="intersects"/> collection has all the <paramref name="types"/>
  352. /// specified (i.e. AND).
  353. /// </summary>
  354. /// <param name="intersects"></param>
  355. /// <param name="types"></param>
  356. /// <returns></returns>
  357. private bool Has (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  358. {
  359. return types.All (t => intersects.Contains (t));
  360. }
  361. /// <summary>
  362. /// Returns true if all requested <paramref name="types"/> appear in <paramref name="intersects"/>
  363. /// and there are no additional <see cref="IntersectionRuneType"/>
  364. /// </summary>
  365. /// <param name="intersects"></param>
  366. /// <param name="types"></param>
  367. /// <returns></returns>
  368. private bool Exactly (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  369. {
  370. return intersects.SetEquals (types);
  371. }
  372. class IntersectionDefinition {
  373. /// <summary>
  374. /// The point at which the intersection happens
  375. /// </summary>
  376. public Point Point { get; }
  377. /// <summary>
  378. /// Defines how <see cref="Line"/> position relates
  379. /// to <see cref="Point"/>.
  380. /// </summary>
  381. public IntersectionType Type { get; }
  382. /// <summary>
  383. /// The line that intersects <see cref="Point"/>
  384. /// </summary>
  385. public StraightLine Line { get; }
  386. public IntersectionDefinition (Point point, IntersectionType type, StraightLine line)
  387. {
  388. Point = point;
  389. Type = type;
  390. Line = line;
  391. }
  392. }
  393. /// <summary>
  394. /// The type of Rune that we will use before considering
  395. /// double width, curved borders etc
  396. /// </summary>
  397. enum IntersectionRuneType {
  398. None,
  399. Dot,
  400. ULCorner,
  401. URCorner,
  402. LLCorner,
  403. LRCorner,
  404. TopTee,
  405. BottomTee,
  406. RightTee,
  407. LeftTee,
  408. Crosshair,
  409. HLine,
  410. VLine,
  411. }
  412. enum IntersectionType {
  413. /// <summary>
  414. /// There is no intersection
  415. /// </summary>
  416. None,
  417. /// <summary>
  418. /// A line passes directly over this point traveling along
  419. /// the horizontal axis
  420. /// </summary>
  421. PassOverHorizontal,
  422. /// <summary>
  423. /// A line passes directly over this point traveling along
  424. /// the vertical axis
  425. /// </summary>
  426. PassOverVertical,
  427. /// <summary>
  428. /// A line starts at this point and is traveling up
  429. /// </summary>
  430. StartUp,
  431. /// <summary>
  432. /// A line starts at this point and is traveling right
  433. /// </summary>
  434. StartRight,
  435. /// <summary>
  436. /// A line starts at this point and is traveling down
  437. /// </summary>
  438. StartDown,
  439. /// <summary>
  440. /// A line starts at this point and is traveling left
  441. /// </summary>
  442. StartLeft,
  443. /// <summary>
  444. /// A line exists at this point who has 0 length
  445. /// </summary>
  446. Dot
  447. }
  448. class StraightLine {
  449. public Point Start { get; }
  450. public int Length { get; }
  451. public Orientation Orientation { get; }
  452. public BorderStyle Style { get; }
  453. public StraightLine (Point start, int length, Orientation orientation, BorderStyle style)
  454. {
  455. this.Start = start;
  456. this.Length = length;
  457. this.Orientation = orientation;
  458. this.Style = style;
  459. }
  460. internal IntersectionDefinition Intersects (int x, int y)
  461. {
  462. if (IsDot ()) {
  463. if (StartsAt (x, y)) {
  464. return new IntersectionDefinition (Start, IntersectionType.Dot, this);
  465. } else {
  466. return null;
  467. }
  468. }
  469. switch (Orientation) {
  470. case Orientation.Horizontal: return IntersectsHorizontally (x, y);
  471. case Orientation.Vertical: return IntersectsVertically (x, y);
  472. default: throw new ArgumentOutOfRangeException (nameof (Orientation));
  473. }
  474. }
  475. private IntersectionDefinition IntersectsHorizontally (int x, int y)
  476. {
  477. if (Start.Y != y) {
  478. return null;
  479. } else {
  480. if (StartsAt (x, y)) {
  481. return new IntersectionDefinition (
  482. Start,
  483. Length < 0 ? IntersectionType.StartLeft : IntersectionType.StartRight,
  484. this
  485. );
  486. }
  487. if (EndsAt (x, y)) {
  488. return new IntersectionDefinition (
  489. Start,
  490. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  491. this
  492. );
  493. } else {
  494. var xmin = Math.Min (Start.X, Start.X + Length);
  495. var xmax = Math.Max (Start.X, Start.X + Length);
  496. if (xmin < x && xmax > x) {
  497. return new IntersectionDefinition (
  498. new Point (x, y),
  499. IntersectionType.PassOverHorizontal,
  500. this
  501. );
  502. }
  503. }
  504. return null;
  505. }
  506. }
  507. private IntersectionDefinition IntersectsVertically (int x, int y)
  508. {
  509. if (Start.X != x) {
  510. return null;
  511. } else {
  512. if (StartsAt (x, y)) {
  513. return new IntersectionDefinition (
  514. Start,
  515. Length < 0 ? IntersectionType.StartUp : IntersectionType.StartDown,
  516. this
  517. );
  518. }
  519. if (EndsAt (x, y)) {
  520. return new IntersectionDefinition (
  521. Start,
  522. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  523. this
  524. );
  525. } else {
  526. var ymin = Math.Min (Start.Y, Start.Y + Length);
  527. var ymax = Math.Max (Start.Y, Start.Y + Length);
  528. if (ymin < y && ymax > y) {
  529. return new IntersectionDefinition (
  530. new Point (x, y),
  531. IntersectionType.PassOverVertical,
  532. this
  533. );
  534. }
  535. }
  536. return null;
  537. }
  538. }
  539. private bool EndsAt (int x, int y)
  540. {
  541. if (Orientation == Orientation.Horizontal) {
  542. return Start.X + Length == x && Start.Y == y;
  543. }
  544. return Start.X == x && Start.Y + Length == y;
  545. }
  546. private bool StartsAt (int x, int y)
  547. {
  548. return Start.X == x && Start.Y == y;
  549. }
  550. private bool IsDot ()
  551. {
  552. return Length == 0;
  553. }
  554. }
  555. }
  556. }