LineCanvas.cs 16 KB

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