Thickness.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /// Returns a rectangle describing the location and size of the inner area of <paramref name="rect"/>
  68. /// with the thickness widths subracted. The height and width of the retunred rect may be zero.
  69. /// </summary>
  70. /// <param name="rect">The source rectangle</param>
  71. /// <returns></returns>
  72. public Rect GetInnerRect (Rect rect)
  73. {
  74. var width = rect.Size.Width - (Left + Right);
  75. var height = rect.Size.Height - (Top + Bottom);
  76. var size = new Size (Math.Max (0, width), Math.Max (0, height));
  77. return new Rect (new Point (rect.X + Left, rect.Y + Top), size);
  78. }
  79. private void FillRect (Rect rect, System.Rune rune = default)
  80. {
  81. for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
  82. for (var c = rect.X; c < rect.X + rect.Width; c++) {
  83. Application.Driver.Move (c, r);
  84. Application.Driver.AddRune (rune == default ? ' ' : rune);
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// Draws the <see cref="Thickness"/> rectangle with an optional diagnostics label.
  90. /// </summary>
  91. /// <remarks>
  92. /// If <see cref="ConsoleDriver.DiagnosticFlags"/> is set to <see cref="ConsoleDriver.DiagnosticFlags.FramePadding"/> then
  93. /// 'T', 'L', 'R', and 'B' glyphs will be used instead of space. If <see cref="ConsoleDriver.DiagnosticFlags"/>
  94. /// is set to <see cref="ConsoleDriver.DiagnosticFlags.FrameRuler"/> then a ruler will be drawn on the outer edge of the
  95. /// Thickness.
  96. /// </remarks>
  97. /// <param name="rect">The location and size of the rectangle that bounds the thickness rectangle, in
  98. /// screen coordinates.</param>
  99. /// <param name="label">The diagnostics label to draw on the bottom of the <see cref="Bottom"/>.</param>
  100. /// <returns>The inner rectangle remaining to be drawn.</returns>
  101. public Rect Draw (Rect rect, string label = null)
  102. {
  103. System.Rune clearChar = ' ';
  104. System.Rune leftChar = clearChar;
  105. System.Rune rightChar = clearChar;
  106. System.Rune topChar = clearChar;
  107. System.Rune bottomChar = clearChar;
  108. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding) == ConsoleDriver.DiagnosticFlags.FramePadding) {
  109. leftChar = 'L';
  110. rightChar = 'R';
  111. topChar = 'T';
  112. bottomChar = 'B';
  113. if (!string.IsNullOrEmpty (label)) {
  114. leftChar = rightChar = bottomChar = topChar = label [0];
  115. }
  116. }
  117. ustring hrule = ustring.Empty;
  118. ustring vrule = ustring.Empty;
  119. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  120. string h = "0123456789";
  121. hrule = h.Repeat ((int)Math.Ceiling ((double)(rect.Width) / (double)h.Length)) [0..(rect.Width)];
  122. string v = "0123456789";
  123. vrule = v.Repeat ((int)Math.Ceiling ((double)(rect.Height * 2) / (double)v.Length)) [0..(rect.Height * 2)];
  124. };
  125. // Draw the Top side
  126. FillRect (new Rect (rect.X, rect.Y, rect.Width, Math.Min (rect.Height, Top)), topChar);
  127. // Draw the Left side
  128. FillRect (new Rect (rect.X, rect.Y, Math.Min (rect.Width, Left), rect.Height), leftChar);
  129. // Draw the Right side
  130. FillRect (new Rect (Math.Max (0, rect.X + rect.Width - Right), rect.Y, Math.Min (rect.Width, Right), rect.Height), rightChar);
  131. // Draw the Bottom side
  132. FillRect (new Rect (rect.X, rect.Y + Math.Max (0, rect.Height - Bottom), rect.Width, Bottom), bottomChar);
  133. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  134. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  135. // Top
  136. Application.Driver.Move (rect.X, rect.Y);
  137. Application.Driver.AddStr (hrule);
  138. //Left
  139. for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
  140. Application.Driver.Move (rect.X, r);
  141. Application.Driver.AddRune (vrule [r - rect.Y]);
  142. }
  143. // Bottom
  144. Application.Driver.Move (rect.X, rect.Y + rect.Height - Bottom + 1);
  145. Application.Driver.AddStr (hrule);
  146. // Right
  147. for (var r = rect.Y + 1; r < rect.Y + rect.Height; r++) {
  148. Application.Driver.Move (rect.X + rect.Width - Right + 1, r);
  149. Application.Driver.AddRune (vrule [r - rect.Y]);
  150. }
  151. }
  152. // Draw the diagnostics label on the bottom
  153. var tf = new TextFormatter () {
  154. Text = label == null ? string.Empty : $"{label} {this}",
  155. Alignment = TextAlignment.Centered,
  156. VerticalAlignment = VerticalTextAlignment.Bottom
  157. };
  158. tf.Draw (rect, Application.Driver.CurrentAttribute, Application.Driver.CurrentAttribute, rect, false);
  159. return GetInnerRect (rect);
  160. }
  161. // TODO: add operator overloads
  162. /// <summary>
  163. /// Gets an empty thickness.
  164. /// </summary>
  165. public static Thickness Empty => new Thickness (0);
  166. /// <inheritdoc/>
  167. public override bool Equals (object obj)
  168. {
  169. //Check for null and compare run-time types.
  170. if ((obj == null) || !this.GetType ().Equals (obj.GetType ())) {
  171. return false;
  172. } else {
  173. return Equals ((Thickness)obj);
  174. }
  175. }
  176. /// <summary>Returns the thickness widths of the Thickness formatted as a string.</summary>
  177. /// <returns>The thickness widths as a string.</returns>
  178. public override string ToString ()
  179. {
  180. return $"(Left={Left},Top={Top},Right={Right},Bottom={Bottom})";
  181. }
  182. // IEquitable
  183. /// <inheritdoc/>
  184. public bool Equals (Thickness other)
  185. {
  186. return other is not null &&
  187. Left == other.Left &&
  188. Right == other.Right &&
  189. Top == other.Top &&
  190. Bottom == other.Bottom;
  191. }
  192. /// <inheritdoc/>
  193. public override int GetHashCode ()
  194. {
  195. int hashCode = 1380952125;
  196. hashCode = hashCode * -1521134295 + Left.GetHashCode ();
  197. hashCode = hashCode * -1521134295 + Right.GetHashCode ();
  198. hashCode = hashCode * -1521134295 + Top.GetHashCode ();
  199. hashCode = hashCode * -1521134295 + Bottom.GetHashCode ();
  200. return hashCode;
  201. }
  202. /// <inheritdoc/>
  203. public static bool operator == (Thickness left, Thickness right)
  204. {
  205. return EqualityComparer<Thickness>.Default.Equals (left, right);
  206. }
  207. /// <inheritdoc/>
  208. public static bool operator != (Thickness left, Thickness right)
  209. {
  210. return !(left == right);
  211. }
  212. }
  213. internal static class StringExtensions {
  214. public static string Repeat (this string instr, int n)
  215. {
  216. if (n <= 0) {
  217. return null;
  218. }
  219. if (string.IsNullOrEmpty (instr) || n == 1) {
  220. return instr;
  221. }
  222. return new StringBuilder (instr.Length * n)
  223. .Insert (0, instr, n)
  224. .ToString ();
  225. }
  226. }
  227. }