diff --git a/UVtools.WPF/Controls/AdvancedImageBox.cs b/UVtools.WPF/Controls/AdvancedImageBox.cs index b6f6837..04f3033 100644 --- a/UVtools.WPF/Controls/AdvancedImageBox.cs +++ b/UVtools.WPF/Controls/AdvancedImageBox.cs @@ -18,8 +18,8 @@ using UVtools.WPF.Extensions; using Bitmap = Avalonia.Media.Imaging.Bitmap; using Brushes = Avalonia.Media.Brushes; using Pen = Avalonia.Media.Pen; -using Point = System.Drawing.Point; -using Size = System.Drawing.Size; +using Point = Avalonia.Point; +using Size = Avalonia.Size; namespace UVtools.WPF.Controls @@ -542,7 +542,7 @@ namespace UVtools.WPF.Controls get { var viewport = GetImageViewPort(); - return new Point( (int) (viewport.Width / 2), (int) (viewport.Height / 2)); + return new Point( (viewport.Width / 2), viewport.Height / 2); } } @@ -702,7 +702,7 @@ namespace UVtools.WPF.Controls // TODO: Really should update the source method to handle multiple increments rather than calling it multiple times for (int i = 0; i < spins; i++) { - ProcessMouseZoom(e.Delta.Y > 0, e.GetPosition(this).ToDotNet()); + ProcessMouseZoom(e.Delta.Y > 0, e.GetPosition(this)); } //InvalidateVisual(); @@ -781,7 +781,7 @@ namespace UVtools.WPF.Controls /// true if the specified point is located within the image view port; otherwise, false. /// public virtual bool IsPointInImage(Point point) - => GetImageViewPort().Contains(point.ToAvalonia()); + => GetImageViewPort().Contains(point); /// /// Determines whether the specified point is located within the image view port @@ -803,7 +803,7 @@ namespace UVtools.WPF.Controls /// true if the specified point is located within the image view port; otherwise, false. /// public bool IsPointInImage(double x, double y) - => IsPointInImage(new Point((int) x, (int) y)); + => IsPointInImage(new Point(x, y)); /// /// Converts the given client size point to represent a coordinate on the source image. @@ -849,20 +849,20 @@ namespace UVtools.WPF.Controls /// Point.Empty if the point could not be matched to the source image, otherwise the new translated point public virtual Point PointToImage(Point point, bool fitToBounds) { - int x; - int y; + double x; + double y; var viewport = GetImageViewPort(); - if (!fitToBounds || viewport.Contains(point.ToAvalonia())) + if (!fitToBounds || viewport.Contains(point)) { - x = (int) ((point.X + Offset.X - viewport.X) / ZoomFactor); - y = (int) ((point.Y + Offset.Y - viewport.Y) / ZoomFactor); + x = ((point.X + Offset.X - viewport.X) / ZoomFactor); + y = ((point.Y + Offset.Y - viewport.Y) / ZoomFactor); if (fitToBounds) { - x = x.Clamp(0, (int) Image.Size.Width); - y = y.Clamp(0, (int) Image.Size.Height); + x = x.Clamp(0, Image.Size.Width); + y = y.Clamp(0, Image.Size.Height); } } else @@ -882,7 +882,7 @@ namespace UVtools.WPF.Controls /// The X co-ordinate relative to the x parameter. /// The Y co-ordinate relative to the y parameter. public void ScrollTo(double x, double y, double relativeX, double relativeY) - => ScrollTo(new Point((int) x, (int) y), new Point((int) relativeX, (int) relativeY)); + => ScrollTo(new Point(x, y), new Point(relativeX, relativeY)); /// /// Scrolls the control to the given point in the image, offset at the specified display point @@ -930,7 +930,7 @@ namespace UVtools.WPF.Controls if (_zoom != value) { _zoom = value; - if (UpdateViewPort()) + if (!UpdateViewPort()) { InvalidateArrange(); } @@ -1016,7 +1016,7 @@ namespace UVtools.WPF.Controls /// The height of the selection region. public void ZoomToRegion(double x, double y, double width, double height) { - ZoomToRegion(new Rectangle((int)x, (int)y, (int)width, (int)height)); + ZoomToRegion(new Rect(x, y, width, height)); } /// @@ -1028,37 +1028,39 @@ namespace UVtools.WPF.Controls /// The height of the selection region. public void ZoomToRegion(int x, int y, int width, int height) { - ZoomToRegion(new Rectangle(x, y, width, height)); + ZoomToRegion(new Rect(x, y, width, height)); } /// /// Adjusts the view port to fit the given region /// /// The rectangle to fit the view port to. - public virtual void ZoomToRegion(Rectangle rectangle) + public virtual void ZoomToRegion(Rectangle rectangle) => ZoomToRegion(rectangle.ToAvalonia()); + + /// + /// Adjusts the view port to fit the given region + /// + /// The rectangle to fit the view port to. + public virtual void ZoomToRegion(Rect rectangle) { - var ratioX = Viewport.Width / rectangle.Width; - var ratioY = Viewport.Height / rectangle.Height; - var zoomFactor = Math.Min(ratioX, ratioY); - var cx = rectangle.X + rectangle.Width / 2; - var cy = rectangle.Y + rectangle.Height / 2; + { + var ratioX = Viewport.Width / rectangle.Width; + var ratioY = Viewport.Height / rectangle.Height; + var zoomFactor = Math.Min(ratioX, ratioY); + var cx = rectangle.X + rectangle.Width / 2; + var cy = rectangle.Y + rectangle.Height / 2; - Zoom = (int)(zoomFactor * 100); - CenterAt(new Point(cx, cy)); + Zoom = (int)(zoomFactor * 100); + CenterAt(new Point(cx, cy)); + } } - /// - /// Adjusts the view port to fit the given region - /// - /// The rectangle to fit the view port to. - public virtual void ZoomToRegion(Rect rectangle) => ZoomToRegion(rectangle.ToDotNet()); - /// /// Centers the given point in the image in the center of the control /// /// The point of the image to attempt to center. public virtual void CenterAt(Point imageLocation) - => ScrollTo(imageLocation, new Point((int) (Viewport.Width / 2), (int) (Viewport.Height / 2))); + => ScrollTo(imageLocation, new Point(Viewport.Width / 2, Viewport.Height / 2)); /// /// Centers the given point in the image in the center of the control @@ -1074,7 +1076,7 @@ namespace UVtools.WPF.Controls /// The X co-ordinate of the point to center. /// The Y co-ordinate of the point to center. public void CenterAt(double x, double y) - => CenterAt(new Point((int) x, (int) y)); + => CenterAt(new Point(x, y)); /// /// Resets the viewport to show the center of the image. @@ -1181,7 +1183,7 @@ namespace UVtools.WPF.Controls /// A which has been scaled to match the current zoom level public virtual Point GetScaledPoint(Point source) { - return new Point((int)(source.X * this.ZoomFactor), (int)(source.Y * this.ZoomFactor)); + return new Point(source.X * ZoomFactor, source.Y * ZoomFactor); } /// @@ -1202,9 +1204,9 @@ namespace UVtools.WPF.Controls /// The width of the rectangle. /// The height of the rectangle. /// A which has been scaled to match the current zoom level - public Rectangle GetScaledRectangle(int x, int y, int width, int height) + public Rect GetScaledRectangle(int x, int y, int width, int height) { - return GetScaledRectangle(new Rectangle(x, y, width, height)); + return GetScaledRectangle(new Rect(x, y, width, height)); } /// @@ -1226,9 +1228,9 @@ namespace UVtools.WPF.Controls /// The location of the source rectangle. /// The size of the source rectangle. /// A which has been scaled to match the current zoom level - public Rectangle GetScaledRectangle(Point location, Size size) + public Rect GetScaledRectangle(Point location, Size size) { - return GetScaledRectangle(new Rectangle(location, size)); + return GetScaledRectangle(new Rect(location, size)); } /// @@ -1247,9 +1249,9 @@ namespace UVtools.WPF.Controls /// /// The source to scale. /// A which has been scaled to match the current zoom level - public virtual Rectangle GetScaledRectangle(Rectangle source) + public virtual Rect GetScaledRectangle(Rect source) { - return new Rectangle((int)(source.Left * ZoomFactor), (int)(source.Top * this.ZoomFactor), (int)(source.Width * this.ZoomFactor), (int)(source.Height * this.ZoomFactor)); + return new Rect(source.Left * ZoomFactor, source.Top * ZoomFactor, source.Width * ZoomFactor, source.Height * ZoomFactor); } /// @@ -1301,7 +1303,7 @@ namespace UVtools.WPF.Controls /// A which has been resized to match the current zoom level public virtual Size GetScaledSize(Size source) { - return new Size((int)(source.Width * this.ZoomFactor), (int)(source.Height * this.ZoomFactor)); + return new Size(source.Width * ZoomFactor, source.Height * ZoomFactor); } /// @@ -1408,7 +1410,7 @@ namespace UVtools.WPF.Controls return new Rect(sourceLeft, sourceTop, sourceWidth, sourceHeight); } - return new Rect(0, 0, (int) Image.Size.Width, (int) Image.Size.Height); + return new Rect(0, 0, Image.Size.Width, Image.Size.Height); } /// @@ -1474,7 +1476,7 @@ namespace UVtools.WPF.Controls if (location.X > Viewport.Width) return; if (location.Y > Viewport.Height) return; - _startMousePosition = location.ToDotNet(); + _startMousePosition = location; IsPanning = true; } diff --git a/UVtools.WPF/MainWindow.axaml.cs b/UVtools.WPF/MainWindow.axaml.cs index 753e4fb..593172c 100644 --- a/UVtools.WPF/MainWindow.axaml.cs +++ b/UVtools.WPF/MainWindow.axaml.cs @@ -658,13 +658,14 @@ namespace UVtools.WPF if (issue.Type == LayerIssue.IssueType.TouchingBound || issue.Type == LayerIssue.IssueType.EmptyLayer || (issue.X == -1 && issue.Y == -1)) { - ZoomToFit(); + ZoomToFit(e.PointerPressedEventArgs); } else if (issue.X >= 0 && issue.Y >= 0) { - if (Settings.LayerPreview.ZoomIssues /*^ (ModifierKeys & Keys.Alt) != 0*/) + + if (Settings.LayerPreview.ZoomIssues ^ (e.PointerPressedEventArgs.KeyModifiers & KeyModifiers.Alt) != 0) { - ZoomToIssue(issue); + ZoomToIssue(issue, e.PointerPressedEventArgs); } else { @@ -675,7 +676,7 @@ namespace UVtools.WPF if (!LayerImageBox.GetSourceImageRegion().Contains(GetTransposedIssueBounds(issue).ToAvalonia())) { - CenterAtIssue(issue); + CenterAtIssue(issue, e.PointerPressedEventArgs); } } } @@ -683,18 +684,18 @@ namespace UVtools.WPF ForceUpdateActualLayer(issue.LayerIndex); return; } - + if (e.PointerPressedEventArgs.ClickCount == 2) { if (pointer.Properties.IsLeftButtonPressed) { - ZoomToIssue(issue); + ZoomToIssue(issue, e.PointerPressedEventArgs); return; } if (pointer.Properties.IsRightButtonPressed) { - ZoomToFit(); + ZoomToFit(e.PointerPressedEventArgs); return; } } @@ -2046,12 +2047,12 @@ namespace UVtools.WPF /// Zoom the layer preview to the passed issue, or if appropriate for issue type, /// Zoom to fit the plate or print bounds. /// - private void ZoomToIssue(LayerIssue issue) + private void ZoomToIssue(LayerIssue issue, PointerPressedEventArgs pointer) { if (issue.Type == LayerIssue.IssueType.TouchingBound || issue.Type == LayerIssue.IssueType.EmptyLayer || (issue.X == -1 && issue.Y == -1)) { - ZoomToFit(); + ZoomToFit(pointer); return; } @@ -2079,12 +2080,12 @@ namespace UVtools.WPF /// Center the layer preview on the passed issue, or if appropriate for issue type, /// Zoom to fit the plate or print bounds. /// - private void CenterAtIssue(LayerIssue issue) + private void CenterAtIssue(LayerIssue issue, PointerPressedEventArgs pointer) { if (issue.Type == LayerIssue.IssueType.TouchingBound || issue.Type == LayerIssue.IssueType.EmptyLayer || (issue.X == -1 && issue.Y == -1)) { - ZoomToFit(); + ZoomToFit(pointer); } if (issue.X >= 0 && issue.Y >= 0) @@ -2103,14 +2104,14 @@ namespace UVtools.WPF LayerImageBox.ZoomToRegion(SlicerFile.LayerManager.BoundingRectangle); } - private void ZoomToFit() + private void ZoomToFit(PointerPressedEventArgs pointer = null) { - if (ReferenceEquals(SlicerFile, null)) return; + if (!IsFileLoaded) return; // If ALT key is pressed when ZoomToFit is performed, the configured option for // zoom to plate vs. zoom to print bounds will be inverted. - if (Settings.LayerPreview.ZoomToFitPrintVolumeBounds /*^ (Application. & KeyModifiers.Alt) != 0*/) + if (Settings.LayerPreview.ZoomToFitPrintVolumeBounds ^ (!ReferenceEquals(pointer, null) && (pointer.KeyModifiers & KeyModifiers.Alt) != 0)) { if (!_showLayerImageRotated) {