Thickness.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System.Text.Json.Serialization;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Describes the thickness of a frame around a rectangle. Four <see cref="int"/> values describe the
  5. /// <see cref="Left"/>, <see cref="Top"/>, <see cref="Right"/>, and <see cref="Bottom"/> sides of the rectangle,
  6. /// respectively.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// Use the helper API (<see cref="GetInside(Rect)"/> to get the rectangle describing the insides of the frame,
  11. /// with the thickness widths subtracted.
  12. /// </para>
  13. /// <para>Use the helper API (<see cref="Draw(Rect, string)"/> to draw the frame with the specified thickness.</para>
  14. /// </remarks>
  15. public class Thickness : IEquatable<Thickness>
  16. {
  17. /// <summary>Gets or sets the width of the lower side of the rectangle.</summary>
  18. [JsonInclude]
  19. public int Bottom;
  20. /// <summary>Gets or sets the width of the left side of the rectangle.</summary>
  21. [JsonInclude]
  22. public int Left;
  23. /// <summary>Gets or sets the width of the right side of the rectangle.</summary>
  24. [JsonInclude]
  25. public int Right;
  26. /// <summary>Gets or sets the width of the upper side of the rectangle.</summary>
  27. [JsonInclude]
  28. public int Top;
  29. /// <summary>Initializes a new instance of the <see cref="Thickness"/> class with all widths set to 0.</summary>
  30. public Thickness () { }
  31. /// <summary>Initializes a new instance of the <see cref="Thickness"/> class with a uniform width to each side.</summary>
  32. /// <param name="width"></param>
  33. public Thickness (int width) : this (width, width, width, width) { }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="Thickness"/> class that has specific widths applied to each side
  36. /// of the rectangle.
  37. /// </summary>
  38. /// <param name="left"></param>
  39. /// <param name="top"></param>
  40. /// <param name="right"></param>
  41. /// <param name="bottom"></param>
  42. public Thickness (int left, int top, int right, int bottom)
  43. {
  44. Left = left;
  45. Top = top;
  46. Right = right;
  47. Bottom = bottom;
  48. }
  49. // TODO: add operator overloads
  50. /// <summary>Gets an empty thickness.</summary>
  51. public static Thickness Empty => new (0);
  52. /// <summary>
  53. /// Gets the total width of the left and right sides of the rectangle. Sets the width of the left and rigth sides
  54. /// of the rectangle to half the specified value.
  55. /// </summary>
  56. public int Horizontal
  57. {
  58. get => Left + Right;
  59. set => Left = Right = value / 2;
  60. }
  61. /// <summary>
  62. /// Gets the total height of the top and bottom sides of the rectangle. Sets the height of the top and bottom
  63. /// sides of the rectangle to half the specified value.
  64. /// </summary>
  65. public int Vertical
  66. {
  67. get => Top + Bottom;
  68. set => Top = Bottom = value / 2;
  69. }
  70. // IEquitable
  71. /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
  72. /// <param name="other"></param>
  73. /// <returns>true if the current object is equal to the other parameter; otherwise, false.</returns>
  74. public bool Equals (Thickness other) { return other is { } && Left == other.Left && Right == other.Right && Top == other.Top && Bottom == other.Bottom; }
  75. /// <summary>
  76. /// Gets whether the specified coordinates lie within the thickness (inside the bounding rectangle but outside of
  77. /// the rectangle described by <see cref="GetInside(Rect)"/>.
  78. /// </summary>
  79. /// <param name="outside">Describes the location and size of the rectangle that contains the thickness.</param>
  80. /// <param name="x">The x coord to check.</param>
  81. /// <param name="y">The y coord to check.</param>
  82. /// <returns><see langword="true"/> if the specified coordinate is within the thickness; <see langword="false"/> otherwise.</returns>
  83. public bool Contains (Rect outside, int x, int y)
  84. {
  85. Rect inside = GetInside (outside);
  86. return outside.Contains (x, y) && !inside.Contains (x, y);
  87. }
  88. /// <summary>Draws the <see cref="Thickness"/> rectangle with an optional diagnostics label.</summary>
  89. /// <remarks>
  90. /// If <see cref="ConsoleDriver.DiagnosticFlags"/> is set to
  91. /// <see cref="ConsoleDriver.DiagnosticFlags.FramePadding"/> then 'T', 'L', 'R', and 'B' glyphs will be used instead of
  92. /// space. If <see cref="ConsoleDriver.DiagnosticFlags"/> is set to
  93. /// <see cref="ConsoleDriver.DiagnosticFlags.FrameRuler"/> then a ruler will be drawn on the outer edge of the
  94. /// Thickness.
  95. /// </remarks>
  96. /// <param name="rect">The location and size of the rectangle that bounds the thickness rectangle, in screen coordinates.</param>
  97. /// <param name="label">The diagnostics label to draw on the bottom of the <see cref="Bottom"/>.</param>
  98. /// <returns>The inner rectangle remaining to be drawn.</returns>
  99. public Rect Draw (Rect rect, string label = null)
  100. {
  101. if (rect.Size.Width < 1 || rect.Size.Height < 1)
  102. {
  103. return Rect.Empty;
  104. }
  105. var clearChar = (Rune)' ';
  106. Rune leftChar = clearChar;
  107. Rune rightChar = clearChar;
  108. Rune topChar = clearChar;
  109. Rune bottomChar = clearChar;
  110. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding)
  111. == ConsoleDriver.DiagnosticFlags.FramePadding)
  112. {
  113. leftChar = (Rune)'L';
  114. rightChar = (Rune)'R';
  115. topChar = (Rune)'T';
  116. bottomChar = (Rune)'B';
  117. if (!string.IsNullOrEmpty (label))
  118. {
  119. leftChar = rightChar = bottomChar = topChar = (Rune)label [0];
  120. }
  121. }
  122. // Draw the Top side
  123. if (Top > 0)
  124. {
  125. Application.Driver.FillRect (new Rect (rect.X, rect.Y, rect.Width, Math.Min (rect.Height, Top)), topChar);
  126. }
  127. // Draw the Left side
  128. if (Left > 0)
  129. {
  130. Application.Driver.FillRect (new Rect (rect.X, rect.Y, Math.Min (rect.Width, Left), rect.Height), leftChar);
  131. }
  132. // Draw the Right side
  133. if (Right > 0)
  134. {
  135. Application.Driver.FillRect (
  136. new Rect (
  137. Math.Max (0, rect.X + rect.Width - Right),
  138. rect.Y,
  139. Math.Min (rect.Width, Right),
  140. rect.Height
  141. ),
  142. rightChar
  143. );
  144. }
  145. // Draw the Bottom side
  146. if (Bottom > 0)
  147. {
  148. Application.Driver.FillRect (
  149. new Rect (
  150. rect.X,
  151. rect.Y + Math.Max (0, rect.Height - Bottom),
  152. rect.Width,
  153. Bottom
  154. ),
  155. bottomChar
  156. );
  157. }
  158. // TODO: This should be moved to LineCanvas as a new LineStyle.Ruler
  159. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler)
  160. == ConsoleDriver.DiagnosticFlags.FrameRuler)
  161. {
  162. // Top
  163. var hruler = new Ruler { Length = rect.Width, Orientation = Orientation.Horizontal };
  164. if (Top > 0)
  165. {
  166. hruler.Draw (new Point (rect.X, rect.Y));
  167. }
  168. //Left
  169. var vruler = new Ruler { Length = rect.Height - 2, Orientation = Orientation.Vertical };
  170. if (Left > 0)
  171. {
  172. vruler.Draw (new Point (rect.X, rect.Y + 1), 1);
  173. }
  174. // Bottom
  175. if (Bottom > 0)
  176. {
  177. hruler.Draw (new Point (rect.X, rect.Y + rect.Height - 1));
  178. }
  179. // Right
  180. if (Right > 0)
  181. {
  182. vruler.Draw (new Point (rect.X + rect.Width - 1, rect.Y + 1), 1);
  183. }
  184. }
  185. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding)
  186. == ConsoleDriver.DiagnosticFlags.FramePadding)
  187. {
  188. // Draw the diagnostics label on the bottom
  189. var tf = new TextFormatter
  190. {
  191. Text = label == null ? string.Empty : $"{label} {this}",
  192. Alignment = TextAlignment.Centered,
  193. VerticalAlignment = VerticalTextAlignment.Bottom
  194. };
  195. tf.Draw (rect, Application.Driver.CurrentAttribute, Application.Driver.CurrentAttribute, rect, false);
  196. }
  197. return GetInside (rect);
  198. }
  199. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  200. /// <param name="obj">The object to compare with the current object.</param>
  201. /// <returns><c>true</c> if the specified object is equal to the current object; otherwise, <c>false</c>.</returns>
  202. public override bool Equals (object obj)
  203. {
  204. //Check for null and compare run-time types.
  205. if (obj == null || !GetType ().Equals (obj.GetType ()))
  206. {
  207. return false;
  208. }
  209. return Equals ((Thickness)obj);
  210. }
  211. /// <inheritdoc/>
  212. public override int GetHashCode ()
  213. {
  214. var hashCode = 1380952125;
  215. hashCode = hashCode * -1521134295 + Left.GetHashCode ();
  216. hashCode = hashCode * -1521134295 + Right.GetHashCode ();
  217. hashCode = hashCode * -1521134295 + Top.GetHashCode ();
  218. hashCode = hashCode * -1521134295 + Bottom.GetHashCode ();
  219. return hashCode;
  220. }
  221. /// <summary>
  222. /// Returns a rectangle describing the location and size of the inside area of <paramref name="rect"/> with the
  223. /// thickness widths subtracted. The height and width of the returned rectangle will never be less than 0.
  224. /// </summary>
  225. /// <remarks>
  226. /// If a thickness width is negative, the inside rectangle will be larger than <paramref name="rect"/>. e.g. a
  227. /// <c>
  228. /// Thickness (-1, -1, -1, -1) will result in a rectangle skewed -1 in the X and Y directions and with a Size
  229. /// increased by 1.
  230. /// </c>
  231. /// </remarks>
  232. /// <param name="rect">The source rectangle</param>
  233. /// <returns></returns>
  234. public Rect GetInside (Rect rect)
  235. {
  236. int x = rect.X + Left;
  237. int y = rect.Y + Top;
  238. int width = Math.Max (0, rect.Size.Width - Horizontal);
  239. int height = Math.Max (0, rect.Size.Height - Vertical);
  240. return new Rect (new Point (x, y), new Size (width, height));
  241. }
  242. /// <inheritdoc/>
  243. public static bool operator == (Thickness left, Thickness right) { return EqualityComparer<Thickness>.Default.Equals (left, right); }
  244. /// <inheritdoc/>
  245. public static bool operator != (Thickness left, Thickness right) { return !(left == right); }
  246. /// <summary>Returns the thickness widths of the Thickness formatted as a string.</summary>
  247. /// <returns>The thickness widths as a string.</returns>
  248. public override string ToString () { return $"(Left={Left},Top={Top},Right={Right},Bottom={Bottom})"; }
  249. private int validate (int width)
  250. {
  251. if (width < 0)
  252. {
  253. throw new ArgumentException ("Thickness widths cannot be negative.");
  254. }
  255. return width;
  256. }
  257. }