LineCanvas.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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.Y + inArea.Height; y++) {
  55. for (int x = inArea.X; x < inArea.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: Support ruler
  164. //var useRuler = intersects.Any (i => i.Line.Style == BorderStyle.Ruler && i.Line.Length != 0);
  165. // TODO: maybe make these resolvers to for simplicity?
  166. // or for dotted lines later on or that kind of thing?
  167. switch (runeType) {
  168. case IntersectionRuneType.None:
  169. return null;
  170. case IntersectionRuneType.Dot:
  171. return (Rune)'.';
  172. case IntersectionRuneType.HLine:
  173. return useDouble ? driver.HDLine : driver.HLine;
  174. case IntersectionRuneType.VLine:
  175. return useDouble ? driver.VDLine : driver.VLine;
  176. default: throw new Exception ("Could not find resolver or switch case for " + nameof (runeType) + ":" + runeType);
  177. }
  178. }
  179. private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition [] intersects)
  180. {
  181. if (intersects.All (i => i.Line.Length == 0)) {
  182. return IntersectionRuneType.Dot;
  183. }
  184. // ignore dots
  185. intersects = intersects.Where (i => i.Type != IntersectionType.Dot).ToArray ();
  186. var set = new HashSet<IntersectionType> (intersects.Select (i => i.Type));
  187. #region Crosshair Conditions
  188. if (Has (set,
  189. IntersectionType.PassOverHorizontal,
  190. IntersectionType.PassOverVertical
  191. )) {
  192. return IntersectionRuneType.Crosshair;
  193. }
  194. if (Has (set,
  195. IntersectionType.PassOverVertical,
  196. IntersectionType.StartLeft,
  197. IntersectionType.StartRight
  198. )) {
  199. return IntersectionRuneType.Crosshair;
  200. }
  201. if (Has (set,
  202. IntersectionType.PassOverHorizontal,
  203. IntersectionType.StartUp,
  204. IntersectionType.StartDown
  205. )) {
  206. return IntersectionRuneType.Crosshair;
  207. }
  208. if (Has (set,
  209. IntersectionType.StartLeft,
  210. IntersectionType.StartRight,
  211. IntersectionType.StartUp,
  212. IntersectionType.StartDown)) {
  213. return IntersectionRuneType.Crosshair;
  214. }
  215. #endregion
  216. #region Corner Conditions
  217. if (Exactly (set,
  218. IntersectionType.StartRight,
  219. IntersectionType.StartDown)) {
  220. return IntersectionRuneType.ULCorner;
  221. }
  222. if (Exactly (set,
  223. IntersectionType.StartLeft,
  224. IntersectionType.StartDown)) {
  225. return IntersectionRuneType.URCorner;
  226. }
  227. if (Exactly (set,
  228. IntersectionType.StartUp,
  229. IntersectionType.StartLeft)) {
  230. return IntersectionRuneType.LRCorner;
  231. }
  232. if (Exactly (set,
  233. IntersectionType.StartUp,
  234. IntersectionType.StartRight)) {
  235. return IntersectionRuneType.LLCorner;
  236. }
  237. #endregion Corner Conditions
  238. #region T Conditions
  239. if (Has (set,
  240. IntersectionType.PassOverHorizontal,
  241. IntersectionType.StartDown)) {
  242. return IntersectionRuneType.TopTee;
  243. }
  244. if (Has (set,
  245. IntersectionType.StartRight,
  246. IntersectionType.StartLeft,
  247. IntersectionType.StartDown)) {
  248. return IntersectionRuneType.TopTee;
  249. }
  250. if (Has (set,
  251. IntersectionType.PassOverHorizontal,
  252. IntersectionType.StartUp)) {
  253. return IntersectionRuneType.BottomTee;
  254. }
  255. if (Has (set,
  256. IntersectionType.StartRight,
  257. IntersectionType.StartLeft,
  258. IntersectionType.StartUp)) {
  259. return IntersectionRuneType.BottomTee;
  260. }
  261. if (Has (set,
  262. IntersectionType.PassOverVertical,
  263. IntersectionType.StartRight)) {
  264. return IntersectionRuneType.LeftTee;
  265. }
  266. if (Has (set,
  267. IntersectionType.StartRight,
  268. IntersectionType.StartDown,
  269. IntersectionType.StartUp)) {
  270. return IntersectionRuneType.LeftTee;
  271. }
  272. if (Has (set,
  273. IntersectionType.PassOverVertical,
  274. IntersectionType.StartLeft)) {
  275. return IntersectionRuneType.RightTee;
  276. }
  277. if (Has (set,
  278. IntersectionType.StartLeft,
  279. IntersectionType.StartDown,
  280. IntersectionType.StartUp)) {
  281. return IntersectionRuneType.RightTee;
  282. }
  283. #endregion
  284. if (All (intersects, Orientation.Horizontal)) {
  285. return IntersectionRuneType.HLine;
  286. }
  287. if (All (intersects, Orientation.Vertical)) {
  288. return IntersectionRuneType.VLine;
  289. }
  290. return IntersectionRuneType.Dot;
  291. }
  292. private bool All (IntersectionDefinition [] intersects, Orientation orientation)
  293. {
  294. return intersects.All (i => i.Line.Orientation == orientation);
  295. }
  296. /// <summary>
  297. /// Returns true if the <paramref name="intersects"/> collection has all the <paramref name="types"/>
  298. /// specified (i.e. AND).
  299. /// </summary>
  300. /// <param name="intersects"></param>
  301. /// <param name="types"></param>
  302. /// <returns></returns>
  303. private bool Has (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  304. {
  305. return types.All (t => intersects.Contains (t));
  306. }
  307. /// <summary>
  308. /// Returns true if all requested <paramref name="types"/> appear in <paramref name="intersects"/>
  309. /// and there are no additional <see cref="IntersectionRuneType"/>
  310. /// </summary>
  311. /// <param name="intersects"></param>
  312. /// <param name="types"></param>
  313. /// <returns></returns>
  314. private bool Exactly (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  315. {
  316. return intersects.SetEquals (types);
  317. }
  318. class IntersectionDefinition {
  319. /// <summary>
  320. /// The point at which the intersection happens
  321. /// </summary>
  322. public Point Point { get; }
  323. /// <summary>
  324. /// Defines how <see cref="Line"/> position relates
  325. /// to <see cref="Point"/>.
  326. /// </summary>
  327. public IntersectionType Type { get; }
  328. /// <summary>
  329. /// The line that intersects <see cref="Point"/>
  330. /// </summary>
  331. public StraightLine Line { get; }
  332. public IntersectionDefinition (Point point, IntersectionType type, StraightLine line)
  333. {
  334. Point = point;
  335. Type = type;
  336. Line = line;
  337. }
  338. }
  339. /// <summary>
  340. /// The type of Rune that we will use before considering
  341. /// double width, curved borders etc
  342. /// </summary>
  343. enum IntersectionRuneType {
  344. None,
  345. Dot,
  346. ULCorner,
  347. URCorner,
  348. LLCorner,
  349. LRCorner,
  350. TopTee,
  351. BottomTee,
  352. RightTee,
  353. LeftTee,
  354. Crosshair,
  355. HLine,
  356. VLine,
  357. }
  358. enum IntersectionType {
  359. /// <summary>
  360. /// There is no intersection
  361. /// </summary>
  362. None,
  363. /// <summary>
  364. /// A line passes directly over this point traveling along
  365. /// the horizontal axis
  366. /// </summary>
  367. PassOverHorizontal,
  368. /// <summary>
  369. /// A line passes directly over this point traveling along
  370. /// the vertical axis
  371. /// </summary>
  372. PassOverVertical,
  373. /// <summary>
  374. /// A line starts at this point and is traveling up
  375. /// </summary>
  376. StartUp,
  377. /// <summary>
  378. /// A line starts at this point and is traveling right
  379. /// </summary>
  380. StartRight,
  381. /// <summary>
  382. /// A line starts at this point and is traveling down
  383. /// </summary>
  384. StartDown,
  385. /// <summary>
  386. /// A line starts at this point and is traveling left
  387. /// </summary>
  388. StartLeft,
  389. /// <summary>
  390. /// A line exists at this point who has 0 length
  391. /// </summary>
  392. Dot
  393. }
  394. class StraightLine {
  395. public Point Start { get; }
  396. public int Length { get; }
  397. public Orientation Orientation { get; }
  398. public BorderStyle Style { get; }
  399. public StraightLine (Point start, int length, Orientation orientation, BorderStyle style)
  400. {
  401. this.Start = start;
  402. this.Length = length;
  403. this.Orientation = orientation;
  404. this.Style = style;
  405. }
  406. internal IntersectionDefinition Intersects (int x, int y)
  407. {
  408. if (IsDot ()) {
  409. if (StartsAt (x, y)) {
  410. return new IntersectionDefinition (Start, IntersectionType.Dot, this);
  411. } else {
  412. return null;
  413. }
  414. }
  415. switch (Orientation) {
  416. case Orientation.Horizontal: return IntersectsHorizontally (x, y);
  417. case Orientation.Vertical: return IntersectsVertically (x, y);
  418. default: throw new ArgumentOutOfRangeException (nameof (Orientation));
  419. }
  420. }
  421. private IntersectionDefinition IntersectsHorizontally (int x, int y)
  422. {
  423. if (Start.Y != y) {
  424. return null;
  425. } else {
  426. if (StartsAt (x, y)) {
  427. return new IntersectionDefinition (
  428. Start,
  429. Length < 0 ? IntersectionType.StartLeft : IntersectionType.StartRight,
  430. this
  431. );
  432. }
  433. if (EndsAt (x, y)) {
  434. return new IntersectionDefinition (
  435. Start,
  436. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  437. this
  438. );
  439. } else {
  440. var xmin = Math.Min (Start.X, Start.X + Length);
  441. var xmax = Math.Max (Start.X, Start.X + Length);
  442. if (xmin < x && xmax > x) {
  443. return new IntersectionDefinition (
  444. new Point (x, y),
  445. IntersectionType.PassOverHorizontal,
  446. this
  447. );
  448. }
  449. }
  450. return null;
  451. }
  452. }
  453. private IntersectionDefinition IntersectsVertically (int x, int y)
  454. {
  455. if (Start.X != x) {
  456. return null;
  457. } else {
  458. if (StartsAt (x, y)) {
  459. return new IntersectionDefinition (
  460. Start,
  461. Length < 0 ? IntersectionType.StartUp : IntersectionType.StartDown,
  462. this
  463. );
  464. }
  465. if (EndsAt (x, y)) {
  466. return new IntersectionDefinition (
  467. Start,
  468. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  469. this
  470. );
  471. } else {
  472. var ymin = Math.Min (Start.Y, Start.Y + Length);
  473. var ymax = Math.Max (Start.Y, Start.Y + Length);
  474. if (ymin < y && ymax > y) {
  475. return new IntersectionDefinition (
  476. new Point (x, y),
  477. IntersectionType.PassOverVertical,
  478. this
  479. );
  480. }
  481. }
  482. return null;
  483. }
  484. }
  485. private bool EndsAt (int x, int y)
  486. {
  487. if (Orientation == Orientation.Horizontal) {
  488. return Start.X + Length == x && Start.Y == y;
  489. }
  490. return Start.X == x && Start.Y + Length == y;
  491. }
  492. private bool StartsAt (int x, int y)
  493. {
  494. return Start.X == x && Start.Y == y;
  495. }
  496. private bool IsDot ()
  497. {
  498. return Length == 0;
  499. }
  500. }
  501. }
  502. }