LineCanvas.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. // TODO: Add other resolvers
  14. };
  15. /// <summary>
  16. /// Add a new line to the canvas starting at <paramref name="from"/>.
  17. /// Use positive <paramref name="length"/> for Right and negative for Left
  18. /// when <see cref="Orientation"/> is <see cref="Orientation.Horizontal"/>.
  19. /// Use positive <paramref name="length"/> for Down and negative for Up
  20. /// when <see cref="Orientation"/> is <see cref="Orientation.Vertical"/>.
  21. /// </summary>
  22. /// <param name="from">Starting point.</param>
  23. /// <param name="length">Length of line. 0 for a dot.
  24. /// Positive for Down/Right. Negative for Up/Left.</param>
  25. /// <param name="orientation">Direction of the line.</param>
  26. /// <param name="style">The style of line to use</param>
  27. public void AddLine (Point from, int length, Orientation orientation, BorderStyle style)
  28. {
  29. lines.Add (new StraightLine (from, length, orientation, style));
  30. }
  31. /// <summary>
  32. /// Evaluate all currently defined lines that lie within
  33. /// <paramref name="inArea"/> and generate a 'bitmap' that
  34. /// shows what characters (if any) should be rendered at each
  35. /// point so that all lines connect up correctly with appropriate
  36. /// intersection symbols.
  37. /// <returns></returns>
  38. /// </summary>
  39. /// <param name="inArea"></param>
  40. /// <returns>Map as 2D array where first index is rows and second is column</returns>
  41. public Rune? [,] GenerateImage (Rect inArea)
  42. {
  43. Rune? [,] canvas = new Rune? [inArea.Height, inArea.Width];
  44. // walk through each pixel of the bitmap
  45. for (int y = 0; y < inArea.Height; y++) {
  46. for (int x = 0; x < inArea.Width; x++) {
  47. var intersects = lines
  48. .Select (l => l.Intersects (x, y))
  49. .Where (i => i != null)
  50. .ToArray ();
  51. // TODO: use Driver and LineStyle to map
  52. canvas [y, x] = GetRuneForIntersects (Application.Driver, intersects);
  53. }
  54. }
  55. return canvas;
  56. }
  57. /// <summary>
  58. /// Draws all the lines that lie within the <paramref name="bounds"/> onto
  59. /// the <paramref name="view"/> client area. This method should be called from
  60. /// <see cref="View.Redraw(Rect)"/>.
  61. /// </summary>
  62. /// <param name="view"></param>
  63. /// <param name="bounds"></param>
  64. public void Draw (View view, Rect bounds)
  65. {
  66. var runes = GenerateImage (bounds);
  67. for (int y = bounds.Y; y < bounds.Height; y++) {
  68. for (int x = bounds.X; x < bounds.Width; x++) {
  69. var rune = runes [y, x];
  70. if (rune.HasValue) {
  71. view.AddRune (x, y, rune.Value);
  72. }
  73. }
  74. }
  75. }
  76. private abstract class IntersectionRuneResolver
  77. {
  78. public Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
  79. {
  80. var useDouble = intersects.Any (i => i.Line.Style == BorderStyle.Double && i.Line.Length != 0);
  81. var useRounded = intersects.Any (i => i.Line.Style == BorderStyle.Rounded && i.Line.Length != 0);
  82. return GetRuneForIntersects (driver, intersects, useDouble, useRounded);
  83. }
  84. protected abstract Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded);
  85. }
  86. private class ULIntersectionRuneResolver : IntersectionRuneResolver
  87. {
  88. protected override Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects, bool useDouble, bool useRounded)
  89. {
  90. // TODO: Handle all relevant permutations of double lines into single lines
  91. // to make F type borders instead.
  92. return useDouble ? driver.ULDCorner : useRounded ? driver.ULRCorner : driver.ULCorner;
  93. }
  94. }
  95. private Rune? GetRuneForIntersects (ConsoleDriver driver, IntersectionDefinition [] intersects)
  96. {
  97. if (!intersects.Any ())
  98. return null;
  99. var runeType = GetRuneTypeForIntersects (intersects);
  100. if(runeResolvers.ContainsKey (runeType)) {
  101. return runeResolvers [runeType].GetRuneForIntersects (driver, intersects);
  102. }
  103. // TODO: Remove these two once we have all of the below ported to IntersectionRuneResolvers
  104. var useDouble = intersects.Any (i => i.Line.Style == BorderStyle.Double && i.Line.Length != 0);
  105. var useRounded = intersects.Any (i => i.Line.Style == BorderStyle.Rounded && i.Line.Length != 0);
  106. // TODO: Turn all these into classes
  107. switch (runeType) {
  108. case IntersectionRuneType.None:
  109. return null;
  110. case IntersectionRuneType.Dot:
  111. return (Rune)'.';
  112. case IntersectionRuneType.ULCorner:
  113. case IntersectionRuneType.URCorner:
  114. return useDouble ? driver.URDCorner : useRounded ? driver.URRCorner : driver.URCorner;
  115. case IntersectionRuneType.LLCorner:
  116. return useDouble ? driver.LLDCorner : useRounded ? driver.LLRCorner : driver.LLCorner;
  117. case IntersectionRuneType.LRCorner:
  118. return useDouble ? driver.LRDCorner : useRounded ? driver.LRRCorner : driver.LRCorner;
  119. case IntersectionRuneType.TopTee:
  120. return useDouble ? '╦' : driver.TopTee;
  121. case IntersectionRuneType.BottomTee:
  122. return useDouble ? '╩' : driver.BottomTee;
  123. case IntersectionRuneType.RightTee:
  124. return useDouble ? '╣' : driver.RightTee;
  125. case IntersectionRuneType.LeftTee:
  126. return useDouble ? '╠' : driver.LeftTee;
  127. case IntersectionRuneType.Crosshair:
  128. return useDouble ? '╬' : '┼';
  129. case IntersectionRuneType.HLine:
  130. return useDouble ? driver.HDLine : driver.HLine;
  131. case IntersectionRuneType.VLine:
  132. return useDouble ? driver.VDLine : driver.VLine;
  133. default: throw new ArgumentOutOfRangeException (nameof (runeType));
  134. }
  135. }
  136. private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition [] intersects)
  137. {
  138. if(intersects.All(i=>i.Line.Length == 0)) {
  139. return IntersectionRuneType.Dot;
  140. }
  141. // ignore dots
  142. intersects = intersects.Where (i => i.Type != IntersectionType.Dot).ToArray ();
  143. var set = new HashSet<IntersectionType> (intersects.Select (i => i.Type));
  144. #region Crosshair Conditions
  145. if (Has (set,
  146. IntersectionType.PassOverHorizontal,
  147. IntersectionType.PassOverVertical
  148. )) {
  149. return IntersectionRuneType.Crosshair;
  150. }
  151. if (Has (set,
  152. IntersectionType.PassOverVertical,
  153. IntersectionType.StartLeft,
  154. IntersectionType.StartRight
  155. )) {
  156. return IntersectionRuneType.Crosshair;
  157. }
  158. if (Has (set,
  159. IntersectionType.PassOverHorizontal,
  160. IntersectionType.StartUp,
  161. IntersectionType.StartDown
  162. )) {
  163. return IntersectionRuneType.Crosshair;
  164. }
  165. if (Has (set,
  166. IntersectionType.StartLeft,
  167. IntersectionType.StartRight,
  168. IntersectionType.StartUp,
  169. IntersectionType.StartDown)) {
  170. return IntersectionRuneType.Crosshair;
  171. }
  172. #endregion
  173. #region Corner Conditions
  174. if (Exactly (set,
  175. IntersectionType.StartRight,
  176. IntersectionType.StartDown)) {
  177. return IntersectionRuneType.ULCorner;
  178. }
  179. if (Exactly (set,
  180. IntersectionType.StartLeft,
  181. IntersectionType.StartDown)) {
  182. return IntersectionRuneType.URCorner;
  183. }
  184. if (Exactly (set,
  185. IntersectionType.StartUp,
  186. IntersectionType.StartLeft)) {
  187. return IntersectionRuneType.LRCorner;
  188. }
  189. if (Exactly (set,
  190. IntersectionType.StartUp,
  191. IntersectionType.StartRight)) {
  192. return IntersectionRuneType.LLCorner;
  193. }
  194. #endregion Corner Conditions
  195. #region T Conditions
  196. if (Has (set,
  197. IntersectionType.PassOverHorizontal,
  198. IntersectionType.StartDown)) {
  199. return IntersectionRuneType.TopTee;
  200. }
  201. if (Has (set,
  202. IntersectionType.StartRight,
  203. IntersectionType.StartLeft,
  204. IntersectionType.StartDown)) {
  205. return IntersectionRuneType.TopTee;
  206. }
  207. if (Has (set,
  208. IntersectionType.PassOverHorizontal,
  209. IntersectionType.StartUp)) {
  210. return IntersectionRuneType.BottomTee;
  211. }
  212. if (Has (set,
  213. IntersectionType.StartRight,
  214. IntersectionType.StartLeft,
  215. IntersectionType.StartUp)) {
  216. return IntersectionRuneType.BottomTee;
  217. }
  218. if (Has (set,
  219. IntersectionType.PassOverVertical,
  220. IntersectionType.StartRight)) {
  221. return IntersectionRuneType.LeftTee;
  222. }
  223. if (Has (set,
  224. IntersectionType.StartRight,
  225. IntersectionType.StartDown,
  226. IntersectionType.StartUp)) {
  227. return IntersectionRuneType.LeftTee;
  228. }
  229. if (Has (set,
  230. IntersectionType.PassOverVertical,
  231. IntersectionType.StartLeft)) {
  232. return IntersectionRuneType.RightTee;
  233. }
  234. if (Has (set,
  235. IntersectionType.StartLeft,
  236. IntersectionType.StartDown,
  237. IntersectionType.StartUp)) {
  238. return IntersectionRuneType.RightTee;
  239. }
  240. #endregion
  241. if (All (intersects, Orientation.Horizontal)) {
  242. return IntersectionRuneType.HLine;
  243. }
  244. if (All (intersects, Orientation.Vertical)) {
  245. return IntersectionRuneType.VLine;
  246. }
  247. return IntersectionRuneType.Dot;
  248. }
  249. private bool All (IntersectionDefinition [] intersects, Orientation orientation)
  250. {
  251. return intersects.All (i => i.Line.Orientation == orientation);
  252. }
  253. /// <summary>
  254. /// Returns true if the <paramref name="intersects"/> collection has all the <paramref name="types"/>
  255. /// specified (i.e. AND).
  256. /// </summary>
  257. /// <param name="intersects"></param>
  258. /// <param name="types"></param>
  259. /// <returns></returns>
  260. private bool Has (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  261. {
  262. return types.All (t => intersects.Contains (t));
  263. }
  264. /// <summary>
  265. /// Returns true if all requested <paramref name="types"/> appear in <paramref name="intersects"/>
  266. /// and there are no additional <see cref="IntersectionRuneType"/>
  267. /// </summary>
  268. /// <param name="intersects"></param>
  269. /// <param name="types"></param>
  270. /// <returns></returns>
  271. private bool Exactly (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  272. {
  273. return intersects.SetEquals (types);
  274. }
  275. class IntersectionDefinition {
  276. /// <summary>
  277. /// The point at which the intersection happens
  278. /// </summary>
  279. public Point Point { get; }
  280. /// <summary>
  281. /// Defines how <see cref="Line"/> position relates
  282. /// to <see cref="Point"/>.
  283. /// </summary>
  284. public IntersectionType Type { get; }
  285. /// <summary>
  286. /// The line that intersects <see cref="Point"/>
  287. /// </summary>
  288. public StraightLine Line { get; }
  289. public IntersectionDefinition (Point point, IntersectionType type, StraightLine line)
  290. {
  291. Point = point;
  292. Type = type;
  293. Line = line;
  294. }
  295. }
  296. /// <summary>
  297. /// The type of Rune that we will use before considering
  298. /// double width, curved borders etc
  299. /// </summary>
  300. enum IntersectionRuneType {
  301. None,
  302. Dot,
  303. ULCorner,
  304. URCorner,
  305. LLCorner,
  306. LRCorner,
  307. TopTee,
  308. BottomTee,
  309. RightTee,
  310. LeftTee,
  311. Crosshair,
  312. HLine,
  313. VLine,
  314. }
  315. enum IntersectionType {
  316. /// <summary>
  317. /// There is no intersection
  318. /// </summary>
  319. None,
  320. /// <summary>
  321. /// A line passes directly over this point traveling along
  322. /// the horizontal axis
  323. /// </summary>
  324. PassOverHorizontal,
  325. /// <summary>
  326. /// A line passes directly over this point traveling along
  327. /// the vertical axis
  328. /// </summary>
  329. PassOverVertical,
  330. /// <summary>
  331. /// A line starts at this point and is traveling up
  332. /// </summary>
  333. StartUp,
  334. /// <summary>
  335. /// A line starts at this point and is traveling right
  336. /// </summary>
  337. StartRight,
  338. /// <summary>
  339. /// A line starts at this point and is traveling down
  340. /// </summary>
  341. StartDown,
  342. /// <summary>
  343. /// A line starts at this point and is traveling left
  344. /// </summary>
  345. StartLeft,
  346. /// <summary>
  347. /// A line exists at this point who has 0 length
  348. /// </summary>
  349. Dot
  350. }
  351. class StraightLine {
  352. public Point Start { get; }
  353. public int Length { get; }
  354. public Orientation Orientation { get; }
  355. public BorderStyle Style { get; }
  356. public StraightLine (Point start, int length, Orientation orientation, BorderStyle style)
  357. {
  358. this.Start = start;
  359. this.Length = length;
  360. this.Orientation = orientation;
  361. this.Style = style;
  362. }
  363. internal IntersectionDefinition Intersects (int x, int y)
  364. {
  365. if (IsDot ()) {
  366. if (StartsAt (x, y)) {
  367. return new IntersectionDefinition (Start, IntersectionType.Dot, this);
  368. } else {
  369. return null;
  370. }
  371. }
  372. switch (Orientation) {
  373. case Orientation.Horizontal: return IntersectsHorizontally (x, y);
  374. case Orientation.Vertical: return IntersectsVertically (x, y);
  375. default: throw new ArgumentOutOfRangeException (nameof (Orientation));
  376. }
  377. }
  378. private IntersectionDefinition IntersectsHorizontally (int x, int y)
  379. {
  380. if (Start.Y != y) {
  381. return null;
  382. } else {
  383. if (StartsAt (x, y)) {
  384. return new IntersectionDefinition (
  385. Start,
  386. Length < 0 ? IntersectionType.StartLeft : IntersectionType.StartRight,
  387. this
  388. );
  389. }
  390. if (EndsAt (x, y)) {
  391. return new IntersectionDefinition (
  392. Start,
  393. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  394. this
  395. );
  396. } else {
  397. var xmin = Math.Min (Start.X, Start.X + Length);
  398. var xmax = Math.Max (Start.X, Start.X + Length);
  399. if (xmin < x && xmax > x) {
  400. return new IntersectionDefinition (
  401. new Point (x, y),
  402. IntersectionType.PassOverHorizontal,
  403. this
  404. );
  405. }
  406. }
  407. return null;
  408. }
  409. }
  410. private IntersectionDefinition IntersectsVertically (int x, int y)
  411. {
  412. if (Start.X != x) {
  413. return null;
  414. } else {
  415. if (StartsAt (x, y)) {
  416. return new IntersectionDefinition (
  417. Start,
  418. Length < 0 ? IntersectionType.StartUp : IntersectionType.StartDown,
  419. this
  420. );
  421. }
  422. if (EndsAt (x, y)) {
  423. return new IntersectionDefinition (
  424. Start,
  425. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  426. this
  427. );
  428. } else {
  429. var ymin = Math.Min (Start.Y, Start.Y + Length);
  430. var ymax = Math.Max (Start.Y, Start.Y + Length);
  431. if (ymin < y && ymax > y) {
  432. return new IntersectionDefinition (
  433. new Point (x, y),
  434. IntersectionType.PassOverVertical,
  435. this
  436. );
  437. }
  438. }
  439. return null;
  440. }
  441. }
  442. private bool EndsAt (int x, int y)
  443. {
  444. if (Orientation == Orientation.Horizontal) {
  445. return Start.X + Length == x && Start.Y == y;
  446. }
  447. return Start.X == x && Start.Y + Length == y;
  448. }
  449. private bool StartsAt (int x, int y)
  450. {
  451. return Start.X == x && Start.Y == y;
  452. }
  453. private bool IsDot ()
  454. {
  455. return Length == 0;
  456. }
  457. }
  458. }
  459. }