LineCanvas.cs 16 KB

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