Kaynağa Gözat

Select ellipse tool

Equbuxu 3 yıl önce
ebeveyn
işleme
e33c57edb2

+ 1 - 1
src/PixiEditor.ChangeableDocument/Changes/Drawing/ClearSelection_Change.cs → src/PixiEditor.ChangeableDocument/Changes/Selection/ClearSelection_Change.cs

@@ -1,6 +1,6 @@
 using SkiaSharp;
 
-namespace PixiEditor.ChangeableDocument.Changes.Drawing;
+namespace PixiEditor.ChangeableDocument.Changes.Selection;
 
 internal class ClearSelection_Change : Change
 {

+ 69 - 0
src/PixiEditor.ChangeableDocument/Changes/Selection/SelectEllipse_UpdateableChange.cs

@@ -0,0 +1,69 @@
+using PixiEditor.ChangeableDocument.Enums;
+using SkiaSharp;
+
+namespace PixiEditor.ChangeableDocument.Changes.Selection;
+
+internal class SelectEllipse_UpdateableChange : UpdateableChange
+{
+    private RectI borders;
+    private readonly SelectionMode mode;
+    private SKPath? originalPath;
+
+    [GenerateUpdateableChangeActions]
+    public SelectEllipse_UpdateableChange(RectI borders, SelectionMode mode)
+    {
+        this.borders = borders;
+        this.mode = mode;
+    }
+
+    [UpdateChangeMethod]
+    public void Update(RectI borders)
+    {
+        this.borders = borders;
+    }
+
+    public override OneOf<Success, Error> InitializeAndValidate(Document target)
+    {
+        originalPath = new SKPath(target.Selection.SelectionPath);
+        return new Success();
+    }
+
+    private Selection_ChangeInfo CommonApply(Document target)
+    {
+        using var ellipsePath = new SKPath() { FillType = SKPathFillType.EvenOdd };
+        ellipsePath.AddOval(borders);
+
+        var toDispose = target.Selection.SelectionPath;
+        if (mode == SelectionMode.New)
+            target.Selection.SelectionPath = new(ellipsePath);
+        else
+            target.Selection.SelectionPath = originalPath!.Op(ellipsePath, mode.ToSKPathOp());
+        toDispose.Dispose();
+
+        return new Selection_ChangeInfo(new SKPath(target.Selection.SelectionPath));
+    }
+
+    public override OneOf<None, IChangeInfo, List<IChangeInfo>> ApplyTemporarily(Document target)
+    {
+        return CommonApply(target);
+    }
+
+    public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply, out bool ignoreInUndo)
+    {
+        var changes = CommonApply(target);
+        ignoreInUndo = false;
+        return changes;
+    }
+
+    public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
+    {
+        (var toDispose, target.Selection.SelectionPath) = (target.Selection.SelectionPath, new SKPath(originalPath));
+        toDispose.Dispose();
+        return new Selection_ChangeInfo(new SKPath(target.Selection.SelectionPath));
+    }
+
+    public override void Dispose()
+    {
+        originalPath?.Dispose();
+    }
+}

+ 2 - 1
src/PixiEditorPrototype/Models/Tool.cs

@@ -8,7 +8,8 @@ internal enum Tool
     LineBasedPen,
     PixelPerfectPen,
     Eraser,
-    Select,
+    SelectRectangle,
+    SelectEllipse,
     Lasso,
     ShiftLayer,
     FloodFill

+ 17 - 0
src/PixiEditorPrototype/ViewModels/DocumentViewModel.cs

@@ -151,6 +151,7 @@ internal class DocumentViewModel : INotifyPropertyChanged
     private bool updateableChangeActive = false;
 
     private bool selectingRect = false;
+    private bool selectingEllipse = false;
     private bool selectingLasso = false;
     private bool drawingRectangle = false;
     private bool drawingEllipse = false;
@@ -550,6 +551,22 @@ internal class DocumentViewModel : INotifyPropertyChanged
         Helpers.ActionAccumulator.AddFinishedActions(new EndSelectRectangle_Action());
     }
 
+    public void StartUpdateEllipseSelection(RectI borders, SelectionMode mode)
+    {
+        selectingEllipse = true;
+        updateableChangeActive = true;
+        Helpers.ActionAccumulator.AddActions(new SelectEllipse_Action(borders, mode));
+    }
+
+    public void EndEllipseSelection()
+    {
+        if (!selectingEllipse)
+            return;
+        selectingEllipse = false;
+        updateableChangeActive = false;
+        Helpers.ActionAccumulator.AddFinishedActions(new EndSelectEllipse_Action());
+    }
+
     private void OnTransformUpdate(object? sender, ShapeCorners newCorners)
     {
         if (transformingRectangle)

+ 10 - 2
src/PixiEditorPrototype/ViewModels/ViewModelMain.cs

@@ -196,11 +196,16 @@ internal class ViewModelMain : INotifyPropertyChanged
                     (int)StrokeWidth);
                 break;
             }
-            case Tool.Select:
+            case Tool.SelectRectangle:
                 ActiveDocument!.StartUpdateRectSelection(
                     RectI.FromTwoPixels((VecI)mouseDownCanvasPos, (VecI)canvasPos),
                     selectionMode);
                 break;
+            case Tool.SelectEllipse:
+                ActiveDocument!.StartUpdateEllipseSelection(
+                    RectI.FromTwoPixels((VecI)mouseDownCanvasPos, (VecI)canvasPos),
+                    selectionMode);
+                break;
             case Tool.ShiftLayer:
                 ActiveDocument!.StartUpdateShiftLayer((VecI)canvasPos - (VecI)mouseDownCanvasPos);
                 break;
@@ -243,9 +248,12 @@ internal class ViewModelMain : INotifyPropertyChanged
                 case Tool.Ellipse:
                     ActiveDocument!.EndEllipse();
                     break;
-                case Tool.Select:
+                case Tool.SelectRectangle:
                     ActiveDocument!.EndRectSelection();
                     break;
+                case Tool.SelectEllipse:
+                    ActiveDocument!.EndEllipseSelection();
+                    break;
                 case Tool.ShiftLayer:
                     ActiveDocument!.EndShiftLayer();
                     break;

+ 8 - 1
src/PixiEditorPrototype/Views/MainWindow.xaml

@@ -570,9 +570,16 @@
                     Width="70"
                     Margin="5"
                     Command="{Binding ChangeActiveToolCommand}"
-                    CommandParameter="{x:Static models:Tool.Select}">
+                    CommandParameter="{x:Static models:Tool.SelectRectangle}">
                     Select
                 </Button>
+                <Button
+                    Width="70"
+                    Margin="5"
+                    Command="{Binding ChangeActiveToolCommand}"
+                    CommandParameter="{x:Static models:Tool.SelectEllipse}">
+                    Select Ellipse
+                </Button>
                 <Button
                     Width="70"
                     Margin="5"