LineCanvas.cs 13 KB

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