Browse Source

PalettesBrowser finished

Krzysztof Krysiński 3 years ago
parent
commit
e43b8502e2

BIN
PixiEditor/Images/Edit.png


+ 4 - 4
PixiEditor/Models/DataProviders/LocalPalettesFetcher.cs

@@ -16,11 +16,11 @@ namespace PixiEditor.Models.DataProviders
             Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
             "PixiEditor", "Palettes");
 
-        private List<Palette> _cachedPalettes;
+        public List<Palette> CachedPalettes { get; private set; }
 
         public override async Task<PaletteList> FetchPaletteList(int startIndex, int count, FilteringSettings filtering)
         {
-            if(_cachedPalettes == null)
+            if(CachedPalettes == null)
             {
                 await RefreshCache();
             }
@@ -30,7 +30,7 @@ namespace PixiEditor.Models.DataProviders
                 Palettes = new WpfObservableRangeCollection<Palette>()
             };
 
-            var filteredPalettes = _cachedPalettes.Where(filtering.Filter).ToArray();
+            var filteredPalettes = CachedPalettes.Where(filtering.Filter).ToArray();
 
             if (startIndex >= filteredPalettes.Length) return result;
 
@@ -48,7 +48,7 @@ namespace PixiEditor.Models.DataProviders
         public async Task RefreshCache()
         {
             string[] files = DirectoryExtensions.GetFiles(PathToPalettesFolder, string.Join("|", AvailableParsers.SelectMany(x => x.SupportedFileExtensions)), SearchOption.TopDirectoryOnly);
-            _cachedPalettes = await ParseAll(files);
+            CachedPalettes = await ParseAll(files);
         }
 
         private async Task<List<Palette>> ParseAll(string[] files)

+ 2 - 0
PixiEditor/PixiEditor.csproj

@@ -139,6 +139,7 @@
 		<None Remove="Images\ChevronsDown.png" />
 		<None Remove="Images\DiagonalRed.png" />
 		<None Remove="Images\Download.png" />
+		<None Remove="Images\Edit.png" />
 		<None Remove="Images\Eye-off.png" />
 		<None Remove="Images\Eye.png" />
 		<None Remove="Images\Folder-add.png" />
@@ -218,6 +219,7 @@
 		<Resource Include="Images\ChevronsDown.png" />
 		<Resource Include="Images\DiagonalRed.png" />
 		<Resource Include="Images\Download.png" />
+		<Resource Include="Images\Edit.png" />
 		<Resource Include="Images\Eye-off.png" />
 		<Resource Include="Images\Eye.png" />
 		<Resource Include="Images\Folder-add.png" />

+ 1 - 0
PixiEditor/Views/Dialogs/PalettesBrowser.xaml

@@ -106,6 +106,7 @@
                         <DataTemplate>
                             <local:PaletteItem Palette="{Binding}"
                                                OnRename="PaletteItem_OnRename"
+                                               DeletePaletteCommand="{Binding DeletePaletteCommand, ElementName=palettesBrowser}"
                                                ImportPaletteCommand="{Binding ImportPaletteCommand, ElementName=palettesBrowser}"/>
                         </DataTemplate>
                     </ItemsControl.ItemTemplate>

+ 61 - 2
PixiEditor/Views/Dialogs/PalettesBrowser.xaml.cs

@@ -15,6 +15,8 @@ using PixiEditor.Models.IO;
 using PixiEditor.Models.IO.JascPalFile;
 using PixiEditor.Views.UserControls.Palettes;
 using SkiaSharp;
+using PixiEditor.Helpers;
+using PixiEditor.Models.Dialogs;
 
 namespace PixiEditor.Views.Dialogs
 {
@@ -48,6 +50,15 @@ namespace PixiEditor.Views.Dialogs
         public static readonly DependencyProperty ImportPaletteCommandProperty =
             DependencyProperty.Register("ImportPaletteCommand", typeof(ICommand), typeof(PalettesBrowser));
 
+        public static readonly DependencyProperty DeletePaletteCommandProperty = DependencyProperty.Register(
+            "DeletePaletteCommand", typeof(ICommand), typeof(PalettesBrowser), new PropertyMetadata(default(ICommand)));
+
+        public ICommand DeletePaletteCommand
+        {
+            get { return (ICommand)GetValue(DeletePaletteCommandProperty); }
+            set { SetValue(DeletePaletteCommandProperty, value); }
+        }
+
         public bool IsFetching
         {
             get { return (bool)GetValue(IsFetchingProperty); }
@@ -108,6 +119,8 @@ namespace PixiEditor.Views.Dialogs
             set { SetValue(NameFilterProperty, value); }
         }
 
+        public RelayCommand AddFromPaletteCommand;
+
         public string SortingType { get; set; } = "Default";
         public ColorsNumberMode ColorsNumberMode { get; set; } = ColorsNumberMode.Any;
 
@@ -119,10 +132,33 @@ namespace PixiEditor.Views.Dialogs
 
         private SortingType _sortingType => (SortingType)Enum.Parse(typeof(SortingType), SortingType.Replace(" ", ""));
         public WpfObservableRangeCollection<SKColor> CurrentEditingPalette { get; set; }
+        public static PalettesBrowser Instance { get; internal set; }
 
         public PalettesBrowser()
         {
             InitializeComponent();
+            Instance = this;
+            DeletePaletteCommand = new RelayCommand<Palette>(DeletePalettte);
+            AddFromPaletteCommand = new RelayCommand(AddFromPalette);
+            Closed += (s, e) => Instance = null;
+        }
+
+        private async void DeletePalettte(Palette palette)
+        {
+            if (palette == null) return;
+
+            string filePath = Path.Join(LocalPalettesFetcher.PathToPalettesFolder, palette.FileName);
+            if (File.Exists(filePath))
+            {
+                if (ConfirmationDialog.Show("Are you sure you want to delete this palette? This cannot be undone.", "Warning!") == ConfirmationType.Yes)
+                {
+                    File.Delete(filePath);
+
+                    LocalPalettesFetcher paletteListDataSource = (LocalPalettesFetcher)PaletteListDataSources.First(x => x is LocalPalettesFetcher);
+                    await paletteListDataSource.RefreshCache();
+                    await UpdatePaletteList();
+                }
+            }
         }
 
         private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
@@ -130,6 +166,11 @@ namespace PixiEditor.Views.Dialogs
             e.CanExecute = true;
         }
 
