StraightLine.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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"></param>
  9. /// <param name="length"></param>
  10. /// <param name="orientation"></param>
  11. /// <param name="style"></param>
  12. /// <param name="attribute"></param>
  13. public StraightLine (
  14. Point start,
  15. int length,
  16. Orientation orientation,
  17. LineStyle style,
  18. Attribute? attribute = default
  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. /// 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. if (StartsAt (x, y))
  97. {
  98. return new IntersectionDefinition (
  99. Start,
  100. GetTypeByLength (
  101. IntersectionType.StartLeft,
  102. IntersectionType.PassOverHorizontal,
  103. IntersectionType.StartRight
  104. ),
  105. this
  106. );
  107. }
  108. if (EndsAt (x, y))
  109. {
  110. return new IntersectionDefinition (
  111. Start,
  112. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  113. this
  114. );
  115. }
  116. int xmin = Math.Min (Start.X, Start.X + Length);
  117. int xmax = Math.Max (Start.X, Start.X + Length);
  118. if (xmin < x && xmax > x)
  119. {
  120. return new IntersectionDefinition (
  121. new Point (x, y),
  122. IntersectionType.PassOverHorizontal,
  123. this
  124. );
  125. }
  126. return null;
  127. }
  128. private IntersectionDefinition? IntersectsVertically (int x, int y)
  129. {
  130. if (Start.X != x)
  131. {
  132. return null;
  133. }
  134. if (StartsAt (x, y))
  135. {
  136. return new IntersectionDefinition (
  137. Start,
  138. GetTypeByLength (
  139. IntersectionType.StartUp,
  140. IntersectionType.PassOverVertical,
  141. IntersectionType.StartDown
  142. ),
  143. this
  144. );
  145. }
  146. if (EndsAt (x, y))
  147. {
  148. return new IntersectionDefinition (
  149. Start,
  150. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  151. this
  152. );
  153. }
  154. int ymin = Math.Min (Start.Y, Start.Y + Length);
  155. int ymax = Math.Max (Start.Y, Start.Y + Length);
  156. if (ymin < y && ymax > y)
  157. {
  158. return new IntersectionDefinition (
  159. new Point (x, y),
  160. IntersectionType.PassOverVertical,
  161. this
  162. );
  163. }
  164. return null;
  165. }
  166. private bool StartsAt (int x, int y) { return Start.X == x && Start.Y == y; }
  167. }