LineCanvas.cs 16 KB

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