Explorar el Código

Flesh out drawing scenario

tznind hace 2 años
padre
commit
b708150eda
Se han modificado 2 ficheros con 131 adiciones y 17 borrados
  1. 23 0
      Terminal.Gui/Core/Graphs/StraightLineCanvas.cs
  2. 108 17
      UICatalog/Scenarios/Drawing.cs

+ 23 - 0
Terminal.Gui/Core/Graphs/StraightLineCanvas.cs

@@ -66,6 +66,28 @@ namespace Terminal.Gui.Graphs {
 			return canvas;
 		}
 
+		/// <summary>
+		/// Draws all the lines that lie within the <paramref name="bounds"/> onto
+		/// the <paramref name="view"/> client area.  This method should be called from
+		/// <see cref="View.Redraw(Rect)"/>.
+		/// </summary>
+		/// <param name="view"></param>
+		/// <param name="bounds"></param>
+		public void Draw (View view, Rect bounds)
+		{
+			var runes = GenerateImage (bounds);
+
+			for (int y = bounds.Y; y < bounds.Height; y++) {
+				for (int x = bounds.X; x < bounds.Width; x++) {
+					var rune = runes [y, x];
+
+					if (rune.HasValue) {
+						view.AddRune (x, y, rune.Value);
+					}
+				}
+			}
+		}
+
 		private Rune? GetRuneForIntersects (IntersectionDefinition [] intersects)
 		{
 			if (!intersects.Any ())
@@ -252,6 +274,7 @@ namespace Terminal.Gui.Graphs {
 		{
 			return intersects.SetEquals (types);
 		}
+
 		class IntersectionDefinition {
 			/// <summary>
 			/// The point at which the intersection happens

+ 108 - 17
UICatalog/Scenarios/Drawing.cs

@@ -1,4 +1,6 @@
 using System;
+using System.Collections.Generic;
+using System.Reflection.Metadata.Ecma335;
 using Terminal.Gui;
 using Terminal.Gui.Graphs;
 
@@ -11,39 +13,122 @@ namespace UICatalog.Scenarios {
 
 		public override void Setup ()
 		{
-			var canvas = new DrawingPanel {
-				Width = Dim.Fill (),
+			var toolsWidth = 8;
+
+			var canvas = new DrawingArea {
+				Width = Dim.Fill (-toolsWidth),
 				Height = Dim.Fill ()
 			};
 
+			var tools = new ToolsView (toolsWidth) {
+				Y = 1,
+				X = Pos.AnchorEnd (toolsWidth+1),
+				Height = Dim.Fill (),
+				Width = Dim.Fill ()				
+			};
+
+
+			tools.ColorChanged += (c) => canvas.SetColor (c);
+
 			Win.Add(canvas);
+			Win.Add (tools);
+			Win.Add (new Label (" -Tools-") { X = Pos.AnchorEnd (toolsWidth + 1) });
 		}
 
-		class DrawingPanel : View
+		class ToolsView : View {
+
+			StraightLineCanvas grid;
+			public event Action<Color> ColorChanged;
+
+			Dictionary<Point, Color> swatches = new Dictionary<Point, Color> {
+				{ new Point(1,1),Color.Red},
+				{ new Point(3,1),Color.Green},
+				{ new Point(5,1),Color.BrightBlue},
+				{ new Point(7,1),Color.Black},
+				{ new Point(1,3),Color.White},
+			};
+
+			public ToolsView (int width)
+			{
+				grid = new StraightLineCanvas (Application.Driver);
+
+				grid.AddLine (new Point (0, 0), int.MaxValue, Orientation.Vertical, BorderStyle.Single);
+				grid.AddLine (new Point (0, 0), width, Orientation.Horizontal, BorderStyle.Single);
+				grid.AddLine (new Point (width, 0), int.MaxValue, Orientation.Vertical, BorderStyle.Single);
+
+				grid.AddLine (new Point (0, 2), width, Orientation.Horizontal, BorderStyle.Single);
+
+				grid.AddLine (new Point (2, 0), int.MaxValue, Orientation.Vertical, BorderStyle.Single);
+				grid.AddLine (new Point (4, 0), int.MaxValue, Orientation.Vertical, BorderStyle.Single);
+				grid.AddLine (new Point (6, 0), int.MaxValue, Orientation.Vertical, BorderStyle.Single);
+
+
+				grid.AddLine (new Point (0, 4), width, Orientation.Horizontal, BorderStyle.Single);
+			}
+			public override void Redraw (Rect bounds)
+			{
+				base.Redraw (bounds);
+
+				Driver.SetAttribute (new Terminal.Gui.Attribute (Color.Gray, ColorScheme.Normal.Background));
+				grid.Draw (this, bounds);
+
+				foreach(var swatch in swatches) {
+					Driver.SetAttribute (new Terminal.Gui.Attribute (swatch.Value, ColorScheme.Normal.Background));
+					AddRune (swatch.Key.X, swatch.Key.Y, '█');
+				}
+			}
+
+			public override bool OnMouseEvent (MouseEvent mouseEvent)
+			{
+				if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)) {
+					foreach (var swatch in swatches) {
+						if(mouseEvent.X == swatch.Key.X && mouseEvent.Y == swatch.Key.Y) {
+
+							ColorChanged?.Invoke (swatch.Value);
+							return true;
+						}
+					}
+				}
+
+				return base.OnMouseEvent (mouseEvent);
+			}
+		}
+
+		class DrawingArea : View
 		{
-			StraightLineCanvas canvas;
+			/// <summary>
+			/// Index into <see cref="canvases"/> by color.
+			/// </summary>
+			Dictionary<Color, int> colorLayers = new Dictionary<Color, int> ();
+			List<StraightLineCanvas> canvases = new List<StraightLineCanvas>();
+			int currentColor;
+
 			Point? currentLineStart = null;
 
-			public DrawingPanel ()
+			public DrawingArea ()
 			{
-				canvas = new StraightLineCanvas (Application.Driver);
+				AddCanvas (Color.White);
+			}
+
+			private void AddCanvas (Color c)
+			{
+				if(colorLayers.ContainsKey(c)) {
+					return;
+				}
+
+				canvases.Add (new StraightLineCanvas (Application.Driver));
+				colorLayers.Add (c, canvases.Count-1);
+				currentColor = canvases.Count - 1;
 			}
 
 			public override void Redraw (Rect bounds)
 			{
 				base.Redraw (bounds);
-
-				var runes = canvas.GenerateImage (bounds);
 				
-				for(int y=0;y<bounds.Height;y++) {
-					for (int x = 0; x < bounds.Width; x++) {
-						var rune = runes [y, x];
-						
-						if(rune.HasValue) {
-							AddRune (x, y, rune.Value);
-						}
-					}
+				foreach(var kvp in colorLayers) {
 
+					Driver.SetAttribute (new Terminal.Gui.Attribute (kvp.Key, ColorScheme.Normal.Background));
+					canvases [kvp.Value].Draw (this, bounds);
 				}
 			}
 			public override bool OnMouseEvent (MouseEvent mouseEvent)
@@ -70,7 +155,7 @@ namespace UICatalog.Scenarios {
 						}
 
 
-						canvas.AddLine (start, length, orientation, BorderStyle.Single);
+						canvases [currentColor].AddLine (start, length, orientation, BorderStyle.Single);
 						currentLineStart = null;
 						SetNeedsDisplay ();
 					}
@@ -78,6 +163,12 @@ namespace UICatalog.Scenarios {
 
 				return base.OnMouseEvent (mouseEvent);
 			}
+
+			internal void SetColor (Color c)
+			{
+				AddCanvas (c);
+				currentColor = colorLayers [c];
+			}
 		}
 	}
 }