浏览代码

Support for drawing at an offset within client area

Thomas 2 年之前
父节点
当前提交
90a6c2c34c
共有 2 个文件被更改,包括 30 次插入27 次删除
  1. 11 6
      Terminal.Gui/Core/Graphs/LineCanvas.cs
  2. 19 21
      UnitTests/Core/LineCanvasTests.cs

+ 11 - 6
Terminal.Gui/Core/Graphs/LineCanvas.cs

@@ -79,15 +79,20 @@ namespace Terminal.Gui.Graphs {
 		}
 		}
 
 
 		/// <summary>
 		/// <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
+		/// Draws all the lines that lie within the <paramref name="sourceRect"/> onto
+		/// the <paramref name="view"/> client area at given <paramref name="drawOffset"/>.
+		///  This method should be called from
 		/// <see cref="View.Redraw(Rect)"/>.
 		/// <see cref="View.Redraw(Rect)"/>.
 		/// </summary>
 		/// </summary>
 		/// <param name="view"></param>
 		/// <param name="view"></param>
-		/// <param name="bounds"></param>
-		public void Draw (View view, Rect bounds)
+		/// <param name="sourceRect">The area of the canvas to draw.</param>
+		/// <param name="drawOffset">The point within the client area of 
+		/// <paramref name="view"/> to draw at.</param>
+		public void Draw (View view, Rect sourceRect, Point? drawOffset = null)
 		{
 		{
-			var runes = GenerateImage (bounds);
+			var offset = drawOffset ?? Point.Empty;
+
+			var runes = GenerateImage (sourceRect);
 
 
 			var runeRows = runes.GetLength (0);
 			var runeRows = runes.GetLength (0);
 			var runeCols = runes.GetLength (1);
 			var runeCols = runes.GetLength (1);
@@ -97,7 +102,7 @@ namespace Terminal.Gui.Graphs {
 					var rune = runes [y, x];
 					var rune = runes [y, x];
 
 
 					if (rune.HasValue) {
 					if (rune.HasValue) {
-						view.AddRune (bounds.X + x, bounds.Y + y, rune.Value);
+						view.AddRune (offset.X + x, offset.Y + y, rune.Value);
 					}
 					}
 				}
 				}
 			}
 			}

+ 19 - 21
UnitTests/Core/LineCanvasTests.cs

@@ -285,32 +285,30 @@ namespace Terminal.Gui.CoreTests {
 		}
 		}
 
 
 		[Fact, AutoInitShutdown]
 		[Fact, AutoInitShutdown]
-		public void TestLineCanvas_PositiveLocation ()
+		public void TestLineCanvas_LeaveMargin_Top1_Left1 ()
 		{
 		{
-			var x = 1;
-			var y = 1;
-			var v = GetCanvas (out var canvas, x, y);
+			// Draw at 1,1 within client area of View (i.e. leave a top and left margin of 1)
+			var v = GetCanvas (out var canvas, 1, 1);
 
 
 			// outer box
 			// outer box
-			canvas.AddLine (new Point (x, y), 9, Orientation.Horizontal, BorderStyle.Single);
-			canvas.AddLine (new Point (x + 9, y + 0), 4, Orientation.Vertical, BorderStyle.Single);
-			canvas.AddLine (new Point (x + 9, y + 4), -9, Orientation.Horizontal, BorderStyle.Single);
-			canvas.AddLine (new Point (x + 0, y + 4), -4, Orientation.Vertical, BorderStyle.Single);
+			canvas.AddLine (new Point (0, 0), 8, Orientation.Horizontal, BorderStyle.Single);
+			canvas.AddLine (new Point (8, 0), 3, Orientation.Vertical, BorderStyle.Single);
+			canvas.AddLine (new Point (8, 3), -8, Orientation.Horizontal, BorderStyle.Single);
+			canvas.AddLine (new Point (0, 3), -3, Orientation.Vertical, BorderStyle.Single);
 
 
 
 
-			canvas.AddLine (new Point (x + 5, y + 0), 4, Orientation.Vertical, BorderStyle.Single);
-			canvas.AddLine (new Point (x + 0, y + 2), 9, Orientation.Horizontal, BorderStyle.Single);
+			canvas.AddLine (new Point (5, 0), 3, Orientation.Vertical, BorderStyle.Single);
+			canvas.AddLine (new Point (0, 2), 8, Orientation.Horizontal, BorderStyle.Single);
 
 
 			v.Redraw (v.Bounds);
 			v.Redraw (v.Bounds);
 
 
 			string looksLike =
 			string looksLike =
-@"    
-
- ┌────┬───┐
- │    │   │
- ├────┼───┤
- │    │   │
- └────┴───┘";
+@"
+ ┌────┬──┐
+ │    │  │
+ ├────┼──┤
+ └────┴──┘
+";
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 		}
 		}
 
 
@@ -364,19 +362,19 @@ namespace Terminal.Gui.CoreTests {
 		/// at <see cref="View.DrawContentComplete"/> time.
 		/// at <see cref="View.DrawContentComplete"/> time.
 		/// </summary>
 		/// </summary>
 		/// <param name="canvas">The <see cref="LineCanvas"/> you can draw into.</param>
 		/// <param name="canvas">The <see cref="LineCanvas"/> you can draw into.</param>
-		/// <param name="offsetX">How far to offset the <see cref="View.Bounds"/> in X</param>
-		/// <param name="offsetX">How far to offset the <see cref="View.Bounds"/> in Y</param>
+		/// <param name="offsetX">How far to offset drawing in X</param>
+		/// <param name="offsetY">How far to offset drawing in Y</param>
 		/// <returns></returns>
 		/// <returns></returns>
 		private View GetCanvas (out LineCanvas canvas, int offsetX = 0, int offsetY = 0)
 		private View GetCanvas (out LineCanvas canvas, int offsetX = 0, int offsetY = 0)
 		{
 		{
 			var v = new View {
 			var v = new View {
 				Width = 10,
 				Width = 10,
 				Height = 5,
 				Height = 5,
-				Bounds = new Rect (offsetX, offsetY, 10, 5)
+				Bounds = new Rect (0, 0, 10, 5)
 			};
 			};
 
 
 			var canvasCopy = canvas = new LineCanvas ();
 			var canvasCopy = canvas = new LineCanvas ();
-			v.DrawContentComplete += (r) => canvasCopy.Draw (v, v.Bounds);
+			v.DrawContentComplete += (r) => canvasCopy.Draw (v, v.Bounds, new Point(offsetX,offsetY));
 
 
 			return v;
 			return v;
 		}
 		}