Преглед изворни кода

Added crop to selection command

CPKreuz пре 2 година
родитељ
комит
eb16a28568

+ 48 - 0
src/PixiEditor.ChangeableDocument/Changes/Root/Crop_Change.cs

@@ -0,0 +1,48 @@
+using PixiEditor.ChangeableDocument.ChangeInfos.Root;
+using PixiEditor.DrawingApi.Core.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changes.Root;
+
+internal class Crop_Change : ResizeBasedChangeBase
+{
+    private RectI rect;
+    
+    [GenerateMakeChangeAction]
+    public Crop_Change(RectI rect)
+    {
+        this.rect = rect;
+    }
+
+    public override bool InitializeAndValidate(Document target)
+    {
+        return base.InitializeAndValidate(target) && rect is { Width: > 0, Height: > 0 };
+    }
+
+    public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply, out bool ignoreInUndo)
+    {
+        if (_originalSize == rect.Size)
+        {
+            ignoreInUndo = true;
+            return new None();
+        }
+
+        target.Size = rect.Size;
+        target.VerticalSymmetryAxisX = Math.Clamp(_originalVerAxisX - rect.Pos.X, 0, rect.Size.X);
+        target.HorizontalSymmetryAxisY = Math.Clamp(_originalHorAxisY - rect.Pos.Y, 0, rect.Size.Y);
+
+        target.ForEveryMember((member) =>
+        {
+            if (member is Layer layer)
+            {
+                Resize(layer.LayerImage, layer.GuidValue, rect.Size, rect.Pos * -1, deletedChunks);
+            }
+            if (member.Mask is null)
+                return;
+
+            Resize(member.Mask, member.GuidValue, rect.Size, rect.Pos * -1, deletedMaskChunks);
+        });
+        
+        ignoreInUndo = false;
+        return new Size_ChangeInfo(rect.Size, target.VerticalSymmetryAxisX, target.HorizontalSymmetryAxisY);
+    }
+}

+ 2 - 1
src/PixiEditor/Data/Localization/Languages/en.json

@@ -562,5 +562,6 @@
   "TRANSFORM_ACTION_DISPLAY_SCALE_ROTATE_SHEAR_PERSPECTIVE": "Drag handles to scale transform. Hold Ctrl and drag a handle to move the handle freely. Hold Shift to scale proportionally. Hold Alt and drag a side handle to shear. Drag outside handles to rotate.",
   "TRANSFORM_ACTION_DISPLAY_SCALE_ROTATE_SHEAR_NOPERSPECTIVE": "Drag handles to scale transform. Hold Shift to scale proportionally. Hold Alt and drag a side handle to shear. Drag outside handles to rotate.",
   "TRANSFORM_ACTION_DISPLAY_SCALE_ROTATE_NOSHEAR_NOPERSPECTIVE": "Drag handles to scale transform. Hold Shift to scale proportionally. Drag outside handles to rotate.",
-  "TRANSFORM_ACTION_DISPLAY_SCALE_NOROTATE_NOSHEAR_NOPERSPECTIVE": "Drag handles to scale transform. Hold Shift to scale proportionally."
+  "TRANSFORM_ACTION_DISPLAY_SCALE_NOROTATE_NOSHEAR_NOPERSPECTIVE": "Drag handles to scale transform. Hold Shift to scale proportionally.",
+  "CROP_TO_SELECTION": "Crop to selection"
 }

BIN
src/PixiEditor/Images/Commands/PixiEditor/Selection/CropToSelection.png


+ 19 - 0
src/PixiEditor/Models/DocumentModels/Public/DocumentOperationsModule.cs

@@ -564,6 +564,25 @@ internal class DocumentOperationsModule
         Internals.ActionAccumulator.AddFinishedActions(new SelectionToMask_Action(member.GuidValue, mode));
     }
 
+    public void CropToSelection(bool clearSelection = true)
+    {
+        if (Document.SelectionPathBindable.IsEmpty)
+            return;
+
+        var bounds = Document.SelectionPathBindable.TightBounds;
+        
+        Internals.ActionAccumulator.AddActions(new Crop_Action((RectI)bounds));
+
+        if (clearSelection)
+        {
+            Internals.ActionAccumulator.AddFinishedActions(new ClearSelection_Action());
+        }
+        else
+        {
+            Internals.ActionAccumulator.AddFinishedActions();
+        }
+    }
+    
     public void InvertSelection()
     {
         var selection = Document.SelectionPathBindable;

+ 2 - 0
src/PixiEditor/PixiEditor.csproj

@@ -444,6 +444,8 @@
 		<Resource Include="Images\Commands\PixiEditor\Selection\InvertSelection.png" />
 		<None Remove="Images\LanguageFlags\zh.png" />
 		<Resource Include="Images\LanguageFlags\zh.png" />
+		<None Remove="Images\Commands\PixiEditor\Selection\CropToSelection.png" />
+		<Resource Include="Images\Commands\PixiEditor\Selection\CropToSelection.png" />
 	</ItemGroup>
 	<ItemGroup>
 		<None Include="..\LICENSE">

+ 8 - 0
src/PixiEditor/ViewModels/SubViewModels/Main/SelectionViewModel.cs

@@ -75,6 +75,14 @@ internal class SelectionViewModel : SubViewModel<ViewModelMain>
         Owner.DocumentManagerSubViewModel.ActiveDocument?.Operations.SelectionToMask(mode);
     }
 
+    [Command.Basic("PixiEditor.Selection.CropToSelection", "CROP_TO_SELECTION", "CROP_TO_SELECTION", CanExecute = "PixiEditor.Selection.IsNotEmpty")]
+    public void CropToSelection()
+    {
+        var document = Owner.DocumentManagerSubViewModel.ActiveDocument;
+        
+        document!.Operations.CropToSelection();
+    }
+
     [Evaluator.CanExecute("PixiEditor.Selection.CanNudgeSelectedObject")]
     public bool CanNudgeSelectedObject(int[] dist) => Owner.DocumentManagerSubViewModel.ActiveDocument?.UpdateableChangeActive == true;
 }