LineCanvas.cs 16 KB

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