Browse Source

Added add from swatches button

Krzysztof Krysiński 3 years ago
parent
commit
2b0da63fbf

BIN
PixiEditor/Images/CopyAdd.png


+ 2 - 0
PixiEditor/PixiEditor.csproj

@@ -138,6 +138,7 @@
 		<None Remove="Images\CheckerTile.png" />
 		<None Remove="Images\ChevronDown.png" />
 		<None Remove="Images\ChevronsDown.png" />
+		<None Remove="Images\CopyAdd.png" />
 		<None Remove="Images\DiagonalRed.png" />
 		<None Remove="Images\Download.png" />
 		<None Remove="Images\Edit.png" />
@@ -219,6 +220,7 @@
 		<Resource Include="Images\CheckerTile.png" />
 		<Resource Include="Images\ChevronDown.png" />
 		<Resource Include="Images\ChevronsDown.png" />
+		<Resource Include="Images\CopyAdd.png" />
 		<Resource Include="Images\DiagonalRed.png" />
 		<Resource Include="Images\Download.png" />
 		<Resource Include="Images\Edit.png" />

+ 2 - 1
PixiEditor/Views/MainWindow.xaml

@@ -391,7 +391,8 @@
                                                 Path=ActualWidth, Converter={converters:PaletteViewerWidthToVisibilityConverter}}"/>
                                             <palettes:PaletteViewer IsEnabled="{Binding DocumentSubViewModel.Owner.BitmapManager.ActiveDocument,
                                         Converter={converters:NotNullToBoolConverter}}" Colors="{Binding BitmapManager.ActiveDocument.Palette}"
-                                                              SelectColorCommand="{Binding ColorsSubViewModel.SelectColorCommand}"
+                                                              Swatches="{Binding BitmapManager.ActiveDocument.Swatches}"
+                                                                    SelectColorCommand="{Binding ColorsSubViewModel.SelectColorCommand}"
                                                               HintColor="{Binding Path=ColorsSubViewModel.PrimaryColor,
                                                 Converter={converters:SKColorToMediaColorConverter}}"
                                                                 DataSources="{Binding ColorsSubViewModel.PaletteDataSources}"

+ 9 - 1
PixiEditor/Views/UserControls/Palettes/PaletteColorAdder.xaml

@@ -6,7 +6,7 @@
              xmlns:local="clr-namespace:PixiEditor.Views.UserControls" 
              xmlns:colorpicker="clr-namespace:ColorPicker;assembly=ColorPicker"
              mc:Ignorable="d" Name="paletteColorAdder"
-             d:DesignHeight="36" d:DesignWidth="100">
+             d:DesignHeight="36" d:DesignWidth="120">
     <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
         <colorpicker:PortableColorPicker
             ColorChanged="PortableColorPicker_ColorChanged"
@@ -21,5 +21,13 @@
                 <ImageBrush ImageSource="/Images/Plus-square.png"/>
             </Button.Background>
         </Button>
+        <Button Name="AddFromSwatches" Margin="10 2 0 0" Width="24" Height="24" 
+                Style="{StaticResource ToolButtonStyle}" 
+                ToolTip="Add from swatches"
+                Cursor="Hand"  Click="AddFromSwatches_OnClick">
+            <Button.Background>
+                <ImageBrush ImageSource="/Images/CopyAdd.png"/>
+            </Button.Background>
+        </Button>
     </StackPanel>
 </UserControl>

+ 55 - 0
PixiEditor/Views/UserControls/Palettes/PaletteColorAdder.xaml.cs

@@ -2,6 +2,7 @@
 using SkiaSharp;
 using System;
 using System.Collections.Generic;
+using System.Collections.Specialized;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -39,7 +40,14 @@ namespace PixiEditor.Views.UserControls.Palettes
         public static readonly DependencyProperty HintColorProperty =
             DependencyProperty.Register("HintColor", typeof(Color), typeof(PaletteColorAdder), new PropertyMetadata(System.Windows.Media.Colors.Transparent));
 
+        public static readonly DependencyProperty SwatchesProperty = DependencyProperty.Register(
+            "Swatches", typeof(WpfObservableRangeCollection<SKColor>), typeof(PaletteColorAdder), new PropertyMetadata(default(WpfObservableRangeCollection<SKColor>), OnSwatchesChanged));
 
