StraightLineCanvas.cs 13 KB

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