StraightLine.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 Viewport.
  42. internal Rectangle Viewport
  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 IntersectionDefinition (p,
  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 (p,
  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 (p,
  120. IntersectionType.PassOverHorizontal,
  121. this
  122. );
  123. }
  124. return null;
  125. }
  126. private IntersectionDefinition? IntersectsVertically (int x, int y)
  127. {
  128. if (Start.X != x)
  129. {
  130. return null;
  131. }
  132. var p = new Point (x, y);
  133. if (StartsAt (x, y))
  134. {
  135. return new IntersectionDefinition (p,
  136. GetTypeByLength (
  137. IntersectionType.StartUp,
  138. IntersectionType.PassOverVertical,
  139. IntersectionType.StartDown
  140. ),
  141. this
  142. );
  143. }
  144. if (EndsAt (x, y))
  145. {
  146. return new IntersectionDefinition (p,
  147. Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp,
  148. this
  149. );
  150. }
  151. int ymin = Math.Min (Start.Y, Start.Y + Length);
  152. int ymax = Math.Max (Start.Y, Start.Y + Length);
  153. if (ymin < y && ymax > y)
  154. {
  155. return new IntersectionDefinition (p,
  156. IntersectionType.PassOverVertical,
  157. this
  158. );
  159. }
  160. return null;
  161. }
  162. private bool StartsAt (int x, int y) { return Start.X == x && Start.Y == y; }
  163. }