ShadowView.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #nullable enable
  2. using Microsoft.VisualBasic;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Draws a shadow on the right or bottom of the view. Used internally by <see cref="Margin"/>.
  7. /// </summary>
  8. internal class ShadowView : View
  9. {
  10. private ShadowStyle _shadowStyle;
  11. /// <inheritdoc/>
  12. public override Attribute GetNormalColor ()
  13. {
  14. if (SuperView is Adornment adornment)
  15. {
  16. var attr = Attribute.Default;
  17. if (adornment.Parent?.SuperView is { })
  18. {
  19. attr = adornment.Parent.SuperView.GetNormalColor ();
  20. }
  21. else if (Application.Top is { })
  22. {
  23. attr = Application.Top.GetNormalColor ();
  24. }
  25. return new (
  26. new Attribute (
  27. ShadowStyle == ShadowStyle.Opaque ? Color.Black : attr.Foreground.GetDarkerColor (),
  28. ShadowStyle == ShadowStyle.Opaque ? attr.Background : attr.Background.GetDarkerColor ()));
  29. }
  30. return base.GetNormalColor ();
  31. }
  32. /// <inheritdoc />
  33. /// <inheritdoc />
  34. protected override bool OnDrawingText ()
  35. {
  36. return true;
  37. }
  38. /// <inheritdoc />
  39. protected override bool OnClearingViewport ()
  40. {
  41. // Prevent clearing (so we can have transparency)
  42. return true;
  43. }
  44. /// <inheritdoc/>
  45. protected override bool OnDrawingContent ()
  46. {
  47. switch (ShadowStyle)
  48. {
  49. case ShadowStyle.Opaque:
  50. if (Orientation == Orientation.Vertical)
  51. {
  52. DrawVerticalShadowOpaque (Viewport);
  53. }
  54. else
  55. {
  56. DrawHorizontalShadowOpaque (Viewport);
  57. }
  58. break;
  59. case ShadowStyle.Transparent:
  60. //Attribute prevAttr = Driver.GetAttribute ();
  61. //var attr = new Attribute (prevAttr.Foreground, prevAttr.Background);
  62. //SetAttribute (attr);
  63. if (Orientation == Orientation.Vertical)
  64. {
  65. DrawVerticalShadowTransparent (Viewport);
  66. }
  67. else
  68. {
  69. DrawHorizontalShadowTransparent (Viewport);
  70. }
  71. //SetAttribute (prevAttr);
  72. break;
  73. }
  74. return true;
  75. }
  76. /// <summary>
  77. /// Gets or sets the orientation of the shadow.
  78. /// </summary>
  79. public Orientation Orientation { get; set; }
  80. public override ShadowStyle ShadowStyle
  81. {
  82. get => _shadowStyle;
  83. set
  84. {
  85. Visible = value != ShadowStyle.None;
  86. _shadowStyle = value;
  87. }
  88. }
  89. private void DrawHorizontalShadowOpaque (Rectangle rectangle)
  90. {
  91. // Draw the start glyph
  92. AddRune (0, 0, Glyphs.ShadowHorizontalStart);
  93. // Fill the rest of the rectangle with the glyph - note we skip the last since vertical will draw it
  94. for (var i = 1; i < rectangle.Width - 1; i++)
  95. {
  96. AddRune (i, 0, Glyphs.ShadowHorizontal);
  97. }
  98. // Last is special
  99. AddRune (rectangle.Width - 1, 0, Glyphs.ShadowHorizontalEnd);
  100. }
  101. private void DrawHorizontalShadowTransparent (Rectangle viewport)
  102. {
  103. Rectangle screen = ViewportToScreen (Viewport);
  104. for (int r = Math.Max (0, screen.Y); r < screen.Y + screen.Height; r++)
  105. {
  106. for (int c = Math.Max (0, screen.X + 1); c < screen.X + screen.Width; c++)
  107. {
  108. Driver?.Move (c, r);
  109. if (c < Driver?.Contents!.GetLength (1) && r < Driver?.Contents?.GetLength (0))
  110. {
  111. Driver.AddRune (Driver.Contents [r, c].Rune);
  112. }
  113. }
  114. }
  115. }
  116. private void DrawVerticalShadowOpaque (Rectangle viewport)
  117. {
  118. // Draw the start glyph
  119. AddRune (0, 0, Glyphs.ShadowVerticalStart);
  120. // Fill the rest of the rectangle with the glyph
  121. for (var i = 1; i < viewport.Height - 1; i++)
  122. {
  123. AddRune (0, i, Glyphs.ShadowVertical);
  124. }
  125. }
  126. private void DrawVerticalShadowTransparent (Rectangle viewport)
  127. {
  128. Rectangle screen = ViewportToScreen (Viewport);
  129. // Fill the rest of the rectangle
  130. for (int c = Math.Max (0, screen.X); c < screen.X + screen.Width; c++)
  131. {
  132. for (int r = Math.Max (0, screen.Y); r < screen.Y + viewport.Height; r++)
  133. {
  134. Driver?.Move (c, r);
  135. if (Driver?.Contents is { } && screen.X < Driver.Contents.GetLength (1) && r < Driver.Contents.GetLength (0))
  136. {
  137. Driver.AddRune (Driver.Contents [r, c].Rune);
  138. }
  139. }
  140. }
  141. }
  142. }