+        private void AddFromPalette(object obj)
+        {
+            
+        }
+
         private void CommandBinding_Executed_Close(object sender, ExecutedRoutedEventArgs e)
         {
             SystemCommands.CloseWindow(this);
@@ -298,11 +339,16 @@ namespace PixiEditor.Views.Dialogs
 
         private async void AddFromPalette_OnClick(object sender, RoutedEventArgs e)
         {
-            string path = Path.Join(LocalPalettesFetcher.PathToPalettesFolder, "Unnamed Palette.pal");
+            if (CurrentEditingPalette?.Count == 0) return;
+
+            string finalFileName = "Unnamed Palette.pal";
+
+            string path = Path.Join(LocalPalettesFetcher.PathToPalettesFolder, finalFileName);
             int i = 1;
             while (File.Exists(path))
             {
-                path = Path.Join(LocalPalettesFetcher.PathToPalettesFolder, $"Unnamed Palette {i}.pal");
+                finalFileName = $"Unnamed Palette {i}.pal";
+                path = Path.Join(LocalPalettesFetcher.PathToPalettesFolder, finalFileName);
                 i++;
             }
 
@@ -310,6 +356,19 @@ namespace PixiEditor.Views.Dialogs
             LocalPalettesFetcher paletteListDataSource = (LocalPalettesFetcher)PaletteListDataSources.First(x => x is LocalPalettesFetcher);
             await paletteListDataSource.RefreshCache();
             await UpdatePaletteList();
+
+            var palette = paletteListDataSource.CachedPalettes.FirstOrDefault(x => x.FileName == finalFileName);
+            if (palette != null)
+            {
+                if (SortedResults.Contains(palette))
+                {
+                    SortedResults.Move(SortedResults.IndexOf(palette), 0);
+                }
+                else
+                {
+                    SortedResults.Insert(0, palette);
+                }
+            }
         }
 
         private async void PaletteItem_OnRename(object sender, EditableTextBlock.TextChangedEventArgs e)

+ 1 - 1
PixiEditor/Views/MainWindow.xaml

@@ -348,7 +348,7 @@
                                                               SelectColorCommand="{Binding ColorsSubViewModel.SelectColorCommand}"
                                                                 DataSources="{Binding ColorsSubViewModel.PaletteDataSources}"
                                                                 FileParsers="{Binding ColorsSubViewModel.PaletteParsers}"
-                                                                    ImportPaletteCommand="{Binding ColorsSubViewModel.ImportPaletteCommand}"/>
+                                                                ImportPaletteCommand="{Binding ColorsSubViewModel.ImportPaletteCommand}"/>
                                     </avalondock:LayoutAnchorable>
                                     <avalondock:LayoutAnchorable ContentId="swatches" Title="Swatches" CanHide="False"
                                                                  CanClose="False" CanAutoHide="False"

+ 43 - 12
PixiEditor/Views/UserControls/Palettes/PaletteItem.xaml

@@ -2,13 +2,14 @@
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
-             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-             xmlns:local="clr-namespace:PixiEditor.Views.UserControls.Palettes" 
-             xmlns:controls="clr-namespace:PixiEditor.Views.UserControls"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:views="clr-namespace:PixiEditor.Views"
-             mc:Ignorable="d" 
+             mc:Ignorable="d"
              d:DesignHeight="200" d:DesignWidth="800" Name="paletteItem">
-    <Grid>
+    <UserControl.Resources>
+        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
+    </UserControl.Resources>
+    <Grid Background="{StaticResource AccentColor}">
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="100*"/>
             <ColumnDefinition Width="75"/>
@@ -19,12 +20,17 @@
         </Grid.RowDefinitions>
         <StackPanel Orientation="Vertical" Grid.RowSpan="2" Grid.ColumnSpan="2">
             <Separator Background="{StaticResource MainColor}"/>
-            <views:EditableTextBlock OnSubmit="EditableTextBlock_OnSubmit" Text="{Binding Palette.Title, ElementName=paletteItem, Mode=TwoWay}" Foreground="White" FontSize="20"/>
+            <StackPanel Orientation="Horizontal">
+                <views:EditableTextBlock x:Name="titleTextBlock" OnSubmit="EditableTextBlock_OnSubmit" Text="{Binding Palette.Title, ElementName=paletteItem, Mode=TwoWay}" Foreground="White" FontSize="20"/>
+                <Button Visibility="{Binding ElementName=paletteItem, Path=IsMouseOver, Converter={StaticResource BoolToVisibilityConverter}}" Click="RenameButton_Click" Style="{StaticResource ImageButtonStyle}" Cursor="Hand" Width="20" Height="20">
+                    <Image Source="/Images/Edit.png"/>
+                </Button>
+            </StackPanel>
             <TextBlock Margin="0 5 0 0">
             <!--<controls:PrependTextBlock PrependColor="Gray" Prepend="Author: " Text="{Binding Palette.User.Name, ElementName=paletteItem}" Foreground="White"/>-->
             </TextBlock>
         </StackPanel>
-        <ItemsControl Margin="0 0 0 0" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding ElementName=paletteItem, Path=Palette.Colors}">
+        <ItemsControl Margin="0 -20 0 10" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding ElementName=paletteItem, Path=Palette.Colors}">
             <ItemsControl.ItemsPanel>
                 <ItemsPanelTemplate>
                     <WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
