Browse Source

Fixed right click color swapping for shapes and 0x0 shape

Krzysztof Krysiński 4 months ago
parent
commit
1b1a65ea9e

+ 1 - 1
src/PixiEditor/Models/DocumentModels/ChangeExecutionController.cs

@@ -44,7 +44,7 @@ internal class ChangeExecutionController
 
     public bool IsChangeOfTypeActive<T>() where T : IExecutorFeature
     {
-        return currentSession is T sessionT && sessionT.IsFeatureEnabled(sessionT);
+        return currentSession is T sessionT && sessionT.IsFeatureEnabled<T>();
     }
 
     public ExecutorType GetCurrentExecutorType()

+ 18 - 1
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/DrawableShapeToolExecutor.cs

@@ -74,7 +74,7 @@ internal abstract class DrawableShapeToolExecutor<T> : SimpleShapeToolExecutor w
 
             lastRect = new RectD(startDrawingPos, VecD.Zero);
 
-            document!.TransformHandler.ShowTransform(TransformMode, false, new ShapeCorners((RectD)lastRect.Inflate(1)),
+            document!.TransformHandler.ShowTransform(TransformMode, false, new ShapeCorners(lastRect),
                 false, UseGlobalUndo ? AddToUndo : null);
             document.TransformHandler.ShowHandles = false;
             document.TransformHandler.IsSizeBoxEnabled = true;
@@ -362,6 +362,23 @@ internal abstract class DrawableShapeToolExecutor<T> : SimpleShapeToolExecutor w
 
                 base.OnLeftMouseButtonUp(argsPositionOnCanvas);
                 onEnded?.Invoke(this);
+
+                if (lastRect.Size == VecD.Zero)
+                {
+                    var member = document!.StructureHelper.Find(memberId);
+                    if (member is not null)
+                    {
+                        document.Operations.DeleteStructureMember(memberId);
+                        document.TransformHandler.HideTransform();
+                    }
+                }
+
+                var layersUnderCursor = QueryLayers<ILayerHandler>(argsPositionOnCanvas);
+                if (layersUnderCursor.Any())
+                {
+                    document.Operations.SetSelectedMember(layersUnderCursor.First().Id);
+                }
+
                 return;
             }
         }

+ 1 - 1
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/Features/IExecutorFeature.cs

@@ -2,5 +2,5 @@
 
 public interface IExecutorFeature
 {
-    public bool IsFeatureEnabled(IExecutorFeature feature);
+    public bool IsFeatureEnabled<T>();
 }

+ 4 - 3
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/PasteImageExecutor.cs

@@ -90,12 +90,13 @@ internal class PasteImageExecutor : UpdateableChangeExecutor, ITransformableExec
         internals!.ActionAccumulator.AddActions(new EndPasteImage_Action());
     }
 
-    public bool IsFeatureEnabled(IExecutorFeature feature)
+    public bool IsFeatureEnabled<T>()
     {
-        if (feature is ITransformableExecutor)
+        Type featureType = typeof(T);
+        if (featureType == typeof(ITransformableExecutor))
             return IsTransforming;
         
-        if (feature is IMidChangeUndoableExecutor)
+        if (featureType == typeof(IMidChangeUndoableExecutor))
             return true;
 
         return false;

+ 8 - 4
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/SimpleShapeToolExecutor.cs

@@ -37,6 +37,9 @@ internal abstract class SimpleShapeToolExecutor : UpdateableChangeExecutor,
         get => activeMode;
         set
         {
+            if (activeMode == value)
+                return;
+
             StopMode(activeMode);
             activeMode = value;
             StartMode(activeMode);
@@ -249,19 +252,20 @@ internal abstract class SimpleShapeToolExecutor : UpdateableChangeExecutor,
     public abstract bool CanUndo { get; }
     public abstract bool CanRedo { get; }
 
-    public virtual bool IsFeatureEnabled(IExecutorFeature feature)
+    public virtual bool IsFeatureEnabled<T>()
     {
-        if (feature is ITransformableExecutor)
+        Type t = typeof(T);
+        if (t == typeof(ITransformableExecutor))
         {
             return IsTransforming;
         }
 
-        if (feature is IMidChangeUndoableExecutor)
+        if (t == typeof(IMidChangeUndoableExecutor))
         {
             return ActiveMode == ShapeToolMode.Transform;
         }
 
-        if (feature is IDelayedColorSwapFeature)
+        if (t == typeof(IDelayedColorSwapFeature))
         {
             return true;
         }

+ 2 - 2
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/TransformReferenceLayerExecutor.cs

@@ -50,8 +50,8 @@ internal class TransformReferenceLayerExecutor : UpdateableChangeExecutor, ITran
         document.ReferenceLayerHandler.IsTransforming = false;
     }
 
-    public bool IsFeatureEnabled(IExecutorFeature feature)
+    public bool IsFeatureEnabled<T>()
     {
-        return feature is ITransformableExecutor;
+        return typeof(T) == typeof(ITransformableExecutor);
     }
 }

+ 6 - 3
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/TransformSelectedExecutor.cs

@@ -13,6 +13,7 @@ using PixiEditor.Models.Controllers.InputDevice;
 using PixiEditor.Models.DocumentPassthroughActions;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels.Document.Nodes;
+using Type = System.Type;
 
 namespace PixiEditor.Models.DocumentModels.UpdateableChangeExecutors;
 #nullable enable
@@ -508,9 +509,11 @@ internal class TransformSelectedExecutor : UpdateableChangeExecutor, ITransforma
         }
     }
 
