Files
OpenNest/OpenNest.Desktop/Interaction/CutSimulationPlayback.cs
T
ajandClaude Opus 5.5 daa4543729 feat(simulator): click the plate to jump to that point
While the cut simulator is open, a left click on the plate (Select tool)
pauses at the time the tool passes nearest the click instead of selecting
parts. Cuts, scribes and pierces win over rapids; overlapping passes
resolve to the one nearest the current time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 16:34:06 -05:00

249 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenNest.CNC;
using OpenNest.Geometry;
namespace OpenNest.Desktop.Interaction;
/// <summary>
/// The Cut Simulator's playhead over a <see cref="CutSimulation"/>, without drawing: the time,
/// play and pause at a speed, the jumps between parts, cutouts and moves, and which parts the
/// torch has not reached. The host runs a timer that calls <see cref="Advance()"/> while
/// <see cref="IsPlaying"/> and redraws after each change.
/// </summary>
public sealed class CutSimulationPlayback
{
/// <summary>The simulator's speed choices, as multiples of real time.</summary>
public static IReadOnlyList<double> Speeds { get; } = new double[] { 1, 2, 5, 10, 25, 50, 100, 250 };
/// <summary>The speed the simulator starts at.</summary>
public const double DefaultSpeed = 5;
/// <summary>The index of <see cref="DefaultSpeed"/> in <see cref="Speeds"/>.</summary>
public static int DefaultSpeedIndex => 2;
private readonly Stopwatch clock = new();
private CutSimulation simulation;
private TimeSpan time;
private bool playing;
/// <summary>Raised when the playhead, play state or timeline changes.</summary>
public event EventHandler Changed;
public CutSimulation Simulation => simulation;
public bool IsActive => simulation != null;
public bool IsPlaying => playing;
/// <summary>Simulated time per wall-clock time.</summary>
public double Speed { get; set; } = 10;
/// <summary>The playhead, kept within the timeline; nothing changes without one.</summary>
public TimeSpan Time
{
get => time;
set
{
if (simulation == null)
return;
time = value < TimeSpan.Zero ? TimeSpan.Zero : value > simulation.TotalTime ? simulation.TotalTime : value;
Changed?.Invoke(this, EventArgs.Empty);
}
}
/// <summary>
/// Shows <paramref name="value"/>, keeping the torch where it was when the moves are the same
/// (new rates or acceleration only retime them), else keeping the playhead's time.
/// </summary>
public void Load(CutSimulation value)
{
var anchored = Anchor(simulation, value, time);
simulation = value;
Time = anchored;
if (simulation == null)
Pause();
Changed?.Invoke(this, EventArgs.Empty);
}
/// <summary>Where <paramref name="time"/> on <paramref name="old"/> falls on <paramref name="next"/>.</summary>
public static TimeSpan Anchor(CutSimulation old, CutSimulation next, TimeSpan time)
{
if (old == null || next == null || old.Steps.Count == 0 || old.Steps.Count != next.Steps.Count)
return time;
var index = old.IndexAt(time);
if (index >= old.Steps.Count)
return next.TotalTime;
return next.TimeAt(index, old.FractionAt(index, time));
}
/// <summary>Plays from the playhead, or from the start once it is at the end.</summary>
public void Play()
{
if (simulation == null || simulation.Steps.Count == 0)
return;
if (time >= simulation.TotalTime)
Time = TimeSpan.Zero;
clock.Restart();
playing = true;
Changed?.Invoke(this, EventArgs.Empty);
}
public void Pause()
{
if (!playing)
return;
playing = false;
clock.Reset();
Changed?.Invoke(this, EventArgs.Empty);
}
/// <summary>Drops the timeline and stops.</summary>
public void Clear()
{
playing = false;
clock.Reset();
simulation = null;
time = TimeSpan.Zero;
Changed?.Invoke(this, EventArgs.Empty);
}
/// <summary>Moves the playhead by the wall time since the last advance (or since Play).</summary>
public void Advance()
{
var elapsed = clock.Elapsed;
clock.Restart();
Advance(elapsed);
}
/// <summary>
/// Moves the playhead by <paramref name="wallElapsed"/> times <see cref="Speed"/>, pausing at
/// the end.
/// </summary>
public void Advance(TimeSpan wallElapsed)
{
if (simulation == null)
return;
var next = time + TimeSpan.FromTicks((long)(wallElapsed.Ticks * Speed));
time = next > simulation.TotalTime ? simulation.TotalTime : next;
Changed?.Invoke(this, EventArgs.Empty);
if (time >= simulation.TotalTime)
Pause();
}
/// <summary>
/// The play button: pauses while playing; otherwise plays, first calling
/// <paramref name="rebuild"/> (parts may have moved, which raises no plate event) when there
/// is no timeline or the playhead is at its start or end.
/// </summary>
public void TogglePlay(Action rebuild)
{
if (playing)
{
Pause();
return;
}
if (simulation == null || time <= TimeSpan.Zero || time >= simulation.TotalTime)
rebuild?.Invoke();
Play();
}
/// <summary>Back to the start (Home), still playing if it was.</summary>
public void Restart() => Time = TimeSpan.Zero;
/// <summary>Pauses at the end.</summary>
public void End()
{
Pause();
Time = simulation?.TotalTime ?? TimeSpan.Zero;
}
public void NextContour() => Jump(sim => sim.NextContour(time));
public void PreviousContour() => Jump(sim => sim.PreviousContour(time));
public void NextPart() => Jump(sim => sim.NextPart(time));
public void PreviousPart() => Jump(sim => sim.PreviousPart(time));
// Jumps pause so the jumped-to contour stays on screen.
private void Jump(Func<CutSimulation, TimeSpan> target)
{
if (simulation is not { } sim || sim.Steps.Count == 0)
return;
Pause();
Time = target(sim);
}
/// <summary>
/// Pauses at the start of the next move (<paramref name="direction"/> &gt; 0), or back at the
/// start of the current move, or of the one before when already there.
/// </summary>
public void StepMove(int direction)
{
var sim = simulation;
if (sim == null || sim.Steps.Count == 0)
return;
Pause();
var index = System.Math.Clamp(sim.IndexAt(time), 0, sim.Steps.Count);
var atStart = index < sim.Steps.Count && time == sim.Steps[index].StartTime;
var target = direction > 0 ? index + 1 : atStart ? index - 1 : index;
Time = target <= 0 ? TimeSpan.Zero
: target >= sim.Steps.Count ? sim.TotalTime
: sim.Steps[target].StartTime;
}
/// <summary>The playhead as a fraction of the timeline (0 without one).</summary>
public double Progress =>
simulation is { } sim && sim.TotalTime > TimeSpan.Zero ? time / sim.TotalTime : 0;
/// <summary>Moves the playhead to <paramref name="fraction"/> of the timeline (a scrub).</summary>
public void Seek(double fraction)
{
if (simulation != null)
Time = simulation.TotalTime * fraction;
}
/// <summary>
/// Pauses with the playhead where the tool passes nearest <paramref name="point"/> (plate
/// coordinates), as <see cref="CutSimulation.TimeNearest"/> picks it; false, changing nothing,
/// when no move comes within <paramref name="tolerance"/>.
/// </summary>
public bool SeekTo(Vector point, double tolerance)
{
if (simulation?.TimeNearest(point, tolerance, time) is not { } target)
return false;
Pause();
Time = target;
return true;
}
/// <summary>True for a part the torch has not started yet.</summary>
public bool IsPartPending(int partIndex)
{
if (simulation == null)
return false;
var index = simulation.IndexAt(time);
if (index >= simulation.Steps.Count)
return false;
if (index < 0)
return true;
var current = simulation.Steps[index];
// Travel toward a part has not touched it yet.
return partIndex > current.PartIndex
|| partIndex == current.PartIndex && current.Kind == CutSimulationStepKind.Rapid && !PartStarted(index);
}
private bool PartStarted(int index)
{
var part = simulation.Steps[index].PartIndex;
for (var i = index - 1; i >= 0 && simulation.Steps[i].PartIndex == part; i--)
{
if (simulation.Steps[i].Kind != CutSimulationStepKind.Rapid)
return true;
}
return false;
}
}