LineCanvas.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. switch (runeType) {
  84. case IntersectionRuneType.None:
  85. return null;
  86. case IntersectionRuneType.Dot:
  87. return (Rune)'.';
  88. case IntersectionRuneType.ULCorner:
  89. return useDouble ? driver.ULDCorner : driver.ULCorner;
  90. case IntersectionRuneType.URCorner:
  91. return useDouble ? driver.URDCorner : driver.URCorner;
  92. case IntersectionRuneType.LLCorner:
  93. return useDouble ? driver.LLDCorner : driver.LLCorner;
  94. case IntersectionRuneType.LRCorner:
  95. return useDouble ? driver.LRDCorner : driver.LRCorner;
  96. case IntersectionRuneType.TopTee:
  97. return useDouble ? '╦' : driver.TopTee;
  98. case IntersectionRuneType.BottomTee:
  99. return useDouble ? '╩' : driver.BottomTee;
  100. case IntersectionRuneType.RightTee:
  101. return useDouble ? '╣' : driver.RightTee;
  102. case IntersectionRuneType.LeftTee:
  103. return useDouble ? '╠' : driver.LeftTee;
  104. case IntersectionRuneType.Crosshair:
  105. return useDouble ? '╬' : '┼';
  106. case IntersectionRuneType.HLine:
  107. return useDouble ? driver.HDLine : driver.HLine;
  108. case IntersectionRuneType.VLine:
  109. return useDouble ? driver.VDLine : driver.VLine;
  110. default: throw new ArgumentOutOfRangeException (nameof (runeType));
  111. }
  112. }
  113. private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition [] intersects)
  114. {
  115. if(intersects.All(i=>i.Line.Length == 0)) {
  116. return IntersectionRuneType.Dot;
  117. }
  118. // ignore dots
  119. intersects = intersects.Where (i => i.Type != IntersectionType.Dot).ToArray ();
  120. var set = new HashSet<IntersectionType> (intersects.Select (i => i.Type));
  121. #region Crosshair Conditions
  122. if (Has (set,
  123. IntersectionType.PassOverHorizontal,
  124. IntersectionType.PassOverVertical
  125. )) {
  126. return IntersectionRuneType.Crosshair;
  127. }
  128. if (Has (set,
  129. IntersectionType.PassOverVertical,
  130. IntersectionType.StartLeft,
  131. IntersectionType.StartRight
  132. )) {
  133. return IntersectionRuneType.Crosshair;
  134. }
  135. if (Has (set,
  136. IntersectionType.PassOverHorizontal,
  137. IntersectionType.StartUp,
  138. IntersectionType.StartDown
  139. )) {
  140. return IntersectionRuneType.Crosshair;
  141. }
  142. if (Has (set,
  143. IntersectionType.StartLeft,
  144. IntersectionType.StartRight,
  145. IntersectionType.StartUp,
  146. IntersectionType.StartDown)) {
  147. return IntersectionRuneType.Crosshair;
  148. }
  149. #endregion
  150. #region Corner Conditions
  151. if (Exactly (set,
  152. IntersectionType.StartRight,
  153. IntersectionType.StartDown)) {
  154. return IntersectionRuneType.ULCorner;
  155. }
  156. if (Exactly (set,
  157. IntersectionType.StartLeft,
  158. IntersectionType.StartDown)) {
  159. return IntersectionRuneType.URCorner;
  160. }
  161. if (Exactly (set,
  162. IntersectionType.StartUp,
  163. IntersectionType.StartLeft)) {
  164. return IntersectionRuneType.LRCorner;
  165. }
  166. if (Exactly (set,
  167. IntersectionType.StartUp,
  168. IntersectionType.StartRight)) {
  169. return IntersectionRuneType.LLCorner;
  170. }
  171. #endregion Corner Conditions
  172. #region T Conditions
  173. if (Has (set,
  174. IntersectionType.PassOverHorizontal,
  175. IntersectionType.StartDown)) {
  176. return IntersectionRuneType.TopTee;
  177. }
  178. if (Has (set,
  179. IntersectionType.StartRight,
  180. IntersectionType.StartLeft,
  181. IntersectionType.StartDown)) {
  182. return IntersectionRuneType.TopTee;
  183. }
  184. if (Has (set,
  185. IntersectionType.PassOverHorizontal,
  186. IntersectionType.StartUp)) {
  187. return IntersectionRuneType.BottomTee;
  188. }
  189. if (Has (set,
  190. IntersectionType.StartRight,
  191. IntersectionType.StartLeft,
  192. IntersectionType.StartUp)) {
  193. return IntersectionRuneType.BottomTee;
  194. }
  195. if (Has (set,
  196. IntersectionType.PassOverVertical,
  197. IntersectionType.StartRight)) {
  198. return IntersectionRuneType.LeftTee;
  199. }
  200. if (Has (set,
  201. IntersectionType.StartRight,
  202. IntersectionType.StartDown,
  203. IntersectionType.StartUp)) {
  204. return IntersectionRuneType.LeftTee;
  205. }
  206. if (Has (set,
  207. IntersectionType.PassOverVertical,
  208. IntersectionType.StartLeft)) {
  209. return IntersectionRuneType.RightTee;
  210. }
  211. if (Has (set,
  212. IntersectionType.StartLeft,
  213. IntersectionType.StartDown,
  214. IntersectionType.StartUp)) {
  215. return IntersectionRuneType.RightTee;
  216. }
  217. #endregion
  218. if (All (intersects, Orientation.Horizontal)) {
  219. return IntersectionRuneType.HLine;
  220. }
  221. if (All (intersects, Orientation.Vertical)) {
  222. return IntersectionRuneType.VLine;
  223. }
  224. return IntersectionRuneType.Dot;
  225. }
  226. private bool All (IntersectionDefinition [] intersects, Orientation orientation)
  227. {
  228. return intersects.All (i => i.Line.Orientation == orientation);
  229. }
  230. /// <summary>
  231. /// Returns true if the <paramref name="intersects"/> collection has all the <paramref name="types"/>
  232. /// specified (i.e. AND).
  233. /// </summary>
  234. /// <param name="intersects"></param>
  235. /// <param name="types"></param>
  236. /// <returns></returns>
  237. private bool Has (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  238. {
  239. return types.All (t => intersects.Contains (t));
  240. }
  241. /// <summary>
  242. /// Returns true if all requested <paramref name="types"/> appear in <paramref name="intersects"/>
  243. /// and there are no additional <see cref="IntersectionRuneType"/>
  244. /// </summary>
  245. /// <param name="intersects"></param>
  246. /// <param name="types"></param>
  247. /// <returns></returns>
  248. private bool Exactly (HashSet<IntersectionType> intersects, params IntersectionType [] types)
  249. {
  250. return intersects.SetEquals (types);
  251. }
  252. class IntersectionDefinition {
  253. /// <summary>
  254. /// The point at which the intersection happens
  255. /// </summary>
  256. public Point Point { get; }
  257. /// <summary>
  258. /// Defines how <see cref="Line"/> position relates
  259. /// to <see cref="Point"/>.
  260. /// </summary>
  261. public IntersectionType Type { get; }
  262. /// <summary>
  263. /// The line that intersects <see cref="Point"/>
  264. /// </summary>
  265. public StraightLine Line { get; }
  266. public IntersectionDefinition (Point point, IntersectionType type, StraightLine line)
  267. {
  268. Point = point;
  269. Type = type;
  270. Line = line;
  271. }
  272. }
  273. /// <summary>
  274. /// The type of Rune that we will use before considering
  275. /// double width, curved borders etc
  276. /// </summary>
  277. enum IntersectionRuneType {
  278. None,
  279. Dot,
  280. ULCorner,
  281. URCorner,
  282. LLCorner,
  283. LRCorner,
  284. TopTee,
  285. BottomTee,
  286. RightTee,
  287. LeftTee,
  288. Crosshair,
  289. HLine,
  290. VLine,
  291. }
  292. enum IntersectionType {
  293. /// <summary>
  294. /// There is no intersection
  295. /// </summary>
  296. None,
  297. /// <summary>
  298. /// A line passes directly over this point traveling along
  299. /// the horizontal axis
  300. /// </summary>
  301. PassOverHorizontal,
  302. /// <summary>
  303. /// A line passes directly over this point traveling along
  304. /// the vertical axis
  305. /// </summary>
  306. PassOverVertical,
  307. /// <summary>
  308. /// A line starts at this point and is traveling up
  309. /// </summary>
  310. StartUp,
  311. /// <summary>
  312. /// A line starts at this point and is traveling right
  313. /// </summary>
  314. StartRight,
  315. /// <summary>
  316. /// A line starts at this point and is traveling down
  317. /// </summary>
  318. StartDown,
  319. /// <summary>
  320. /// A line starts at this point and is traveling left
  321. /// </summary>
  322. StartLeft,
  323. /// <summary>
  324. /// A line exists at this point who has 0 length
  325. /// </summary>
  326. Dot
  327. }
  328. class StraightLine {
  329. public Point Start { get; }
  330. public int Length { get; }
  331. public Orientation Orientation { get; }
  332. public BorderStyle Style { get; }
  333. public StraightLine (Point start, int length, Orientation orientation, BorderStyle style)
  334. {
  335. this.Start = start;
  336. this.Length = length;
  337. this.Orientation = orientation;
  338. this.Style = style;
  339. }
  340. internal IntersectionDefinition Intersects (int x, int y)
  341. {
  342. if (IsDot ()) {
  343. if (StartsAt (x, y)) {
  344. return new IntersectionDefinition (Start, IntersectionType.Dot, this);
  345. } else {
  346. return null;
  347. }
  348. }
  349. switch (Orientation) {
  350. case Orientation.Horizontal: return IntersectsHorizontally (x, y);
  351. case Orientation.Vertical: return IntersectsVertically (x, y);
  352. default: throw new ArgumentOutOfRangeException (nameof (Orientation));
  353. }
  354. }
  355. private IntersectionDefinition IntersectsHorizontally (int x, int y)
  356. {
  357. if (Start.Y != y) {
  358. return null;
  359. } else {
  360. if (StartsAt (x, y)) {
  361. return new IntersectionDefinition (
  362. Start,
  363. Length < 0 ? IntersectionType.StartLeft : IntersectionType.StartRight,
  364. this
  365. );
  366. }
  367. if (EndsAt (x, y)) {
  368. return new IntersectionDefinition (
  369. Start,
  370. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  371. this
  372. );
  373. } else {
  374. var xmin = Math.Min (Start.X, Start.X + Length);
  375. var xmax = Math.Max (Start.X, Start.X + Length);
  376. if (xmin < x && xmax > x) {
  377. return new IntersectionDefinition (
  378. new Point (x, y),
  379. IntersectionType.PassOverHorizontal,
  380. this
  381. );
  382. }
  383. }
  384. return null;
  385. }
  386. }
  387. private IntersectionDefinition IntersectsVertically (int x, int y)
  388. {
  389. if (Start.X != x) {
  390. return null;
  391. } else {
  392. if (StartsAt (x, y)) {
  393. return new IntersectionDefinition (
  394. Start,
  395. Length < 0 ? IntersectionType.StartUp : IntersectionType.StartDown,
  396. this
  397. );
  398. }
  399. if (EndsAt (x, y)) {
  400. return new IntersectionDefinition (
  401. Start,
  402. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  403. this
  404. );
  405. } else {
  406. var ymin = Math.Min (Start.Y, Start.Y + Length);
  407. var ymax = Math.Max (Start.Y, Start.Y + Length);
  408. if (ymin < y && ymax > y) {
  409. return new IntersectionDefinition (
  410. new Point (x, y),
  411. IntersectionType.PassOverVertical,
  412. this
  413. );
  414. }
  415. }
  416. return null;
  417. }
  418. }
  419. private bool EndsAt (int x, int y)
  420. {
  421. if (Orientation == Orientation.Horizontal) {
  422. return Start.X + Length == x && Start.Y == y;
  423. }
  424. return Start.X == x && Start.Y + Length == y;
  425. }
  426. private bool StartsAt (int x, int y)
  427. {
  428. return Start.X == x && Start.Y == y;
  429. }
  430. private bool IsDot ()
  431. {
  432. return Length == 0;
  433. }
  434. }
  435. }
  436. }