StraightLine.cs 5.1 KB

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