LineCanvas.cs 16 KB

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