Browse Source

Pixi palette parser

Krzysztof Krysiński 2 years ago
parent
commit
4c3102cc57

+ 1 - 0
src/PixiEditor/Helpers/Extensions/ServiceCollectionHelpers.cs

@@ -66,6 +66,7 @@ internal static class ServiceCollectionHelpers
         .AddSingleton<PaletteFileParser, PaintNetTxtParser>()
         .AddSingleton<PaletteFileParser, HexPaletteParser>()
         .AddSingleton<PaletteFileParser, GimpGplParser>()
+        .AddSingleton<PaletteFileParser, PixiPaletteParser>()
         // Palette data sources
         .AddSingleton<PaletteListDataSource, LocalPalettesFetcher>();
 }

+ 2 - 0
src/PixiEditor/Models/IO/PaletteFileParser.cs

@@ -9,6 +9,8 @@ internal abstract class PaletteFileParser
     public abstract string FileName { get; }
     public abstract string[] SupportedFileExtensions { get; }
 
+    public virtual bool CanSave => true;
+
     protected static async Task<string[]> ReadTextLines(string path)
     {
         using var stream = File.OpenText(path);

+ 39 - 0
src/PixiEditor/Models/IO/PaletteParsers/PixiPaletteParser.cs

@@ -0,0 +1,39 @@
+using System.IO;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.Parser;
+
+namespace PixiEditor.Models.IO.PaletteParsers;
+
+internal class PixiPaletteParser : PaletteFileParser
+{
+    public override string FileName { get; } = "Palette from PixiEditor .pixi";
+    public override string[] SupportedFileExtensions { get; } = { ".pixi" };
+    public override async Task<PaletteFileData> Parse(string path)
+    {
+        try
+        {
+            return await ParseFile(path);
+        }
+        catch
+        {
+            return PaletteFileData.Corrupted;
+        }
+    }
+
+    private async Task<PaletteFileData> ParseFile(string path)
+    {
+        var file = await PixiParser.DeserializeAsync(path);
+        if(file.Palette == null) return PaletteFileData.Corrupted;
+
+        string name = Path.GetFileNameWithoutExtension(path);
+
+        return new PaletteFileData(name, file.Palette.Select(x => new Color(x.R, x.G, x.B, x.A)).ToArray());
+    }
+
+    public override bool CanSave => false;
+
+    public override Task<bool> Save(string path, PaletteFileData data)
+    {
+        throw new NotImplementedException("Saving palette to .pixi directly is not supported.");
+    }
+}

+ 6 - 0
src/PixiEditor/Models/IO/PaletteParsers/SavingNotSupportedException.cs

@@ -0,0 +1,6 @@
+namespace PixiEditor.Models.IO.PaletteParsers;
+
+public class SavingNotSupportedException : Exception
+{
+
+}

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

@@ -132,7 +132,7 @@ internal partial class PaletteViewer : UserControl
     {
         SaveFileDialog saveFileDialog = new SaveFileDialog
         {
-            Filter = PaletteHelpers.GetFilter(FileParsers, false)
+            Filter = PaletteHelpers.GetFilter(FileParsers.Where(x => x.CanSave).ToList(), false)
         };
 
         if (saveFileDialog.ShowDialog() == true)