using System;
using System.Collections.Generic;
using OpenNest.CutOffs;
using OpenNest.Geometry;
namespace OpenNest.Desktop.Interaction;
///
/// The WinForms Trim/Extend Cut-Off action, toolkit-neutral: trims or extends a cut-off against
/// the cut-offs that cross it (see ). Trim removes the clicked piece,
/// leaving a gap when it lies between two crossing lines; Extend closes the gap or grows the end
/// nearer the click. Tab () switches mode. Hovering a cut-off previews
/// the edit (), and the status hint says why one is refused; a left
/// click applies it as one "Trim Cut-Off" or "Extend Cut-Off" step. The tool stays active until
/// Escape; nothing is applied while the host's edits are locked.
///
public sealed class TrimExtendCutOffTool : PlateTool
{
private CutOff hovered;
private CutOffEdit preview;
private string message;
private Vector? lastPoint;
public TrimExtendCutOffTool(IPlateToolHost host)
: base(host)
{
}
public override string Name => "Trim/Extend Cut-Off";
public override bool IsBusy => false;
/// Whether clicks extend rather than trim.
public bool Extending { get; private set; }
/// The edit a click at the pointer would make, or null off any cut-off.
public CutOffEdit Preview => preview;
public override string Hint => message ?? (Extending
? "click near the end of a cut-off to extend it or close a gap, Tab switches to Trim, Esc finishes"
: "click the piece of a cut-off to remove, Tab switches to Extend, Esc finishes");
///
/// The stretches the hovered edit removes (dashed red) and adds (dashed green); empty when
/// there is none or it is refused.
///
public IReadOnlyList<(Line Line, bool Added)> PreviewPieces => CutOffEdits.PreviewPieces(Host.Plate, preview);
public override void Cancel() { }
public override void Deactivate()
{
hovered = null;
preview = null;
lastPoint = null;
Host.Invalidate();
}
private double Tolerance => Host.View.LengthToWorld(SelectTool.CutOffPickPixels);
private CutOffEdit EditAt(CutOff cutOff, Vector point) => Extending
? CutOffTrim.Extend(Host.Plate, cutOff, point)
: CutOffTrim.Trim(Host.Plate, cutOff, point, Host.CutOffSettings);
public override bool PointerMoved(ToolPointer pointer)
{
var plate = Host.Plate;
lastPoint = pointer.World;
hovered = plate == null ? null : Host.HitTester.CutOffAt(plate, pointer.World, Tolerance);
preview = hovered == null ? null : EditAt(hovered, pointer.World);
SetMessage(preview?.Refusal);
Host.Invalidate();
return true;
}
public override bool PointerPressed(ToolPointer pointer)
{
var plate = Host.Plate;
if (pointer.Button != ToolButton.Left || plate == null || Host.EditsLocked)
return false;
var cutOff = Host.HitTester.CutOffAt(plate, pointer.World, Tolerance);
if (cutOff == null)
return false;
var edit = EditAt(cutOff, pointer.World);
if (CutOffEdits.Apply(plate, edit, Extending ? CutOffEdits.ExtendLabel : CutOffEdits.TrimLabel,
Host.CutOffSettings, label => Host.BeginChange(label)))
{
hovered = null;
preview = null;
SetMessage(edit.Notice);
}
else
{
SetMessage(edit.Refusal);
}
Host.Invalidate();
return true;
}
/// Tab: switches between Trim and Extend, re-previewing the hovered cut-off.
public void ToggleMode()
{
Extending = !Extending;
preview = hovered == null || lastPoint is not { } at ? null : EditAt(hovered, at);
message = preview?.Refusal;
RaiseHintChanged();
Host.Invalidate();
}
private void SetMessage(string value)
{
if (message == value)
return;
message = value;
RaiseHintChanged();
}
}