using System;
using System.Collections.Generic;
using System.Linq;
using OpenNest.CutOffs;
namespace OpenNest.Desktop.Interaction;
///
/// The parts and cut-offs selected on one plate view. Selecting a common-line member brings its
/// whole group, so the joined parts move, rotate and push as one unit and keep their shared spans
/// (see docs/common-line-cutting.md). Cut-off segments are never selected as parts; cut-offs are
/// selected as their definitions. is raised once per call that changes it.
///
public sealed class PlateSelection
{
private readonly List parts = new();
private readonly HashSet partSet = new(ReferenceEqualityComparer.Instance);
private readonly List cutOffs = new();
private Plate plate;
/// The plate selected on; changing it clears the selection.
public Plate Plate
{
get => plate;
set
{
if (ReferenceEquals(plate, value))
return;
plate = value;
Clear();
}
}
/// The selected parts, in the order they were selected.
public IReadOnlyList Parts => parts;
/// The selected cut-off definitions.
public IReadOnlyList CutOffs => cutOffs;
public bool IsEmpty => parts.Count == 0 && cutOffs.Count == 0;
public event EventHandler Changed;
public bool Contains(Part part) => part != null && partSet.Contains(part);
public bool Contains(CutOff cutOff) => cutOffs.Any(c => ReferenceEquals(c, cutOff));
/// Deselects every part and cut-off.
public void Clear() => Change(() => ClearPartsCore() | ClearCutOffsCore());
public void ClearParts() => Change(ClearPartsCore);
public void ClearCutOffs() => Change(ClearCutOffsCore);
/// Adds and the rest of its common-line group.
public void Select(Part part) => Change(() => AddCore(part));
/// Adds each of with the rest of its common-line group.
public void Select(IEnumerable toSelect) =>
Change(() => toSelect.Aggregate(false, (changed, part) => AddCore(part) | changed));
/// Replaces the selection with and its common-line group.
public void SelectOnly(Part part) => Change(() => ClearPartsCore() | ClearCutOffsCore() | AddCore(part));
/// Adds the cut-off definition .
public void Select(CutOff cutOff) => Change(() => AddCore(cutOff));
/// Adds each of .
public void Select(IEnumerable toSelect) =>
Change(() => toSelect.Aggregate(false, (changed, cutOff) => AddCore(cutOff) | changed));
/// Replaces the selection with the cut-off definition .
public void SelectOnly(CutOff cutOff) => Change(() => ClearPartsCore() | ClearCutOffsCore() | AddCore(cutOff));
/// Selects every part on the plate (cut-off segments excepted), keeping what is selected.
public void SelectAll()
{
if (plate != null)
Select(plate.Parts.ToList());
}
///
/// The selected parts as indices into 's parts, in plate order; parts no
/// longer on the plate are left out. With , keeps a selection across
/// an edit that replaces the plate's parts (an undo, say).
///
public IReadOnlyList PartIndices
{
get
{
var indices = new List();
if (plate == null || parts.Count == 0)
return indices;
for (var i = 0; i < plate.Parts.Count; i++)
if (partSet.Contains(plate.Parts[i]))
indices.Add(i);
return indices;
}
}
///
/// Replaces the selection with the parts at in 's
/// parts (each with its common-line group); indices off the plate are skipped.
///
public void SelectIndices(IEnumerable indices) =>
Change(() =>
{
var changed = ClearPartsCore() | ClearCutOffsCore();
if (plate == null || indices == null)
return changed;
foreach (var i in indices)
if (i >= 0 && i < plate.Parts.Count)
changed |= AddCore(plate.Parts[i]);
return changed;
});
///
/// Drops parts and cut-offs that are no longer on the plate, as after an edit that replaced
/// them; true when any were dropped.
///
public bool Prune()
{
if (IsEmpty)
return false;
var onPlate = plate == null ? new HashSet() : new HashSet(plate.Parts);
partSet.RemoveWhere(part => !onPlate.Contains(part));
var removed = parts.RemoveAll(part => !onPlate.Contains(part))
+ cutOffs.RemoveAll(cutOff => plate?.CutOffs.Contains(cutOff) != true);
if (removed > 0)
Changed?.Invoke(this, EventArgs.Empty);
return removed > 0;
}
///
/// followed by the other members of its common-line group on
/// , in plate order.
///
public static IEnumerable WithCommonLineGroup(Plate plate, Part part)
{
yield return part;
var group = plate?.CommonLineGroupOf(part);
if (group == null)
yield break;
foreach (var other in plate.Parts)
{
if (!ReferenceEquals(other, part) && group.Contains(other))
yield return other;
}
}
private bool AddCore(Part part)
{
if (!PlateHitTester.IsSelectable(part))
return false;
var changed = false;
foreach (var member in WithCommonLineGroup(plate, part))
{
if (!partSet.Add(member))
continue;
parts.Add(member);
changed = true;
}
return changed;
}
private bool AddCore(CutOff cutOff)
{
if (cutOff == null || Contains(cutOff))
return false;
cutOffs.Add(cutOff);
return true;
}
private bool ClearPartsCore()
{
if (parts.Count == 0)
return false;
parts.Clear();
partSet.Clear();
return true;
}
private bool ClearCutOffsCore()
{
if (cutOffs.Count == 0)
return false;
cutOffs.Clear();
return true;
}
private void Change(Func change)
{
if (change())
Changed?.Invoke(this, EventArgs.Empty);
}
}