From 57e7a1fdd8215990b95bbecbf13b51075a7deb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Fri, 25 Sep 2020 19:20:11 +0100 Subject: [PATCH] Fix zoom on ShowLayer --- UVtools.GUI/FrmMain.cs | 1 - UVtools.WPF/Controls/AdvancedImageBox.cs | 65 ++++++++++--------- .../Extensions/PrimitivesExtensions.cs | 5 ++ UVtools.WPF/MainWindow.axaml.cs | 16 ++--- UVtools.WPF/UVtools.WPF.csproj | 6 +- 5 files changed, 46 insertions(+), 47 deletions(-) diff --git a/UVtools.GUI/FrmMain.cs b/UVtools.GUI/FrmMain.cs index e462eac..d7c8b0d 100644 --- a/UVtools.GUI/FrmMain.cs +++ b/UVtools.GUI/FrmMain.cs @@ -3663,7 +3663,6 @@ namespace UVtools.GUI pbLayer.ZoomOut(true); return; } - CenterLayerAt(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2, zoomLevel); } diff --git a/UVtools.WPF/Controls/AdvancedImageBox.cs b/UVtools.WPF/Controls/AdvancedImageBox.cs index a3ff991..b6f6837 100644 --- a/UVtools.WPF/Controls/AdvancedImageBox.cs +++ b/UVtools.WPF/Controls/AdvancedImageBox.cs @@ -480,8 +480,9 @@ namespace UVtools.WPF.Controls } else { - SizedContainer.Width = _image.Size.Width; - SizedContainer.Height = _image.Size.Height; + UpdateViewPort(); + //SizedContainer.Width = _image.Size.Width; + //SizedContainer.Height = _image.Size.Height; } InvalidateVisual(); @@ -540,8 +541,8 @@ namespace UVtools.WPF.Controls { get { - Rectangle viewport = GetImageViewPort(); - return new Point((int) (viewport.Width / 2), (int) (viewport.Height / 2)); + var viewport = GetImageViewPort(); + return new Point( (int) (viewport.Width / 2), (int) (viewport.Height / 2)); } } @@ -780,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); + => GetImageViewPort().Contains(point.ToAvalonia()); /// /// Determines whether the specified point is located within the image view port @@ -853,7 +854,7 @@ namespace UVtools.WPF.Controls var viewport = GetImageViewPort(); - if (!fitToBounds || viewport.Contains(point)) + if (!fitToBounds || viewport.Contains(point.ToAvalonia())) { x = (int) ((point.X + Offset.X - viewport.X) / ZoomFactor); y = (int) ((point.Y + Offset.Y - viewport.Y) / ZoomFactor); @@ -929,7 +930,7 @@ namespace UVtools.WPF.Controls if (_zoom != value) { _zoom = value; - if (!UpdateViewPort()) + if (UpdateViewPort()) { InvalidateArrange(); } @@ -1107,7 +1108,7 @@ namespace UVtools.WPF.Controls if (changed) { - //Debug.WriteLine($"Update ViewPort: {DateTime.Now.Ticks}"); + Debug.WriteLine($"Update ViewPort: {DateTime.Now.Ticks}"); InvalidateArrange(); } @@ -1361,8 +1362,8 @@ namespace UVtools.WPF.Controls if (Image is null) return; // Draw iamge context.DrawImage(Image, - GetSourceImageRegion().ToAvalonia(), - GetImageViewPort().ToAvalonia() + GetSourceImageRegion(), + GetImageViewPort() ); //SkiaContext.SkCanvas.dr // Draw pixel grid @@ -1384,7 +1385,7 @@ namespace UVtools.WPF.Controls context.DrawLine(pen, new Avalonia.Point(viewport.Y, y), new Avalonia.Point(viewport.Right, y)); } - context.DrawRectangle(pen, viewport.ToAvalonia()); + context.DrawRectangle(pen, viewport); } } @@ -1392,55 +1393,55 @@ namespace UVtools.WPF.Controls /// Gets the source image region. /// /// - public virtual Rectangle GetSourceImageRegion() + public virtual Rect GetSourceImageRegion() { - if (Image is null) return Rectangle.Empty; + if (Image is null) return Rect.Empty; if (SizeMode != SizeModes.Stretch) { var viewPort = GetImageViewPort(); - int sourceLeft = (int) (Offset.X / ZoomFactor); - int sourceTop = (int) (Offset.Y / ZoomFactor); - int sourceWidth = (int) (viewPort.Width / ZoomFactor); - int sourceHeight = (int) (viewPort.Height / ZoomFactor); + double sourceLeft = (Offset.X / ZoomFactor); + double sourceTop = (Offset.Y / ZoomFactor); + double sourceWidth = (viewPort.Width / ZoomFactor); + double sourceHeight = (viewPort.Height / ZoomFactor); - return new Rectangle(sourceLeft, sourceTop, sourceWidth, sourceHeight); + return new Rect(sourceLeft, sourceTop, sourceWidth, sourceHeight); } - return new Rectangle(0, 0, (int) Image.Size.Width, (int) Image.Size.Height); + return new Rect(0, 0, (int) Image.Size.Width, (int) Image.Size.Height); } /// /// Gets the image view port. /// /// - public virtual Rectangle GetImageViewPort() + public virtual Rect GetImageViewPort() { - if (Viewport.Width == 0 && Viewport.Height == 0) return Rectangle.Empty; + if (Viewport.Width == 0 && Viewport.Height == 0) return Rect.Empty; - int xOffset = 0; - int yOffset = 0; - int width; - int height; + double xOffset = 0; + double yOffset = 0; + double width; + double height; if (SizeMode != SizeModes.Stretch) { if (AutoCenter) { - xOffset = (int) (!IsHorizontalBarVisible ? (Viewport.Width - ScaledImageWidth) / 2 : 0); - yOffset = (int) (!IsVerticalBarVisible ? (Viewport.Height - ScaledImageHeight) / 2 : 0); + xOffset = (!IsHorizontalBarVisible ? (Viewport.Width - ScaledImageWidth) / 2 : 0); + yOffset = (!IsVerticalBarVisible ? (Viewport.Height - ScaledImageHeight) / 2 : 0); } - width = (int) Math.Min(ScaledImageWidth - Math.Abs(Offset.X), Viewport.Width); - height = (int) Math.Min(ScaledImageHeight - Math.Abs(Offset.Y), Viewport.Height); + width = Math.Min(ScaledImageWidth - Math.Abs(Offset.X), Viewport.Width); + height = Math.Min(ScaledImageHeight - Math.Abs(Offset.Y), Viewport.Height); } else { - width = (int) Viewport.Width; - height = (int) Viewport.Height; + width = Viewport.Width; + height = Viewport.Height; } - return new Rectangle(xOffset, yOffset, width, height); + return new Rect(xOffset, yOffset, width, height); } /// diff --git a/UVtools.WPF/Extensions/PrimitivesExtensions.cs b/UVtools.WPF/Extensions/PrimitivesExtensions.cs index 8a96a33..def046b 100644 --- a/UVtools.WPF/Extensions/PrimitivesExtensions.cs +++ b/UVtools.WPF/Extensions/PrimitivesExtensions.cs @@ -17,6 +17,11 @@ namespace UVtools.WPF.Extensions return new System.Drawing.Point((int) point.X, (int) point.Y); } + public static Point ToAvalonia(this System.Drawing.Point point) + { + return new Point(point.X, point.Y); + } + public static bool IsEmpty(this Point point) { return point.X == 0 && point.Y == 0; diff --git a/UVtools.WPF/MainWindow.axaml.cs b/UVtools.WPF/MainWindow.axaml.cs index cb7d836..753e4fb 100644 --- a/UVtools.WPF/MainWindow.axaml.cs +++ b/UVtools.WPF/MainWindow.axaml.cs @@ -673,7 +673,7 @@ namespace UVtools.WPF // Issues already in view will not be centered, though their color may // change and the crosshair may move to reflect active selections. - if (!LayerImageBox.GetSourceImageRegion().Contains(GetTransposedIssueBounds(issue))) + if (!LayerImageBox.GetSourceImageRegion().Contains(GetTransposedIssueBounds(issue).ToAvalonia())) { CenterAtIssue(issue); } @@ -2016,28 +2016,21 @@ namespace UVtools.WPF } - public void CenterLayerAt(Rectangle rectangle, int zoomLevel = 0, bool zoomToRegion = false) => CenterLayerAt(rectangle.ToAvalonia(), zoomLevel, zoomToRegion); - - /// - /// Centers layer view on a middle of a given rectangle - /// - /// Rectangle holding coordinates and bounds - /// Zoom level to set, 0 to ignore or negative value to get current locked zoom level - /// Auto zoom to a region and ensure that region area stays all visible when possible, when true this will overwrite zoomLevel - public void CenterLayerAt(Rect rectangle, int zoomLevel = 0, bool zoomToRegion = false) + public void CenterLayerAt(Rectangle rectangle, int zoomLevel = 0, bool zoomToRegion = false) { var viewPort = LayerImageBox.GetSourceImageRegion(); if (zoomToRegion || rectangle.Width * AppSettings.LockedZoomLevel / LayerImageBox.Zoom > viewPort.Width || rectangle.Height * AppSettings.LockedZoomLevel / LayerImageBox.Zoom > viewPort.Height) { + Debug.WriteLine("zoom to region"); //SupressLayerZoomEvent = true; LayerImageBox.ZoomToRegion(rectangle); //SupressLayerZoomEvent = false; //pbLayer.ZoomOut(true); return; } - + Debug.WriteLine($"Center at {zoomLevel}"); CenterLayerAt(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2, zoomLevel); } @@ -2075,6 +2068,7 @@ namespace UVtools.WPF ShowLayer(); tsLayerImageShowCrosshairs.Checked = true; }*/ + CenterLayerAt(GetTransposedIssueBounds(issue), AppSettings.LockedZoomLevel); diff --git a/UVtools.WPF/UVtools.WPF.csproj b/UVtools.WPF/UVtools.WPF.csproj index 4bfc8f1..4712ba3 100644 --- a/UVtools.WPF/UVtools.WPF.csproj +++ b/UVtools.WPF/UVtools.WPF.csproj @@ -20,10 +20,10 @@ true - + - - + +