mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
ZoomWindowTool and MeasureAreaTool port the WinForms Zoom Window and Select Area actions onto the shared plate tool framework; PartHoverTip is the plate view's delayed drawing-name tooltip; PlateZoom gives the Zoom To areas. ViewTransform now scales with the viewport on resize as DrawControl does and zooms about its center by the menu's 1.5 step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
194 lines
7.7 KiB
C#
194 lines
7.7 KiB
C#
using System;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Desktop.Rendering;
|
|
|
|
/// <summary>A point in a view's screen (control) pixels, y down.</summary>
|
|
public readonly record struct ScreenPoint(double X, double Y);
|
|
|
|
/// <summary>
|
|
/// A plate or CAD view's world-to-screen mapping: world Y up, screen Y down, a uniform
|
|
/// <see cref="Scale"/> in pixels per drawing unit and <see cref="OriginX"/>/<see cref="OriginY"/>, the
|
|
/// screen position of the world origin. The zoom rules are the WinForms <c>DrawControl</c>'s: the
|
|
/// scale stays within <see cref="MinScale"/>..<see cref="MaxScale"/>, a zoom about a point keeps that
|
|
/// point still, and fitting an area leaves <see cref="FitBorder"/> pixels of margin in all.
|
|
/// </summary>
|
|
public sealed class ViewTransform
|
|
{
|
|
/// <summary>The DrawControl's zoom step for one Zoom In and one notch of the wheel.</summary>
|
|
public const double ZoomInFactor = 1.15;
|
|
|
|
/// <summary>
|
|
/// The step of View > Zoom In and the nest header's zoom buttons (the WinForms main form's
|
|
/// and nest window's factor), larger than a wheel notch.
|
|
/// </summary>
|
|
public const double CommandZoomFactor = 1.5;
|
|
|
|
/// <summary>Total pixels left around a fitted area (half on each side).</summary>
|
|
public const double FitBorder = 50;
|
|
|
|
public double Scale { get; private set; } = 1;
|
|
|
|
public double OriginX { get; private set; } = 100;
|
|
|
|
public double OriginY { get; private set; } = 100;
|
|
|
|
public double MinScale { get; set; } = 0.3;
|
|
|
|
public double MaxScale { get; set; } = 10000;
|
|
|
|
public double ViewportWidth { get; private set; }
|
|
|
|
public double ViewportHeight { get; private set; }
|
|
|
|
/// <summary>Raised after the scale, origin or viewport changes.</summary>
|
|
public event EventHandler Changed;
|
|
|
|
public ScreenPoint WorldToScreen(Vector world) => WorldToScreen(world.X, world.Y);
|
|
|
|
public ScreenPoint WorldToScreen(double x, double y) => new(OriginX + x * Scale, OriginY - y * Scale);
|
|
|
|
public Vector ScreenToWorld(ScreenPoint screen) => ScreenToWorld(screen.X, screen.Y);
|
|
|
|
public Vector ScreenToWorld(double x, double y) => new((x - OriginX) / Scale, (OriginY - y) / Scale);
|
|
|
|
public double LengthToScreen(double worldLength) => worldLength * Scale;
|
|
|
|
public double LengthToWorld(double screenLength) => screenLength / Scale;
|
|
|
|
/// <summary>The world area the viewport shows.</summary>
|
|
public Box VisibleWorld()
|
|
{
|
|
var bottomLeft = ScreenToWorld(0, ViewportHeight);
|
|
return new Box(bottomLeft.X, bottomLeft.Y, ViewportWidth / Scale, ViewportHeight / Scale);
|
|
}
|
|
|
|
// The view the resize scaling is measured from: the viewport and scale when the user last
|
|
// zoomed. Measuring from it, not from the previous size, lets a panel that opens and closes
|
|
// beside the view hand back exactly the zoom it took away.
|
|
private double referenceWidth;
|
|
private double referenceHeight;
|
|
private double referenceScale;
|
|
private double appliedScale = double.NaN;
|
|
|
|
/// <summary>
|
|
/// Sets the viewport size, as the WinForms view resizes: while both the old and new sizes
|
|
/// are larger than <see cref="FitBorder"/>, the world point at the center stays at the center
|
|
/// and the scale follows the usable size (measured from the size at the last zoom), so a view
|
|
/// that fit the plate still fits it when a side panel takes part of the width. From or to a
|
|
/// smaller size, the origin moves half the change, keeping the view centered.
|
|
/// </summary>
|
|
public void Resize(double width, double height)
|
|
{
|
|
if (width == ViewportWidth && height == ViewportHeight)
|
|
return;
|
|
if (CanScaleOnResize(ViewportWidth, ViewportHeight) && CanScaleOnResize(width, height))
|
|
ScaleToSize(width, height);
|
|
else
|
|
{
|
|
OriginX += (width - ViewportWidth) * 0.5;
|
|
OriginY += (height - ViewportHeight) * 0.5;
|
|
}
|
|
ViewportWidth = width;
|
|
ViewportHeight = height;
|
|
OnChanged();
|
|
}
|
|
|
|
private static bool CanScaleOnResize(double width, double height) => width > FitBorder && height > FitBorder;
|
|
|
|
private void ScaleToSize(double width, double height)
|
|
{
|
|
if (Scale != appliedScale || !CanScaleOnResize(referenceWidth, referenceHeight))
|
|
{
|
|
referenceWidth = ViewportWidth;
|
|
referenceHeight = ViewportHeight;
|
|
referenceScale = Scale;
|
|
}
|
|
var center = ScreenToWorld(ViewportWidth / 2, ViewportHeight / 2);
|
|
var ratio = System.Math.Min(
|
|
(width - FitBorder) / (referenceWidth - FitBorder),
|
|
(height - FitBorder) / (referenceHeight - FitBorder));
|
|
Scale = System.Math.Clamp(referenceScale * ratio, MinScale, MaxScale);
|
|
OriginX = width * 0.5 - center.X * Scale;
|
|
OriginY = height * 0.5 + center.Y * Scale;
|
|
appliedScale = Scale;
|
|
}
|
|
|
|
/// <summary>Moves the view by a screen offset.</summary>
|
|
public void Pan(double dx, double dy)
|
|
{
|
|
if (dx == 0 && dy == 0)
|
|
return;
|
|
OriginX += dx;
|
|
OriginY += dy;
|
|
OnChanged();
|
|
}
|
|
|
|
/// <summary>Zooms by <paramref name="factor"/> about the world point under <paramref name="screen"/>.</summary>
|
|
public void ZoomAt(ScreenPoint screen, double factor) => ZoomToPoint(ScreenToWorld(screen), factor);
|
|
|
|
/// <summary>
|
|
/// Zooms by <paramref name="factor"/> keeping <paramref name="world"/> at the same screen point;
|
|
/// the factor is cut so the scale stays within its limits.
|
|
/// </summary>
|
|
public void ZoomToPoint(Vector world, double factor)
|
|
{
|
|
var newScale = Scale * factor;
|
|
if (newScale < MinScale)
|
|
factor = MinScale / Scale;
|
|
else if (newScale > MaxScale)
|
|
factor = MaxScale / Scale;
|
|
|
|
OriginX -= (world.X * factor - world.X) * Scale;
|
|
OriginY += (world.Y * factor - world.Y) * Scale;
|
|
Scale *= factor;
|
|
OnChanged();
|
|
}
|
|
|
|
/// <summary>Zooms in or out by one step about the viewport center.</summary>
|
|
public void ZoomStep(bool zoomIn) => ZoomAboutCenter(zoomIn ? ZoomInFactor : 1 / ZoomInFactor);
|
|
|
|
/// <summary>
|
|
/// Zooms by <paramref name="factor"/> about the viewport center, as View > Zoom In and Zoom
|
|
/// Out do with <see cref="CommandZoomFactor"/>.
|
|
/// </summary>
|
|
public void ZoomAboutCenter(double factor) =>
|
|
ZoomAt(new ScreenPoint(ViewportWidth / 2, ViewportHeight / 2), factor);
|
|
|
|
/// <summary>
|
|
/// Fits <paramref name="area"/> in the viewport, centered, with <see cref="FitBorder"/> pixels of
|
|
/// margin in all; an empty area or viewport changes nothing. Returns whether it changed the view.
|
|
/// </summary>
|
|
public bool ZoomToArea(Box area)
|
|
{
|
|
if (area == null || area.Length <= 0 || area.Width <= 0 || ViewportWidth <= 0 || ViewportHeight <= 0)
|
|
return false;
|
|
|
|
var a = (ViewportHeight - FitBorder) / area.Width;
|
|
var b = (ViewportWidth - FitBorder) / area.Length;
|
|
Scale = System.Math.Clamp(a < b ? a : b, MinScale, MaxScale);
|
|
|
|
var px = area.X * Scale;
|
|
var py = area.Y * Scale;
|
|
var pw = area.Length * Scale;
|
|
var ph = area.Width * Scale;
|
|
OriginX = (ViewportWidth - pw) * 0.5 - px;
|
|
OriginY = (ViewportHeight + ph) * 0.5 + py;
|
|
OnChanged();
|
|
return true;
|
|
}
|
|
|
|
/// <summary>The current scale and origin, to restore later.</summary>
|
|
public (double Scale, double OriginX, double OriginY) Capture() => (Scale, OriginX, OriginY);
|
|
|
|
public void Restore((double Scale, double OriginX, double OriginY) state)
|
|
{
|
|
Scale = System.Math.Clamp(state.Scale, MinScale, MaxScale);
|
|
OriginX = state.OriginX;
|
|
OriginY = state.OriginY;
|
|
OnChanged();
|
|
}
|
|
|
|
private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
|
|
}
|