View.Drawing.Primitives.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using static Terminal.Gui.SpinnerStyle;
  2. namespace Terminal.Gui;
  3. public partial class View
  4. {
  5. /// <summary>Moves the drawing cursor to the specified <see cref="Viewport"/>-relative location in the view.</summary>
  6. /// <remarks>
  7. /// <para>
  8. /// If the provided coordinates are outside the visible content area, this method does nothing.
  9. /// </para>
  10. /// <para>
  11. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  12. /// </para>
  13. /// </remarks>
  14. /// <param name="col">Column (viewport-relative).</param>
  15. /// <param name="row">Row (viewport-relative).</param>
  16. public bool Move (int col, int row)
  17. {
  18. if (Driver is null || Driver?.Rows == 0)
  19. {
  20. return false;
  21. }
  22. if (col < 0 || row < 0 || col >= Viewport.Width || row >= Viewport.Height)
  23. {
  24. return false;
  25. }
  26. Point screen = ViewportToScreen (new Point (col, row));
  27. Driver?.Move (screen.X, screen.Y);
  28. return true;
  29. }
  30. /// <summary>Draws the specified character at the current draw position.</summary>
  31. /// <param name="rune">The Rune.</param>
  32. public void AddRune (Rune rune)
  33. {
  34. Driver?.AddRune (rune);
  35. }
  36. /// <summary>
  37. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  38. /// convenience method that calls <see cref="AddRune(Rune)"/> with the <see cref="Rune"/> constructor.
  39. /// </summary>
  40. /// <param name="c">Character to add.</param>
  41. public void AddRune (char c) { AddRune (new Rune (c)); }
  42. /// <summary>Draws the specified character in the specified viewport-relative column and row of the View.</summary>
  43. /// <para>
  44. /// If the provided coordinates are outside the visible content area, this method does nothing.
  45. /// </para>
  46. /// <remarks>
  47. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  48. /// </remarks>
  49. /// <param name="col">Column (viewport-relative).</param>
  50. /// <param name="row">Row (viewport-relative).</param>
  51. /// <param name="rune">The Rune.</param>
  52. public void AddRune (int col, int row, Rune rune)
  53. {
  54. if (Move (col, row))
  55. {
  56. Driver?.AddRune (rune);
  57. }
  58. }
  59. /// <summary>Adds the <paramref name="str"/> to the display at the current draw position.</summary>
  60. /// <remarks>
  61. /// <para>
  62. /// When the method returns, the draw position will be incremented by the number of columns
  63. /// <paramref name="str"/> required, unless the new column value is outside the <see cref="GetClip()"/> or <see cref="Application.Screen"/>.
  64. /// </para>
  65. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  66. /// </remarks>
  67. /// <param name="str">String.</param>
  68. public void AddStr (string str)
  69. {
  70. Driver?.AddStr (str);
  71. }
  72. /// <summary>Utility function to draw strings that contain a hotkey.</summary>
  73. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  74. /// <param name="hotColor">Hot color.</param>
  75. /// <param name="normalColor">Normal color.</param>
  76. /// <remarks>
  77. /// <para>
  78. /// The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by
  79. /// default.
  80. /// </para>
  81. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  82. /// </remarks>
  83. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  84. {
  85. Rune hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  86. SetAttribute (normalColor);
  87. foreach (Rune rune in text.EnumerateRunes ())
  88. {
  89. if (rune == new Rune (hotkeySpec.Value))
  90. {
  91. SetAttribute (hotColor);
  92. continue;
  93. }
  94. AddRune (rune);
  95. SetAttribute (normalColor);
  96. }
  97. }
  98. /// <summary>
  99. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused"
  100. /// state.
  101. /// </summary>
  102. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  103. /// <param name="focused">
  104. /// If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise
  105. /// the regular ones.
  106. /// </param>
  107. public void DrawHotString (string text, bool focused)
  108. {
  109. if (focused)
  110. {
  111. DrawHotString (text, GetHotFocusColor (), GetFocusColor ());
  112. }
  113. else
  114. {
  115. DrawHotString (
  116. text,
  117. Enabled ? GetHotNormalColor () : ColorScheme!.Disabled,
  118. Enabled ? GetNormalColor () : ColorScheme!.Disabled
  119. );
  120. }
  121. }
  122. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle with the specified color.</summary>
  123. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  124. /// <param name="color">The color to use to fill the rectangle. If not provided, the Normal background color will be used.</param>
  125. public void FillRect (Rectangle rect, Color? color = null)
  126. {
  127. if (Driver is null)
  128. {
  129. return;
  130. }
  131. Region prevClip = ClipViewport ();
  132. Rectangle toClear = ViewportToScreen (rect);
  133. Attribute prev = SetAttribute (new (color ?? GetNormalColor ().Background));
  134. Driver.FillRect (toClear);
  135. SetAttribute (prev);
  136. SetClip (prevClip);
  137. }
  138. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle.</summary>
  139. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  140. /// <param name="rune">The Rune to fill with.</param>
  141. public void FillRect (Rectangle rect, Rune rune)
  142. {
  143. if (Driver is null)
  144. {
  145. return;
  146. }
  147. Region prevClip = ClipViewport ();
  148. Rectangle toClear = ViewportToScreen (rect);
  149. Driver.FillRect (toClear, rune);
  150. SetClip (prevClip);
  151. }
  152. }