mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-17 23:27:40 -05:00
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.
This commit is contained in:
1 parent
587613ff89
commit
810cc1ed01
3 files changed
+255
No files matched your search
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace OpenNest.Desktop.Interaction;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public sealed class DrawingPickup
|
||||
{
|
||||
/// <summary>Whether a drawing is picked up.</summary>
|
||||
public bool IsArmed { get; private set; }
|
||||
|
||||
/// <summary>A single left press on a drawing in the list.</summary>
|
||||
public void PickUp() => IsArmed = true;
|
||||
|
||||
/// <summary>A drop on the plate, a double-click or an Undo: nothing left to place.</summary>
|
||||
public void PutDown() => IsArmed = false;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public Drawing TakeOnPlateEnter(Drawing selected)
|
||||
{
|
||||
if (!IsArmed || selected == null)
|
||||
return null;
|
||||
IsArmed = false;
|
||||
return selected;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using OpenNest.Engine.Fill;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Desktop.Interaction;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static class DrawingPlacement
|
||||
{
|
||||
/// <summary>
|
||||
/// A drawing dropped on the plate: one part with its bounding box centred on
|
||||
/// <paramref name="at"/>, 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.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The place tool with one new part of <paramref name="drawing"/>.</summary>
|
||||
public static PlaceTool CreatePlaceTool(
|
||||
IPlateToolHost host, Drawing drawing, Func<SettleOrder> placementOrder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(host);
|
||||
ArgumentNullException.ThrowIfNull(drawing);
|
||||
return new PlaceTool(host, new[] { new Part(drawing) }, placementOrder);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user