Transparent.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // ReSharper disable AccessToDisposedClosure
  2. #nullable enable
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Transparent", "Demonstrates View Transparency")]
  5. public sealed class Transparent : Scenario
  6. {
  7. public override void Main ()
  8. {
  9. // Init
  10. Application.Init ();
  11. // Setup - Create a top-level application window and configure it.
  12. Window appWindow = new ()
  13. {
  14. Title = GetQuitKeyAndName (),
  15. };
  16. appWindow.BorderStyle = LineStyle.None;
  17. appWindow.SchemeName = "Error";
  18. appWindow.Text = "App Text - Centered Vertically and Horizontally.\n2nd Line of Text.\n3rd Line of Text.";
  19. appWindow.TextAlignment = Alignment.Center;
  20. appWindow.VerticalTextAlignment = Alignment.Center;
  21. appWindow.ClearingViewport += (s, e) =>
  22. {
  23. if (s is View sender)
  24. {
  25. sender.FillRect (sender.Viewport, Glyphs.Stipple);
  26. }
  27. e.Cancel = true;
  28. };
  29. ViewportSettingsEditor viewportSettingsEditor = new ViewportSettingsEditor ()
  30. {
  31. Y = Pos.AnchorEnd (),
  32. //X = Pos.Right (adornmentsEditor),
  33. AutoSelectViewToEdit = true
  34. };
  35. appWindow.Add (viewportSettingsEditor);
  36. Button appButton = new Button ()
  37. {
  38. X = 10,
  39. Y = 4,
  40. Title = "_AppButton",
  41. };
  42. appButton.Accepting += (sender, args) =>
  43. {
  44. MessageBox.Query ((sender as View)?.App, "AppButton", "Transparency is cool!", "_Ok");
  45. args.Handled = true;
  46. };
  47. appWindow.Add (appButton);
  48. var tv = new TransparentView ()
  49. {
  50. X = 2,
  51. Y = 2,
  52. Width = Dim.Fill (10),
  53. Height = Dim.Fill (10)
  54. };
  55. appWindow.ViewportChanged += (sender, args) =>
  56. {
  57. // Little hack to convert the Dim.Fill to actual size
  58. // So resizing works
  59. tv.Width = appWindow!.Frame.Width - 10;
  60. tv.Height = appWindow!.Frame.Height - 10;
  61. };
  62. appWindow.Add (tv);
  63. // Run - Start the application.
  64. Application.Run (appWindow);
  65. appWindow.Dispose ();
  66. // Shutdown - Calling Application.Shutdown is required.
  67. Application.Shutdown ();
  68. }
  69. public class TransparentView : FrameView
  70. {
  71. public TransparentView ()
  72. {
  73. Title = "Transparent View - Move and Resize To See Transparency In Action";
  74. base.Text = "View.Text.\nThis should be opaque. Note how clipping works?";
  75. Arrangement = ViewArrangement.Overlapped | ViewArrangement.Resizable | ViewArrangement.Movable;
  76. ViewportSettings |= ViewportSettingsFlags.Transparent | ViewportSettingsFlags.TransparentMouse;
  77. BorderStyle = LineStyle.RoundedDotted;
  78. SchemeName = "Base";
  79. var transparentSubView = new View ()
  80. {
  81. Text = "Sizable/Movable SubView with border and shadow.",
  82. Id = "transparentSubView",
  83. X = Pos.Center (),
  84. Y = Pos.Center (),
  85. Width = 20,
  86. Height = 8,
  87. BorderStyle = LineStyle.Dashed,
  88. Arrangement = ViewArrangement.Movable | ViewArrangement.Resizable,
  89. ShadowStyle = ShadowStyle.Transparent,
  90. };
  91. transparentSubView.Border!.Thickness = new (1, 1, 1, 1);
  92. transparentSubView.SchemeName = "Dialog";
  93. Button button = new Button ()
  94. {
  95. Title = "_Opaque Shadow",
  96. X = Pos.Center (),
  97. Y = 2,
  98. SchemeName = "Dialog",
  99. };
  100. button.Accepting += (sender, args) =>
  101. {
  102. MessageBox.Query (App, "Clicked!", "Button in Transparent View", "_Ok");
  103. args.Handled = true;
  104. };
  105. var shortcut = new Shortcut ()
  106. {
  107. Id = "shortcut",
  108. X = Pos.Center (),
  109. Y = Pos.AnchorEnd (),
  110. Title = "A _Shortcut",
  111. HelpText = "Help!",
  112. Key = Key.F11,
  113. SchemeName = "Base"
  114. };
  115. button.ClearingViewport += (sender, args) =>
  116. {
  117. args.Cancel = true;
  118. };
  119. // Subscribe to DrawingContent event to draw "TUI"
  120. DrawingContent += TransparentView_DrawingContent;
  121. base.Add (button);
  122. base.Add (shortcut);
  123. base.Add (transparentSubView);
  124. Padding!.Thickness = new (1);
  125. Padding.Text = "This is the Padding";
  126. }
  127. private void TransparentView_DrawingContent (object? sender, DrawEventArgs e)
  128. {
  129. // Draw "TUI" text using rectangular regions, positioned after "Hi"
  130. // Letter "T"
  131. Rectangle tTop = new (20, 5, 7, 2); // Top horizontal bar
  132. Rectangle tStem = new (23, 7, 2, 8); // Vertical stem
  133. // Letter "U"
  134. Rectangle uLeft = new (30, 5, 2, 8); // Left vertical bar
  135. Rectangle uBottom = new (32, 13, 3, 2); // Bottom horizontal bar
  136. Rectangle uRight = new (35, 5, 2, 8); // Right vertical bar
  137. // Letter "I"
  138. Rectangle iTop = new (39, 5, 4, 2); // Bar on top
  139. Rectangle iStem = new (40, 7, 2, 6); // Vertical stem
  140. Rectangle iBottom = new (39, 13, 4, 2); // Bar on Bottom
  141. // Draw "TUI" using the HotActive attribute
  142. SetAttributeForRole (VisualRole.HotActive);
  143. FillRect (tTop, Glyphs.BlackCircle);
  144. FillRect (tStem, Glyphs.BlackCircle);
  145. FillRect (uLeft, Glyphs.BlackCircle);
  146. FillRect (uBottom, Glyphs.BlackCircle);
  147. FillRect (uRight, Glyphs.BlackCircle);
  148. FillRect (iTop, Glyphs.BlackCircle);
  149. FillRect (iStem, Glyphs.BlackCircle);
  150. FillRect (iBottom, Glyphs.BlackCircle);
  151. Region tuiRegion = new Region (ViewportToScreen (tTop));
  152. tuiRegion.Union (ViewportToScreen (tStem));
  153. tuiRegion.Union (ViewportToScreen (uLeft));
  154. tuiRegion.Union (ViewportToScreen (uBottom));
  155. tuiRegion.Union (ViewportToScreen (uRight));
  156. tuiRegion.Union (ViewportToScreen (iTop));
  157. tuiRegion.Union (ViewportToScreen (iStem));
  158. tuiRegion.Union (ViewportToScreen (iBottom));
  159. // Register the drawn region for "TUI" to enable transparency effects
  160. e.DrawContext?.AddDrawnRegion (tuiRegion);
  161. }
  162. /// <inheritdoc />
  163. protected override bool OnDrawingContent (DrawContext? context)
  164. {
  165. base.OnDrawingContent (context);
  166. // Draw "Hi" text using rectangular regions
  167. // Letter "H"
  168. Rectangle hLeft = new (5, 5, 2, 10); // Left vertical bar
  169. Rectangle hMiddle = new (7, 9, 3, 2); // Middle horizontal bar
  170. Rectangle hRight = new (10, 5, 2, 10); // Right vertical bar
  171. // Letter "i" (with some space between H and i)
  172. Rectangle iDot = new (15, 5, 2, 2); // Dot on top
  173. Rectangle iStem = new (15, 9, 2, 6); // Vertical stem
  174. // Draw "Hi" using the Highlight attribute
  175. SetAttributeForRole (VisualRole.Highlight);
  176. FillRect (hLeft, Glyphs.BlackCircle);
  177. FillRect (hMiddle, Glyphs.BlackCircle);
  178. FillRect (hRight, Glyphs.BlackCircle);
  179. FillRect (iDot, Glyphs.BlackCircle);
  180. FillRect (iStem, Glyphs.BlackCircle);
  181. // Register the drawn region for "Hi" to enable transparency effects
  182. Region hiRegion = new Region (ViewportToScreen (hLeft));
  183. hiRegion.Union (ViewportToScreen (hMiddle));
  184. hiRegion.Union (ViewportToScreen (hRight));
  185. hiRegion.Union (ViewportToScreen (iDot));
  186. hiRegion.Union (ViewportToScreen (iStem));
  187. context?.AddDrawnRegion (hiRegion);
  188. // Return false to allow DrawingContent event to fire
  189. return false;
  190. }
  191. protected override bool OnRenderingLineCanvas ()
  192. {
  193. // Draw "dotnet" using LineCanvas
  194. Point screenPos = ViewportToScreen (new Point (7, 16));
  195. DrawDotnet (LineCanvas, screenPos.X, screenPos.Y, LineStyle.Single, GetAttributeForRole (VisualRole.Normal));
  196. return false;
  197. }
  198. /// <inheritdoc />
  199. protected override bool OnClearingViewport () { return false; }
  200. /// <inheritdoc />
  201. protected override bool OnMouseEvent (MouseEventArgs mouseEvent) { return false; }
  202. /// <summary>
  203. /// Draws "dotnet" text using LineCanvas. The 'd' is 8 cells high.
  204. /// </summary>
  205. /// <param name="canvas">The LineCanvas to draw on</param>
  206. /// <param name="x">Starting X position</param>
  207. /// <param name="y">Starting Y position</param>
  208. /// <param name="style">Line style to use</param>
  209. /// <param name="attribute">Optional attribute for the lines</param>
  210. private void DrawDotnet (LineCanvas canvas, int x, int y, LineStyle style = LineStyle.Single, Attribute? attribute = null)
  211. {
  212. int currentX = x;
  213. int letterHeight = 8;
  214. int letterSpacing = 2;
  215. // Letter 'd' - lowercase, height 8
  216. // Vertical stem on right (goes up full 8 cells)
  217. canvas.AddLine (new (currentX + 3, y), letterHeight, Orientation.Vertical, style, attribute);
  218. // Top horizontal
  219. canvas.AddLine (new (currentX, y + 3), 4, Orientation.Horizontal, style, attribute);
  220. // Left vertical (only bottom 5 cells, leaving top 3 for ascender space)
  221. canvas.AddLine (new (currentX, y + 3), 5, Orientation.Vertical, style, attribute);
  222. // Bottom horizontal
  223. canvas.AddLine (new (currentX, y + 7), 4, Orientation.Horizontal, style, attribute);
  224. currentX += 4 + letterSpacing;
  225. // Letter 'o' - height 5 (x-height)
  226. int oY = y + 3; // Align with x-height (leaving 3 cells for ascenders)
  227. // Top
  228. canvas.AddLine (new (currentX, oY), 4, Orientation.Horizontal, style, attribute);
  229. // Left
  230. canvas.AddLine (new (currentX, oY), 5, Orientation.Vertical, style, attribute);
  231. // Right
  232. canvas.AddLine (new (currentX + 3, oY), 5, Orientation.Vertical, style, attribute);
  233. // Bottom
  234. canvas.AddLine (new (currentX, oY + 4), 4, Orientation.Horizontal, style, attribute);
  235. currentX += 4 + letterSpacing;
  236. // Letter 't' - height 7 (has ascender above x-height)
  237. int tY = y + 1; // Starts 1 cell above x-height
  238. // Vertical stem
  239. canvas.AddLine (new (currentX + 1, tY), 7, Orientation.Vertical, style, attribute);
  240. // Top cross bar (at x-height)
  241. canvas.AddLine (new (currentX, tY + 2), 3, Orientation.Horizontal, style, attribute);
  242. // Bottom horizontal (foot)
  243. canvas.AddLine (new (currentX + 1, tY + 6), 2, Orientation.Horizontal, style, attribute);
  244. currentX += 3 + letterSpacing;
  245. // Letter 'n' - height 5 (x-height)
  246. int nY = y + 3;
  247. // Left vertical
  248. canvas.AddLine (new (currentX, nY), 5, Orientation.Vertical, style, attribute);
  249. // Top horizontal
  250. canvas.AddLine (new (currentX + 1, nY), 3, Orientation.Horizontal, style, attribute);
  251. // Right vertical
  252. canvas.AddLine (new (currentX + 3, nY), 5, Orientation.Vertical, style, attribute);
  253. currentX += 4 + letterSpacing;
  254. // Letter 'e' - height 5 (x-height)
  255. int eY = y + 3;
  256. // Top
  257. canvas.AddLine (new (currentX, eY), 4, Orientation.Horizontal, style, attribute);
  258. // Left
  259. canvas.AddLine (new (currentX, eY), 5, Orientation.Vertical, style, attribute);
  260. // Right
  261. canvas.AddLine (new (currentX + 3, eY), 3, Orientation.Vertical, style, attribute);
  262. // Middle horizontal bar
  263. canvas.AddLine (new (currentX, eY + 2), 4, Orientation.Horizontal, style, attribute);
  264. // Bottom
  265. canvas.AddLine (new (currentX, eY + 4), 4, Orientation.Horizontal, style, attribute);
  266. currentX += 4 + letterSpacing;
  267. // Letter 't' - height 7 (has ascender above x-height) - second 't'
  268. int t2Y = y + 1;
  269. // Vertical stem
  270. canvas.AddLine (new (currentX + 1, t2Y), 7, Orientation.Vertical, style, attribute);
  271. // Top cross bar (at x-height)
  272. canvas.AddLine (new (currentX, t2Y + 2), 3, Orientation.Horizontal, style, attribute);
  273. // Bottom horizontal (foot)
  274. canvas.AddLine (new (currentX + 1, t2Y + 6), 2, Orientation.Horizontal, style, attribute);
  275. }
  276. }
  277. }