LineCanvas.cs 16 KB

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