Browse Source

Added replace color commands

CPKreuz 2 years ago
parent
commit
f236b4e758

+ 1 - 1
src/PixiEditor/Models/Commands/Commands/BasicCommand.cs

@@ -8,7 +8,7 @@ internal partial class Command
     {
         public object Parameter { get; init; }
 
-        protected override object GetParameter() => Parameter;
+        public override object GetParameter() => Parameter;
 
         public BasicCommand(Action<object> onExecute, CanExecuteEvaluator canExecute) : base(onExecute, canExecute) { }
     }

+ 1 - 1
src/PixiEditor/Models/Commands/Commands/Command.cs

@@ -40,7 +40,7 @@ internal abstract partial class Command : NotifyableObject
 
     public event ShortcutChangedEventHandler ShortcutChanged;
 
-    protected abstract object GetParameter();
+    public abstract object GetParameter();
 
     protected Command(Action<object> onExecute, CanExecuteEvaluator canExecute) =>
         Methods = new(this, onExecute, canExecute);

+ 1 - 1
src/PixiEditor/Models/Commands/Commands/ToolCommand.cs

@@ -11,7 +11,7 @@ internal partial class Command
 
         public Key TransientKey { get; init; }
 
-        protected override object GetParameter() => ToolType;
+        public override object GetParameter() => ToolType;
 
         public ToolCommand() : base(ViewModelMain.Current.ToolsSubViewModel.SetTool, CommandController.Current.CanExecuteEvaluators["PixiEditor.HasDocument"]) { }
     }

+ 45 - 1
src/PixiEditor/ViewModels/SubViewModels/Main/ColorsViewModel.cs

@@ -1,4 +1,5 @@
-using System.Windows.Input;
+using System.Windows;
+using System.Windows.Input;
 using System.Windows.Media;
 using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Helpers;
@@ -81,6 +82,49 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>
         doc.Operations.ReplaceColor(colors.oldColor, colors.newColor);
     }
 
+    [Command.Basic("PixiEditor.Colors.ReplaceSecondaryByPrimaryColor", false, "Replace secondary color by primary", "Replace the secondary color by the primary color", IconEvaluator = "PixiEditor.Colors.ReplaceColorIcon")]
+    [Command.Basic("PixiEditor.Colors.ReplacePrimaryBySecondaryColor", true, "Replace primary color by secondary", "Replace the primary color by the secondary color", IconEvaluator = "PixiEditor.Colors.ReplaceColorIcon")]
+    public void ReplaceColors(bool replacePrimary)
+    {
+        var oldColor = replacePrimary ? PrimaryColor : SecondaryColor;
+        var newColor = replacePrimary ? SecondaryColor : PrimaryColor;
+        
+        ReplaceColors((oldColor, newColor));
+    }
+
+    [Evaluator.Icon("PixiEditor.Colors.ReplaceColorIcon")]
+    public ImageSource ReplaceColorsIcon(object command)
+    {
+        bool replacePrimary = command switch
+        {
+            CommandSearchResult result => (bool)result.Command.GetParameter(),
+            Models.Commands.Commands.Command cmd => (bool)cmd.GetParameter(),
+            _ => false
+        };
+        
+        var oldColor = replacePrimary ? PrimaryColor : SecondaryColor;
+        var newColor = replacePrimary ? SecondaryColor : PrimaryColor;
+        
+        var oldDrawing = new GeometryDrawing { Brush = new SolidColorBrush(oldColor.ToOpaqueMediaColor()), Pen = new(Brushes.Gray, .5) };
+        var oldGeometry = new EllipseGeometry(new Point(5, 5), 5, 5);
+        
+        oldDrawing.Geometry = oldGeometry;
+        
+        var newDrawing = new GeometryDrawing { Brush = new SolidColorBrush(newColor.ToOpaqueMediaColor()), Pen = new(Brushes.White, 1) };
+        var newGeometry = new EllipseGeometry(new Point(10, 10), 6, 6);
+
+        newDrawing.Geometry = newGeometry;
+        
+        return new DrawingImage(new DrawingGroup
+        {
+            Children = new DrawingCollection
+            {
+                oldDrawing,
+                newDrawing
+            }
+        });
+    }
+
     private async void OwnerOnStartupEvent(object sender, EventArgs e)
     {
         await ImportLospecPalette();