Thickness.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.Json.Serialization;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Describes the thickness of a frame around a rectangle. Four <see cref="int"/> values describe
  9. /// the <see cref="Left"/>, <see cref="Top"/>, <see cref="Right"/>, and <see cref="Bottom"/> sides
  10. /// of the rectangle, respectively. Provides a helper API (<see cref="Draw(Rect, string)"/> for
  11. /// drawing a frame with the specified thickness.
  12. /// </summary>
  13. public class Thickness : IEquatable<Thickness> {
  14. private int validate (int width)
  15. {
  16. if (width < 0) {
  17. throw new ArgumentException ("Thickness widths cannot be negative.");
  18. }
  19. return width;
  20. }
  21. /// <summary>
  22. /// Gets or sets the width of the left side of the rectangle.
  23. /// </summary>
  24. [JsonInclude]
  25. public int Left;
  26. /// <summary>
  27. /// Gets or sets the width of the upper side of the rectangle.
  28. /// </summary>
  29. [JsonInclude]
  30. public int Top;
  31. /// <summary>
  32. /// Gets or sets the width of the right side of the rectangle.
  33. /// </summary>
  34. [JsonInclude]
  35. public int Right;
  36. /// <summary>
  37. /// Gets or sets the width of the lower side of the rectangle.
  38. /// </summary>
  39. [JsonInclude]
  40. public int Bottom;
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="Thickness"/> class with all widths
  43. /// set to 0.
  44. /// </summary>
  45. public Thickness () { }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="Thickness"/> class with a uniform width to each side.
  48. /// </summary>
  49. /// <param name="width"></param>
  50. public Thickness (int width) : this (width, width, width, width) { }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="Thickness"/> class that has specific
  53. /// widths applied to each side of the rectangle.
  54. /// </summary>
  55. /// <param name="left"></param>
  56. /// <param name="top"></param>
  57. /// <param name="right"></param>
  58. /// <param name="bottom"></param>
  59. public Thickness (int left, int top, int right, int bottom)
  60. {
  61. Left = left;
  62. Top = top;
  63. Right = right;
  64. Bottom = bottom;
  65. }
  66. ///// <summary>
  67. ///// <see langword="true"/> if all dimensions are 0.
  68. ///// </summary>
  69. //public bool Empty => Left == 0 && Top == 0 && Right == 0 && Bottom == 0;
  70. /// <summary>
  71. /// Returns a rectangle describing the location and size of the inner area of <paramref name="rect"/>
  72. /// with the thickness widths subracted. The height and width of the retunred rect may be zero.
  73. /// </summary>
  74. /// <param name="rect">The source rectangle</param>
  75. /// <returns></returns>
  76. public Rect GetInnerRect (Rect rect)
  77. {
  78. var width = rect.Size.Width - (Left + Right);
  79. var height = rect.Size.Height - (Top + Bottom);
  80. var size = new Size (Math.Max (0, width), Math.Max (0, height));
  81. return new Rect (new Point (rect.X + Left, rect.Y + Top), size);
  82. }
  83. private void FillRect (Rect rect, System.Rune rune = default)
  84. {
  85. for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
  86. for (var c = rect.X; c < rect.X + rect.Width; c++) {
  87. Application.Driver.Move (c, r);
  88. Application.Driver.AddRune (rune == default ? ' ' : rune);
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// Draws the <see cref="Thickness"/> rectangle with an optional diagnostics label.
  94. /// </summary>
  95. /// <remarks>
  96. /// If <see cref="ConsoleDriver.DiagnosticFlags"/> is set to <see cref="ConsoleDriver.DiagnosticFlags.FramePadding"/> then
  97. /// 'T', 'L', 'R', and 'B' glyphs will be used instead of space. If <see cref="ConsoleDriver.DiagnosticFlags"/>
  98. /// is set to <see cref="ConsoleDriver.DiagnosticFlags.FrameRuler"/> then a ruler will be drawn on the outer edge of the
  99. /// Thickness.
  100. /// </remarks>
  101. /// <param name="rect">The location and size of the rectangle that bounds the thickness rectangle, in
  102. /// screen coordinates.</param>
  103. /// <param name="label">The diagnostics label to draw on the bottom of the <see cref="Bottom"/>.</param>
  104. /// <returns>The inner rectangle remaining to be drawn.</returns>
  105. public Rect Draw (Rect rect, string label = null)
  106. {
  107. System.Rune clearChar = ' ';
  108. System.Rune leftChar = clearChar;
  109. System.Rune rightChar = clearChar;
  110. System.Rune topChar = clearChar;
  111. System.Rune bottomChar = clearChar;
  112. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding) == ConsoleDriver.DiagnosticFlags.FramePadding) {
  113. leftChar = 'L';
  114. rightChar = 'R';
  115. topChar = 'T';
  116. bottomChar = 'B';
  117. if (!string.IsNullOrEmpty (label)) {
  118. leftChar = rightChar = bottomChar = topChar = label [0];
  119. }
  120. }
  121. ustring hrule = ustring.Empty;
  122. ustring vrule = ustring.Empty;
  123. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  124. string h = "0123456789";
  125. hrule = h.Repeat ((int)Math.Ceiling ((double)(rect.Width) / (double)h.Length)) [0..(rect.Width)];
  126. string v = "0123456789";
  127. vrule = v.Repeat ((int)Math.Ceiling ((double)(rect.Height * 2) / (double)v.Length)) [0..(rect.Height * 2)];
  128. };
  129. // Draw the Top side
  130. FillRect (new Rect (rect.X, rect.Y, rect.Width, Math.Min (rect.Height, Top)), topChar);
  131. // Draw the Left side
  132. FillRect (new Rect (rect.X, rect.Y, Math.Min (rect.Width, Left), rect.Height), leftChar);
  133. // Draw the Right side
  134. FillRect (new Rect (Math.Max (0, rect.X + rect.Width - Right), rect.Y, Math.Min (rect.Width, Right), rect.Height), rightChar);
  135. // Draw the Bottom side
  136. FillRect (new Rect (rect.X, rect.Y + Math.Max (0, rect.Height - Bottom), rect.Width, Bottom), bottomChar);
  137. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  138. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  139. // Top
  140. Application.Driver.Move (rect.X, rect.Y);
  141. Application.Driver.AddStr (hrule);
  142. //Left
  143. for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
  144. Application.Driver.Move (rect.X, r);
  145. Application.Driver.AddRune (vrule [r - rect.Y]);
  146. }
  147. // Bottom
  148. Application.Driver.Move (rect.X, rect.Y + rect.Height - Bottom + 1);
  149. Application.Driver.AddStr (hrule);
  150. // Right
  151. for (var r = rect.Y + 1; r < rect.Y + rect.Height; r++) {
  152. Application.Driver.Move (rect.X + rect.Width - Right + 1, r);
  153. Application.Driver.AddRune (vrule [r - rect.Y]);
  154. }
  155. }
  156. // Draw the diagnostics label on the bottom
  157. var tf = new TextFormatter () {
  158. Text = label == null ? string.Empty : $"{label} {this}",
  159. Alignment = TextAlignment.Centered,
  160. VerticalAlignment = VerticalTextAlignment.Bottom
  161. };
  162. tf.Draw (rect, Application.Driver.CurrentAttribute, Application.Driver.CurrentAttribute, rect, false);
  163. return GetInnerRect (rect);
  164. }
  165. // TODO: add operator overloads
  166. /// <summary>
  167. /// Gets an empty thickness.
  168. /// </summary>
  169. public static Thickness Empty => new Thickness (0);
  170. /// <inheritdoc/>
  171. public override bool Equals (object obj)
  172. {
  173. //Check for null and compare run-time types.
  174. if ((obj == null) || !this.GetType ().Equals (obj.GetType ())) {
  175. return false;
  176. } else {
  177. return Equals ((Thickness)obj);
  178. }
  179. }
  180. /// <summary>Returns the thickness widths of the Thickness formatted as a string.</summary>
  181. /// <returns>The thickness widths as a string.</returns>
  182. public override string ToString ()
  183. {
  184. return $"(Left={Left},Top={Top},Right={Right},Bottom={Bottom})";
  185. }
  186. // IEquitable
  187. /// <inheritdoc/>
  188. public bool Equals (Thickness other)
  189. {
  190. return other is not null &&
  191. Left == other.Left &&
  192. Right == other.Right &&
  193. Top == other.Top &&
  194. Bottom == other.Bottom;
  195. }
  196. /// <inheritdoc/>
  197. public override int GetHashCode ()
  198. {
  199. int hashCode = 1380952125;
  200. hashCode = hashCode * -1521134295 + Left.GetHashCode ();
  201. hashCode = hashCode * -1521134295 + Right.GetHashCode ();
  202. hashCode = hashCode * -1521134295 + Top.GetHashCode ();
  203. hashCode = hashCode * -1521134295 + Bottom.GetHashCode ();
  204. return hashCode;
  205. }
  206. /// <inheritdoc/>
  207. public static bool operator == (Thickness left, Thickness right)
  208. {
  209. return EqualityComparer<Thickness>.Default.Equals (left, right);
  210. }
  211. /// <inheritdoc/>
  212. public static bool operator != (Thickness left, Thickness right)
  213. {
  214. return !(left == right);
  215. }
  216. }
  217. internal static class StringExtensions {
  218. public static string Repeat (this string instr, int n)
  219. {
  220. if (n <= 0) {
  221. return null;
  222. }
  223. if (string.IsNullOrEmpty (instr) || n == 1) {
  224. return instr;
  225. }
  226. return new StringBuilder (instr.Length * n)
  227. .Insert (0, instr, n)
  228. .ToString ();
  229. }
  230. }
  231. }