using System.Linq; using OpenNest.CutOffs; using OpenNest.Desktop.Interaction; using OpenNest.Geometry; using OpenNest.Undo; namespace OpenNest.Desktop.Tests.Interaction; public class CutOffToolsTests { // An empty 100 x 100 sheet with a full-length vertical cut-off at x = 50. private static Plate EmptyPlate() { var plate = new Plate(new Size(100, 100)); plate.CutOffs.Add(new CutOff(new Vector(50, 0), CutOffAxis.Vertical)); plate.RegenerateCutOffs(new CutOffSettings()); return plate; } private static CutOff Horizontal(Plate plate) => plate.CutOffs.Single(c => c.Axis == CutOffAxis.Horizontal); private static CutOff Vertical(Plate plate) => plate.CutOffs.Single(c => c.Axis == CutOffAxis.Vertical); [Fact] public void New_cut_off_stops_at_the_first_crossing_unless_full_length() { var plate = EmptyPlate(); var settings = new CutOffSettings(); var (stopped, _) = CutOffEdits.NewCutOff(plate, new Vector(80, 30), CutOffAxis.Horizontal, false, settings); var (full, _) = CutOffEdits.NewCutOff(plate, new Vector(80, 30), CutOffAxis.Horizontal, true, settings); Assert.Equal(50, stopped.EndLimit!.Value, 9); Assert.Null(stopped.StartLimit); Assert.Null(full.EndLimit); Assert.Null(full.StartLimit); // Neither is on the plate until a click adds it. Assert.Single(plate.CutOffs); } [Fact] public void New_cut_off_takes_the_head_locked_overcut() { var plate = EmptyPlate(); var settings = new CutOffSettings { LockHeadHeight = true, HeadLockOvercut = 1.5 }; var (cutOff, _) = CutOffEdits.NewCutOff(plate, new Vector(20, 30), CutOffAxis.Vertical, true, settings); Assert.Equal(1.5, cutOff.HeadLockOvercut); } [Fact] public void Sheet_cut_off_click_adds_one_Add_Cut_Off_step() { var plate = EmptyPlate(); var host = new TestToolHost(plate); var recorder = host.RecordHistory(); var tool = new SheetCutOffTool(host, new CutOffToolOptions()); host.Tools.SetTool(tool); tool.PointerMoved(host.At(20, 40, ToolButton.None)); Assert.NotNull(tool.Preview); host.Click(20, 40); Assert.Equal(2, plate.CutOffs.Count); var added = plate.CutOffs[1]; Assert.Equal(CutOffAxis.Vertical, added.Axis); Assert.Equal(20, added.Position.X, 9); Assert.Equal(1, recorder.History.UndoCount); Assert.Equal(CutOffEdits.AddLabel, recorder.History.UndoLabel); // The new line's segments are parts on the plate. Assert.NotEmpty(CutOffSegments.Of(plate, added)); } [Fact] public void Space_turns_the_line_and_Tab_keeps_full_length_for_the_view() { var plate = EmptyPlate(); var host = new TestToolHost(plate); var options = new CutOffToolOptions(); var tool = new SheetCutOffTool(host, options); host.Tools.SetTool(tool); Assert.StartsWith("Sheet Cut-Off: click to add a vertical line stopped at the first crossing", host.Tools.Status); tool.ToggleAxis(); Assert.Equal(CutOffAxis.Horizontal, tool.Axis); Assert.Contains("Space: turn vertical", tool.CursorTag); host.Click(80, 30); Assert.Equal(50, Horizontal(plate).EndLimit!.Value, 9); tool.ToggleFullLength(); Assert.True(options.FullLengthNewLines); Assert.StartsWith("Sheet Cut-Off: click to add a full-length horizontal line", host.Tools.Status); // A new tool for the same view keeps the choice. Assert.True(new SheetCutOffTool(host, options).FullLength); } [Fact] public void Sheet_cut_off_adds_nothing_while_edits_are_locked() { var plate = EmptyPlate(); var host = new TestToolHost(plate) { EditsLocked = true }; var tool = new SheetCutOffTool(host, new CutOffToolOptions()); host.Tools.SetTool(tool); host.Click(20, 40); Assert.Single(plate.CutOffs); Assert.Empty(host.ChangeLabels); } [Fact] public void Escape_ends_the_sheet_cut_off_tool() { var host = new TestToolHost(EmptyPlate()); host.Tools.SetTool(new SheetCutOffTool(host, new CutOffToolOptions())); host.Tools.Escape(); Assert.IsType(host.Tools.Current); } // A full-length horizontal line at y = 30 across the vertical at x = 50. private static Plate CrossedPlate() { var plate = EmptyPlate(); plate.CutOffs.Add(new CutOff(new Vector(0, 30), CutOffAxis.Horizontal)); plate.RegenerateCutOffs(new CutOffSettings()); return plate; } [Fact] public void Trim_removes_the_clicked_piece_as_one_Trim_Cut_Off_step() { var plate = CrossedPlate(); var host = new TestToolHost(plate); var recorder = host.RecordHistory(); var tool = new TrimExtendCutOffTool(host); host.Tools.SetTool(tool); tool.PointerMoved(host.At(50, 70, ToolButton.None)); Assert.NotNull(tool.Preview); Assert.Contains(tool.PreviewPieces, piece => !piece.Added); host.Click(50, 70); Assert.Equal(30, Vertical(plate).EndLimit!.Value, 9); Assert.Equal(CutOffEdits.TrimLabel, recorder.History.UndoLabel); Assert.Equal(1, recorder.History.UndoCount); } [Fact] public void Tab_switches_to_Extend_which_runs_the_end_back_to_the_edge() { var plate = CrossedPlate(); Vertical(plate).EndLimit = 30; plate.RegenerateCutOffs(new CutOffSettings()); var host = new TestToolHost(plate); var recorder = host.RecordHistory(); var tool = new TrimExtendCutOffTool(host); host.Tools.SetTool(tool); tool.PointerMoved(host.At(50, 25, ToolButton.None)); tool.ToggleMode(); Assert.True(tool.Extending); Assert.Contains("Tab switches to Trim", host.Tools.Status); host.Click(50, 25); Assert.Null(Vertical(plate).EndLimit); Assert.Equal(CutOffEdits.ExtendLabel, recorder.History.UndoLabel); } [Fact] public void A_refused_trim_changes_nothing_and_says_why() { var plate = EmptyPlate(); var host = new TestToolHost(plate); var recorder = host.RecordHistory(); var tool = new TrimExtendCutOffTool(host); host.Tools.SetTool(tool); host.Click(50, 70); Assert.Null(Vertical(plate).EndLimit); Assert.Equal(0, recorder.History.UndoCount); Assert.Equal(CutOffTrim.NothingToTrimTo, tool.Hint); } [Fact] public void Swap_through_cut_makes_the_stopped_line_run_through() { var plate = EmptyPlate(); var settings = new CutOffSettings(); var (horizontal, _) = CutOffEdits.NewCutOff(plate, new Vector(10, 30), CutOffAxis.Horizontal, false, settings); CutOffEdits.Add(plate, horizontal, settings, _ => ChangeScope.Detached()); var host = new TestToolHost(plate); var recorder = host.RecordHistory(); var junction = CutOffEdits.JunctionAt(plate, new Vector(50, 30), 0.5); Assert.NotNull(junction); Assert.Same(Vertical(plate), junction.Value.Through); var (applied, status) = CutOffEdits.Swap(plate, junction.Value.Through, junction.Value.Stopper, settings, label => host.BeginChange(label)); Assert.True(applied); Assert.Equal(CutOffEdits.SwappedStatus, status); Assert.Null(Horizontal(plate).EndLimit); Assert.Equal(30, Vertical(plate).EndLimit!.Value, 9); Assert.Equal(CutOffEdits.SwapLabel, recorder.History.UndoLabel); Assert.Null(CutOffEdits.JunctionAt(plate, new Vector(20, 70), 0.5)); } [Fact] public void Dragging_a_cut_off_in_Select_moves_it_and_its_joined_line_as_one_step() { var plate = EmptyPlate(); var settings = new CutOffSettings(); var (horizontal, _) = CutOffEdits.NewCutOff(plate, new Vector(10, 30), CutOffAxis.Horizontal, false, settings); CutOffEdits.Add(plate, horizontal, settings, _ => ChangeScope.Detached()); var host = new TestToolHost(plate); var recorder = host.RecordHistory(); var select = Assert.IsType(host.Tools.Current); select.PointerPressed(host.At(50, 70)); Assert.True(select.IsDraggingCutOff); select.PointerMoved(host.At(55, 72)); select.PointerMoved(host.At(60, 75)); select.PointerReleased(host.At(60, 75)); Assert.False(select.IsDraggingCutOff); Assert.Equal(60, Vertical(plate).Position.X, 9); // The horizontal line stopped on it follows. Assert.Equal(60, Horizontal(plate).EndLimit!.Value, 9); Assert.Equal(1, recorder.History.UndoCount); Assert.Equal(CutOffEdits.MoveLabel, recorder.History.UndoLabel); Assert.Equal(new[] { Vertical(plate) }, host.Selection.CutOffs); } [Fact] public void A_click_on_a_cut_off_selects_it_and_records_nothing() { var plate = EmptyPlate(); var host = new TestToolHost(plate); var recorder = host.RecordHistory(); host.Click(50, 70); Assert.Equal(new[] { Vertical(plate) }, host.Selection.CutOffs); Assert.Equal(0, recorder.History.UndoCount); Assert.False(recorder.HasUnrecordedChange); Assert.False(recorder.IsChangeInProgress); } [Fact] public void Cancelling_mid_drag_keeps_the_partial_cut_off_move() { var plate = EmptyPlate(); var host = new TestToolHost(plate); var recorder = host.RecordHistory(); var select = host.Tools.Current; select.PointerPressed(host.At(50, 70)); select.PointerMoved(host.At(40, 70)); select.Cancel(); Assert.Equal(40, Vertical(plate).Position.X, 9); Assert.Equal(1, recorder.History.UndoCount); Assert.False(recorder.IsChangeInProgress); } }