using System;
using System.Collections.Generic;
using OpenNest.CutOffs;
using OpenNest.Geometry;
namespace OpenNest.Desktop.Interaction;
///
/// Choices the cut-off tools keep for a view while its nest stays open, as the WinForms plate
/// view's cut-off handler does.
///
public sealed class CutOffToolOptions
{
/// Whether Sheet Cut-Off draws full-length lines instead of stopping at the first crossing.
public bool FullLengthNewLines { get; set; }
}
///
/// The WinForms Sheet Cut-Off action (Plate > Sheet Cut-Off), toolkit-neutral: as the pointer
/// moves, is the line a click adds there; a left click adds it as one
/// "Add Cut-Off" step (). The line is vertical at first; Space
/// () turns it. Unless the view's ask for
/// full-length lines, a new line stops at the first crossing cut-off from its start edge; Tab
/// () switches, and the choice outlasts the tool. The tool stays
/// active until Escape; nothing happens while the host's edits are locked.
///
public sealed class SheetCutOffTool : PlateTool
{
private readonly CutOffToolOptions options;
private Dictionary perimeterCache;
private Vector? lastPoint;
private string notice;
public SheetCutOffTool(IPlateToolHost host, CutOffToolOptions options)
: base(host)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
}
public override string Name => "Sheet Cut-Off";
public override bool IsBusy => false;
/// The axis the next line runs along.
public CutOffAxis Axis { get; private set; } = CutOffAxis.Vertical;
/// Whether new lines run full length rather than stopping at the first crossing.
public bool FullLength => options.FullLengthNewLines;
/// The line a click would add at the pointer, its segments regenerated; null before the pointer moves.
public CutOff Preview { get; private set; }
///
/// The tag shown beside the cursor, since the status bar is easy to miss: how to turn the
/// line and switch its length.
///
public string CursorTag =>
$"Space: turn {AxisName(OtherAxis(Axis))}\nTab: {(FullLength ? "stop at first crossing" : "full length")}";
public override string Hint
{
get
{
var hint = FullLength
? $"click to add a full-length {AxisName(Axis)} line, Space turns it {AxisName(OtherAxis(Axis))}, Tab stops it at the first crossing, Esc finishes"
: $"click to add a {AxisName(Axis)} line stopped at the first crossing, Space turns it {AxisName(OtherAxis(Axis))}, Tab makes it full length, Esc finishes";
return notice == null ? hint : $"{notice} {hint}";
}
}
public override void Activate()
{
if (Host.Plate != null)
perimeterCache = Plate.BuildPerimeterCache(Host.Plate);
}
public override void Deactivate()
{
Preview = null;
perimeterCache = null;
notice = null;
lastPoint = null;
Host.Invalidate();
}
public override void Cancel() { }
public override bool PointerMoved(ToolPointer pointer)
{
lastPoint = pointer.World;
UpdatePreview();
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 = NewCutOff(pointer.World);
CutOffEdits.Add(plate, cutOff, Host.CutOffSettings, label => Host.BeginChange(label));
Host.Invalidate();
return true;
}
/// Space: turns the next line to the other axis.
public void ToggleAxis()
{
Axis = OtherAxis(Axis);
RaiseHintChanged();
if (Preview != null)
UpdatePreview();
}
/// Tab: switches between full-length lines and lines stopped at the first crossing.
public void ToggleFullLength()
{
options.FullLengthNewLines = !options.FullLengthNewLines;
RaiseHintChanged();
if (Preview != null)
UpdatePreview();
}
private CutOff NewCutOff(Vector point)
{
var (cutOff, newNotice) = CutOffEdits.NewCutOff(Host.Plate, point, Axis, FullLength, Host.CutOffSettings);
if (notice != newNotice)
{
notice = newNotice;
RaiseHintChanged();
}
return cutOff;
}
private void UpdatePreview()
{
var plate = Host.Plate;
if (plate == null || lastPoint is not { } point)
return;
perimeterCache ??= Plate.BuildPerimeterCache(plate);
var preview = NewCutOff(point);
preview.Regenerate(plate, Host.CutOffSettings, perimeterCache);
Preview = preview;
Host.Invalidate();
}
private static CutOffAxis OtherAxis(CutOffAxis axis) =>
axis == CutOffAxis.Vertical ? CutOffAxis.Horizontal : CutOffAxis.Vertical;
private static string AxisName(CutOffAxis axis) =>
axis == CutOffAxis.Vertical ? "vertical" : "horizontal";
}