View.Drawing.Primitives.cs 6.0 KB

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