LineCanvas.cs 17 KB

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