From 810cc1ed013fd4c78e19ad5b6ff753d61b0eedb4 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 17 Nov 2026 17:17:16 -0500 Subject: [PATCH] feat(desktop): share adding parts from the drawing list DrawingPlacement.AddPart is the WinForms drop of a dragged drawing on the plate: one part with its bounding box centred on the drop point, as one Add Part step, refused without a plate or while edits are locked. As in WinForms it leaves the cut-offs as they are. CreatePlaceTool starts the place tool with one new part of a drawing. DrawingPickup is the drawing list's pick-up: a single press arms it, the plate view taking the keyboard takes the drawing then selected (staying armed when none is), and a drop, double-click or Undo puts it down. --- .../Interaction/DrawingPlacementTests.cs | 177 ++++++++++++++++++ OpenNest.Desktop/Interaction/DrawingPickup.cs | 31 +++ .../Interaction/DrawingPlacement.cs | 47 +++++ 3 files changed, 255 insertions(+) create mode 100644 OpenNest.Desktop.Tests/Interaction/DrawingPlacementTests.cs create mode 100644 OpenNest.Desktop/Interaction/DrawingPickup.cs create mode 100644 OpenNest.Desktop/Interaction/DrawingPlacement.cs diff --git a/OpenNest.Desktop.Tests/Interaction/DrawingPlacementTests.cs b/OpenNest.Desktop.Tests/Interaction/DrawingPlacementTests.cs new file mode 100644 index 00000000..868df06f --- /dev/null +++ b/OpenNest.Desktop.Tests/Interaction/DrawingPlacementTests.cs @@ -0,0 +1,177 @@ +using System.Linq; +using OpenNest.Desktop.Interaction; +using OpenNest.Engine.Fill; +using OpenNest.Geometry; + +namespace OpenNest.Desktop.Tests.Interaction; + +public class DrawingPlacementTests +{ + private readonly Plate plate = InteractionFixtures.Plate(); + private readonly TestToolHost host; + private readonly Drawing drawing = new("dropped", InteractionFixtures.Rect(10, 6)); + + public DrawingPlacementTests() + { + host = new TestToolHost(plate); + } + + private static SettleOrder Order() => SettleOrder.ClosestToCorner; + + [Fact] + public void A_drop_adds_one_part_centred_on_the_drop_point_as_one_step() + { + var recorder = host.RecordHistory(); + var count = plate.Parts.Count; + + var part = DrawingPlacement.AddPart(host, drawing, new Vector(60, 50)); + + Assert.NotNull(part); + Assert.Equal(count + 1, plate.Parts.Count); + Assert.Same(part, plate.Parts[^1]); + Assert.Same(drawing, part.BaseDrawing); + Assert.True(part.BoundingBox.Center.DistanceTo(new Vector(60, 50)) < 1e-9, + $"centred at {part.BoundingBox.Center}"); + Assert.Equal(new[] { "Add Part" }, host.ChangeLabels); + Assert.Equal(1, recorder.History.UndoCount); + } + + [Fact] + public void A_drop_keeps_the_cut_offs_as_they_are() + { + var crossing = new Part(plate.Parts[1].BaseDrawing, new Vector(75, 40)); + plate.Parts.Add(crossing); + plate.RegenerateCutOffs(host.CutOffSettings); + var strokes = PlateEditsTests.CutOffStrokes(plate); + host.ThrowOnCutOffSettings = true; + + DrawingPlacement.AddPart(host, drawing, new Vector(20, 80)); + + Assert.Equal(strokes, PlateEditsTests.CutOffStrokes(plate)); + } + + [Fact] + public void A_drop_does_nothing_while_locked() + { + var count = plate.Parts.Count; + host.EditsLocked = true; + + Assert.Null(DrawingPlacement.AddPart(host, drawing, new Vector(60, 50))); + + Assert.Equal(count, plate.Parts.Count); + Assert.Empty(host.ChangeLabels); + } + + [Fact] + public void A_drop_does_nothing_without_a_plate() + { + var count = plate.Parts.Count; + host.Plate = null; + + Assert.Null(DrawingPlacement.AddPart(host, drawing, new Vector(60, 50))); + + Assert.Equal(count, plate.Parts.Count); + Assert.Empty(host.ChangeLabels); + } + + [Fact] + public void Undo_takes_a_dropped_part_off_and_redo_puts_it_back() + { + var recorder = host.RecordHistory(); + // The list offers the nest's own drawings. + recorder.Document.Nest.Drawings.Add(drawing); + recorder.Reset(); + static int Real(Plate p) => p.Parts.Count(part => !part.BaseDrawing.IsCutOff); + DrawingPlacement.AddPart(host, drawing, new Vector(60, 50)); + + recorder.Undo(); + var undone = recorder.Document.Nest.Plates[0]; + Assert.DoesNotContain(undone.Parts, p => p.BaseDrawing.Name == "dropped"); + var count = Real(undone); + recorder.Redo(); + + var restored = recorder.Document.Nest.Plates[0]; + Assert.Equal(count + 1, Real(restored)); + var dropped = restored.Parts.Single(p => p.BaseDrawing.Name == "dropped"); + Assert.True(dropped.BoundingBox.Center.DistanceTo(new Vector(60, 50)) < 1e-9); + } + + [Fact] + public void An_off_centre_drawing_is_centred_on_its_bounding_box() + { + // A drawing whose program does not start at its bounding box's corner. + var offset = new Drawing("offset", InteractionFixtures.Rect(8, 4, 3, 7)); + + var part = DrawingPlacement.AddPart(host, offset, new Vector(40, 30)); + + Assert.True(part.BoundingBox.Center.DistanceTo(new Vector(40, 30)) < 1e-9, + $"centred at {part.BoundingBox.Center}"); + } + + [Fact] + public void The_place_tool_for_a_drawing_places_a_new_part_of_it() + { + var tool = DrawingPlacement.CreatePlaceTool(host, drawing, Order); + host.Tools.SetTool(tool); + var count = plate.Parts.Count; + + tool.PointerMoved(host.At(30, 60)); + tool.Apply(ToolModifiers.None); + + Assert.Single(tool.Previews); + Assert.Equal(count + 1, plate.Parts.Count); + Assert.Same(drawing, plate.Parts[^1].BaseDrawing); + Assert.True(plate.Parts[^1].BoundingBox.Location.DistanceTo(new Vector(30, 60)) < 1e-9); + Assert.Equal(new[] { "Add Part" }, host.ChangeLabels); + } + + [Fact] + public void Delete_with_a_drawing_tool_removes_nothing() + { + var tool = DrawingPlacement.CreatePlaceTool(host, drawing, Order); + host.Tools.SetTool(tool); + var count = plate.Parts.Count; + var recorder = host.RecordHistory(); + + tool.DeleteSources(); + + Assert.Equal(count, plate.Parts.Count); + Assert.Equal(0, recorder.History.UndoCount); + } + + [Fact] + public void A_pick_up_is_taken_once_by_the_plate() + { + var pickup = new DrawingPickup(); + Assert.Null(pickup.TakeOnPlateEnter(drawing)); + + pickup.PickUp(); + + Assert.Same(drawing, pickup.TakeOnPlateEnter(drawing)); + Assert.Null(pickup.TakeOnPlateEnter(drawing)); + } + + [Fact] + public void A_pick_up_with_nothing_selected_stays_armed() + { + var pickup = new DrawingPickup(); + pickup.PickUp(); + + Assert.Null(pickup.TakeOnPlateEnter(null)); + + Assert.True(pickup.IsArmed); + Assert.Same(drawing, pickup.TakeOnPlateEnter(drawing)); + } + + [Fact] + public void Putting_down_disarms_the_pick_up() + { + var pickup = new DrawingPickup(); + pickup.PickUp(); + + pickup.PutDown(); + + Assert.False(pickup.IsArmed); + Assert.Null(pickup.TakeOnPlateEnter(drawing)); + } +} diff --git a/OpenNest.Desktop/Interaction/DrawingPickup.cs b/OpenNest.Desktop/Interaction/DrawingPickup.cs new file mode 100644 index 00000000..1bdaf0ca --- /dev/null +++ b/OpenNest.Desktop/Interaction/DrawingPickup.cs @@ -0,0 +1,31 @@ +namespace OpenNest.Desktop.Interaction; + +/// +/// The drawing list's pick-up, as WinForms arms it: a single left press on a drawing picks it +/// up, and the next time the plate view takes the keyboard (the pointer entering it focuses +/// it) the drawing then selected in the list starts the place tool. A drop of the dragged +/// drawing on the plate, a double-click and an Undo or Redo put it down. +/// +public sealed class DrawingPickup +{ + /// Whether a drawing is picked up. + public bool IsArmed { get; private set; } + + /// A single left press on a drawing in the list. + public void PickUp() => IsArmed = true; + + /// A drop on the plate, a double-click or an Undo: nothing left to place. + public void PutDown() => IsArmed = false; + + /// + /// The plate view took the keyboard: the drawing to place, the one selected in the list, + /// or null. As in WinForms, a pick-up with nothing selected stays armed. + /// + public Drawing TakeOnPlateEnter(Drawing selected) + { + if (!IsArmed || selected == null) + return null; + IsArmed = false; + return selected; + } +} diff --git a/OpenNest.Desktop/Interaction/DrawingPlacement.cs b/OpenNest.Desktop/Interaction/DrawingPlacement.cs new file mode 100644 index 00000000..7a8fbcad --- /dev/null +++ b/OpenNest.Desktop/Interaction/DrawingPlacement.cs @@ -0,0 +1,47 @@ +using System; +using OpenNest.Engine.Fill; +using OpenNest.Geometry; + +namespace OpenNest.Desktop.Interaction; + +/// +/// Adding a part from the nest's drawing list, as the WinForms nest window does: dropping a +/// dragged drawing on the plate adds one part centred where it was dropped, and a drawing +/// picked in the list then carried into the plate view starts the place tool with it. +/// +public static class DrawingPlacement +{ + /// + /// A drawing dropped on the plate: one part with its bounding box centred on + /// , as one "Add Part" undo step; null without a plate or while edits + /// are locked. As in WinForms, the plate's cut-offs are not rebuilt. + /// + public static Part AddPart(IPlateToolHost host, Drawing drawing, Vector at) + { + ArgumentNullException.ThrowIfNull(host); + ArgumentNullException.ThrowIfNull(drawing); + var plate = host.Plate; + if (plate == null || host.EditsLocked) + return null; + + var part = new Part(drawing, at); + var center = part.BoundingBox.Center; + part.Offset(at.X - center.X, at.Y - center.Y); + + using var change = host.BeginChange("Add Part"); + change.MarkChanged(); + plate.Parts.Add(part); + host.Invalidate(); + change.Complete(); + return part; + } + + /// The place tool with one new part of . + public static PlaceTool CreatePlaceTool( + IPlateToolHost host, Drawing drawing, Func placementOrder) + { + ArgumentNullException.ThrowIfNull(host); + ArgumentNullException.ThrowIfNull(drawing); + return new PlaceTool(host, new[] { new Part(drawing) }, placementOrder); + } +}