mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-18 08:17:40 -05:00
Port the WinForms Clone Parts action as the toolkit-neutral PlaceTool: copies of the sources follow the pointer by their lower-left corner (a move to the same screen point is ignored, keeping arrow-key nudges), and each left click places another copy as one Add Part/Add Parts step. Shift settles toward the quadrant corner on geometry, Ctrl alone on bounding boxes, reading the placement order at each click. Copied common-line groups are joined again among the copies and cut-offs are rebuilt. Delete removes the sources plus selected plate parts in one Delete step, recorded (and cut-offs rebuilt) only when any were on the plate. Settling runs before the step opens, so a failure there leaves nothing to roll back; the copy does not follow the pointer while edits are locked, as WinForms raises no mouse move then. PlateClipboard.Copy/Cut start the tool with the selected parts; Cut then removes the sources as one Cut step without rebuilding cut-offs, as WinForms does. While placing, PlateEdits nudges, steps, pushes and rotates the previews instead of the selection. Unlike WinForms, which records an empty undo step there, these edits open no undo step, and they never rebuild cut-offs since the previews are not on the plate.
256 lines
9.4 KiB
C#
256 lines
9.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using OpenNest.Engine.Fill;
|
|
using OpenNest.Geometry;
|
|
using OpenNest.Math;
|
|
using OpenNest.Undo;
|
|
|
|
namespace OpenNest.Desktop.Interaction;
|
|
|
|
/// <summary>
|
|
/// The WinForms plate view's edits of the selection, toolkit-neutral: delete, nudge, step, push
|
|
/// and rotate. Each edit is one undo step named as in WinForms; repeated nudges and steps of the
|
|
/// same parts, and wheel rotations, undo as one. Every edit does nothing without a plate or
|
|
/// selected parts (Delete also acts on selected cut-offs alone) and redraws the view, and nothing
|
|
/// at all while the host's edits are locked (<see cref="IPlateToolHost.EditsLocked"/>).
|
|
/// <para>
|
|
/// While a <see cref="PlaceTool"/> is active in <see cref="Tools"/>, nudge, step, push and rotate
|
|
/// move its previews instead, which are not on the plate: no undo step is opened (WinForms
|
|
/// records an empty one) and cut-offs are not rebuilt. Delete then removes the tool's sources
|
|
/// (<see cref="PlaceTool.DeleteSources"/>).
|
|
/// </para>
|
|
/// </summary>
|
|
public sealed class PlateEdits
|
|
{
|
|
public PlateEdits(IPlateToolHost host)
|
|
{
|
|
Host = host ?? throw new ArgumentNullException(nameof(host));
|
|
}
|
|
|
|
public IPlateToolHost Host { get; }
|
|
|
|
/// <summary>
|
|
/// The view's tools, so edits can act on a place tool's previews; null for none.
|
|
/// </summary>
|
|
public PlateToolManager Tools { get; set; }
|
|
|
|
/// <summary>
|
|
/// Whether nudge, push and rotate have parts to move: a place tool's previews, or selected
|
|
/// parts still on the plate. The view checks it before a middle click or Shift+wheel rotates.
|
|
/// </summary>
|
|
public bool HasPartsToEdit =>
|
|
Placing is { Previews.Count: > 0 }
|
|
|| (Host.Plate != null && Host.Selection.Parts.Any(Host.Plate.Parts.Contains));
|
|
|
|
private PlaceTool Placing => Tools?.Current as PlaceTool;
|
|
|
|
/// <summary>How far an arrow-key nudge moves the selection, in plate units.</summary>
|
|
public double OffsetIncrement { get; set; } = 10;
|
|
|
|
/// <summary>
|
|
/// Whether an arrow key steps the selection by its own size instead (<see cref="Step"/>).
|
|
/// </summary>
|
|
public bool CalculatedOffset { get; set; }
|
|
|
|
/// <summary>How far one wheel notch rotates the selection, in degrees.</summary>
|
|
public double RotateIncrementDegrees { get; set; } = 10;
|
|
|
|
/// <summary>
|
|
/// Removes the selected parts and cut-offs and clears the selection. When the plate had
|
|
/// cut-offs, the remaining ones are rebuilt once both kinds are gone, so they close the gaps
|
|
/// left around deleted parts.
|
|
/// </summary>
|
|
public void Delete()
|
|
{
|
|
var plate = Host.Plate;
|
|
if (plate == null || Host.EditsLocked)
|
|
return;
|
|
if (Placing is { } placing)
|
|
{
|
|
placing.DeleteSources();
|
|
return;
|
|
}
|
|
var selection = Host.Selection;
|
|
selection.Prune();
|
|
var parts = selection.Parts.ToList();
|
|
var cutOffs = selection.CutOffs.ToList();
|
|
if (parts.Count == 0 && cutOffs.Count == 0)
|
|
return;
|
|
|
|
using var change = Host.BeginChange("Delete");
|
|
var regenerateCutOffs = plate.CutOffs.Count > 0;
|
|
change.MarkChanged();
|
|
foreach (var cutOff in cutOffs)
|
|
plate.CutOffs.Remove(cutOff);
|
|
foreach (var part in parts)
|
|
plate.Parts.Remove(part);
|
|
selection.Clear();
|
|
if (regenerateCutOffs)
|
|
plate.RegenerateCutOffs(Host.CutOffSettings);
|
|
Host.Invalidate();
|
|
change.Complete();
|
|
}
|
|
|
|
/// <summary>
|
|
/// An arrow key: moves by <see cref="OffsetIncrement"/>, or with
|
|
/// <see cref="CalculatedOffset"/> steps.
|
|
/// </summary>
|
|
public void Nudge(PushDirection direction)
|
|
{
|
|
if (CalculatedOffset)
|
|
{
|
|
Step(direction);
|
|
return;
|
|
}
|
|
|
|
var offset = SpatialQuery.DirectionToOffset(direction, OffsetIncrement);
|
|
NudgeBy(offset.X, offset.Y);
|
|
}
|
|
|
|
/// <summary>Moves the selection by (<paramref name="dx"/>, <paramref name="dy"/>).</summary>
|
|
public void NudgeBy(double dx, double dy)
|
|
{
|
|
if (!TryGetEditParts(out var plate, out var parts, out var previews))
|
|
return;
|
|
|
|
using var change = BeginEdit(previews, "Nudge", new SelectionKey("Nudge", plate, parts));
|
|
change.MarkChanged();
|
|
foreach (var part in parts)
|
|
part.Offset(dx, dy);
|
|
Host.Invalidate();
|
|
change.Complete();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Moves the selection one repeat step in <paramref name="direction"/>: its size along that
|
|
/// axis plus the part spacing, measured on its geometry (<see cref="Compactor.Step"/>).
|
|
/// </summary>
|
|
public void Step(PushDirection direction)
|
|
{
|
|
if (!TryGetEditParts(out var plate, out var parts, out var previews))
|
|
return;
|
|
|
|
using var change = BeginEdit(previews, "Nudge", new SelectionKey("Nudge", plate, parts));
|
|
if (Compactor.Step(parts, plate, direction) > 0)
|
|
change.MarkChanged();
|
|
Host.Invalidate();
|
|
change.Complete();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pushes the selection in <paramref name="direction"/> until it meets a part or the edge.
|
|
/// </summary>
|
|
public void Push(PushDirection direction)
|
|
{
|
|
if (!TryGetEditParts(out var plate, out var parts, out var previews))
|
|
return;
|
|
|
|
using var change = BeginEdit(previews, "Push");
|
|
change.MarkChanged();
|
|
Compactor.Push(parts, plate, direction);
|
|
Host.Invalidate();
|
|
change.Complete();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Rotates the selection by <paramref name="radians"/> about its center, keeping its lower-left
|
|
/// corner; with <paramref name="coalesce"/>, quick repeats undo as one.
|
|
/// </summary>
|
|
public void Rotate(double radians, bool coalesce = false)
|
|
{
|
|
if (!TryGetEditParts(out var plate, out var parts, out var previews))
|
|
return;
|
|
|
|
using var change = BeginEdit(previews, "Rotate",
|
|
coalesce ? new SelectionKey("Rotate", plate, parts) : null);
|
|
change.MarkChanged();
|
|
var bounds = parts.GetBoundingBox();
|
|
var center = bounds.Center;
|
|
var anchor = bounds.Location;
|
|
foreach (var part in parts)
|
|
part.Rotate(radians, center);
|
|
|
|
var diff = anchor - parts.GetBoundingBox().Location;
|
|
foreach (var part in parts)
|
|
part.Offset(diff);
|
|
|
|
if (!previews && plate.CutOffs.Count > 0)
|
|
plate.RegenerateCutOffs(Host.CutOffSettings);
|
|
Host.Invalidate();
|
|
change.Complete();
|
|
}
|
|
|
|
/// <summary>A middle click: a quarter turn counterclockwise, its own undo step.</summary>
|
|
public void RotateQuarterTurn() => Rotate(Angle.ToRadians(90));
|
|
|
|
/// <summary>
|
|
/// Shift+wheel: <paramref name="notches"/> wheel notches, positive turned away from the user,
|
|
/// rotate clockwise by <see cref="RotateIncrementDegrees"/> each (a tenth of it when
|
|
/// <paramref name="fine"/>, Ctrl held); a turn of the wheel undoes as one rotation.
|
|
/// </summary>
|
|
public void RotateByWheel(int notches, bool fine)
|
|
{
|
|
if (notches == 0)
|
|
return;
|
|
var increment = fine ? RotateIncrementDegrees * 0.1 : RotateIncrementDegrees;
|
|
var degrees = (notches > 0 ? -increment : increment) * System.Math.Abs(notches);
|
|
var angle = Angle.ToRadians(degrees);
|
|
Rotate(angle, coalesce: true);
|
|
}
|
|
|
|
// The parts an edit moves: a place tool's previews (previews is true), else the selection.
|
|
private bool TryGetEditParts(out Plate plate, out List<Part> parts, out bool previews)
|
|
{
|
|
plate = Host.Plate;
|
|
parts = null;
|
|
previews = false;
|
|
if (plate == null || Host.EditsLocked)
|
|
return false;
|
|
if (Placing is { } placing)
|
|
{
|
|
previews = true;
|
|
parts = placing.Previews.ToList();
|
|
return parts.Count > 0;
|
|
}
|
|
Host.Selection.Prune();
|
|
parts = Host.Selection.Parts.ToList();
|
|
return parts.Count > 0;
|
|
}
|
|
|
|
// Previews are not on the nest, so moving them opens no undo step.
|
|
private ChangeScope BeginEdit(bool previews, string label, object coalesceKey = null) =>
|
|
previews ? ChangeScope.Detached() : Host.BeginChange(label, coalesceKey);
|
|
|
|
/// <summary>
|
|
/// Equal for the same edit of the same parts, in the same order, on the same plate, so
|
|
/// repeated nudges or wheel turns coalesce into one undo entry.
|
|
/// </summary>
|
|
private sealed class SelectionKey : IEquatable<SelectionKey>
|
|
{
|
|
private readonly string operation;
|
|
private readonly Plate plate;
|
|
private readonly Part[] parts;
|
|
|
|
public SelectionKey(string operation, Plate plate, IEnumerable<Part> selection)
|
|
{
|
|
this.operation = operation;
|
|
this.plate = plate;
|
|
parts = selection.ToArray();
|
|
}
|
|
|
|
public bool Equals(SelectionKey other) =>
|
|
other != null
|
|
&& operation == other.operation
|
|
&& ReferenceEquals(plate, other.plate)
|
|
&& parts.Length == other.parts.Length
|
|
&& parts.Zip(other.parts).All(pair => ReferenceEquals(pair.First, pair.Second));
|
|
|
|
public override bool Equals(object obj) => Equals(obj as SelectionKey);
|
|
|
|
public override int GetHashCode() =>
|
|
HashCode.Combine(operation, RuntimeHelpers.GetHashCode(plate), parts.Length);
|
|
}
|
|
}
|