2
0

View.Drawing.Primitives.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. namespace Terminal.Gui;
  2. public partial class View
  3. {
  4. #region Drawing Primitives
  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 in the specified viewport-relative column and row of the View.</summary>
  31. /// <para>
  32. /// If the provided coordinates are outside the visible content area, this method does nothing.
  33. /// </para>
  34. /// <remarks>
  35. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  36. /// </remarks>
  37. /// <param name="col">Column (viewport-relative).</param>
  38. /// <param name="row">Row (viewport-relative).</param>
  39. /// <param name="rune">The Rune.</param>
  40. public void AddRune (int col, int row, Rune rune)
  41. {
  42. if (Move (col, row))
  43. {
  44. Driver?.AddRune (rune);
  45. }
  46. }
  47. /// <summary>Utility function to draw strings that contain a hotkey.</summary>
  48. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  49. /// <param name="hotColor">Hot color.</param>
  50. /// <param name="normalColor">Normal color.</param>
  51. /// <remarks>
  52. /// <para>
  53. /// The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by
  54. /// default.
  55. /// </para>
  56. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  57. /// </remarks>
  58. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  59. {
  60. Rune hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  61. SetAttribute (normalColor);
  62. foreach (Rune rune in text.EnumerateRunes ())
  63. {
  64. if (rune == new Rune (hotkeySpec.Value))
  65. {
  66. SetAttribute (hotColor);
  67. continue;
  68. }
  69. Application.Driver?.AddRune (rune);
  70. SetAttribute (normalColor);
  71. }
  72. }
  73. /// <summary>
  74. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused"
  75. /// state.
  76. /// </summary>
  77. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  78. /// <param name="focused">
  79. /// If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise
  80. /// the regular ones.
  81. /// </param>
  82. public void DrawHotString (string text, bool focused)
  83. {
  84. if (focused)
  85. {
  86. DrawHotString (text, GetHotFocusColor (), GetFocusColor ());
  87. }
  88. else
  89. {
  90. DrawHotString (
  91. text,
  92. Enabled ? GetHotNormalColor () : ColorScheme!.Disabled,
  93. Enabled ? GetNormalColor () : ColorScheme!.Disabled
  94. );
  95. }
  96. }
  97. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle with the specified color.</summary>
  98. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  99. /// <param name="color">The color to use to fill the rectangle. If not provided, the Normal background color will be used.</param>
  100. public void FillRect (Rectangle rect, Color? color = null)
  101. {
  102. if (Driver is null)
  103. {
  104. return;
  105. }
  106. Region prevClip = SetClip ();
  107. Rectangle toClear = ViewportToScreen (rect);
  108. Attribute prev = SetAttribute (new (color ?? GetNormalColor ().Background));
  109. Driver.FillRect (toClear);
  110. SetAttribute (prev);
  111. Driver.Clip = prevClip;
  112. }
  113. #endregion Drawing Primitives
  114. }