StraightLineCanvas.cs 12 KB

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