StraightLine.cs 6.4 KB

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