using OpenNest.Desktop.Interaction; using OpenNest.Desktop.Rendering; using OpenNest.Geometry; namespace OpenNest.Desktop.Tests.Interaction; public class ZoomToolsTests { private readonly TestToolHost host = new(InteractionFixtures.Plate()); private ZoomWindowTool StartZoomWindow() { var tool = new ZoomWindowTool(host, host.Tools); host.Tools.SetTool(tool); return tool; } private void AssertFits(Box box) { var expected = new ViewTransform(); expected.Resize(host.View.ViewportWidth, host.View.ViewportHeight); expected.ZoomToArea(box); Assert.Equal(expected.Scale, host.View.Scale, 9); Assert.Equal(expected.OriginX, host.View.OriginX, 9); Assert.Equal(expected.OriginY, host.View.OriginY, 9); } [Fact] public void Zoom_areas_are_the_plate_with_parts_the_sheet_and_the_selected_parts() { var plate = host.Plate; plate.Parts.Add(new Part(new Drawing("outside", InteractionFixtures.Rect(10, 10)), new Vector(120, 0))); Assert.Equal(130, PlateZoom.FitArea(plate).Right, 9); Assert.Equal(100, PlateZoom.PlateArea(plate).Right, 9); Assert.Null(PlateZoom.SelectedArea(plate, host.Selection)); host.Selection.Select(plate.Parts[1]); host.Selection.Select(plate.Parts[4]); var selected = PlateZoom.SelectedArea(plate, host.Selection); Assert.Equal(20, selected.Left, 9); Assert.Equal(0, selected.Bottom, 9); Assert.Equal(30, selected.Right, 9); Assert.Equal(30, selected.Top, 9); Assert.Null(PlateZoom.FitArea(null)); } [Fact] public void A_long_drag_zooms_to_the_box_and_returns_to_select() { var tool = StartZoomWindow(); // 20 units at about ten pixels each: well past the 40-pixel drag. host.Drag(10, 10, 30, 25); AssertFits(new Box(10, 10, 20, 15)); Assert.IsType(host.Tools.Current); Assert.Null(tool.Window); // One zoom ends the tool for good: Escape from Select does not resume it. host.Tools.Escape(); Assert.IsType(host.Tools.Current); } [Fact] public void A_short_press_sets_the_first_corner_and_the_next_click_zooms() { var tool = StartZoomWindow(); var before = host.View.Capture(); host.Click(10, 10); Assert.True(tool.IsBusy); Assert.Same(tool, host.Tools.Current); Assert.Equal(before, host.View.Capture()); // Without a button held, a move stretches the box toward the second corner. tool.PointerMoved(host.At(40, 30, ToolButton.None)); Assert.Equal(new Vector(40, 30), tool.Window.Value.Second); host.Click(50, 40); AssertFits(new Box(10, 10, 40, 30)); Assert.IsType(host.Tools.Current); } [Fact] public void A_right_press_cancels_the_box_and_Escape_cancels_then_ends() { var tool = StartZoomWindow(); host.Click(10, 10); tool.PointerPressed(host.At(10, 10, ToolButton.Right)); Assert.Null(tool.Window); Assert.False(tool.IsBusy); Assert.Same(tool, host.Tools.Current); host.Click(10, 10); host.Tools.Escape(); Assert.Same(tool, host.Tools.Current); Assert.Null(tool.Window); host.Tools.Escape(); Assert.IsType(host.Tools.Current); } [Fact] public void Losing_the_pointer_mid_drag_drops_the_box() { var tool = StartZoomWindow(); var before = host.View.Capture(); tool.PointerPressed(host.At(10, 10)); tool.PointerMoved(host.At(30, 30)); tool.PointerCaptureLost(); Assert.Null(tool.Window); Assert.Equal(before, host.View.Capture()); Assert.Same(tool, host.Tools.Current); } [Fact] public void Measure_area_finds_the_largest_clear_box_and_Space_turns_it() { var tool = new MeasureAreaTool(host); host.Tools.SetTool(tool); Assert.Null(tool.Area); // Between the square at x 20..30 and the common-line pair from x 40. tool.PointerMoved(host.At(35, 50, ToolButton.None)); Assert.Equal(new Box(30, 0, 10, 100).ToString(), tool.Area.ToString()); Assert.Equal(tool.Area.Size.ToString(2), tool.Label); // Sideways first: from the left edge to the cut-off at x = 80, above the parts. tool.ToggleDirection(); Assert.True(tool.Horizontal); Assert.Equal(new Box(0, 30, 80, 70).ToString(), tool.Area.ToString()); Assert.False(tool.IsBusy); host.Tools.Escape(); Assert.IsType(host.Tools.Current); } [Fact] public void Measure_area_stays_clear_of_the_part_spacing() { host.Plate.PartSpacing = 1; var tool = new MeasureAreaTool(host); tool.PointerMoved(host.At(35, 50, ToolButton.None)); Assert.Equal(new Box(31, 0, 8, 100).ToString(), tool.Area.ToString()); } }