Browse Source

Added renaming

Krzysztof Krysiński 3 years ago
parent
commit
82413f9eee

+ 5 - 4
PixiEditor/Models/DataHolders/Palettes/Palette.cs

@@ -1,4 +1,5 @@
-using PixiEditor.Helpers;
+#nullable enable
+using PixiEditor.Helpers;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 
@@ -8,13 +9,13 @@ namespace PixiEditor.Models.DataHolders.Palettes
     {
         public string Title { get; set; }
         public List<string> Colors { get; set; }
-        public string[] Tags { get; set; }
+        public string FileName { get; set; }
 
-        public Palette(string title, List<string> colors, string[] tags)
+        public Palette(string title, List<string> colors, string fileName)
         {
             Title = title;
             Colors = colors;
-            Tags = tags;
+            FileName = fileName;
         }
     }
 }

+ 12 - 5
PixiEditor/Models/DataProviders/LocalPalettesFetcher.cs

@@ -3,7 +3,6 @@ using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders.Palettes;
 using PixiEditor.Models.IO;
 using System;
-using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
@@ -21,11 +20,9 @@ namespace PixiEditor.Models.DataProviders
 
         public override async Task<PaletteList> FetchPaletteList(int startIndex, int count, FilteringSettings filtering)
         {
-            string[] files = DirectoryExtensions.GetFiles(PathToPalettesFolder, string.Join("|", AvailableParsers.SelectMany(x => x.SupportedFileExtensions)), SearchOption.TopDirectoryOnly);
-
             if(_cachedPalettes == null)
             {
-                _cachedPalettes = await ParseAll(files);
+                await RefreshCache();
             }
 
             PaletteList result = new PaletteList
@@ -48,6 +45,12 @@ namespace PixiEditor.Models.DataProviders
             return result;
         }
 
+        public async Task RefreshCache()
+        {
+            string[] files = DirectoryExtensions.GetFiles(PathToPalettesFolder, string.Join("|", AvailableParsers.SelectMany(x => x.SupportedFileExtensions)), SearchOption.TopDirectoryOnly);
+            _cachedPalettes = await ParseAll(files);
+        }
+
         private async Task<List<Palette>> ParseAll(string[] files)
         {
             List<Palette> result = new List<Palette>();
@@ -57,7 +60,11 @@ namespace PixiEditor.Models.DataProviders
                 var foundParser = AvailableParsers.First(x => x.SupportedFileExtensions.Contains(extension));
                 {
                     PaletteFileData fileData = await foundParser.Parse(file);
-                    result.Add(new Palette(fileData.Title, new List<string>(fileData.GetHexColors()), fileData.Tags));
+                    result.Add(
+                        new Palette(
+                            fileData.Title,
+                            new List<string>(fileData.GetHexColors()),
+                            Path.GetFileName(file)));
                 }
             }
 

+ 1 - 1
PixiEditor/Models/IO/JascPalFile/JascFileParser.cs

@@ -31,7 +31,7 @@ public class JascFileParser : PaletteFileParser
                 colors[i] = new SKColor(byte.Parse(colorData[0]), byte.Parse(colorData[1]), byte.Parse(colorData[2]));
             }
 
-            return new PaletteFileData(name, colors, Array.Empty<string>());
+            return new PaletteFileData(name, colors);
         }
 
         throw new JascFileException("Invalid JASC-PAL file.");

+ 12 - 4
PixiEditor/Models/IO/PaletteFileData.cs

@@ -8,20 +8,28 @@ namespace PixiEditor.Models.IO
     {
         public string Title { get; set; }
         public SKColor[] Colors { get; set; }
-        public string[] Tags { get; set; }
 
         public PaletteFileData(SKColor[] colors)
         {
             Colors = colors;
             Title = "";
-            Tags = Array.Empty<string>();
         }
 
-        public PaletteFileData(string title, SKColor[] colors, string[] tags)
+        public PaletteFileData(List<string> colors)
+        {
+            Colors = new SKColor[colors.Count];
+            for (int i = 0; i < colors.Count; i++)
+            {
+                Colors[i] = SKColor.Parse(colors[i]);
+            }
+
+            Title = "";
+        }
+
+        public PaletteFileData(string title, SKColor[] colors)
         {
             Title = title;
             Colors = colors;
-            Tags = tags;
         }
 
         public string[] GetHexColors()

+ 3 - 2
PixiEditor/Views/Dialogs/PalettesBrowser.xaml

@@ -86,7 +86,7 @@
                                    Value="{Binding ElementName=palettesBrowser, Path=ColorsNumber, Mode=TwoWay}"/>
             </StackPanel>
             <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 10 0">
-                <Button Click="AddFromPalette_OnClick" Cursor="Hand" Margin="10 0" Style="{StaticResource ImageButtonStyle}" Width="24" Height="24">
+                <Button ToolTip="Add from current palette" Click="AddFromPalette_OnClick" Cursor="Hand" Margin="10 0" Style="{StaticResource ImageButtonStyle}" Width="24" Height="24">
                     <Image Source="/Images/Plus-square.png"/>
                 </Button>
                 <Button Cursor="Hand" Click="OpenFolder_OnClick" Style="{StaticResource ImageButtonStyle}" Width="24" Height="24">
@@ -104,7 +104,8 @@
                 Converter={StaticResource BoolToVisibilityConverter}}">
                     <ItemsControl.ItemTemplate>
                         <DataTemplate>
