StraightLine.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. namespace Terminal.Gui;
  2. #nullable enable
  3. // TODO: Add events that notify when StraightLine changes to enable dynamic layout
  4. /// <summary>A line between two points on a horizontal or vertical <see cref="Orientation"/> and a given style/color.</summary>
  5. public class StraightLine
  6. {
  7. /// <summary>Creates a new instance of the <see cref="StraightLine"/> class.</summary>
  8. /// <param name="start">The start location.</param>
  9. /// <param name="length">The length of the line.</param>
  10. /// <param name="orientation">The orientation of the line.</param>
  11. /// <param name="style">The line style.</param>
  12. /// <param name="attribute">The attribute to be used for rendering the line.</param>
  13. public StraightLine (
  14. Point start,
  15. int length,
  16. Orientation orientation,
  17. LineStyle style,
  18. Attribute? attribute = null
  19. )
  20. {
  21. Start = start;
  22. Length = length;
  23. Orientation = orientation;
  24. Style = style;
  25. Attribute = attribute;
  26. }
  27. /// <summary>Gets or sets the color of the line.</summary>
  28. public Attribute? Attribute { get; set; }
  29. /// <summary>Gets or sets the length of the line.</summary>
  30. public int Length { get; set; }
  31. /// <summary>Gets or sets the orientation (horizontal or vertical) of the line.</summary>
  32. public Orientation Orientation { get; set; }
  33. /// <summary>Gets or sets where the line begins.</summary>
  34. public Point Start { get; set; }
  35. /// <summary>Gets or sets the line style of the line (e.g. dotted, double).</summary>
  36. public LineStyle Style { get; set; }
  37. /// <summary>
  38. /// Gets the rectangle that describes the bounds of the canvas. Location is the coordinates of the line that is
  39. /// the furthest left/top and Size is defined by the line that extends the furthest right/bottom.
  40. /// </summary>
  41. // PERF: Probably better to store the rectangle rather than make a new one on every single access to Bounds.
  42. internal Rectangle Bounds
  43. {
  44. get
  45. {
  46. // 0 and 1/-1 Length means a size (width or height) of 1
  47. int size = Math.Max (1, Math.Abs (Length));
  48. // How much to offset x or y to get the start of the line
  49. int offset = Math.Abs (Length < 0 ? Length + 1 : 0);
  50. int x = Start.X - (Orientation == Orientation.Horizontal ? offset : 0);
  51. int y = Start.Y - (Orientation == Orientation.Vertical ? offset : 0);
  52. int width = Orientation == Orientation.Horizontal ? size : 1;
  53. int height = Orientation == Orientation.Vertical ? size : 1;
  54. return new (x, y, width, height);
  55. }
  56. }
  57. /// <summary>Formats the Line as a string in (Start.X,Start.Y,Length,Orientation) notation.</summary>
  58. public override string ToString () { return $"({Start.X},{Start.Y},{Length},{Orientation})"; }
  59. internal IntersectionDefinition? Intersects (int x, int y)
  60. {
  61. switch (Orientation)
  62. {
  63. case Orientation.Horizontal: return IntersectsHorizontally (x, y);
  64. case Orientation.Vertical: return IntersectsVertically (x, y);
  65. default: throw new ArgumentOutOfRangeException (nameof (Orientation));
  66. }
  67. }
  68. private bool EndsAt (int x, int y)
  69. {
  70. int sub = Length == 0 ? 0 :
  71. Length > 0 ? 1 : -1;
  72. if (Orientation == Orientation.Horizontal)
  73. {
  74. return Start.X + Length - sub == x && Start.Y == y;
  75. }
  76. return Start.X == x && Start.Y + Length - sub == y;
  77. }
  78. private IntersectionType GetTypeByLength (
  79. IntersectionType typeWhenNegative,
  80. IntersectionType typeWhenZero,
  81. IntersectionType typeWhenPositive
  82. )
  83. {
  84. if (Length == 0)
  85. {
  86. return typeWhenZero;
  87. }
  88. return Length < 0 ? typeWhenNegative : typeWhenPositive;
  89. }
  90. private IntersectionDefinition? IntersectsHorizontally (int x, int y)
  91. {
  92. if (Start.Y != y)
  93. {
  94. return null;
  95. }
  96. var p = new Point (x, y);
  97. if (StartsAt (x, y))
  98. {
  99. return new (
  100. p,
  101. GetTypeByLength (
  102. IntersectionType.StartLeft,
  103. IntersectionType.PassOverHorizontal,
  104. IntersectionType.StartRight
  105. ),
  106. this
  107. );
  108. }
  109. if (EndsAt (x, y))
  110. {
  111. return new (
  112. p,
  113. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  114. this
  115. );
  116. }
  117. int xmin = Math.Min (Start.X, Start.X + Length);
  118. int xmax = Math.Max (Start.X, Start.X + Length);
  119. if (xmin < x && xmax > x)
  120. {
  121. return new (
  122. p,
  123. IntersectionType.PassOverHorizontal,
  124. this
  125. );
  126. }
  127. return null;
  128. }
  129. private IntersectionDefinition? IntersectsVertically (int x, int y)
  130. {
  131. if (Start.X != x)
  132. {
  133. return null;
  134. }
  135. var p = new Point (x, y);
  136. if (StartsAt (x, y))
  137. {
  138. return new (
  139. p,
  140. GetTypeByLength (
  141. IntersectionType.StartUp,
  142. IntersectionType.PassOverVertical,
  143. IntersectionType.StartDown
  144. ),
  145. this
  146. );
  147. }
  148. if (EndsAt (x, y))
  149. {
  150. return new (
  151. p,
  152. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  153. this
  154. );
  155. }
  156. int ymin = Math.Min (Start.Y, Start.Y + Length);
  157. int ymax = Math.Max (Start.Y, Start.Y + Length);
  158. if (ymin < y && ymax > y)
  159. {
  160. return new (
  161. p,
  162. IntersectionType.PassOverVertical,
  163. this
  164. );
  165. }
  166. return null;
  167. }
  168. private bool StartsAt (int x, int y) { return Start.X == x && Start.Y == y; }
  169. }