LineCanvas.cs 13 KB

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