Browse Source

Replacing colors control very first version working

Krzysztof Krysiński 3 years ago
parent
commit
fef9bf3936

+ 20 - 0
PixiEditor/Helpers/Converters/ReplaceColorsPackConverter.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Globalization;
+using System.Windows.Media;
+using SkiaSharp;
+
+namespace PixiEditor.Helpers.Converters
+{
+    public class ReplaceColorsPackConverter : SingleInstanceMultiValueConverter<ReplaceColorsPackConverter>
+    {
+        public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
+        {
+            SKColor first = (SKColor)values[0];
+            Color rawSecond = (Color)values[1];
+
+            SKColor second = new SKColor(rawSecond.R, rawSecond.G, rawSecond.B, rawSecond.A);
+
+            return (first, second);
+        }
+    }
+}

+ 8 - 0
PixiEditor/ViewModels/SubViewModels/Main/ColorsViewModel.cs

@@ -22,6 +22,8 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 {
     public class ColorsViewModel : SubViewModel<ViewModelMain>
     {
+        public RelayCommand<(SKColor, SKColor)> ReplaceColorsCommand { get; set; }
+
         public RelayCommand SwapColorsCommand { get; set; }
 
         public RelayCommand SelectColorCommand { get; set; }
@@ -74,9 +76,15 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             SwapColorsCommand = new RelayCommand(SwapColors);
             SelectPaletteColorCommand = new RelayCommand<int>(SelectPaletteColor);
             ImportPaletteCommand = new RelayCommand<List<string>>(ImportPalette, Owner.DocumentIsNotNull);
+            ReplaceColorsCommand = new RelayCommand<(SKColor oldColor, SKColor newColor)>(ReplaceColors, Owner.DocumentIsNotNull);
             Owner.OnStartupEvent += OwnerOnStartupEvent;
         }
 
+        private void ReplaceColors((SKColor oldColor, SKColor newColor) colors)
+        {
+            Owner.BitmapManager?.ActiveDocument?.ReplaceColor(colors.oldColor, colors.newColor);
+        }
+
         private async void OwnerOnStartupEvent(object? sender, EventArgs e)
         {
             await ImportLospecPalette();

+ 7 - 0
PixiEditor/ViewModels/ViewModelMain.cs

@@ -20,6 +20,7 @@ using PixiEditor.Models.Tools.Tools;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels.SubViewModels.Main;
 using PixiEditor.Views.Dialogs;
+using SkiaSharp;
 
 namespace PixiEditor.ViewModels
 {
@@ -257,6 +258,12 @@ namespace PixiEditor.ViewModels
         {
             return BitmapManager.ActiveDocument != null;
         }
+
+        public bool DocumentIsNotNull((SKColor oldColor, SKColor newColor) obj)
+        {
+            return DocumentIsNotNull(null);
+        }
+
         public void CloseWindow(object property)
         {
             if (!(property is CancelEventArgs))

+ 2 - 1
PixiEditor/Views/MainWindow.xaml

@@ -399,7 +399,8 @@
                                                                     Visibility="{Binding RelativeSource={RelativeSource Mode=Self}, 
                                                 Path=ActualWidth, Converter={converters:PaletteViewerWidthToVisibilityConverter},
                                                 ConverterParameter=Hidden}"
-                                                                ImportPaletteCommand="{Binding ColorsSubViewModel.ImportPaletteCommand}"/>
+                                                                ImportPaletteCommand="{Binding ColorsSubViewModel.ImportPaletteCommand}"
+                                                                           ReplaceColorsCommand="{Binding ColorsSubViewModel.ReplaceColorsCommand}"/>
                                         </Grid>
                                     </avalondock:LayoutAnchorable>
                                     <avalondock:LayoutAnchorable ContentId="swatches" Title="Swatches" CanHide="False"

+ 11 - 3
PixiEditor/Views/UserControls/Palettes/ColorReplacer.xaml

@@ -31,14 +31,22 @@
                     <Image Source="/Images/Arrow-right.png" Height="20" Width="20" Margin="10 0"/>
                     <colorPicker:PortableColorPicker 
                         UseHintColor="True"
-                        Color="{Binding ElementName=uc, Path=ColorToReplace, Mode=TwoWay}"
+                        SelectedColor="{Binding ElementName=uc, Path=NewColor, Mode=TwoWay}"
                         HintColor="{Binding ElementName=uc, Path=HintColor}"
                         Height="20"
                         Style="{StaticResource DefaultColorPickerStyle}"
                         Width="50" Focusable="False" Margin="0 0 10 0"
                         ShowAlpha="False"/>
-                    <Button Content="Replace" Style="{StaticResource AccentDarkRoundButton}" Height="30" Width="60"/>
-                        </StackPanel>
+                    <Button Content="Replace" Command="{Binding ElementName=uc, Path=ReplaceColorsCommand}"
+                            Style="{StaticResource AccentDarkRoundButton}" Height="30" Width="60">
+                        <Button.CommandParameter>
+                            <MultiBinding Converter="{converters:ReplaceColorsPackConverter}">
+                                <Binding ElementName="uc" Path="ColorToReplace"/>
+                                <Binding ElementName="uc" Path="NewColor"/>
+                            </MultiBinding>
+                        </Button.CommandParameter>
+                    </Button>
+                </StackPanel>
                     <CheckBox Focusable="False" Panel.ZIndex="10" Name="VisibilityCheckbox" Margin="0,0,5,0" Height="16" HorizontalAlignment="Right">
                         <CheckBox.Triggers>
                             <EventTrigger RoutedEvent="CheckBox.Checked">

+ 12 - 1
PixiEditor/Views/UserControls/Palettes/ColorReplacer.xaml.cs

@@ -1,6 +1,7 @@
 using SkiaSharp;
 using System.Windows;
 using System.Windows.Controls;
+using System.Windows.Input;
 using System.Windows.Media;
 
 namespace PixiEditor.Views.UserControls.Palettes
@@ -29,10 +30,20 @@ namespace PixiEditor.Views.UserControls.Palettes
 
         public Color NewColor
         {
-            get { return (Color)GetValue(NewColorProperty); }
+            get { return (Color) GetValue(NewColorProperty); }
             set { SetValue(NewColorProperty, value); }
         }
 
+        public static readonly DependencyProperty ReplaceColorsCommandProperty = DependencyProperty.Register(
+            "ReplaceColorsCommand", typeof(ICommand), typeof(ColorReplacer), new PropertyMetadata(default(ICommand)));
+
+        public ICommand ReplaceColorsCommand
+        {
+            get { return (ICommand) GetValue(ReplaceColorsCommandProperty); }
+            set { SetValue(ReplaceColorsCommandProperty, value); }
+        }
+
+
         // Using a DependencyProperty as the backing store for NewColor.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty NewColorProperty =
             DependencyProperty.Register("NewColor", typeof(Color), typeof(ColorReplacer), new PropertyMetadata(Colors.Black));

+ 3 - 1
PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml

@@ -92,6 +92,8 @@
                </ItemsControl.ItemTemplate>
             </ItemsControl>
         </ScrollViewer>
-        <palettes:ColorReplacer Grid.Row="3" VerticalAlignment="Bottom" HintColor="{Binding ElementName=paletteControl, Path=HintColor}"/>
+        <palettes:ColorReplacer Grid.Row="3" VerticalAlignment="Bottom" 
+                                ReplaceColorsCommand="{Binding ElementName=paletteControl, Path=ReplaceColorsCommand}"
+                                HintColor="{Binding ElementName=paletteControl, Path=HintColor}"/>
     </Grid>
 </UserControl>

+ 7 - 0
PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml.cs

@@ -42,7 +42,14 @@ namespace PixiEditor.Views.UserControls.Palettes
         public static readonly DependencyProperty HintColorProperty =
             DependencyProperty.Register("HintColor", typeof(Color), typeof(PaletteViewer), new PropertyMetadata(System.Windows.Media.Colors.Transparent));
 
+        public static readonly DependencyProperty ReplaceColorsCommandProperty = DependencyProperty.Register(
+            "ReplaceColorsCommand", typeof(ICommand), typeof(PaletteViewer), new PropertyMetadata(default(ICommand)));
 
+        public ICommand ReplaceColorsCommand
+        {
+            get { return (ICommand) GetValue(ReplaceColorsCommandProperty); }
+            set { SetValue(ReplaceColorsCommandProperty, value); }
+        }
 
         public ICommand SelectColorCommand
         {