View.Drawing.Primitives.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 cursor position.</summary>
  60. /// <remarks>
  61. /// <para>
  62. /// When the method returns, <see cref="Col"/> will be incremented by the number of columns
  63. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="Clip"/> or screen
  64. /// dimensions defined by <see cref="Cols"/>.
  65. /// </para>
  66. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  67. /// </remarks>
  68. /// <param name="str">String.</param>
  69. public void AddStr (string str)
  70. {
  71. Driver?.AddStr (str);
  72. }
  73. /// <summary>Utility function to draw strings that contain a hotkey.</summary>
  74. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  75. /// <param name="hotColor">Hot color.</param>
  76. /// <param name="normalColor">Normal color.</param>
  77. /// <remarks>
  78. /// <para>
  79. /// The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by
  80. /// default.
  81. /// </para>
  82. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  83. /// </remarks>
  84. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  85. {
  86. Rune hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  87. SetAttribute (normalColor);
  88. foreach (Rune rune in text.EnumerateRunes ())
  89. {
  90. if (rune == new Rune (hotkeySpec.Value))
  91. {
  92. SetAttribute (hotColor);
  93. continue;
  94. }
  95. AddRune (rune);
  96. SetAttribute (normalColor);
  97. }
  98. }
  99. /// <summary>
  100. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused"
  101. /// state.
  102. /// </summary>
  103. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  104. /// <param name="focused">
  105. /// If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise
  106. /// the regular ones.
  107. /// </param>
  108. public void DrawHotString (string text, bool focused)
  109. {
  110. if (focused)
  111. {
  112. DrawHotString (text, GetHotFocusColor (), GetFocusColor ());
  113. }
  114. else
  115. {
  116. DrawHotString (
  117. text,
  118. Enabled ? GetHotNormalColor () : ColorScheme!.Disabled,
  119. Enabled ? GetNormalColor () : ColorScheme!.Disabled
  120. );
  121. }
  122. }
  123. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle with the specified color.</summary>
  124. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  125. /// <param name="color">The color to use to fill the rectangle. If not provided, the Normal background color will be used.</param>
  126. public void FillRect (Rectangle rect, Color? color = null)
  127. {
  128. if (Driver is null)
  129. {
  130. return;
  131. }
  132. Region prevClip = ClipViewport ();
  133. Rectangle toClear = ViewportToScreen (rect);
  134. Attribute prev = SetAttribute (new (color ?? GetNormalColor ().Background));
  135. Driver.FillRect (toClear);
  136. SetAttribute (prev);
  137. SetClip (prevClip);
  138. }
  139. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle.</summary>
  140. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  141. /// <param name="rune">The Rune to fill with.</param>
  142. public void FillRect (Rectangle rect, Rune rune)
  143. {
  144. if (Driver is null)
  145. {
  146. return;
  147. }
  148. Region prevClip = ClipViewport ();
  149. Rectangle toClear = ViewportToScreen (rect);
  150. Driver.FillRect (toClear, rune);
  151. SetClip (prevClip);
  152. }
  153. }