Browse Source

#277 Add regular image formats to the save as dialog

tomaszkot 3 years ago
parent
commit
1ff305ce6a

+ 3 - 0
PixiEditor/Models/Constants.cs

@@ -8,5 +8,8 @@
 
 
         public const int MaxCanvasWidth = 9999;
         public const int MaxCanvasWidth = 9999;
         public const int MaxCanvasHeight = 9999;
         public const int MaxCanvasHeight = 9999;
+
+        public const string NativeExtensionNoDot = "pixi";
+        public const string NativeExtension = "." + NativeExtensionNoDot;
     }
     }
 }
 }

+ 45 - 5
PixiEditor/Models/IO/Exporter.cs

@@ -8,6 +8,8 @@ using System.Collections.Generic;
 using System.Drawing.Imaging;
 using System.Drawing.Imaging;
 using System.IO;
 using System.IO;
 using System.IO.Compression;
 using System.IO.Compression;
+using System.Linq;
+using System.Reflection;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 using System.Windows;
 using System.Windows;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
@@ -16,8 +18,8 @@ namespace PixiEditor.Models.IO
 {
 {
     public class Exporter
     public class Exporter
     {
     {
-
-
+        static ImageFormat[] _formats = new[] { ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Tiff };
+        
         /// <summary>
         /// <summary>
         ///     Saves document as .pixi file that contains all document data.
         ///     Saves document as .pixi file that contains all document data.
         /// </summary>
         /// </summary>
@@ -25,10 +27,11 @@ namespace PixiEditor.Models.IO
         /// <param name="path">Path where file was saved.</param>
         /// <param name="path">Path where file was saved.</param>
         public static bool SaveAsEditableFileWithDialog(Document document, out string path)
         public static bool SaveAsEditableFileWithDialog(Document document, out string path)
         {
         {
+            var pixi = GetFormattedString("PixiEditor File", Constants.NativeExtensionNoDot);
             SaveFileDialog dialog = new SaveFileDialog
             SaveFileDialog dialog = new SaveFileDialog
             {
             {
-                Filter = "PixiEditor Files | *.pixi",
-                DefaultExt = "pixi"
+                Filter = pixi + "|" + BuildFilter(),
+                FilterIndex = 0
             };
             };
             if ((bool)dialog.ShowDialog())
             if ((bool)dialog.ShowDialog())
             {
             {
@@ -40,6 +43,23 @@ namespace PixiEditor.Models.IO
             return false;
             return false;
         }
         }
 
 
+        public static string BuildFilter()
+        {
+          var filter = string.Join("|", Formats.Select(i => GetFormattedString(i)));
+          return filter;
+        }
+
+        public static string GetFormattedString(ImageFormat imageFormat)
+        {
+            var formatLower = imageFormat.ToString().ToLower();
+            return GetFormattedString(imageFormat.ToString() + " Image", formatLower);
+        }
+
+        private static string GetFormattedString(string imageFormat, string formatLower)
+        {
+            return $"{imageFormat}|*.{formatLower}";
+        }
+
         /// <summary>
         /// <summary>
         /// Saves editable file to chosen path and returns it.
         /// Saves editable file to chosen path and returns it.
         /// </summary>
         /// </summary>
@@ -48,14 +68,34 @@ namespace PixiEditor.Models.IO
         /// <returns>Path.</returns>
         /// <returns>Path.</returns>
         public static string SaveAsEditableFile(Document document, string path)
         public static string SaveAsEditableFile(Document document, string path)
         {
         {
-            Parser.PixiParser.Serialize(ParserHelpers.ToSerializable(document), path);
+            if (Path.GetExtension(path) != Constants.NativeExtension)
+            {
+                var chosenFormat = ParseImageFormat(Path.GetExtension(path));
+                var bitmap = document.Renderer.FinalBitmap;
+                SaveAs(encodersFactory[chosenFormat](), path, bitmap.PixelWidth, bitmap.PixelHeight, bitmap);
+            }
+            else
+            {
+                Parser.PixiParser.Serialize(ParserHelpers.ToSerializable(document), path);
+            }
+
             return path;
             return path;
         }
         }
 
 
+        public static ImageFormat ParseImageFormat(string fileExtension)
+        {
+            fileExtension = fileExtension.Replace(".", "");
+            return (ImageFormat)typeof(ImageFormat)
+                    .GetProperty(fileExtension, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase)
+                    .GetValue(null);
+        }
+
         //static Dictionary<ImageFormat, Action<ExportFileDialog, WriteableBitmap>> encoders = new Dictionary<ImageFormat, Action<ExportFileDialog, WriteableBitmap>>();
         //static Dictionary<ImageFormat, Action<ExportFileDialog, WriteableBitmap>> encoders = new Dictionary<ImageFormat, Action<ExportFileDialog, WriteableBitmap>>();
         //TODO remove static methods/members
         //TODO remove static methods/members
         static Dictionary<ImageFormat, Func<BitmapEncoder>> encodersFactory = new Dictionary<ImageFormat, Func<BitmapEncoder>>();
         static Dictionary<ImageFormat, Func<BitmapEncoder>> encodersFactory = new Dictionary<ImageFormat, Func<BitmapEncoder>>();
 
 
+        public static ImageFormat[] Formats { get => _formats; }
+
         static Exporter()
         static Exporter()
         {
         {
             encodersFactory[ImageFormat.Png] = () => { return new PngBitmapEncoder(); };
             encodersFactory[ImageFormat.Png] = () => { return new PngBitmapEncoder(); };

+ 5 - 25
PixiEditor/ViewModels/SaveFilePopupViewModel.cs

@@ -1,5 +1,6 @@
 using Microsoft.Win32;
 using Microsoft.Win32;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
+using PixiEditor.Models.IO;
 using System;
 using System;
 using System.Drawing.Imaging;
 using System.Drawing.Imaging;
 using System.IO;
 using System.IO;
@@ -11,7 +12,6 @@ namespace PixiEditor.ViewModels
 {
 {
     internal class SaveFilePopupViewModel : ViewModelBase
     internal class SaveFilePopupViewModel : ViewModelBase
     {
     {
-        ImageFormat[] _formats = new[] { ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Tiff };
         private string _filePath;
         private string _filePath;
         private ImageFormat _chosenFormat;
         private ImageFormat _chosenFormat;
 
 
@@ -51,27 +51,7 @@ namespace PixiEditor.ViewModels
                 }
                 }
             }
             }
         }
         }
-
-        string GetFormattedString(ImageFormat imageFormat)
-        {
-            var formatLower = imageFormat.ToString().ToLower();
-            return $"{imageFormat} Image (.{formatLower}) | *.{formatLower}";
-        }
-
-        string BuildFilter()
-        {
-            var filter = string.Join("|", _formats.Select(i => GetFormattedString(i)));
-            return filter;
-        }
-
-        ImageFormat ParseImageFormat(string fileExtension)
-        {
-            fileExtension = fileExtension.Replace(".", "");
-            return (ImageFormat)typeof(ImageFormat)
-                    .GetProperty(fileExtension, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase)
-                    .GetValue(null);
-        }
-
+                
         /// <summary>
         /// <summary>
         ///     Command that handles Path choosing to save file
         ///     Command that handles Path choosing to save file
         /// </summary>
         /// </summary>
@@ -81,14 +61,14 @@ namespace PixiEditor.ViewModels
             {
             {
                 Title = "Export path",
                 Title = "Export path",
                 CheckPathExists = true,
                 CheckPathExists = true,
-                DefaultExt = "." + _formats.First().ToString().ToLower(),
-                Filter = BuildFilter()
+                DefaultExt = "." + Exporter.Formats.First().ToString().ToLower(),
+                Filter = Exporter.BuildFilter()
             };
             };
             if (path.ShowDialog() == true)
             if (path.ShowDialog() == true)
             {
             {
                 if (string.IsNullOrEmpty(path.FileName) == false)
                 if (string.IsNullOrEmpty(path.FileName) == false)
                 {
                 {
-                    ChosenFormat = ParseImageFormat(Path.GetExtension(path.SafeFileName));
+                    ChosenFormat = Exporter.ParseImageFormat(Path.GetExtension(path.SafeFileName));
                     return path.FileName;
                     return path.FileName;
                 }
                 }
             }
             }

+ 1 - 2
PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -244,8 +244,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
         {
             bool paramIsAsNew = parameter != null && parameter.ToString()?.ToLower() == "asnew";
             bool paramIsAsNew = parameter != null && parameter.ToString()?.ToLower() == "asnew";
             if (paramIsAsNew ||
             if (paramIsAsNew ||
-                string.IsNullOrEmpty(Owner.BitmapManager.ActiveDocument.DocumentFilePath) ||
-                !Owner.BitmapManager.ActiveDocument.DocumentFilePath.EndsWith(".pixi"))
+                string.IsNullOrEmpty(Owner.BitmapManager.ActiveDocument.DocumentFilePath)) 
             {
             {
                 Owner.BitmapManager.ActiveDocument.SaveWithDialog();
                 Owner.BitmapManager.ActiveDocument.SaveWithDialog();
             }
             }