LineCanvas.cs 16 KB

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