Browse Source

Fixed saving palettes

flabbet 1 year ago
parent
commit
8e42b29934

+ 5 - 0
src/PixiEditor.Extensions.CommonApi/Palettes/Parsers/PaletteFileParser.cs

@@ -16,4 +16,9 @@ public abstract class PaletteFileParser
         string[] lines = fileContent.Split('\n');
         return lines;
     }
+
+    public override string ToString()
+    {
+        return FileName;
+    }
 }

+ 5 - 1
src/PixiEditor/Data/Localization/Languages/en.json

@@ -716,5 +716,9 @@
   "ADD_EMPTY_FRAME": "Add empty frame",
   "DUPLICATE_FRAME": "Duplicate frame",
   "DELETE_FRAME": "Remove frame",
-  "DEFAULT_MEMBER_NAME": "New Element"
+  "DEFAULT_MEMBER_NAME": "New Element",
+  "NO_PARSER_FOUND": "No file parser found for extension '{0}'",
+  "SELECT_FILE_FORMAT": "Select file format",
+  "SELECT_FILE_FORMAT_DESCRIPTION": "Multiple file types of the same format are supported. Please select the one you want to use.",
+  "NEW_PALETTE_FILE": "palette"
 }

+ 2 - 1
src/PixiEditor/Views/Dialogs/ExportFilePopup.axaml.cs

@@ -7,6 +7,7 @@ using Avalonia.Threading;
 using ChunkyImageLib;
 using CommunityToolkit.Mvvm.Input;
 using PixiEditor.DrawingApi.Core;
+using PixiEditor.DrawingApi.Core.Bridge;
 using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Files;
@@ -300,7 +301,7 @@ internal partial class ExportFilePopup : PixiEditorPopup
                             int x = index % clampedColumns;
                             int y = index / clampedColumns;
                             var resized = frame.ResizeNearestNeighbor(new VecI(singleFrameSize.X, singleFrameSize.Y));
-                            Dispatcher.UIThread.Post(() =>
+                            DrawingBackendApi.Current.RenderingServer.Invoke(() =>
                             {
                                 if (ExportPreview.IsDisposed) return;
                                 ExportPreview!.DrawingSurface.Canvas.DrawSurface(resized.DrawingSurface,

+ 44 - 3
src/PixiEditor/Views/Palettes/PaletteViewer.axaml.cs

@@ -10,6 +10,7 @@ using Avalonia.Input;
 using Avalonia.Interactivity;
 using Avalonia.Media;
 using Avalonia.Platform.Storage;
+using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Extensions.CommonApi.Palettes;
 using PixiEditor.Extensions.CommonApi.Palettes.Parsers;
@@ -167,13 +168,25 @@ internal partial class PaletteViewer : UserControl
             List<PaletteFileParser> availableParsers = PaletteProvider.AvailableParsers.Where(x => x.CanSave).ToList();
             var file = await window.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions()
             {
-                FileTypeChoices = PaletteHelpers.GetFilter(availableParsers, false)
+                FileTypeChoices = PaletteHelpers.GetFilter(availableParsers, false),
+                SuggestedFileName = new LocalizedString("NEW_PALETTE_FILE"),
             });
 
             if (file is null) return;
+            
+            string extension = Path.GetExtension(file.Path.LocalPath);
 
-            int filterIndex = 0; //file.FilterIndex; //TODO get chosen filter index somehow
-            var foundParser = availableParsers[filterIndex - 1];
+            var parsersOfExtensions =
+                availableParsers.Where(x => x.SupportedFileExtensions.Contains(extension)).ToList();
+            
+            var foundParser = await GetPaletteFileParser(parsersOfExtensions);
+
+            if (foundParser is null)
+            {
+                NoticeDialog.Show(new LocalizedString("NO_PARSER_FOUND", extension), "ERROR");
+                return;
+            }
+            
             if (Colors is null || Colors.Count == 0)
             {
                 NoticeDialog.Show("NO_COLORS_TO_SAVE", "ERROR");
@@ -195,6 +208,34 @@ internal partial class PaletteViewer : UserControl
         });
     }
 
+    private static async Task<PaletteFileParser?> GetPaletteFileParser(List<PaletteFileParser> parsersOfExtensions)
+    {
+        PaletteFileParser foundParser = null;
+
+        if (parsersOfExtensions.Count > 1)
+        {
+            var optionsDialog = new OptionsDialog<PaletteFileParser>(
+                new LocalizedString("SELECT_FILE_FORMAT"),
+                new LocalizedString("SELECT_FILE_FORMAT_DESCRIPTION"), MainWindow.Current);
+
+            foreach (var pars in parsersOfExtensions)
+            {
+                optionsDialog.Add(pars);
+            }
+                
+            if (await optionsDialog.ShowDialog())
+            {
+                foundParser = optionsDialog.Result;
+            }
+        }
+        else if (parsersOfExtensions.Count == 1)
+        {
+            foundParser = parsersOfExtensions.First();
+        }
+
+        return foundParser;
+    }
+
     private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
     {
         if (IsSupportedFilePresent(e, out _))