View.Drawing.Primitives.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 
  2. namespace Terminal.Gui.ViewBase;
  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>Draws the specified <paramref name="str"/> in the specified viewport-relative column and row of the View.</summary>
  66. /// <para>
  67. /// If the provided coordinates are outside the visible content area, this method does nothing.
  68. /// </para>
  69. /// <remarks>
  70. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  71. /// </remarks>
  72. /// <param name="col">Column (viewport-relative).</param>
  73. /// <param name="row">Row (viewport-relative).</param>
  74. /// <param name="str">The Text.</param>
  75. public void AddStr (int col, int row, string str)
  76. {
  77. if (Move (col, row))
  78. {
  79. Driver?.AddStr (str);
  80. }
  81. }
  82. /// <summary>Utility function to draw strings that contain a hotkey.</summary>
  83. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  84. /// <param name="hotColor">Hot color.</param>
  85. /// <param name="normalColor">Normal color.</param>
  86. /// <remarks>
  87. /// <para>
  88. /// The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by
  89. /// default.
  90. /// </para>
  91. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  92. /// </remarks>
  93. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  94. {
  95. Rune hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  96. SetAttribute (normalColor);
  97. foreach (Rune rune in text.EnumerateRunes ())
  98. {
  99. if (rune == new Rune (hotkeySpec.Value))
  100. {
  101. SetAttribute (hotColor);
  102. continue;
  103. }
  104. AddRune (rune);
  105. SetAttribute (normalColor);
  106. }
  107. }
  108. /// <summary>
  109. /// Utility function to draw strings that contains a hotkey using a <see cref="Scheme"/> and the "focused"
  110. /// state.
  111. /// </summary>
  112. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  113. /// <param name="focused">
  114. /// If set to <see langword="true"/> this uses the focused colors from the scheme, otherwise
  115. /// the regular ones.
  116. /// </param>
  117. public void DrawHotString (string text, bool focused)
  118. {
  119. if (focused)
  120. {
  121. DrawHotString (text, GetAttributeForRole (VisualRole.HotFocus), GetAttributeForRole (VisualRole.Focus));
  122. }
  123. else
  124. {
  125. DrawHotString (
  126. text,
  127. Enabled ? GetAttributeForRole (VisualRole.HotNormal) : GetScheme ().Disabled,
  128. Enabled ? GetAttributeForRole (VisualRole.Normal) : GetScheme ().Disabled
  129. );
  130. }
  131. }
  132. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle with the specified color.</summary>
  133. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  134. /// <param name="color">The color to use to fill the rectangle. If not provided, the Normal background color will be used.</param>
  135. public void FillRect (Rectangle rect, Color? color = null)
  136. {
  137. if (Driver is null)
  138. {
  139. return;
  140. }
  141. Region? prevClip = AddViewportToClip ();
  142. Rectangle toClear = ViewportToScreen (rect);
  143. Attribute prev = SetAttribute (new (color ?? GetAttributeForRole (VisualRole.Normal).Background));
  144. Driver.FillRect (toClear);
  145. SetAttribute (prev);
  146. SetClip (prevClip);
  147. }
  148. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle.</summary>
  149. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  150. /// <param name="rune">The Rune to fill with.</param>
  151. public void FillRect (Rectangle rect, Rune rune)
  152. {
  153. if (Driver is null)
  154. {
  155. return;
  156. }
  157. Region? prevClip = AddViewportToClip ();
  158. Rectangle toClear = ViewportToScreen (rect);
  159. Driver.FillRect (toClear, rune);
  160. SetClip (prevClip);
  161. }
  162. }