@@ -36,14 +42,39 @@
                 </DataTemplate>
             </ItemsControl.ItemTemplate>
         </ItemsControl>
-        <Button Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" VerticalAlignment="Center"
+        <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" VerticalAlignment="Center">
+            <Button Width="24" Height="24" Margin="2"
                 ToolTip="Import"
                 Style="{StaticResource ToolButtonStyle}" Cursor="Hand" 
                     Command="{Binding ImportPaletteCommand, ElementName=paletteItem}"
                     CommandParameter="{Binding ElementName=paletteItem, Path=Palette.Colors}">
-            <Button.Background>
-                <ImageBrush ImageSource="/Images/Download.png"/>
-            </Button.Background>
-        </Button>
+                <Button.Background>
+                    <ImageBrush ImageSource="/Images/Download.png"/>
+                </Button.Background>
+            </Button>
+            <Border Width="28" Height="28" CornerRadius="2.5"
+                    Margin="5 0 0 0" Padding="2">
+                <Border.Style>
+                    <Style TargetType="Border">
+                        <Style.Triggers>
+                            <Trigger Property="IsMouseOver" Value="True">
+                                <Setter Property="Background" Value="Red"/>
+                            </Trigger>
+                            <Trigger Property="IsMouseOver" Value="False">
+                                <Setter Property="Background" Value="Transparent"/>
+                            </Trigger>
+                        </Style.Triggers>
+                    </Style>
+                </Border.Style>
+                <Button Command="{Binding DeletePaletteCommand, ElementName=paletteItem}"
+                        CommandParameter="{Binding ElementName=paletteItem, Path=Palette}"
+                ToolTip="Delete" Width="24" Height="24" Margin="0"
+                Style="{StaticResource ToolButtonStyle}" Cursor="Hand">
+                    <Button.Background>
+                        <ImageBrush ImageSource="/Images/Trash.png"/>
+                    </Button.Background>
+                </Button>
+            </Border>
+        </StackPanel>
     </Grid>
 </UserControl>

+ 17 - 0
PixiEditor/Views/UserControls/Palettes/PaletteItem.xaml.cs

@@ -31,6 +31,18 @@ namespace PixiEditor.Views.UserControls.Palettes
         public static readonly DependencyProperty ImportPaletteCommandProperty =
             DependencyProperty.Register("ImportPaletteCommand", typeof(ICommand), typeof(PaletteItem));
 
+        public ICommand DeletePaletteCommand
+        {
+            get { return (ICommand)GetValue(DeletePaletteCommandProperty); }
+            set { SetValue(DeletePaletteCommandProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for DeletePaletteCommand.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty DeletePaletteCommandProperty =
+            DependencyProperty.Register("DeletePaletteCommand", typeof(ICommand), typeof(PaletteItem));
+
+
+
 
         public event EventHandler<EditableTextBlock.TextChangedEventArgs> OnRename;
 
@@ -43,5 +55,10 @@ namespace PixiEditor.Views.UserControls.Palettes
         {
             OnRename?.Invoke(this, e);
         }
+
+        private void RenameButton_Click(object sender, RoutedEventArgs e)
+        {
+            titleTextBlock.IsEditing = true;
+        }
     }
 }

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

@@ -217,6 +217,7 @@ namespace PixiEditor.Views.UserControls.Palettes
 
         private async void BrowsePalettes_Click(object sender, RoutedEventArgs e)
         {
+            if (PalettesBrowser.Instance != null) return;
             PalettesBrowser browser = new PalettesBrowser
             {
                 Owner = Application.Current.MainWindow,