LineDrawing.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection.Metadata.Ecma335;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios {
  6. [ScenarioMetadata (Name: "Line Drawing", Description: "Demonstrates LineCanvas.")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("Layout")]
  9. public class LineDrawing : Scenario {
  10. public override void Setup ()
  11. {
  12. var toolsWidth = 8;
  13. var canvas = new DrawingArea {
  14. Width = Dim.Fill (-toolsWidth),
  15. Height = Dim.Fill ()
  16. };
  17. var tools = new ToolsView (toolsWidth) {
  18. Y = 1,
  19. X = Pos.AnchorEnd (toolsWidth + 1),
  20. Height = Dim.Fill (),
  21. Width = Dim.Fill ()
  22. };
  23. tools.ColorChanged += (c) => canvas.SetColor (c);
  24. tools.SetStyle += (b) => canvas.BorderStyle = b;
  25. Win.Add (canvas);
  26. Win.Add (tools);
  27. Win.Add (new Label (" -Tools-") { X = Pos.AnchorEnd (toolsWidth + 1) });
  28. }
  29. class ToolsView : View {
  30. LineCanvas grid;
  31. public event Action<Color> ColorChanged;
  32. public event Action<LineStyle> SetStyle;
  33. Dictionary<Point, Color> swatches = new Dictionary<Point, Color> {
  34. { new Point(1,1),Color.Red},
  35. { new Point(3,1),Color.Green},
  36. { new Point(5,1),Color.BrightBlue},
  37. { new Point(7,1),Color.Black},
  38. { new Point(1,3),Color.White},
  39. };
  40. public ToolsView (int width)
  41. {
  42. grid = new LineCanvas ();
  43. grid.AddLine (new Point (0, 0), int.MaxValue, Orientation.Vertical, LineStyle.Single);
  44. grid.AddLine (new Point (0, 0), width, Orientation.Horizontal, LineStyle.Single);
  45. grid.AddLine (new Point (width, 0), int.MaxValue, Orientation.Vertical, LineStyle.Single);
  46. grid.AddLine (new Point (0, 2), width, Orientation.Horizontal, LineStyle.Single);
  47. grid.AddLine (new Point (2, 0), int.MaxValue, Orientation.Vertical, LineStyle.Single);
  48. grid.AddLine (new Point (4, 0), int.MaxValue, Orientation.Vertical, LineStyle.Single);
  49. grid.AddLine (new Point (6, 0), int.MaxValue, Orientation.Vertical, LineStyle.Single);
  50. grid.AddLine (new Point (0, 4), width, Orientation.Horizontal, LineStyle.Single);
  51. }
  52. public override void Redraw (Rect bounds)
  53. {
  54. base.Redraw (bounds);
  55. Driver.SetAttribute (new Terminal.Gui.Attribute (Color.DarkGray, ColorScheme.Normal.Background));
  56. foreach(var p in grid.GetMap(bounds))
  57. {
  58. this.AddRune(p.Key.X,p.Key.Y,p.Value);
  59. }
  60. foreach (var swatch in swatches) {
  61. Driver.SetAttribute (new Terminal.Gui.Attribute (swatch.Value, ColorScheme.Normal.Background));
  62. AddRune (swatch.Key.X, swatch.Key.Y, '█');
  63. }
  64. Driver.SetAttribute (new Terminal.Gui.Attribute (ColorScheme.Normal.Foreground, ColorScheme.Normal.Background));
  65. AddRune (3, 3, Application.Driver.HDLine);
  66. AddRune (5, 3, Application.Driver.HLine);
  67. AddRune (7, 3, Application.Driver.ULRCorner);
  68. }
  69. public override bool OnMouseEvent (MouseEvent mouseEvent)
  70. {
  71. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)) {
  72. foreach (var swatch in swatches) {
  73. if (mouseEvent.X == swatch.Key.X && mouseEvent.Y == swatch.Key.Y) {
  74. ColorChanged?.Invoke (swatch.Value);
  75. return true;
  76. }
  77. }
  78. if (mouseEvent.X == 3 && mouseEvent.Y == 3) {
  79. SetStyle?.Invoke (LineStyle.Double);
  80. return true;
  81. }
  82. if (mouseEvent.X == 5 && mouseEvent.Y == 3) {
  83. SetStyle?.Invoke (LineStyle.Single);
  84. return true;
  85. }
  86. if (mouseEvent.X == 7 && mouseEvent.Y == 3) {
  87. SetStyle?.Invoke (LineStyle.Rounded);
  88. return true;
  89. }
  90. }
  91. return base.OnMouseEvent (mouseEvent);
  92. }
  93. }
  94. class DrawingArea : View {
  95. /// <summary>
  96. /// Index into <see cref="canvases"/> by color.
  97. /// </summary>
  98. Dictionary<Color, int> colorLayers = new Dictionary<Color, int> ();
  99. List<LineCanvas> canvases = new List<LineCanvas> ();
  100. int currentColor;
  101. Point? currentLineStart = null;
  102. public DrawingArea ()
  103. {
  104. AddCanvas (Color.White);
  105. }
  106. private void AddCanvas (Color c)
  107. {
  108. if (colorLayers.ContainsKey (c)) {
  109. return;
  110. }
  111. canvases.Add (new LineCanvas ());
  112. colorLayers.Add (c, canvases.Count - 1);
  113. currentColor = canvases.Count - 1;
  114. }
  115. public override void Redraw (Rect bounds)
  116. {
  117. base.Redraw (bounds);
  118. foreach (var kvp in colorLayers) {
  119. Driver.SetAttribute (new Terminal.Gui.Attribute (kvp.Key, ColorScheme.Normal.Background));
  120. var canvas = canvases [kvp.Value];
  121. foreach(var p in canvas.GetMap(bounds))
  122. {
  123. this.AddRune(p.Key.X,p.Key.Y,p.Value);
  124. }
  125. }
  126. }
  127. public override bool OnMouseEvent (MouseEvent mouseEvent)
  128. {
  129. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)) {
  130. if (currentLineStart == null) {
  131. currentLineStart = new Point (mouseEvent.X, mouseEvent.Y);
  132. }
  133. } else {
  134. if (currentLineStart != null) {
  135. var start = currentLineStart.Value;
  136. var end = new Point (mouseEvent.X, mouseEvent.Y);
  137. var orientation = Orientation.Vertical;
  138. var length = end.Y - start.Y;
  139. // if line is wider than it is tall switch to horizontal
  140. if (Math.Abs (start.X - end.X) > Math.Abs (start.Y - end.Y)) {
  141. orientation = Orientation.Horizontal;
  142. length = end.X - start.X;
  143. }
  144. canvases [currentColor].AddLine (
  145. start,
  146. length,
  147. orientation,
  148. BorderStyle);
  149. currentLineStart = null;
  150. SetNeedsDisplay ();
  151. }
  152. }
  153. return base.OnMouseEvent (mouseEvent);
  154. }
  155. internal void SetColor (Color c)
  156. {
  157. AddCanvas (c);
  158. currentColor = colorLayers [c];
  159. }
  160. }
  161. }
  162. }