mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
feat(desktop): share zoom window, measure area, hover tip and resize scaling
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>
This commit is contained in:
1 parent
db18075bec
commit
cf6a7df3be
8 files changed
+669
-9
No files matched your search
@@ -0,0 +1,73 @@
|
||||
using OpenNest.Desktop.Interaction;
|
||||
using OpenNest.Desktop.Rendering;
|
||||
|
||||
namespace OpenNest.Desktop.Tests.Interaction;
|
||||
|
||||
public class PartHoverTipTests
|
||||
{
|
||||
private readonly Part part = new(new Drawing("bracket", InteractionFixtures.Rect(10, 10)), Geometry.Vector.Zero);
|
||||
|
||||
[Fact]
|
||||
public void The_tip_shows_the_drawing_name_only_after_the_delay_at_the_rest_point()
|
||||
{
|
||||
var tip = new PartHoverTip();
|
||||
ScreenPoint? asked = null;
|
||||
|
||||
Assert.False(tip.Elapse(_ => part));
|
||||
Assert.False(tip.Move(new ScreenPoint(5, 6)));
|
||||
Assert.True(tip.IsPending);
|
||||
Assert.Null(tip.Text);
|
||||
|
||||
Assert.True(tip.Elapse(at =>
|
||||
{
|
||||
asked = at;
|
||||
return part;
|
||||
}));
|
||||
|
||||
Assert.Equal(new ScreenPoint(5, 6), asked);
|
||||
Assert.Equal("bracket", tip.Text);
|
||||
Assert.Same(part, tip.Part);
|
||||
Assert.False(tip.IsPending);
|
||||
// The timer elapsing again changes nothing.
|
||||
Assert.False(tip.Elapse(_ => null));
|
||||
Assert.Same(part, tip.Part);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_move_or_clear_hides_the_tip_and_reports_a_redraw()
|
||||
{
|
||||
var tip = new PartHoverTip();
|
||||
tip.Move(new ScreenPoint(1, 1));
|
||||
tip.Elapse(_ => part);
|
||||
|
||||
Assert.True(tip.Move(new ScreenPoint(2, 2)));
|
||||
Assert.Null(tip.Text);
|
||||
Assert.False(tip.Move(new ScreenPoint(3, 3)));
|
||||
|
||||
tip.Elapse(_ => part);
|
||||
Assert.True(tip.Clear());
|
||||
Assert.False(tip.IsPending);
|
||||
Assert.False(tip.Elapse(_ => part));
|
||||
Assert.False(tip.Clear());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Over_bare_plate_no_tip_shows()
|
||||
{
|
||||
var tip = new PartHoverTip();
|
||||
tip.Move(new ScreenPoint(1, 1));
|
||||
|
||||
Assert.False(tip.Elapse(_ => null));
|
||||
Assert.Null(tip.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_tip_sits_above_right_and_flips_at_the_right_and_top_edges()
|
||||
{
|
||||
Assert.Equal((116.0, 74.0, 46.0, 24.0), PartHoverTip.Place(new ScreenPoint(100, 100), 40, 20, 1000));
|
||||
// Past the right edge: to the left of the pointer.
|
||||
Assert.Equal((52.0, 74.0, 46.0, 24.0), PartHoverTip.Place(new ScreenPoint(100, 100), 40, 20, 150));
|
||||
// Past the top: below the pointer.
|
||||
Assert.Equal((116.0, 30.0, 46.0, 24.0), PartHoverTip.Place(new ScreenPoint(100, 10), 40, 20, 1000));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
using OpenNest.Desktop.Interaction;
|
||||
using OpenNest.Desktop.Rendering;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Desktop.Tests.Interaction;
|
||||
|
||||
public class ZoomToolsTests
|
||||
{
|
||||
private readonly TestToolHost host = new(InteractionFixtures.Plate());
|
||||
|
||||
private ZoomWindowTool StartZoomWindow()
|
||||
{
|
||||
var tool = new ZoomWindowTool(host, host.Tools);
|
||||
host.Tools.SetTool(tool);
|
||||
return tool;
|
||||
}
|
||||
|
||||
private void AssertFits(Box box)
|
||||
{
|
||||
var expected = new ViewTransform();
|
||||
expected.Resize(host.View.ViewportWidth, host.View.ViewportHeight);
|
||||
expected.ZoomToArea(box);
|
||||
Assert.Equal(expected.Scale, host.View.Scale, 9);
|
||||
Assert.Equal(expected.OriginX, host.View.OriginX, 9);
|
||||
Assert.Equal(expected.OriginY, host.View.OriginY, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zoom_areas_are_the_plate_with_parts_the_sheet_and_the_selected_parts()
|
||||
{
|
||||
var plate = host.Plate;
|
||||
plate.Parts.Add(new Part(new Drawing("outside", InteractionFixtures.Rect(10, 10)), new Vector(120, 0)));
|
||||
|
||||
Assert.Equal(130, PlateZoom.FitArea(plate).Right, 9);
|
||||
Assert.Equal(100, PlateZoom.PlateArea(plate).Right, 9);
|
||||
Assert.Null(PlateZoom.SelectedArea(plate, host.Selection));
|
||||
|
||||
host.Selection.Select(plate.Parts[1]);
|
||||
host.Selection.Select(plate.Parts[4]);
|
||||
var selected = PlateZoom.SelectedArea(plate, host.Selection);
|
||||
Assert.Equal(20, selected.Left, 9);
|
||||
Assert.Equal(0, selected.Bottom, 9);
|
||||
Assert.Equal(30, selected.Right, 9);
|
||||
Assert.Equal(30, selected.Top, 9);
|
||||
Assert.Null(PlateZoom.FitArea(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_long_drag_zooms_to_the_box_and_returns_to_select()
|
||||
{
|
||||
var tool = StartZoomWindow();
|
||||
|
||||
// 20 units at about ten pixels each: well past the 40-pixel drag.
|
||||
host.Drag(10, 10, 30, 25);
|
||||
|
||||
AssertFits(new Box(10, 10, 20, 15));
|
||||
Assert.IsType<SelectTool>(host.Tools.Current);
|
||||
Assert.Null(tool.Window);
|
||||
// One zoom ends the tool for good: Escape from Select does not resume it.
|
||||
host.Tools.Escape();
|
||||
Assert.IsType<SelectTool>(host.Tools.Current);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_short_press_sets_the_first_corner_and_the_next_click_zooms()
|
||||
{
|
||||
var tool = StartZoomWindow();
|
||||
var before = host.View.Capture();
|
||||
|
||||
host.Click(10, 10);
|
||||
|
||||
Assert.True(tool.IsBusy);
|
||||
Assert.Same(tool, host.Tools.Current);
|
||||
Assert.Equal(before, host.View.Capture());
|
||||
|
||||
// Without a button held, a move stretches the box toward the second corner.
|
||||
tool.PointerMoved(host.At(40, 30, ToolButton.None));
|
||||
Assert.Equal(new Vector(40, 30), tool.Window.Value.Second);
|
||||
|
||||
host.Click(50, 40);
|
||||
|
||||
AssertFits(new Box(10, 10, 40, 30));
|
||||
Assert.IsType<SelectTool>(host.Tools.Current);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_right_press_cancels_the_box_and_Escape_cancels_then_ends()
|
||||
{
|
||||
var tool = StartZoomWindow();
|
||||
host.Click(10, 10);
|
||||
|
||||
tool.PointerPressed(host.At(10, 10, ToolButton.Right));
|
||||
|
||||
Assert.Null(tool.Window);
|
||||
Assert.False(tool.IsBusy);
|
||||
Assert.Same(tool, host.Tools.Current);
|
||||
|
||||
host.Click(10, 10);
|
||||
host.Tools.Escape();
|
||||
Assert.Same(tool, host.Tools.Current);
|
||||
Assert.Null(tool.Window);
|
||||
host.Tools.Escape();
|
||||
Assert.IsType<SelectTool>(host.Tools.Current);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Losing_the_pointer_mid_drag_drops_the_box()
|
||||
{
|
||||
var tool = StartZoomWindow();
|
||||
var before = host.View.Capture();
|
||||
tool.PointerPressed(host.At(10, 10));
|
||||
tool.PointerMoved(host.At(30, 30));
|
||||
|
||||
tool.PointerCaptureLost();
|
||||
|
||||
Assert.Null(tool.Window);
|
||||
Assert.Equal(before, host.View.Capture());
|
||||
Assert.Same(tool, host.Tools.Current);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Measure_area_finds_the_largest_clear_box_and_Space_turns_it()
|
||||
{
|
||||
var tool = new MeasureAreaTool(host);
|
||||
host.Tools.SetTool(tool);
|
||||
|
||||
Assert.Null(tool.Area);
|
||||
// Between the square at x 20..30 and the common-line pair from x 40.
|
||||
tool.PointerMoved(host.At(35, 50, ToolButton.None));
|
||||
|
||||
Assert.Equal(new Box(30, 0, 10, 100).ToString(), tool.Area.ToString());
|
||||
Assert.Equal(tool.Area.Size.ToString(2), tool.Label);
|
||||
|
||||
// Sideways first: from the left edge to the cut-off at x = 80, above the parts.
|
||||
tool.ToggleDirection();
|
||||
Assert.True(tool.Horizontal);
|
||||
Assert.Equal(new Box(0, 30, 80, 70).ToString(), tool.Area.ToString());
|
||||
|
||||
Assert.False(tool.IsBusy);
|
||||
host.Tools.Escape();
|
||||
Assert.IsType<SelectTool>(host.Tools.Current);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Measure_area_stays_clear_of_the_part_spacing()
|
||||
{
|
||||
host.Plate.PartSpacing = 1;
|
||||
var tool = new MeasureAreaTool(host);
|
||||
|
||||
tool.PointerMoved(host.At(35, 50, ToolButton.None));
|
||||
|
||||
Assert.Equal(new Box(31, 0, 8, 100).ToString(), tool.Area.ToString());
|
||||
}
|
||||
}
|
||||
@@ -124,6 +124,75 @@ public class ViewTransformTests
|
||||
Assert.Equal(250, moved.Y, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resizing_scales_with_the_usable_size_as_the_desktop_view()
|
||||
{
|
||||
var view = Sized(800, 600);
|
||||
view.ZoomToArea(new Box(0, 0, 100, 100));
|
||||
var scale = view.Scale;
|
||||
|
||||
// Narrower: the width limits, (500 - 50) / (800 - 50).
|
||||
view.Resize(500, 600);
|
||||
Assert.Equal(scale * 450 / 750, view.Scale, 9);
|
||||
|
||||
// Back to the size at the last zoom: exactly the zoom the narrowing took away.
|
||||
view.Resize(800, 600);
|
||||
Assert.Equal(scale, view.Scale, 9);
|
||||
var fitted = new ViewTransform();
|
||||
fitted.Resize(800, 600);
|
||||
fitted.ZoomToArea(new Box(0, 0, 100, 100));
|
||||
Assert.Equal(fitted.OriginX, view.OriginX, 9);
|
||||
Assert.Equal(fitted.OriginY, view.OriginY, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resizing_measures_from_the_size_at_the_last_zoom()
|
||||
{
|
||||
var view = Sized(800, 600);
|
||||
view.ZoomToArea(new Box(0, 0, 100, 100));
|
||||
view.Resize(500, 600);
|
||||
// A zoom at the narrow size makes it the reference.
|
||||
view.ZoomAboutCenter(2);
|
||||
var scale = view.Scale;
|
||||
|
||||
view.Resize(800, 600);
|
||||
|
||||
Assert.Equal(scale * System.Math.Min(750.0 / 450, 550.0 / 550), view.Scale, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resizing_from_or_to_a_tiny_viewport_only_moves_the_origin()
|
||||
{
|
||||
var view = Sized(800, 600);
|
||||
view.ZoomToArea(new Box(0, 0, 100, 100));
|
||||
var scale = view.Scale;
|
||||
var originX = view.OriginX;
|
||||
|
||||
view.Resize(40, 600);
|
||||
Assert.Equal(scale, view.Scale, 9);
|
||||
Assert.Equal(originX - 380, view.OriginX, 9);
|
||||
view.Resize(800, 600);
|
||||
Assert.Equal(scale, view.Scale, 9);
|
||||
Assert.Equal(originX, view.OriginX, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_zoom_is_half_again_about_the_center()
|
||||
{
|
||||
var view = Sized(800, 600);
|
||||
view.ZoomToArea(new Box(0, 0, 100, 100));
|
||||
var scale = view.Scale;
|
||||
var center = view.ScreenToWorld(400, 300);
|
||||
|
||||
view.ZoomAboutCenter(ViewTransform.CommandZoomFactor);
|
||||
Assert.Equal(scale * 1.5, view.Scale, 9);
|
||||
view.ZoomAboutCenter(1 / ViewTransform.CommandZoomFactor);
|
||||
Assert.Equal(scale, view.Scale, 9);
|
||||
var after = view.WorldToScreen(center);
|
||||
Assert.Equal(400, after.X, 6);
|
||||
Assert.Equal(300, after.Y, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Panning_moves_the_origin_and_the_visible_area_follows()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
using System.Collections.Generic;
|
||||
using OpenNest.CutOffs;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Desktop.Interaction;
|
||||
|
||||
/// <summary>
|
||||
/// The WinForms Select Area action behind Tools > Measure Area, toolkit-neutral: as the pointer
|
||||
/// moves, <see cref="Area"/> is the largest empty box around it inside the plate's work area,
|
||||
/// clear of the parts (their bounding boxes grown by the part spacing) and the cut-off lines.
|
||||
/// The box grows up and down first, then sideways; Space (<see cref="ToggleDirection"/>) grows
|
||||
/// it sideways first instead. The tool stays active until Escape.
|
||||
/// </summary>
|
||||
public sealed class MeasureAreaTool : PlateTool
|
||||
{
|
||||
private readonly List<Box> obstacles = new();
|
||||
private Box bounds;
|
||||
private Vector? lastPoint;
|
||||
|
||||
public MeasureAreaTool(IPlateToolHost host)
|
||||
: base(host)
|
||||
{
|
||||
Collect();
|
||||
}
|
||||
|
||||
public override string Name => "Measure Area";
|
||||
|
||||
public override bool IsBusy => false;
|
||||
|
||||
/// <summary>Whether the box grows sideways first (Space toggles it).</summary>
|
||||
public bool Horizontal { get; private set; }
|
||||
|
||||
/// <summary>The box measured at the pointer, or null when there is none.</summary>
|
||||
public Box Area { get; private set; }
|
||||
|
||||
/// <summary>The box's size as WinForms labels it (its <see cref="Box.Size"/> to two decimals), or null.</summary>
|
||||
public string Label => Area?.Size.ToString(2);
|
||||
|
||||
public override void Cancel() => Host.Invalidate();
|
||||
|
||||
public override bool PointerMoved(ToolPointer pointer)
|
||||
{
|
||||
lastPoint = pointer.World;
|
||||
Update();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Space: switches which way the box grows first, at the pointer's last point.</summary>
|
||||
public void ToggleDirection()
|
||||
{
|
||||
Horizontal = !Horizontal;
|
||||
Update();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (bounds == null || lastPoint is not { } point)
|
||||
Area = null;
|
||||
else
|
||||
{
|
||||
var area = Horizontal
|
||||
? SpatialQuery.GetLargestBoxHorizontally(point, bounds, obstacles)
|
||||
: SpatialQuery.GetLargestBoxVertically(point, bounds, obstacles);
|
||||
Area = ReferenceEquals(area, Box.Empty) ? null : area;
|
||||
}
|
||||
Host.Invalidate();
|
||||
}
|
||||
|
||||
// Read once when the tool starts, as WinForms: the parts and cut-offs the box must clear.
|
||||
private void Collect()
|
||||
{
|
||||
var plate = Host.Plate;
|
||||
if (plate == null)
|
||||
return;
|
||||
foreach (var part in plate.Parts)
|
||||
{
|
||||
if (part.BaseDrawing.IsCutOff)
|
||||
continue;
|
||||
obstacles.Add(part.BoundingBox.Offset(plate.PartSpacing));
|
||||
}
|
||||
|
||||
// A cut-off part's bounds span from the plate origin (its program is absolute), so each
|
||||
// cut-off is a thin box along its line, from its definition.
|
||||
var plateBounds = plate.BoundingBox(includeParts: false);
|
||||
foreach (var cutOff in plate.CutOffs)
|
||||
{
|
||||
var line = cutOff.Axis == CutOffAxis.Vertical
|
||||
? new Box(cutOff.Position.X, plateBounds.Y, 0, plateBounds.Width)
|
||||
: new Box(plateBounds.X, cutOff.Position.Y, plateBounds.Length, 0);
|
||||
obstacles.Add(line.Offset(plate.PartSpacing));
|
||||
}
|
||||
bounds = plate.WorkArea();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using OpenNest.Desktop.Rendering;
|
||||
|
||||
namespace OpenNest.Desktop.Interaction;
|
||||
|
||||
/// <summary>
|
||||
/// The WinForms plate view's hover tooltip, toolkit-neutral: each pointer move (in Select, no
|
||||
/// button held) hides the tip and restarts a <see cref="Delay"/> timer; when the timer elapses
|
||||
/// the part under the point last moved to, if any, has its drawing name shown beside the
|
||||
/// pointer. The host owns the timer: it calls <see cref="Move"/>, <see cref="Clear"/> and
|
||||
/// <see cref="Elapse"/>, and redraws when they return true.
|
||||
/// </summary>
|
||||
public sealed class PartHoverTip
|
||||
{
|
||||
/// <summary>How long the pointer must rest before the tip shows, as the WinForms hover timer.</summary>
|
||||
public static readonly TimeSpan Delay = TimeSpan.FromSeconds(1);
|
||||
|
||||
private bool pending;
|
||||
|
||||
/// <summary>The part whose tip is shown, or null.</summary>
|
||||
public Part Part { get; private set; }
|
||||
|
||||
/// <summary>Where the pointer rested, in screen pixels.</summary>
|
||||
public ScreenPoint Anchor { get; private set; }
|
||||
|
||||
/// <summary>The tip's text, the part's drawing name, or null while no tip shows.</summary>
|
||||
public string Text => Part?.BaseDrawing.Name;
|
||||
|
||||
/// <summary>Whether a move started the delay and the timer has not elapsed since.</summary>
|
||||
public bool IsPending => pending;
|
||||
|
||||
/// <summary>The pointer moved: the tip hides and the delay starts again.</summary>
|
||||
/// <returns>Whether a shown tip was hidden, so the view must redraw.</returns>
|
||||
public bool Move(ScreenPoint at)
|
||||
{
|
||||
Anchor = at;
|
||||
pending = true;
|
||||
return Hide();
|
||||
}
|
||||
|
||||
/// <summary>The pointer left, pressed a button or the view changed: no tip until the next move.</summary>
|
||||
/// <returns>Whether a shown tip was hidden.</returns>
|
||||
public bool Clear()
|
||||
{
|
||||
pending = false;
|
||||
return Hide();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The delay elapsed: shows the tip for the part <paramref name="partAt"/> finds at the
|
||||
/// anchor. Nothing happens unless a move is pending.
|
||||
/// </summary>
|
||||
/// <returns>Whether a tip is now shown.</returns>
|
||||
public bool Elapse(Func<ScreenPoint, Part> partAt)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(partAt);
|
||||
if (!pending)
|
||||
return false;
|
||||
pending = false;
|
||||
Part = partAt(Anchor);
|
||||
return Part != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Where the tip's box goes for text of the given size, as WinForms places it: above and to
|
||||
/// the right of the anchor, flipped to the left when it would pass the view's right edge and
|
||||
/// below when it would pass the top. The box is the text plus a 3 by 2 pixel padding.
|
||||
/// </summary>
|
||||
public static (double X, double Y, double Width, double Height) Place(
|
||||
ScreenPoint anchor, double textWidth, double textHeight, double viewWidth)
|
||||
{
|
||||
var x = anchor.X + 16;
|
||||
var y = anchor.Y - textHeight - 6;
|
||||
if (x + textWidth + 8 > viewWidth)
|
||||
x = anchor.X - textWidth - 8;
|
||||
if (y < 0)
|
||||
y = anchor.Y + 20;
|
||||
return (x, y, textWidth + 6, textHeight + 4);
|
||||
}
|
||||
|
||||
private bool Hide()
|
||||
{
|
||||
if (Part == null)
|
||||
return false;
|
||||
Part = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Linq;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Desktop.Interaction;
|
||||
|
||||
/// <summary>
|
||||
/// The areas View > Zoom To fits, as the WinForms plate view's: Fit takes the plate with its
|
||||
/// parts, Plate the sheet alone and Selected the selected parts.
|
||||
/// </summary>
|
||||
public static class PlateZoom
|
||||
{
|
||||
/// <summary>Zoom To > Fit (and the F key): the plate and every part on it, or null.</summary>
|
||||
public static Box FitArea(Plate plate) => plate?.BoundingBox(true);
|
||||
|
||||
/// <summary>Zoom To > Plate: the sheet without its parts, or null.</summary>
|
||||
public static Box PlateArea(Plate plate) => plate?.BoundingBox(false);
|
||||
|
||||
/// <summary>
|
||||
/// Zoom To > Selected: the selected parts on the plate (cut-offs left out, as WinForms),
|
||||
/// or null when no part is selected.
|
||||
/// </summary>
|
||||
public static Box SelectedArea(Plate plate, PlateSelection selection)
|
||||
{
|
||||
if (plate == null || selection == null)
|
||||
return null;
|
||||
var parts = selection.Parts.Where(plate.Parts.Contains).ToList();
|
||||
return parts.Count == 0 ? null : parts.GetBoundingBox();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Desktop.Interaction;
|
||||
|
||||
/// <summary>
|
||||
/// The WinForms Zoom Window action (View > Zoom To > Area), toolkit-neutral: a left drag of
|
||||
/// more than <see cref="DragPixels"/> zooms to the box dragged; a shorter press sets the first
|
||||
/// corner, and the next click sets the second and zooms. A right press cancels the box. One zoom
|
||||
/// ends the tool, and Escape from Select does not bring it back.
|
||||
/// </summary>
|
||||
public sealed class ZoomWindowTool : PlateTool
|
||||
{
|
||||
/// <summary>A drag longer than this, in pixels, zooms on release; a shorter one waits for a second click.</summary>
|
||||
public const double DragPixels = 40;
|
||||
|
||||
private readonly PlateToolManager tools;
|
||||
private bool awaitingSecondCorner;
|
||||
|
||||
/// <param name="tools">The manager hosting the tool, which returns to Select after the zoom.</param>
|
||||
public ZoomWindowTool(IPlateToolHost host, PlateToolManager tools)
|
||||
: base(host)
|
||||
{
|
||||
this.tools = tools ?? throw new ArgumentNullException(nameof(tools));
|
||||
}
|
||||
|
||||
public override string Name => "Zoom Window";
|
||||
|
||||
// One zoom ends this tool; Escape from Select must not bring it back.
|
||||
public override bool ResumeOnEscape => false;
|
||||
|
||||
/// <summary>Busy once the first corner is set and the tool waits for the second click.</summary>
|
||||
public override bool IsBusy => awaitingSecondCorner;
|
||||
|
||||
/// <summary>The box between the corners so far, or null before the first press.</summary>
|
||||
public SelectionWindow? Window { get; private set; }
|
||||
|
||||
public override void Cancel()
|
||||
{
|
||||
Window = null;
|
||||
awaitingSecondCorner = false;
|
||||
Host.Invalidate();
|
||||
}
|
||||
|
||||
public override bool PointerPressed(ToolPointer pointer)
|
||||
{
|
||||
if (pointer.Button == ToolButton.Right)
|
||||
{
|
||||
Cancel();
|
||||
return true;
|
||||
}
|
||||
if (pointer.Button != ToolButton.Left)
|
||||
return false;
|
||||
if (!awaitingSecondCorner)
|
||||
{
|
||||
Window = new SelectionWindow(pointer.World, pointer.World);
|
||||
Host.Invalidate();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool PointerMoved(ToolPointer pointer)
|
||||
{
|
||||
if (Window is not { } window || !(awaitingSecondCorner || pointer.Button == ToolButton.Left))
|
||||
return false;
|
||||
Window = window with { Second = pointer.World };
|
||||
Host.Invalidate();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool PointerReleased(ToolPointer pointer)
|
||||
{
|
||||
if (pointer.Button != ToolButton.Left || Window is not { } window)
|
||||
return false;
|
||||
window = window with { Second = pointer.World };
|
||||
Window = window;
|
||||
if (!awaitingSecondCorner
|
||||
&& Host.View.LengthToScreen(window.First.DistanceTo(window.Second)) <= DragPixels)
|
||||
{
|
||||
awaitingSecondCorner = true;
|
||||
Host.Invalidate();
|
||||
return true;
|
||||
}
|
||||
ZoomAndFinish(window);
|
||||
return true;
|
||||
}
|
||||
|
||||
// A drag cut short (Alt+Tab, say) drops the box; a box waiting for its second click stays.
|
||||
public override void PointerCaptureLost()
|
||||
{
|
||||
if (awaitingSecondCorner)
|
||||
return;
|
||||
Window = null;
|
||||
Host.Invalidate();
|
||||
}
|
||||
|
||||
private void ZoomAndFinish(SelectionWindow window)
|
||||
{
|
||||
// A box with no width or height zooms nothing, as WinForms, but still ends the tool.
|
||||
Host.View.ZoomToArea(window.Box);
|
||||
Window = null;
|
||||
awaitingSecondCorner = false;
|
||||
tools.Select();
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,15 @@ public readonly record struct ScreenPoint(double X, double Y);
|
||||
/// </summary>
|
||||
public sealed class ViewTransform
|
||||
{
|
||||
/// <summary>The DrawControl's zoom step for one Zoom In.</summary>
|
||||
/// <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;
|
||||
|
||||
@@ -57,21 +63,57 @@ public sealed class ViewTransform
|
||||
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. After the first size, a resize keeps the view centred by moving
|
||||
/// the origin half the change, as the WinForms view does.
|
||||
/// 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;
|
||||
OriginX += (width - ViewportWidth) * 0.5;
|
||||
OriginY += (height - ViewportHeight) * 0.5;
|
||||
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)
|
||||
{
|
||||
@@ -103,12 +145,18 @@ public sealed class ViewTransform
|
||||
OnChanged();
|
||||
}
|
||||
|
||||
/// <summary>Zooms in or out by one step about the viewport centre.</summary>
|
||||
public void ZoomStep(bool zoomIn) =>
|
||||
ZoomAt(new ScreenPoint(ViewportWidth / 2, ViewportHeight / 2), zoomIn ? ZoomInFactor : 1 / ZoomInFactor);
|
||||
/// <summary>Zooms in or out by one step about the viewport center.</summary>
|
||||
public void ZoomStep(bool zoomIn) => ZoomAboutCenter(zoomIn ? ZoomInFactor : 1 / ZoomInFactor);
|
||||
|
||||
/// <summary>
|
||||
/// Fits <paramref name="area"/> in the viewport, centred, with <see cref="FitBorder"/> pixels of
|
||||
/// 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)
|
||||
|
||||
Reference in new issue
Block a user