2
0

StraightLineCanvas.cs 12 KB

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