+        public WpfObservableRangeCollection<SKColor> Swatches
+        {
+            get { return (WpfObservableRangeCollection<SKColor>) GetValue(SwatchesProperty); }
+            set { SetValue(SwatchesProperty, value); }
+        }
 
         public Color SelectedColor
         {
@@ -63,6 +71,7 @@ namespace PixiEditor.Views.UserControls.Palettes
             if (adder == null || adder.Colors == null) return;
             if (e.NewValue != null)
             {
+                adder.UpdateAddButton();
                 adder.Colors.CollectionChanged += adder.Colors_CollectionChanged;
             }
             else if(e.OldValue != null)
@@ -72,10 +81,41 @@ namespace PixiEditor.Views.UserControls.Palettes
         }
 
         private void Colors_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
+        {
+            UpdateAddSwatchesButton();
+            UpdateAddButton();
+        }
+
+        private void UpdateAddButton()
         {
             AddButton.IsEnabled = !Colors.Contains(ToSKColor(SelectedColor));
         }
 
+        private static void OnSwatchesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        {
+            PaletteColorAdder adder = (PaletteColorAdder)d;
+            if (adder == null || adder.Swatches == null) return;
+            if (e.NewValue != null)
+            {
+                adder.UpdateAddSwatchesButton();
+                adder.Swatches.CollectionChanged += adder.Swatches_CollectionChanged;
+            }
+            else if (e.OldValue != null)
+            {
+                adder.Swatches.CollectionChanged -= adder.Swatches_CollectionChanged;
+            }
+        }
+
+        private void Swatches_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
+        {
+            UpdateAddSwatchesButton();
+        }
+
+        private void UpdateAddSwatchesButton()
+        {
+            AddFromSwatches.IsEnabled = Swatches != null && Swatches.Any(x => !Colors.Contains(x));
+        }
+
         public PaletteColorAdder()
         {
             InitializeComponent();
@@ -95,5 +135,20 @@ namespace PixiEditor.Views.UserControls.Palettes
             AddButton.IsEnabled = !Colors.Contains(ToSKColor(SelectedColor));
 
         private static SKColor ToSKColor(Color color) => new SKColor(color.R, color.G, color.B, color.A);
+
+        private void AddFromSwatches_OnClick(object sender, RoutedEventArgs e)
+        {
+            if (Swatches == null) return;
+
+            foreach (var color in Swatches)
+            {
+                if (!Colors.Contains(color))
+                {
+                    Colors.Add(color);
+                }
+            }
+
+            AddFromSwatches.IsEnabled = false;
+        }
     }
 }

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

@@ -19,6 +19,7 @@
         <StackPanel Orientation="Vertical" Grid.Row="0" Background="{StaticResource MainColor}">
             <DockPanel VerticalAlignment="Center" Margin="0 5 0 0">
                 <palettes:PaletteColorAdder DockPanel.Dock="Left" Margin="5 0 0 0"
+                                            Swatches="{Binding ElementName=paletteControl, Path=Swatches}"
                                             HintColor="{Binding ElementName=paletteControl, Path=HintColor}"
                                             Colors="{Binding ElementName=paletteControl, Path=Colors}"/>
                 <StackPanel Margin="0, 0, 5, 0" HorizontalAlignment="Right" Width="85" VerticalAlignment="Center" Orientation="Horizontal">

+ 8 - 1
PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml.cs

@@ -10,7 +10,6 @@ using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders.Palettes;
 using PixiEditor.Models.DataProviders;
 using PixiEditor.Models.IO;
-using PixiEditor.Models.IO.JascPalFile;
 using PixiEditor.Views.Dialogs;
 using SkiaSharp;
 
@@ -21,6 +20,14 @@ namespace PixiEditor.Views.UserControls.Palettes
     /// </summary>
     public partial class PaletteViewer : UserControl
     {
+        public static readonly DependencyProperty SwatchesProperty = DependencyProperty.Register(
+            "Swatches", typeof(WpfObservableRangeCollection<SKColor>), typeof(PaletteViewer), new PropertyMetadata(default(WpfObservableRangeCollection<SKColor>)));
+
+        public WpfObservableRangeCollection<SKColor> Swatches
+        {
+            get { return (WpfObservableRangeCollection<SKColor>) GetValue(SwatchesProperty); }
+            set { SetValue(SwatchesProperty, value); }
+        }
         public static readonly DependencyProperty ColorsProperty = DependencyProperty.Register(
             "Colors", typeof(WpfObservableRangeCollection<SKColor>), typeof(PaletteViewer));