StraightLine.cs 5.1 KB

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