-                            <local:PaletteItem Palette="{Binding}" 
+                            <local:PaletteItem Palette="{Binding}"
+                                               OnRename="PaletteItem_OnRename"
                                                ImportPaletteCommand="{Binding ImportPaletteCommand, ElementName=palettesBrowser}"/>
                         </DataTemplate>
                     </ItemsControl.ItemTemplate>

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

@@ -11,6 +11,10 @@ using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;
+using PixiEditor.Models.IO;
+using PixiEditor.Models.IO.JascPalFile;
+using PixiEditor.Views.UserControls.Palettes;
+using SkiaSharp;
 
 namespace PixiEditor.Views.Dialogs
 {
@@ -114,6 +118,7 @@ namespace PixiEditor.Views.Dialogs
         private char[] _separators = new char[] { ' ', ',' };
 
         private SortingType _sortingType => (SortingType)Enum.Parse(typeof(SortingType), SortingType.Replace(" ", ""));
+        public WpfObservableRangeCollection<SKColor> CurrentEditingPalette { get; set; }
 
         public PalettesBrowser()
         {
@@ -291,9 +296,40 @@ namespace PixiEditor.Views.Dialogs
             }
         }
 
-        private void AddFromPalette_OnClick(object sender, RoutedEventArgs e)
+        private async void AddFromPalette_OnClick(object sender, RoutedEventArgs e)
         {
-            
+            string path = Path.Join(LocalPalettesFetcher.PathToPalettesFolder, "Unnamed Palette.pal");
+            int i = 1;
+            while (File.Exists(path))
+            {
+                path = Path.Join(LocalPalettesFetcher.PathToPalettesFolder, $"Unnamed Palette {i}.pal");
+                i++;
+            }
+
+            await JascFileParser.SaveFile(path, new PaletteFileData(CurrentEditingPalette.ToArray()));
+            LocalPalettesFetcher paletteListDataSource = (LocalPalettesFetcher)PaletteListDataSources.First(x => x is LocalPalettesFetcher);
+            await paletteListDataSource.RefreshCache();
+            await UpdatePaletteList();
+        }
+
+        private async void PaletteItem_OnRename(object sender, EditableTextBlock.TextChangedEventArgs e)
+        {
+            PaletteItem item = (PaletteItem)sender;
+            if (string.IsNullOrWhiteSpace(e.NewText) || e.NewText.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || e.NewText == item.Palette.Title)
+            {
+                return;
+            }
+
+            string oldFileName = item.Palette.FileName;
+            item.Palette.FileName = $"{e.NewText}.pal";
+            File.Move(
+                Path.Join(LocalPalettesFetcher.PathToPalettesFolder, oldFileName),
+                Path.Join(LocalPalettesFetcher.PathToPalettesFolder, item.Palette.FileName));
+
+
+            LocalPalettesFetcher paletteListDataSource = (LocalPalettesFetcher)PaletteListDataSources.First(x => x is LocalPalettesFetcher);
+            await paletteListDataSource.RefreshCache();
+            await UpdatePaletteList();
         }
     }
 }

+ 16 - 0
PixiEditor/Views/UserControls/EditableTextBlock.xaml.cs

@@ -37,6 +37,8 @@ namespace PixiEditor.Views
                 typeof(EditableTextBlock),
                 new PropertyMetadata(OnIsEditingChanged));
 
+        public event EventHandler<TextChangedEventArgs> OnSubmit;
+
         public EditableTextBlock()
         {
             InitializeComponent();
@@ -80,6 +82,7 @@ namespace PixiEditor.Views
             TextBlockVisibility = Visibility.Visible;
             ShortcutController.UnblockShortcutExecution("EditableTextBlock");
             IsEditing = false;
+            OnSubmit?.Invoke(this, new TextChangedEventArgs(textBox.Text, Text));
         }
 
         private static void OnIsEditingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -116,5 +119,18 @@ namespace PixiEditor.Views
         {
             DisableEditing();
         }