-    public bool IsFeatureEnabled(IExecutorFeature feature)
+    public bool IsFeatureEnabled<T>()
     {
-        return feature is ITransformableExecutor && IsTransforming || feature is IMidChangeUndoableExecutor ||
-               feature is ITransformStoppedEvent || feature is ITransformDraggedEvent;
+        Type feature = typeof(T);
+        return feature == typeof(ITransformableExecutor) && IsTransforming ||
+               feature == typeof(IMidChangeUndoableExecutor) ||
+               feature == typeof(ITransformStoppedEvent) || feature == typeof(ITransformDraggedEvent);
     }
 }

+ 3 - 3
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/VectorEllipseToolExecutor.cs

@@ -125,9 +125,9 @@ internal class VectorEllipseToolExecutor : DrawableShapeToolExecutor<IVectorElli
         return new EndSetShapeGeometry_Action();
     }
 
-    public override bool IsFeatureEnabled(IExecutorFeature feature)
+    public override bool IsFeatureEnabled<T>()
     {
-        if(feature is IMidChangeUndoableExecutor) return false;
-        return base.IsFeatureEnabled(feature);
+        if(typeof(T) == typeof(IMidChangeUndoableExecutor)) return false;
+        return base.IsFeatureEnabled<T>();
     }
 }

+ 4 - 3
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/VectorLineToolExecutor.cs

@@ -60,10 +60,11 @@ internal class VectorLineToolExecutor : LineExecutor<IVectorLineToolHandler>
         return new EndSetShapeGeometry_Action();
     }
 
-    public override bool IsFeatureEnabled(IExecutorFeature feature)
+    public override bool IsFeatureEnabled<T>()
     {
-        if(feature is IMidChangeUndoableExecutor) return false;
+        Type feature = typeof(T);
+        if(feature == typeof(IMidChangeUndoableExecutor)) return false;
         
-        return base.IsFeatureEnabled(feature);
+        return base.IsFeatureEnabled<T>();
     }
 }

+ 23 - 20
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/VectorPathToolExecutor.cs

@@ -98,9 +98,9 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
                 }
 
                 //below forces undo before starting new path
-               // internals.ActionAccumulator.AddFinishedActions(new EndSetShapeGeometry_Action());
+                // internals.ActionAccumulator.AddFinishedActions(new EndSetShapeGeometry_Action());
 
-               // internals.ActionAccumulator.AddActions(new SetShapeGeometry_Action(member.Id, ConstructShapeData(startingPath)));
+                // internals.ActionAccumulator.AddActions(new SetShapeGeometry_Action(member.Id, ConstructShapeData(startingPath)));
             }
         }
         else
@@ -150,11 +150,11 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
             }
         }
     }
-    
+
     private bool WholePathClosed()
     {
         EditableVectorPath editablePath = new EditableVectorPath(startingPath);
-        
+
         return editablePath.SubShapes.Count > 0 && editablePath.SubShapes.All(x => x.IsClosed);
     }
 
@@ -176,7 +176,9 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
     {
         if (document.PathOverlayHandler.IsActive)
         {
-            internals.ActionAccumulator.AddFinishedActions(new SetShapeGeometry_Action(member.Id, ConstructShapeData(startingPath)), new EndSetShapeGeometry_Action());
+            internals.ActionAccumulator.AddFinishedActions(
+                new SetShapeGeometry_Action(member.Id, ConstructShapeData(startingPath)),
+                new EndSetShapeGeometry_Action());
         }
     }
 
