From c7d2c0efd49b451ee0acaf8804590d39b0822317 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Fri, 11 Oct 2024 13:45:27 +0100 Subject: [PATCH] Update boundary logic --- .../ContentCard/ContentCard.cs | 6 +- .../ContentCropper/ContentCropper.axaml | 32 ++++ .../ContentCropper/ContentCropper.cs | 157 +++++++++++++++--- 3 files changed, 172 insertions(+), 23 deletions(-) diff --git a/Toolkit.UI.Controls.Avalonia/ContentCard/ContentCard.cs b/Toolkit.UI.Controls.Avalonia/ContentCard/ContentCard.cs index 38584a7..2e9543e 100644 --- a/Toolkit.UI.Controls.Avalonia/ContentCard/ContentCard.cs +++ b/Toolkit.UI.Controls.Avalonia/ContentCard/ContentCard.cs @@ -2,7 +2,5 @@ namespace Toolkit.UI.Controls.Avalonia; -public class ContentCard : ContentControl -{ - -} +public class ContentCard : + ContentControl; \ No newline at end of file diff --git a/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.axaml b/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.axaml index 9794b98..25644d0 100644 --- a/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.axaml +++ b/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.axaml @@ -25,6 +25,10 @@ 0,3,3,0 0,0,3,3 3,0,0,3 + 3,0,0,0 + 0,3,0,0 + 0,0,3,0 + 0,0,0,3 @@ -91,6 +95,34 @@ BorderThickness="{StaticResource ContentCropperBottomRightThumbBorderThickness}" Cursor="BottomRightCorner" Theme="{StaticResource ContentCropperThumbStyle}" /> + + + + diff --git a/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.cs b/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.cs index 2c14336..608273f 100644 --- a/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.cs +++ b/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.cs @@ -36,6 +36,10 @@ public class ContentCropper : ContentControl private Path? overlayPath; private Thumb? topLeftButton; private Thumb? topRightButton; + private Thumb? leftButton; + private Thumb? rightButton; + private Thumb? topButton; + private Thumb? bottomButton; static ContentCropper() { @@ -96,6 +100,31 @@ public class ContentCropper : ContentControl { bottomRightButton.DragDelta += OnThumbDragDelta; } + + leftButton = args.NameScope.Find("LeftButton"); + if (leftButton is not null) + { + leftButton.DragDelta += OnThumbDragDelta; + } + + rightButton = args.NameScope.Find("RightButton"); + if (rightButton is not null) + { + rightButton.DragDelta += OnThumbDragDelta; + } + + topButton = args.NameScope.Find("TopButton"); + if (topButton is not null) + { + topButton.DragDelta += OnThumbDragDelta; + } + + bottomButton = args.NameScope.Find("BottomButton"); + if (bottomButton is not null) + { + bottomButton.DragDelta += OnThumbDragDelta; + } + } protected override void OnLoaded(RoutedEventArgs args) @@ -155,7 +184,6 @@ public class ContentCropper : ContentControl RenderOverLays(); } - private void InitializeCropRect() { if (canvas is null || Content is not Control content) @@ -225,12 +253,17 @@ public class ContentCropper : ContentControl UpdateCropRatios(); } + + private void OnThumbDragDelta(object? sender, VectorEventArgs args) { if (canvas is null || border is null || sender is not Thumb thumb) { return; } + + double minimumWidth = 20; + double minimumHeight = 20; double deltaX = args.Vector.X; double deltaY = args.Vector.Y; @@ -240,45 +273,111 @@ public class ContentCropper : ContentControl double newWidth = border.Width; double newHeight = border.Height; + bool canResizeWidth = true; + bool canResizeHeight = true; + switch (thumb.Name) { case "TopLeftButton": - newWidth = Math.Max(0, border.Width - deltaX); - newHeight = Math.Max(0, border.Height - deltaY); - if (newWidth > 0) + if (border.Width - deltaX < minimumWidth) { + canResizeWidth = false; + } + + if (border.Height - deltaY < minimumHeight) + { + canResizeHeight = false; + } + + if (canResizeWidth) + { + newWidth = border.Width - deltaX; leftPosition += deltaX; } - if (newHeight > 0) + + if (canResizeHeight) { + newHeight = border.Height - deltaY; topPosition += deltaY; } break; case "TopRightButton": - newWidth = Math.Max(0, border.Width + deltaX); - newHeight = Math.Max(0, border.Height - deltaY); - if (newHeight > 0) + if (border.Height - deltaY < minimumHeight) { + canResizeHeight = false; + } + + newWidth = border.Width + deltaX; + if (canResizeHeight) + { + newHeight = border.Height - deltaY; topPosition += deltaY; } break; case "BottomLeftButton": - newWidth = Math.Max(0, border.Width - deltaX); - newHeight = Math.Max(0, border.Height + deltaY); - if (newWidth > 0) + if (border.Width - deltaX < minimumWidth) { + canResizeWidth = false; + } + + newWidth = border.Width - deltaX; + if (canResizeWidth) + { + leftPosition += deltaX; + } + + newHeight = border.Height + deltaY; + break; + + case "BottomRightButton": + newWidth = border.Width + deltaX; + newHeight = border.Height + deltaY; + break; + + case "TopButton": + if (border.Height - deltaY < minimumHeight) + { + canResizeHeight = false; + } + + if (canResizeHeight) + { + newHeight = border.Height - deltaY; + topPosition += deltaY; + } + break; + + case "BottomButton": + newHeight = border.Height + deltaY; + break; + + case "LeftButton": + if (border.Width - deltaX < minimumWidth) + { + canResizeWidth = false; + } + + if (canResizeWidth) + { + newWidth = border.Width - deltaX; leftPosition += deltaX; } break; - case "BottomRightButton": - newWidth = Math.Max(0, border.Width + deltaX); - newHeight = Math.Max(0, border.Height + deltaY); + case "RightButton": + newWidth = border.Width + deltaX; break; } + if (leftPosition < 0 || leftPosition + newWidth > canvas.Width || + topPosition < 0 || topPosition + newHeight > canvas.Height || + newWidth < minimumWidth || newHeight < minimumHeight) + { + return; + } + border.Width = newWidth; border.Height = newHeight; @@ -294,11 +393,7 @@ public class ContentCropper : ContentControl private void PositionThumbs() { - if (border == null || - canvas == null) - { - return; - } + if (border == null || canvas == null) return; double borderLeft = Canvas.GetLeft(border); double borderTop = Canvas.GetTop(border); @@ -328,6 +423,30 @@ public class ContentCropper : ContentControl Canvas.SetLeft(bottomRightButton, borderLeft + borderWidth - bottomRightButton.Width); Canvas.SetTop(bottomRightButton, borderTop + borderHeight - bottomRightButton.Height); } + + if (leftButton is not null) + { + Canvas.SetLeft(leftButton, borderLeft); + Canvas.SetTop(leftButton, borderTop + borderHeight / 2 - leftButton.Height / 2); + } + + if (rightButton is not null) + { + Canvas.SetLeft(rightButton, borderLeft + borderWidth - rightButton.Width); + Canvas.SetTop(rightButton, borderTop + borderHeight / 2 - rightButton.Height / 2); + } + + if (topButton is not null) + { + Canvas.SetLeft(topButton, borderLeft + borderWidth / 2 - topButton.Width / 2); + Canvas.SetTop(topButton, borderTop); + } + + if (bottomButton is not null) + { + Canvas.SetLeft(bottomButton, borderLeft + borderWidth / 2 - bottomButton.Width / 2); + Canvas.SetTop(bottomButton, borderTop + borderHeight - bottomButton.Height); + } } private void RenderOverLays()