StraightLine.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. internal Rect Bounds
  42. {
  43. get
  44. {
  45. // 0 and 1/-1 Length means a size (width or height) of 1
  46. int size = Math.Max (1, Math.Abs (Length));
  47. // How much to offset x or y to get the start of the line
  48. int offset = Math.Abs (Length < 0 ? Length + 1 : 0);
  49. int x = Start.X - (Orientation == Orientation.Horizontal ? offset : 0);
  50. int y = Start.Y - (Orientation == Orientation.Vertical ? offset : 0);
  51. int width = Orientation == Orientation.Horizontal ? size : 1;
  52. int height = Orientation == Orientation.Vertical ? size : 1;
  53. return new Rect (x, y, width, height);
  54. }
  55. }
  56. /// <summary>Formats the Line as a string in (Start.X,Start.Y,Length,Orientation) notation.</summary>
  57. public override string ToString () { return $"({Start.X},{Start.Y},{Length},{Orientation})"; }
  58. internal IntersectionDefinition? Intersects (int x, int y)
  59. {
  60. switch (Orientation)
  61. {
  62. case Orientation.Horizontal: return IntersectsHorizontally (x, y);
  63. case Orientation.Vertical: return IntersectsVertically (x, y);
  64. default: throw new ArgumentOutOfRangeException (nameof (Orientation));
  65. }
  66. }
  67. private bool EndsAt (int x, int y)
  68. {
  69. int sub = Length == 0 ? 0 :
  70. Length > 0 ? 1 : -1;
  71. if (Orientation == Orientation.Horizontal)
  72. {
  73. return Start.X + Length - sub == x && Start.Y == y;
  74. }
  75. return Start.X == x && Start.Y + Length - sub == y;
  76. }
  77. private IntersectionType GetTypeByLength (
  78. IntersectionType typeWhenNegative,
  79. IntersectionType typeWhenZero,
  80. IntersectionType typeWhenPositive
  81. )
  82. {
  83. if (Length == 0)
  84. {
  85. return typeWhenZero;
  86. }
  87. return Length < 0 ? typeWhenNegative : typeWhenPositive;
  88. }
  89. private IntersectionDefinition? IntersectsHorizontally (int x, int y)
  90. {
  91. if (Start.Y != y)
  92. {
  93. return null;
  94. }
  95. if (StartsAt (x, y))
  96. {
  97. return new IntersectionDefinition (
  98. Start,
  99. GetTypeByLength (
  100. IntersectionType.StartLeft,
  101. IntersectionType.PassOverHorizontal,
  102. IntersectionType.StartRight
  103. ),
  104. this
  105. );
  106. }
  107. if (EndsAt (x, y))
  108. {
  109. return new IntersectionDefinition (
  110. Start,
  111. Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft,
  112. this
  113. );
  114. }
  115. int xmin = Math.Min (Start.X, Start.X + Length);
  116. int xmax = Math.Max (Start.X, Start.X + Length);
  117. if (xmin < x && xmax > x)
  118. {
  119. return new IntersectionDefinition (
  120. new Point (x, y),
  121. IntersectionType.PassOverHorizontal,
  122. this
  123. );
  124. }
  125. return null;
  126. }
  127. private IntersectionDefinition? IntersectsVertically (int x, int y)
  128. {
  129. if (Start.X != x)
  130. {
  131. return null;
  132. }
  133. if (StartsAt (x, y))
  134. {
  135. return new IntersectionDefinition (
  136. Start,
  137. GetTypeByLength (
  138. IntersectionType.StartUp,
  139. IntersectionType.PassOverVertical,
  140. IntersectionType.StartDown
  141. ),
  142. this
  143. );
  144. }
  145. if (EndsAt (x, y))
  146. {
  147. return new IntersectionDefinition (
  148. Start,
  149. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  150. this
  151. );
  152. }
  153. int ymin = Math.Min (Start.Y, Start.Y + Length);
  154. int ymax = Math.Max (Start.Y, Start.Y + Length);
  155. if (ymin < y && ymax > y)
  156. {
  157. return new IntersectionDefinition (
  158. new Point (x, y),
  159. IntersectionType.PassOverVertical,
  160. this
  161. );
  162. }
  163. return null;
  164. }
  165. private bool StartsAt (int x, int y) { return Start.X == x && Start.Y == y; }
  166. }