LineCanvas.cs 16 KB

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