Browse Source

Fixed importer and exported

Added confirmation dialog on open file
flabbet 5 years ago
parent
commit
f8ec7aa6ad

+ 7 - 1
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -95,6 +95,12 @@ namespace PixiEditor.Models.Controllers
             LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
         }
 
+        public void AddNewLayer(string name, WriteableBitmap bitmap, bool setAsActive = true)
+        {
+            AddNewLayer(name, bitmap.PixelWidth, bitmap.PixelHeight, setAsActive);
+            ActiveDocument.Layers.Last().LayerBitmap = bitmap;
+        }
+
         public void AddNewLayer(string name, bool setAsActive = true)
         {
             AddNewLayer(name, 0, 0, setAsActive);
@@ -181,7 +187,7 @@ namespace PixiEditor.Models.Controllers
 
         public WriteableBitmap GetCombinedLayersBitmap()
         {
-            return BitmapUtils.CombineBitmaps(ActiveDocument.Layers.ToArray());
+            return BitmapUtils.CombineLayers(ActiveDocument.Layers.ToArray());
         }
 
 

+ 1 - 1
PixiEditor/Models/Controllers/ClipboardController.cs

@@ -20,7 +20,7 @@ namespace PixiEditor.Models.Controllers
         public void CopyToClipboard(Layer[] layers, Coordinates[] selection)
         {
             Clipboard.Clear();
-            WriteableBitmap combinedBitmaps = BitmapUtils.CombineBitmaps(layers);
+            WriteableBitmap combinedBitmaps = BitmapUtils.CombineLayers(layers);
             using (var pngStream = new MemoryStream())
             {
                 DataObject data = new DataObject();

+ 3 - 1
PixiEditor/Models/DataHolders/SerializableDocument.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.ObjectModel;
 using System.Linq;
+using System.Windows;
 using System.Windows.Media;
 using PixiEditor.Models.Images;
 using PixiEditor.Models.Layers;
@@ -44,7 +45,8 @@ namespace PixiEditor.Models.DataHolders
                     new Layer(BitmapUtils.BytesToWriteableBitmap(serLayer.Width, serLayer.Height, serLayer.BitmapBytes))
                     {
                         IsVisible = serLayer.IsVisible,
-                        Name = serLayer.Name
+                        Name = serLayer.Name,
+                        Offset = new Thickness(serLayer.OffsetX, serLayer.OffsetY, 0, 0)
                     };
                 layers.Add(layer);
             }

+ 2 - 1
PixiEditor/Models/IO/Exporter.cs

@@ -34,8 +34,8 @@ namespace PixiEditor.Models.IO
         /// <summary>
         ///     Creates ExportFileDialog to get width, height and path of file.
         /// </summary>
-        /// <param name="type">Type of file to be saved in.</param>
         /// <param name="bitmap">Bitmap to be saved as file.</param>
+        /// <param name="fileDimensions">Size of file</param>
         public static void Export(WriteableBitmap bitmap, Size fileDimensions)
         {
             ExportFileDialog info = new ExportFileDialog(fileDimensions);
@@ -61,6 +61,7 @@ namespace PixiEditor.Models.IO
         /// <param name="savePath">Save file path</param>
         /// <param name="exportWidth">File width</param>
         /// <param name="exportHeight">File height</param>
+        /// <param name="bitmap">Bitmap to save</param>
         private static void SaveAsPng(string savePath, int exportWidth, int exportHeight, WriteableBitmap bitmap)
         {
             try

+ 8 - 10
PixiEditor/Models/Images/BitmapUtils.cs

@@ -7,6 +7,7 @@ using System.Windows.Interop;
 using System.Windows.Media.Imaging;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
+using PixiEditor.ViewModels;
 using Color = System.Windows.Media.Color;
 
 namespace PixiEditor.Models.Images
@@ -30,27 +31,24 @@ namespace PixiEditor.Models.Images
                 BitmapSizeOptions.FromEmptyOptions());
         }
 
-        public static WriteableBitmap CombineBitmaps(Layer[] layers)
+        public static WriteableBitmap CombineLayers(Layer[] layers)
         {
-            return CombineBitmaps(layers.Select(x => x.LayerBitmap).ToArray());
-        }
+            int width = ViewModelMain.Current.BitmapManager.ActiveDocument.Width;
+            int height = ViewModelMain.Current.BitmapManager.ActiveDocument.Height;
+
+            WriteableBitmap finalBitmap = BitmapFactory.New(width, height);
 
-        public static WriteableBitmap CombineBitmaps(WriteableBitmap[] bitmaps)
-        {
-            WriteableBitmap finalBitmap = bitmaps[0].Clone();
-            finalBitmap.Lock();
             using (finalBitmap.GetBitmapContext())
             {
-                for (int i = 1; i < bitmaps.Length; i++)
+                for (int i = 0; i < layers.Length; i++)
                 for (int y = 0; y < finalBitmap.Height; y++)
                 for (int x = 0; x < finalBitmap.Width; x++)
                 {
-                    Color color = bitmaps[i].GetPixel(x, y);
+                    Color color = layers[i].GetPixelWithOffset(x, y);
                     if (color.A != 0 || color.R != 0 || color.B != 0 || color.G != 0) finalBitmap.SetPixel(x, y, color);
                 }
             }
 
-            finalBitmap.Unlock();
             return finalBitmap;
         }
 

+ 5 - 4
PixiEditor/Models/Layers/SerializableLayer.cs

@@ -1,8 +1,6 @@
 using System;
-using System.Windows;
-using PixiEditor.Models.Layers;
 
-namespace PixiEditor.Models.DataHolders
+namespace PixiEditor.Models.Layers
 {
     [Serializable]
     public class SerializableLayer
@@ -12,7 +10,8 @@ namespace PixiEditor.Models.DataHolders
         public int Height { get; set; }
         public byte[] BitmapBytes { get; set; }
         public bool IsVisible { get; set; }
-        public Thickness Offset { get; set; }
+        public int OffsetX { get; set; }
+        public int OffsetY { get; set; }
 
         public SerializableLayer(Layer layer)
         {
@@ -21,6 +20,8 @@ namespace PixiEditor.Models.DataHolders
             Height = layer.Height;
             BitmapBytes = layer.ConvertBitmapToBytes();
             IsVisible = layer.IsVisible;
+            OffsetX = (int)layer.Offset.Left;
+            OffsetY = (int)layer.Offset.Top;
         }
     }
 }

+ 1 - 1
PixiEditor/Models/Tools/Tools/MoveTool.cs

@@ -86,7 +86,7 @@ namespace PixiEditor.Models.Tools.Tools
             if (_lastStartMousePos != start) //I am aware that this could be moved to OnMouseDown, but it is executed before Use, so I didn't want to complicate for now
             {
                 ResetSelectionValues(start);
-                if (ViewModelMain.Current.ActiveSelection.SelectedPoints.Count > 0) //Move offset if no selection
+                if (ViewModelMain.Current.ActiveSelection != null && ViewModelMain.Current.ActiveSelection.SelectedPoints.Count > 0) //Move offset if no selection
                 {
                     _currentSelection = ViewModelMain.Current.ActiveSelection.SelectedPoints.ToArray();
                 }

+ 35 - 15
PixiEditor/ViewModels/ViewModelMain.cs

@@ -24,6 +24,8 @@ namespace PixiEditor.ViewModels
 {
     internal class ViewModelMain : ViewModelBase
     {
+        private const string ConfirmationDialogMessage = "Document was modified. Do you want to save changes?";
+
         private double _mouseXonCanvas;
 
         private double _mouseYonCanvas;
@@ -292,7 +294,7 @@ namespace PixiEditor.ViewModels
             ConfirmationType result = ConfirmationType.No;
             if (_unsavedDocumentModified)
             {
-                result = ConfirmationDialog.Show("Document was modified. Do you want to save changes?");
+                result = ConfirmationDialog.Show(ConfirmationDialogMessage);
                 if (result == ConfirmationType.Yes) SaveDocument(null);
             }
 
@@ -322,21 +324,32 @@ namespace PixiEditor.ViewModels
             };
             if ((bool) dialog.ShowDialog())
             {
-                if (dialog.FileName.EndsWith(".png") || dialog.FileName.EndsWith(".jpeg") ||
-                    dialog.FileName.EndsWith(".jpg"))
-                    OpenFile(dialog.FileName);
-                else if (dialog.FileName.EndsWith(".pixi")) OpenDocument(dialog.FileName);
+                if (Importer.IsSupportedFile(dialog.FileName))
+                    Open(dialog.FileName);
                 RecenterZoombox = !RecenterZoombox;
             }
         }
 
         private void Open(string path)
         {
+            if (_unsavedDocumentModified)
+            {
+                var result = ConfirmationDialog.Show(ConfirmationDialogMessage);
+                if (result == ConfirmationType.Yes)
+                {
+                    SaveDocument(null);
+                }
+                else if (result == ConfirmationType.Canceled)
+                {
+                    return;
+                }
+            }
+
+            ResetProgramStateValues();
             if (path.EndsWith(".pixi"))
                 OpenDocument(path);
             else
                 OpenFile(path);
-            RecenterZoombox = !RecenterZoombox;
         }
 
         private void OpenDocument(string path)
@@ -348,7 +361,7 @@ namespace PixiEditor.ViewModels
 
         private void SaveDocument(object parameter)
         {
-            bool paramIsAsNew = parameter != null && parameter.ToString().ToLower() == "asnew";
+            bool paramIsAsNew = parameter != null && parameter.ToString()?.ToLower() == "asnew";
             if (paramIsAsNew || Exporter.SaveDocumentPath == null)
                 Exporter.SaveAsNewEditableFile(BitmapManager.ActiveDocument, !paramIsAsNew);
             else
@@ -366,8 +379,7 @@ namespace PixiEditor.ViewModels
 
         private void SelectColor(object parameter)
         {
-            if (!(parameter is Color)) throw new ArgumentException();
-            PrimaryColor = (Color) parameter;
+            PrimaryColor = parameter as Color? ?? throw new ArgumentException();
         }
 
         private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
@@ -405,7 +417,7 @@ namespace PixiEditor.ViewModels
 
         public void ClipCanvas(object parameter)
         {
-            if (BitmapManager.ActiveDocument != null) BitmapManager.ActiveDocument.ClipCanvas();
+            BitmapManager.ActiveDocument?.ClipCanvas();
         }
 
         public void Duplicate(object parameter)
@@ -632,16 +644,24 @@ namespace PixiEditor.ViewModels
 
             if (dialog.ShowDialog())
             {
-                NewDocument(dialog.FileWidth, dialog.FileHeight);
-                BitmapManager.ActiveDocument.ActiveLayer.LayerBitmap =
-                    Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
+                NewDocument(dialog.FileWidth, dialog.FileHeight, false);
+                BitmapManager.AddNewLayer("Image",Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight));
             }
         }
 
-        private void NewDocument(int width, int height)
+        private void NewDocument(int width, int height, bool addBaseLayer = true)
         {
             BitmapManager.ActiveDocument = new Document(width, height);
-            BitmapManager.AddNewLayer("Base Layer");
+            if(addBaseLayer)
+                BitmapManager.AddNewLayer("Base Layer");
+            ResetProgramStateValues();
+        }
+
+        /// <summary>
+        ///     Resets most variables and controller, so new documents can be handled.
+        /// </summary>
+        public void ResetProgramStateValues()
+        {
             BitmapManager.PreviewLayer = null;
             UndoManager.UndoStack.Clear();
             UndoManager.RedoStack.Clear();

+ 1 - 1
PixiEditor/Views/MainWindow.xaml

@@ -75,7 +75,7 @@
                     <MenuItem Header="_Open" InputGestureText="Ctrl+O" Command="{Binding OpenFileCommand}" />
                     <MenuItem Header="_Save" InputGestureText="Ctrl+S" Command="{Binding SaveDocumentCommand}" />
                     <MenuItem Header="_Save As..." InputGestureText="Ctrl+Shift+S"
-                              Command="{Binding SaveDocumentCommand}" />
+                              Command="{Binding SaveDocumentCommand}" CommandParameter="AsNew" />
                     <MenuItem Header="_Export" InputGestureText="Ctrl+Shift+Alt+S" Command="{Binding SaveFileCommand}" />
                 </MenuItem>
                 <MenuItem Header="_Edit">