@@ -198,7 +200,7 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
 
     private PathVectorData ConstructShapeData(VectorPath? path)
     {
-        if(path is null)
+        if (path is null)
         {
             return new PathVectorData(new VectorPath() { FillType = (PathFillType)vectorPathToolHandler.FillMode })
             {
@@ -210,7 +212,7 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
                 StrokeLineJoin = vectorPathToolHandler.StrokeLineJoin
             };
         }
-        
+
         return new PathVectorData(new VectorPath(path) { FillType = (PathFillType)vectorPathToolHandler.FillMode })
         {
             StrokeWidth = (float)toolbar.ToolSize,
@@ -227,19 +229,17 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
         if (document.PathOverlayHandler.IsActive)
         {
             startingPath = path;
-            internals.ActionAccumulator.AddActions(new SetShapeGeometry_Action(member.Id, ConstructShapeData(startingPath)));
+            internals.ActionAccumulator.AddActions(new SetShapeGeometry_Action(member.Id,
+                ConstructShapeData(startingPath)));
         }
     }
 
-    public bool IsFeatureEnabled(IExecutorFeature feature)
+    public bool IsFeatureEnabled<T>()
     {
-        return feature switch
-        {
-            IPathExecutorFeature _ => true,
-            IMidChangeUndoableExecutor _ => true,
-            ITransformableExecutor _ => true,
-            _ => false
-        };
+        Type feature = typeof(T);
+        return feature == typeof(IMidChangeUndoableExecutor)
+               || feature == typeof(ITransformableExecutor)
+               || feature == typeof(IPathExecutorFeature);
     }
 
     protected void HighlightSnapping(string? snapX, string? snapY)
@@ -259,7 +259,7 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
 
         return shapeData is not IReadOnlyPathData pathData || pathData.Path.IsClosed;
     }
-    
+
     private void ApplySettings(PathVectorData pathData)
     {
         toolbar.ToolSize = pathData.StrokeWidth;
@@ -267,8 +267,11 @@ internal class VectorPathToolExecutor : UpdateableChangeExecutor, IPathExecutorF
         toolbar.ToolSize = pathData.StrokeWidth;
         toolbar.Fill = pathData.Fill;
         toolbar.FillBrush = pathData.FillPaintable.ToBrush();
-        toolbar.GetSetting<EnumSettingViewModel<VectorPathFillType>>(nameof(VectorPathToolViewModel.FillMode)).Value = (VectorPathFillType)pathData.Path.FillType;
-        toolbar.GetSetting<EnumSettingViewModel<StrokeCap>>(nameof(VectorPathToolViewModel.StrokeLineCap)).Value = pathData.StrokeLineCap;
-        toolbar.GetSetting<EnumSettingViewModel<StrokeJoin>>(nameof(VectorPathToolViewModel.StrokeLineJoin)).Value = pathData.StrokeLineJoin;
+        toolbar.GetSetting<EnumSettingViewModel<VectorPathFillType>>(nameof(VectorPathToolViewModel.FillMode)).Value =
+            (VectorPathFillType)pathData.Path.FillType;
+        toolbar.GetSetting<EnumSettingViewModel<StrokeCap>>(nameof(VectorPathToolViewModel.StrokeLineCap)).Value =
+            pathData.StrokeLineCap;
+        toolbar.GetSetting<EnumSettingViewModel<StrokeJoin>>(nameof(VectorPathToolViewModel.StrokeLineJoin)).Value =
+            pathData.StrokeLineJoin;
     }
 }

+ 3 - 3
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/VectorRectangleToolExecutor.cs

@@ -132,9 +132,9 @@ internal class VectorRectangleToolExecutor : DrawableShapeToolExecutor<IVectorRe
         return new EndSetShapeGeometry_Action();
     }
 
-    public override bool IsFeatureEnabled(IExecutorFeature feature)
+    public override bool IsFeatureEnabled<T>()
     {
-        if (feature is IMidChangeUndoableExecutor) return false;
-        return base.IsFeatureEnabled(feature);
+        if (typeof(T) == typeof(IMidChangeUndoableExecutor)) return false;
+        return base.IsFeatureEnabled<T>();
     }
 }

+ 2 - 2
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/VectorTextToolExecutor.cs

@@ -273,8 +273,8 @@ internal class VectorTextToolExecutor : UpdateableChangeExecutor, ITextOverlayEv
         };
     }
 
-    bool IExecutorFeature.IsFeatureEnabled(IExecutorFeature feature)
+    bool IExecutorFeature.IsFeatureEnabled<T>()
     {
-        return feature is ITextOverlayEvents || feature is IQuickToolSwitchable;
+        return typeof(T) == typeof(ITextOverlayEvents) || typeof(T) == typeof(IQuickToolSwitchable);
     }
 }