diff --git a/OpenNest.WinForms.Tests/Controls/GrainIndicatorTests.cs b/OpenNest.WinForms.Tests/Controls/GrainIndicatorTests.cs new file mode 100644 index 00000000..09568e98 --- /dev/null +++ b/OpenNest.WinForms.Tests/Controls/GrainIndicatorTests.cs @@ -0,0 +1,112 @@ +using System.Drawing; +using OpenNest.Controls; +using OpenNest.Math; +using OpenNest.Undo; +using static OpenNest.WinForms.Tests.FormTesting; + +namespace OpenNest.WinForms.Tests.Controls; + +/// +/// The grain arrow off the plate's lower-left corner: shown with bend lines, and a click turns +/// the grain a quarter turn without touching the selection. +/// +public class GrainIndicatorTests +{ + [Theory] + [InlineData(0, 90)] + [InlineData(90, 0)] + [InlineData(30, 120)] + [InlineData(150, 60)] + public void Turned_TurnsTheGrainAQuarterTurn(double degrees, double expected) + { + Assert.Equal(Angle.ToRadians(expected), GrainIndicator.Turned(Angle.ToRadians(degrees)), 9); + } + + [Fact] + public void Bounds_SitDiagonallyOffThePlatesLowerLeftCornerWhenThereIsRoom() + { + using var view = NewView(out var plate); + view.ZoomToControlPoint(new Point(view.Width / 2, view.Height / 2), 0.5f); + + var bounds = GrainIndicator.Bounds(view, plate); + var corner = view.PointWorldToGraph(0, 0); + + Assert.True(bounds.Right < corner.X, "left of the plate"); + Assert.True(bounds.Top > corner.Y, "below the plate"); + } + + [Fact] + public void Bounds_StayInsideTheViewWhenZoomedToFit() + { + using var view = NewView(out var plate); + + var bounds = GrainIndicator.Bounds(view, plate); + var topLeft = view.PointGraphToControl(bounds.Location); + var bottomRight = view.PointGraphToControl(new PointF(bounds.Right, bounds.Bottom)); + + Assert.True(topLeft.X >= 0, "inside the left edge"); + Assert.True(bottomRight.Y <= view.ClientSize.Height, "inside the bottom edge"); + } + + [Fact] + public void ClickingTheIndicator_TurnsTheGrainAndLeavesTheSelection() + { + using var view = NewView(out var plate); + view.ShowBendLines = true; + view.SelectAll(); + + Click(view, plate); + + Assert.Equal(Angle.HalfPI, plate.GrainAngle, 9); + Assert.Single(view.SelectedParts); + + Click(view, plate); + + Assert.Equal(0, plate.GrainAngle, 9); + } + + [Fact] + public void ClickingWhereTheIndicatorWouldBe_DoesNothingWithoutBendLines() + { + using var view = NewView(out var plate); + + Click(view, plate); + + Assert.Equal(0, plate.GrainAngle); + } + + [Fact] + public void TurnGrain_IsOneUndoableChange() + { + using var view = NewView(out var plate); + var labels = new List(); + view.BeginChangeHandler = (label, _) => + { + labels.Add(label); + return ChangeScope.Detached(); + }; + + Assert.True(view.TurnGrain()); + + Assert.Equal(["Turn Grain"], labels); + Assert.Equal(Angle.HalfPI, plate.GrainAngle, 9); + } + + private static PlateView NewView(out Plate plate) + { + plate = new Plate(new OpenNest.Geometry.Size(60, 120)) { Quadrant = 1 }; + plate.Parts.Add(new Part(new Drawing("square", new CNC.Program()), new OpenNest.Geometry.Vector(10, 10))); + var view = new PlateView { Plate = plate, Size = new Size(800, 600) }; + view.ZoomToFit(); + return view; + } + + private static void Click(PlateView view, Plate plate) + { + var bounds = GrainIndicator.Bounds(view, plate); + var center = view.PointGraphToControl( + new PointF(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2)); + Mouse(view, "OnMouseDown", center); + Mouse(view, "OnMouseUp", center); + } +} diff --git a/OpenNest/Controls/GrainIndicator.cs b/OpenNest/Controls/GrainIndicator.cs new file mode 100644 index 00000000..0c29a01f --- /dev/null +++ b/OpenNest/Controls/GrainIndicator.cs @@ -0,0 +1,78 @@ +using System.Drawing; +using System.Drawing.Drawing2D; +using OpenNest.Math; + +namespace OpenNest.Controls +{ + /// + /// The plate's grain direction as a double-headed arrow just off the plate's lower-left + /// corner. It keeps a fixed screen size at any zoom, and clicking it turns the grain 90°. + /// + internal static class GrainIndicator + { + private const float Size = 64f; + private const float Gap = 8f; + + /// + /// The indicator's square in graph coordinates, diagonally off the plate's lower-left + /// corner. When the view is too tight for that (Zoom to Fit leaves little margin), it + /// is pulled inside the view's own lower-left corner so it never scrolls out of reach. + /// + public static RectangleF Bounds(DrawControl view, Plate plate) + { + var box = plate.BoundingBox(includeParts: false); + var corner = view.PointWorldToGraph(box.X, box.Y); + var x = corner.X - Gap - Size; + var y = corner.Y + Gap; + + var viewTopLeft = view.PointControlToGraph(0, 0); + var viewBottom = view.PointControlToGraph(0, view.ClientSize.Height).Y; + x = System.Math.Max(x, viewTopLeft.X + Gap); + y = System.Math.Min(y, viewBottom - Gap - Size); + + return new RectangleF(x, y, Size, Size); + } + + /// The grain after a click: a quarter turn, as a line in [0, π). + public static double Turned(double grainAngle) => + Plate.NormalizeGrainAngle(grainAngle + Angle.HalfPI); + + public static void Draw(Graphics g, RectangleF bounds, double grainAngle, bool hot, bool enabled) + { + var previous = g.SmoothingMode; + g.SmoothingMode = SmoothingMode.AntiAlias; + + var color = !enabled ? Color.Gray : hot ? Color.FromArgb(255, 140, 0) : Color.FromArgb(70, 70, 70); + + // A light backing keeps the arrow readable where it overlaps the plate or parts. + using (var backing = new SolidBrush(hot && enabled + ? Color.FromArgb(200, 255, 225, 190) + : Color.FromArgb(170, 255, 255, 255))) + g.FillEllipse(backing, bounds); + + using var font = new Font(FontFamily.GenericSansSerif, 8f, FontStyle.Bold); + using var textBrush = new SolidBrush(color); + var label = "GRAIN"; + var labelSize = g.MeasureString(label, font); + + // The arrow sits above the label; screen Y runs down, so the world angle's sine flips. + var center = new PointF(bounds.X + bounds.Width / 2, bounds.Y + (bounds.Height - labelSize.Height) / 2); + var half = (bounds.Width - 16) / 2; + var dx = (float)(System.Math.Cos(grainAngle) * half); + var dy = (float)(-System.Math.Sin(grainAngle) * half); + + using var head = new AdjustableArrowCap(3.5f, 3.5f); + using var pen = new Pen(color, 2.5f) + { + CustomStartCap = head, + CustomEndCap = head, + }; + g.DrawLine(pen, center.X - dx, center.Y - dy, center.X + dx, center.Y + dy); + + g.DrawString(label, font, textBrush, + bounds.X + (bounds.Width - labelSize.Width) / 2, bounds.Bottom - labelSize.Height); + + g.SmoothingMode = previous; + } + } +} diff --git a/OpenNest/Controls/PlateView.cs b/OpenNest/Controls/PlateView.cs index f273ff6d..07c09faa 100644 --- a/OpenNest/Controls/PlateView.cs +++ b/OpenNest/Controls/PlateView.cs @@ -226,8 +226,24 @@ namespace OpenNest.Controls public bool DrawCutDirection { get; set; } + /// + /// Draws bend lines, up-bend etch marks and grain warnings, and the grain indicator + /// off the plate's corner that turns the grain when clicked. + /// public bool ShowBendLines { get; set; } + private bool grainIndicatorHot; + private bool grainIndicatorClicked; + + /// Whether a click on the grain indicator would turn the live plate's grain. + private bool CanTurnGrain => + ShowBendLines && plate != null && !HasProposalDisplay && !EditsLocked + && actionManager.CurrentAction is ActionSelect; + + private bool HitsGrainIndicator(Point controlPoint) => + plate != null + && GrainIndicator.Bounds(this, plate).Contains(controlPoint.X - origin.X, controlPoint.Y - origin.Y); + public double OffsetTolerance { get; set; } = 0.001; public bool FillParts { get; set; } @@ -442,6 +458,7 @@ namespace OpenNest.Controls hoverPending = false; hoveredPart = null; showTooltip = false; + grainIndicatorHot = false; Invalidate(); } @@ -506,6 +523,14 @@ namespace OpenNest.Controls if (EditsLocked) return; + if (e.Button == MouseButtons.Left && CanTurnGrain && HitsGrainIndicator(e.Location)) + { + // The click is the indicator's; selection never sees it, down or up. + grainIndicatorClicked = true; + TurnGrain(); + return; + } + if (e.Button == MouseButtons.Left && actionManager.CurrentAction is ActionSelect) { var hitCutOff = cutOffHandler.TryStartDrag(CurrentPoint, 5.0 / ViewScale); @@ -530,6 +555,11 @@ namespace OpenNest.Controls { if (EditsLocked) return; + if (e.Button == MouseButtons.Left && grainIndicatorClicked) + { + grainIndicatorClicked = false; + return; + } if (e.Button == MouseButtons.Middle && SelectedParts.Count > 0) { var dx = e.X - middleMouseDownPoint.X; @@ -632,6 +662,16 @@ namespace OpenNest.Controls return; } + if (e.Button == MouseButtons.None) + { + var hot = CanTurnGrain && HitsGrainIndicator(e.Location); + if (hot != grainIndicatorHot) + { + grainIndicatorHot = hot; + Invalidate(); + } + } + if (e.Button == MouseButtons.None && actionManager.CurrentAction is ActionSelect) { hoverPoint = e.Location; @@ -832,6 +872,11 @@ namespace OpenNest.Controls validationOverlay.Draw(e.Graphics); if (!HasProposalDisplay) cutSimulation.Draw(e.Graphics); + if (ShowBendLines && DisplayPlate != null) + { + GrainIndicator.Draw(e.Graphics, GrainIndicator.Bounds(this, DisplayPlate), + DisplayPlate.GrainAngle, grainIndicatorHot, enabled: CanTurnGrain); + } base.OnPaint(e); @@ -981,6 +1026,22 @@ namespace OpenNest.Controls return true; } + /// + /// Turns the plate's grain a quarter turn (side to side ↔ front to back), as an undoable + /// edit. Use it when the sheet's grain runs the other way from the plate as entered. + /// + public bool TurnGrain() + { + if (Plate == null) + return false; + using var change = BeginChange("Turn Grain"); + change.MarkChanged(); + Plate.GrainAngle = GrainIndicator.Turned(Plate.GrainAngle); + Invalidate(); + change.Complete(); + return true; + } + public void AlignSelected(AlignType alignType, LayoutPart fixedPart) => selection.AlignSelected(alignType, fixedPart);