+
+        public class TextChangedEventArgs : EventArgs
+        {
+            public string NewText { get; set; }
+
+            public string OldText { get; set; }
+
+            public TextChangedEventArgs(string newText, string oldText)
+            {
+                NewText = newText;
+                OldText = oldText;
+            }
+        }
     }
 }

+ 2 - 17
PixiEditor/Views/UserControls/Palettes/PaletteItem.xaml

@@ -5,6 +5,7 @@
              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:views="clr-namespace:PixiEditor.Views"
              mc:Ignorable="d" 
              d:DesignHeight="200" d:DesignWidth="800" Name="paletteItem">
     <Grid>
@@ -15,11 +16,10 @@
         <Grid.RowDefinitions>
             <RowDefinition Height="60"/>
             <RowDefinition Height="60*"/>
-            <RowDefinition Height="30"/>
         </Grid.RowDefinitions>
         <StackPanel Orientation="Vertical" Grid.RowSpan="2" Grid.ColumnSpan="2">
             <Separator Background="{StaticResource MainColor}"/>
-            <TextBlock Text="{Binding Palette.Title, ElementName=paletteItem}" Foreground="White" FontSize="20"/>
+            <views:EditableTextBlock OnSubmit="EditableTextBlock_OnSubmit" Text="{Binding Palette.Title, ElementName=paletteItem, Mode=TwoWay}" Foreground="White" FontSize="20"/>
             <TextBlock Margin="0 5 0 0">
             <!--<controls:PrependTextBlock PrependColor="Gray" Prepend="Author: " Text="{Binding Palette.User.Name, ElementName=paletteItem}" Foreground="White"/>-->
             </TextBlock>
@@ -45,20 +45,5 @@
                 <ImageBrush ImageSource="/Images/Download.png"/>
             </Button.Background>
         </Button>
-        <StackPanel Grid.Row="2" Margin="0 10 0 0" Orientation="Horizontal">
-            <TextBlock Text="Tags: " Foreground="Gray"/>
-            <ItemsControl ItemsSource="{Binding ElementName=paletteItem, Path=Palette.Tags}">
-                <ItemsControl.ItemsPanel>
-                    <ItemsPanelTemplate>
-                        <StackPanel Orientation="Horizontal"/>
-                    </ItemsPanelTemplate>
-                </ItemsControl.ItemsPanel>
-                <ItemsControl.ItemTemplate>
-                    <DataTemplate d:DataType="{x:Type TextBlock}">
-                        <TextBlock Text="{Binding}" Margin="5 0 0 0" Foreground="White"/>
-                    </DataTemplate>
-                </ItemsControl.ItemTemplate>
-            </ItemsControl>
-        </StackPanel>
     </Grid>
 </UserControl>

+ 8 - 3
PixiEditor/Views/UserControls/Palettes/PaletteItem.xaml.cs

@@ -1,10 +1,8 @@
-using PixiEditor.Models.DataHolders;
+using System;
 using PixiEditor.Models.DataHolders.Palettes;
-using System.Diagnostics;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;
-using System.Windows.Navigation;
 
 namespace PixiEditor.Views.UserControls.Palettes
 {
@@ -34,9 +32,16 @@ namespace PixiEditor.Views.UserControls.Palettes
             DependencyProperty.Register("ImportPaletteCommand", typeof(ICommand), typeof(PaletteItem));
 
 
+        public event EventHandler<EditableTextBlock.TextChangedEventArgs> OnRename;
+
         public PaletteItem()
         {
             InitializeComponent();
         }
+
+        private void EditableTextBlock_OnSubmit(object sender, EditableTextBlock.TextChangedEventArgs e)
+        {
+            OnRename?.Invoke(this, e);
+        }
     }
 }

+ 5 - 3
PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml.cs

@@ -220,8 +220,8 @@ namespace PixiEditor.Views.UserControls.Palettes
             PalettesBrowser browser = new PalettesBrowser
             {
                 Owner = Application.Current.MainWindow,
-                ImportPaletteCommand = this.ImportPaletteCommand,
-                PaletteListDataSources = DataSources,
+                ImportPaletteCommand = ImportPaletteCommand,
+                PaletteListDataSources = DataSources
             };
 
             if(_cachedPaletteList != null)
@@ -229,11 +229,13 @@ namespace PixiEditor.Views.UserControls.Palettes
                 browser.PaletteList = _cachedPaletteList;
             }
 
-            browser.OnListFetched += (PaletteList list) =>
+            browser.OnListFetched += list =>
             {
                 _cachedPaletteList = list;
             };
 
+            browser.CurrentEditingPalette = Colors;
+
             browser.Show();
             await browser.UpdatePaletteList();
         }