Browse Source

Add support for double lines into StraightLineCanvas

tznind 2 years ago
parent
commit
ab5f4ccfb5
2 changed files with 79 additions and 40 deletions
  1. 31 17
      Terminal.Gui/Core/Graphs/StraightLineCanvas.cs
  2. 48 23
      UICatalog/Scenarios/Drawing.cs

+ 31 - 17
Terminal.Gui/Core/Graphs/StraightLineCanvas.cs

@@ -94,21 +94,35 @@ namespace Terminal.Gui.Graphs {
 				return null;
 
 			var runeType = GetRuneTypeForIntersects (intersects);
+			var useDouble = intersects.Any (i => i.Line.Style == BorderStyle.Double && i.Line.Length != 0);
 
 			switch (runeType) {
-			case IntersectionRuneType.None: return null;
-			case IntersectionRuneType.Dot: return (Rune)'.';
-			case IntersectionRuneType.ULCorner: return driver.ULCorner;
-			case IntersectionRuneType.URCorner: return driver.URCorner;
-			case IntersectionRuneType.LLCorner: return driver.LLCorner;
-			case IntersectionRuneType.LRCorner: return driver.LRCorner;
-			case IntersectionRuneType.TopTee: return driver.TopTee;
-			case IntersectionRuneType.BottomTee: return driver.BottomTee;
-			case IntersectionRuneType.RightTee: return driver.RightTee;
-			case IntersectionRuneType.LeftTee: return driver.LeftTee;
-			case IntersectionRuneType.Crosshair: return '┼';
-			case IntersectionRuneType.HLine: return driver.HLine;
-			case IntersectionRuneType.VLine: return driver.VLine;
+			case IntersectionRuneType.None: 
+				return null;
+			case IntersectionRuneType.Dot: 
+				return (Rune)'.';
+			case IntersectionRuneType.ULCorner:
+				return useDouble ? driver.ULDCorner : driver.ULCorner;
+			case IntersectionRuneType.URCorner: 
+				return useDouble ? driver.URDCorner : driver.URCorner;
+			case IntersectionRuneType.LLCorner: 
+				return useDouble ? driver.LLDCorner : driver.LLCorner;
+			case IntersectionRuneType.LRCorner: 
+				return useDouble ? driver.LRDCorner : driver.LRCorner;
+			case IntersectionRuneType.TopTee: 
+				return useDouble ? '╦' : driver.TopTee;
+			case IntersectionRuneType.BottomTee: 
+				return useDouble ? '╩' : driver.BottomTee;
+			case IntersectionRuneType.RightTee: 
+				return useDouble ? '╣' : driver.RightTee;
+			case IntersectionRuneType.LeftTee: 
+				return useDouble ? '╠' : driver.LeftTee;
+			case IntersectionRuneType.Crosshair: 
+				return useDouble ? '╬' : '┼';
+			case IntersectionRuneType.HLine: 
+				return useDouble ? driver.HDLine : driver.HLine;
+			case IntersectionRuneType.VLine: 
+				return useDouble ? driver.VDLine : driver.VLine;
 			default: throw new ArgumentOutOfRangeException (nameof (runeType));
 			}
 
@@ -120,7 +134,7 @@ namespace Terminal.Gui.Graphs {
 			// ignore dots
 			intersects = intersects.Where (i => i.Type != IntersectionType.Dot).ToArray ();
 
-			var set = new HashSet<IntersectionType>(intersects.Select(i=>i.Type));
+			var set = new HashSet<IntersectionType> (intersects.Select (i => i.Type));
 
 			#region Crosshair Conditions
 			if (Has (set,
@@ -235,7 +249,7 @@ namespace Terminal.Gui.Graphs {
 			}
 			#endregion
 
-			if(All(intersects, Orientation.Horizontal)) {
+			if (All (intersects, Orientation.Horizontal)) {
 				return IntersectionRuneType.HLine;
 			}
 
@@ -246,9 +260,9 @@ namespace Terminal.Gui.Graphs {
 			return IntersectionRuneType.Dot;
 		}
 
-		private bool All (IntersectionDefinition[] intersects, Orientation orientation)
+		private bool All (IntersectionDefinition [] intersects, Orientation orientation)
 		{
-			return intersects.All (i=>i.Line.Orientation == orientation);
+			return intersects.All (i => i.Line.Orientation == orientation);
 		}
 
 		/// <summary>

+ 48 - 23
UICatalog/Scenarios/Drawing.cs

@@ -22,15 +22,16 @@ namespace UICatalog.Scenarios {
 
 			var tools = new ToolsView (toolsWidth) {
 				Y = 1,
-				X = Pos.AnchorEnd (toolsWidth+1),
+				X = Pos.AnchorEnd (toolsWidth + 1),
 				Height = Dim.Fill (),
-				Width = Dim.Fill ()				
+				Width = Dim.Fill ()
 			};
 
 
 			tools.ColorChanged += (c) => canvas.SetColor (c);
+			tools.SetHeavy += (b) => canvas.Heavy = b;
 
-			Win.Add(canvas);
+			Win.Add (canvas);
 			Win.Add (tools);
 			Win.Add (new Label (" -Tools-") { X = Pos.AnchorEnd (toolsWidth + 1) });
 		}
@@ -39,6 +40,7 @@ namespace UICatalog.Scenarios {
 
 			StraightLineCanvas grid;
 			public event Action<Color> ColorChanged;
+			public event Action<bool> SetHeavy;
 
 			Dictionary<Point, Color> swatches = new Dictionary<Point, Color> {
 				{ new Point(1,1),Color.Red},
@@ -62,49 +64,68 @@ namespace UICatalog.Scenarios {
 				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));
+				Driver.SetAttribute (new Terminal.Gui.Attribute (Color.DarkGray, ColorScheme.Normal.Background));
 				grid.Draw (this, bounds);
 
-				foreach(var swatch in swatches) {
+				foreach (var swatch in swatches) {
 					Driver.SetAttribute (new Terminal.Gui.Attribute (swatch.Value, ColorScheme.Normal.Background));
 					AddRune (swatch.Key.X, swatch.Key.Y, '█');
 				}
+
+				Driver.SetAttribute (new Terminal.Gui.Attribute (ColorScheme.Normal.Foreground, ColorScheme.Normal.Background));
+				AddRune (3, 3, Application.Driver.HDLine);
+				AddRune (5, 3, Application.Driver.HLine);
 			}
 
 			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) {
+						if (mouseEvent.X == swatch.Key.X && mouseEvent.Y == swatch.Key.Y) {
 
 							ColorChanged?.Invoke (swatch.Value);
 							return true;
 						}
 					}
+
+					if (mouseEvent.X == 3 && mouseEvent.Y == 3) {
+
+						SetHeavy?.Invoke (true);
+						return true;
+					}
+					if (mouseEvent.X == 5 && mouseEvent.Y == 3) {
+
+						SetHeavy?.Invoke (false);
+						return true;
+					}
 				}
 
 				return base.OnMouseEvent (mouseEvent);
 			}
 		}
 
-		class DrawingArea : View
-		{
+		class DrawingArea : View {
 			/// <summary>
 			/// Index into <see cref="canvases"/> by color.
 			/// </summary>
 			Dictionary<Color, int> colorLayers = new Dictionary<Color, int> ();
-			List<StraightLineCanvas> canvases = new List<StraightLineCanvas>();
+			List<StraightLineCanvas> canvases = new List<StraightLineCanvas> ();
 			int currentColor;
 
 			Point? currentLineStart = null;
 
+			/// <summary>
+			/// True to use <see cref="BorderStyle.Double"/> instead of <see cref="BorderStyle.Single"/>
+			/// for lines.
+			/// </summary>
+			public bool Heavy { get; internal set; }
+
 			public DrawingArea ()
 			{
 				AddCanvas (Color.White);
@@ -112,20 +133,20 @@ namespace UICatalog.Scenarios {
 
 			private void AddCanvas (Color c)
 			{
-				if(colorLayers.ContainsKey(c)) {
+				if (colorLayers.ContainsKey (c)) {
 					return;
 				}
 
 				canvases.Add (new StraightLineCanvas (Application.Driver));
-				colorLayers.Add (c, canvases.Count-1);
+				colorLayers.Add (c, canvases.Count - 1);
 				currentColor = canvases.Count - 1;
 			}
 
 			public override void Redraw (Rect bounds)
 			{
 				base.Redraw (bounds);
-				
-				foreach(var kvp in colorLayers) {
+
+				foreach (var kvp in colorLayers) {
 
 					Driver.SetAttribute (new Terminal.Gui.Attribute (kvp.Key, ColorScheme.Normal.Background));
 					canvases [kvp.Value].Draw (this, bounds);
@@ -134,28 +155,31 @@ namespace UICatalog.Scenarios {
 			public override bool OnMouseEvent (MouseEvent mouseEvent)
 			{
 
-				if(mouseEvent.Flags.HasFlag(MouseFlags.Button1Pressed)) {
-					if(currentLineStart == null) {
-						currentLineStart = new Point(mouseEvent.X,mouseEvent.Y);
+				if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)) {
+					if (currentLineStart == null) {
+						currentLineStart = new Point (mouseEvent.X, mouseEvent.Y);
 					}
-				}
-				else {
+				} else {
 					if (currentLineStart != null) {
 
 						var start = currentLineStart.Value;
-						var end = new Point(mouseEvent.X, mouseEvent.Y);
+						var end = new Point (mouseEvent.X, mouseEvent.Y);
 						var orientation = Orientation.Vertical;
 						var length = end.Y - start.Y;
 
 						// if line is wider than it is tall switch to horizontal
-						if(Math.Abs(start.X - end.X) > Math.Abs(start.Y - end.Y)) 
-						{
+						if (Math.Abs (start.X - end.X) > Math.Abs (start.Y - end.Y)) {
 							orientation = Orientation.Horizontal;
 							length = end.X - start.X;
 						}
 
 
-						canvases [currentColor].AddLine (start, length, orientation, BorderStyle.Single);
+						canvases [currentColor].AddLine (
+							start,
+							length,
+							orientation,
+							Heavy ? BorderStyle.Double : BorderStyle.Single);
+
 						currentLineStart = null;
 						SetNeedsDisplay ();
 					}
@@ -169,6 +193,7 @@ namespace UICatalog.Scenarios {
 				AddCanvas (c);
 				currentColor = colorLayers [c];
 			}
+
 		}
 	}
 }