Files
OpenNest/OpenNest.Avalonia/Controls/PlateCanvas.Zoom.cs
T
ajandClaude Opus 5.5 ca6e8cd4ce feat(avalonia): zoom commands, zoom window, measure area and plate menu items
View > Zoom To (Area, Fit, Plate, Selected), Zoom In (Ctrl+Plus) and Zoom Out
(Ctrl+Minus), the nest header's zoom buttons, Nest > First/Last/Next
(Ctrl+Right)/Previous (Ctrl+Left) Plate and Tools > Measure Area, as WinForms.
The wheel zooms by the WinForms step, panning hides the hover highlight, and
resting on a part shows its drawing name after a second.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:09:17 -05:00

229 lines
9.1 KiB
C#

using Avalonia;
using Avalonia.Input;
using Avalonia.Threading;
using OpenNest.Desktop.Interaction;
using OpenNest.Desktop.Rendering;
using OpenNest.Geometry;
using SkiaSharp;
namespace OpenNest.Avalonia.Controls
{
/// <summary>
/// View &gt; Zoom In, Zoom Out and Zoom To (Area, Plate, Selected), Tools &gt; Measure Area and
/// the delayed hover tip, as the WinForms plate view: the zoom and measure tools are the
/// shared <see cref="ZoomWindowTool"/> and <see cref="MeasureAreaTool"/>, the tip the shared
/// <see cref="PartHoverTip"/> on a dispatcher timer.
/// </summary>
public sealed partial class PlateCanvas
{
private readonly PartHoverTip hoverTip = new();
private DispatcherTimer hoverTimer;
/// <summary>View &gt; Zoom In and the header's button: half again about the view center.</summary>
public void ZoomIn() => ZoomAboutCenter(ViewTransform.CommandZoomFactor);
/// <summary>View &gt; Zoom Out and the header's button.</summary>
public void ZoomOut() => ZoomAboutCenter(1 / ViewTransform.CommandZoomFactor);
/// <summary>View &gt; Zoom To &gt; Plate: the sheet without parts outside it.</summary>
public void ZoomToPlate() => ZoomTo(PlateZoom.PlateArea(plate));
/// <summary>View &gt; Zoom To &gt; Selected: nothing happens with no part selected.</summary>
public void ZoomToSelected() => ZoomTo(PlateZoom.SelectedArea(plate, selection));
/// <summary>View &gt; Zoom To &gt; Area and the header's button: starts the Zoom Window tool.</summary>
public void StartZoomWindow()
{
if (plate != null)
tools.SetTool(new ZoomWindowTool(this, tools));
}
/// <summary>Tools &gt; Measure Area: starts the tool that shows the largest clear box at the pointer.</summary>
public void StartMeasureArea()
{
if (plate != null)
tools.SetTool(new MeasureAreaTool(this));
}
private void ZoomAboutCenter(double factor)
{
if (plate == null || Bounds.Width <= 0 || Bounds.Height <= 0)
return;
view.Resize(Bounds.Width, Bounds.Height);
view.ZoomAboutCenter(factor);
fitPending = false;
InvalidateVisual();
ViewChanged?.Invoke(this, System.EventArgs.Empty);
}
private void ZoomTo(Box area)
{
if (area == null || Bounds.Width <= 0 || Bounds.Height <= 0)
return;
view.Resize(Bounds.Width, Bounds.Height);
if (!view.ZoomToArea(area))
return;
fitPending = false;
InvalidateVisual();
ViewChanged?.Invoke(this, System.EventArgs.Empty);
}
// Space, on release as in WinForms, turns the measured box.
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
if (e.Key == Key.Space && e.KeyModifiers == KeyModifiers.None && tools.Current is MeasureAreaTool measure)
{
measure.ToggleDirection();
e.Handled = true;
}
}
private void InitializeHoverTip()
{
hoverTimer = new DispatcherTimer { Interval = PartHoverTip.Delay };
hoverTimer.Tick += OnHoverTimer;
// As WinForms: any zoom, pan or resize hides the tip. No redraw is asked for here:
// a fit made while rendering changes the view, and the frame drawn next has no tip.
view.Changed += (_, _) => ClearHoverTip();
}
// As the WinForms view: in Select, with no button held and edits not locked, each move
// hides the tip and restarts the delay.
private void UpdateHoverTip(Point at, ToolButton button)
{
if (button != ToolButton.None || EditsLocked || tools.Current is not SelectTool)
{
if (ClearHoverTip())
InvalidateVisual();
return;
}
if (hoverTip.Move(new ScreenPoint(at.X, at.Y)))
InvalidateVisual();
hoverTimer.Stop();
hoverTimer.Start();
}
/// <returns>Whether a shown tip was hidden.</returns>
private bool ClearHoverTip()
{
hoverTimer?.Stop();
return hoverTip.Clear();
}
private void OnHoverTimer(object sender, System.EventArgs e)
{
hoverTimer.Stop();
if (hoverTip.Elapse(at => hitTester.PartAt(plate, ToWorld(new Point(at.X, at.Y)))))
InvalidateVisual();
}
private void BuildToolOverlays(Scene scene)
{
switch (tools.Current)
{
case ZoomWindowTool { Window: { } window }:
scene.ZoomWindow = window.Box;
break;
case MeasureAreaTool { Area: { } area } measure:
scene.MeasureArea = area;
scene.MeasureLabel = measure.Label;
break;
}
if (hoverTip.Text is { } tip)
{
scene.Tip = tip;
scene.TipAnchor = hoverTip.Anchor;
}
}
private sealed partial class Scene
{
public Box ZoomWindow;
public Box MeasureArea;
public string MeasureLabel;
public string Tip;
public ScreenPoint TipAnchor;
}
private sealed partial class PlatePainter
{
private static readonly SKColor ZoomWindowBorder = new(99, 162, 228);
private static readonly SKColor ZoomWindowFill = new(150, 200, 255, 90);
private static readonly SKColor MeasureBorder = new(50, 255, 50);
private static readonly SKColor MeasureFill = new(50, 255, 50, 50);
private static readonly SKColor MeasureText = new(0, 128, 0); // Green
private static readonly SKColor TipBack = new(255, 255, 255, 230);
private static readonly SKColor TipBorder = new(105, 105, 105); // DimGray
private void DrawToolOverlays()
{
DrawZoomWindow();
DrawMeasureArea();
DrawTip();
}
// The WinForms Zoom Window box: a blue rectangle with a cross at its center.
private void DrawZoomWindow()
{
if (scene.ZoomWindow is not { } box)
return;
var rect = Screen(box);
using var fill = Fill(ZoomWindowFill);
fill.IsAntialias = false;
canvas.DrawRect(rect, fill);
using var border = Stroke(ZoomWindowBorder);
border.IsAntialias = false;
canvas.DrawRect(rect, border);
const float halfWidth = 10;
canvas.DrawLine(rect.MidX, rect.MidY - halfWidth, rect.MidX, rect.MidY + halfWidth, border);
canvas.DrawLine(rect.MidX - halfWidth, rect.MidY, rect.MidX + halfWidth, rect.MidY, border);
}
// The measured box in green with its size centered in it, as WinForms (14 pt text).
private void DrawMeasureArea()
{
if (scene.MeasureArea is not { } box)
return;
var rect = Screen(box);
using var border = Stroke(MeasureBorder);
border.IsAntialias = false;
canvas.DrawRect(rect, border);
using var fill = Fill(MeasureFill);
fill.IsAntialias = false;
canvas.DrawRect(rect, fill);
if (string.IsNullOrEmpty(scene.MeasureLabel))
return;
using var text = new SKPaint
{
Color = MeasureText,
IsAntialias = true,
TextSize = 18.7f,
TextAlign = SKTextAlign.Center,
};
var metrics = text.FontMetrics;
canvas.DrawText(scene.MeasureLabel, rect.MidX, rect.MidY - (metrics.Ascent + metrics.Descent) / 2, text);
}
// The drawing name beside the pointer on a white box, placed as WinForms places it.
private void DrawTip()
{
if (string.IsNullOrEmpty(scene.Tip))
return;
using var text = new SKPaint { Color = SKColors.Black, IsAntialias = true, TextSize = 12 };
var metrics = text.FontMetrics;
var height = metrics.Descent - metrics.Ascent;
var (x, y, width, boxHeight) = PartHoverTip.Place(
scene.TipAnchor, text.MeasureText(scene.Tip), height, scene.Bounds.Width);
var rect = SKRect.Create((float)x, (float)y, (float)width, (float)boxHeight);
using var back = Fill(TipBack);
canvas.DrawRect(rect, back);
using var border = Stroke(TipBorder);
border.IsAntialias = false;
canvas.DrawRect(rect, border);
canvas.DrawText(scene.Tip, (float)x + 3, (float)y + 2 - metrics.Ascent, text);
}
}
}
}