Browse Source

Layer now require max width and height, fixed clipboard stuff

flabbet 3 years ago
parent
commit
234ca17b7e

+ 3 - 5
PixiEditor/Helpers/Extensions/ParserHelpers.cs

@@ -35,17 +35,15 @@ namespace PixiEditor.Helpers.Extensions
             WpfObservableRangeCollection<Layer> layers = new();
             foreach (SerializableLayer slayer in document)
             {
-                layers.Add(slayer.ToLayer());
-                layers[^1].MaxHeight = document.Height;
-                layers[^1].MaxWidth = document.Width;
+                layers.Add(slayer.ToLayer(document.Width, document.Height));
             }
 
             return layers;
         }
 
-        public static Layer ToLayer(this SerializableLayer layer)
+        public static Layer ToLayer(this SerializableLayer layer, int maxWidth, int maxHeight)
         {
-            return new Layer(layer.Name, new Surface(layer.ToSKImage()))
+            return new Layer(layer.Name, new Surface(layer.ToSKImage()), maxWidth, maxHeight)
             {
                 Opacity = layer.Opacity,
                 IsVisible = layer.IsVisible,

+ 19 - 10
PixiEditor/Models/Controllers/ClipboardController.cs

@@ -111,34 +111,43 @@ namespace PixiEditor.Models.Controllers
         /// <summary>
         ///     Pastes image from clipboard into new layer.
         /// </summary>
-        public static void PasteFromClipboard()
+        public static void PasteFromClipboard(Document document)
         {
             IEnumerable<Layer> layers;
             try
             {
-                layers = GetLayersFromClipboard();
+                layers = GetLayersFromClipboard(document);
             }
             catch
             {
                 return;
             }
 
-            Document activeDocument = ViewModelMain.Current.BitmapManager.ActiveDocument;
-            int startIndex = activeDocument.Layers.Count;
+            int startIndex = document.Layers.Count;
+
+            int resizedCount = 0;
 
             foreach (var layer in layers)
             {
-                activeDocument.Layers.Add(layer);
+                if(layer.Width > document.Width || layer.Height > document.Height)
+                {
+                    document.ResizeCanvas(Math.Max(document.Width, layer.Width), Math.Max(document.Height, layer.Height), Enums.AnchorPoint.Left | Enums.AnchorPoint.Top);
+                    resizedCount++;
+                }
+
+                document.Layers.Add(layer);
             }
 
-            activeDocument.UndoManager.AddUndoChange(
+            document.UndoManager.AddUndoChange(
                 new Change(RemoveLayersProcess, new object[] { startIndex }, AddLayersProcess, new object[] { layers }) { DisposeProcess = DisposeProcess });
+
+            document.UndoManager.SquashUndoChanges(resizedCount + 1, "Paste from clipboard");
         }
 
         /// <summary>
         ///     Gets image from clipboard, supported PNG, Dib and Bitmap.
         /// </summary>
-        private static IEnumerable<Layer> GetLayersFromClipboard()
+        private static IEnumerable<Layer> GetLayersFromClipboard(Document document)
         {
             DataObject data = ClipboardHelper.TryGetDataObject();
             if (data == null)
@@ -172,7 +181,7 @@ namespace PixiEditor.Models.Controllers
             else */
             if (TryFromSingleImage(data, out Surface singleImage))
             {
-                yield return new Layer("Image", singleImage);
+                yield return new Layer("Image", singleImage, document.Width, document.Height);
             }
             else if (data.GetDataPresent(DataFormats.FileDrop))
             {
@@ -187,13 +196,13 @@ namespace PixiEditor.Models.Controllers
 
                     try
                     {
-                        layer = new(Path.GetFileName(path), Importer.ImportSurface(path));
+                        layer = new(Path.GetFileName(path), Importer.ImportSurface(path), document.Width, document.Height);
                     }
                     catch (CorruptedFileException)
                     {
                     }
 
-                    yield return layer ?? new($"Corrupt {path}");
+                    yield return layer ?? new($"Corrupt {path}", document.Width, document.Height);
                 }
             }
             else

+ 2 - 0
PixiEditor/Models/DataHolders/Document/Document.Constructors.cs

@@ -1,5 +1,6 @@
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
 using PixiEditor.ViewModels;
 using System;
 using System.Linq;
@@ -29,6 +30,7 @@ namespace PixiEditor.Models.DataHolders
             Layers.CollectionChanged += Layers_CollectionChanged;
             LayerStructure.Groups.CollectionChanged += Groups_CollectionChanged;
             LayerStructure.LayerStructureChanged += LayerStructure_LayerStructureChanged;
+            ActiveSelection = new Selection(Array.Empty<Coordinates>(), new PixelSize(Width, Height));
             DocumentSizeChanged += (sender, args) =>
             {
                 Renderer.Resize(args.NewWidth, args.NewHeight);

+ 2 - 4
PixiEditor/Models/DataHolders/Document/Document.Layers.cs

@@ -221,9 +221,7 @@ namespace PixiEditor.Models.DataHolders
             if (width <= 0 || height <= 0)
                 throw new ArgumentException("Dimensions must be greater than 0");
 
-            layer = bitmap == null ? new Layer(name, width, height) : new Layer(name, bitmap);
-            layer.MaxHeight = Height;
-            layer.MaxWidth = Width;
+            layer = bitmap == null ? new Layer(name, width, height, Width, Height) : new Layer(name, bitmap, Width, Height);
 
             Layers.Add(layer);
 
@@ -441,7 +439,7 @@ namespace PixiEditor.Models.DataHolders
 
             var groupParent = LayerStructure.GetGroupByLayer(layersToMerge[^1].GuidValue);
 
-            Layer placeholderLayer = new("_placeholder");
+            Layer placeholderLayer = new("_placeholder", Width, Height);
             Layers.Insert(index, placeholderLayer);
             LayerStructure.AssignParent(placeholderLayer.GuidValue, groupParent?.GroupGuid);
 

+ 1 - 5
PixiEditor/Models/DataHolders/Document/Document.Preview.cs

@@ -43,11 +43,7 @@ namespace PixiEditor.Models.DataHolders
 
         public void GeneratePreviewLayer()
         {
-            PreviewLayer = new Layer("_previewLayer")
-            {
-                MaxWidth = Width,
-                MaxHeight = Height
-            };
+            PreviewLayer = new Layer("_previewLayer", Width, Height);
         }
     }
 }

+ 1 - 1
PixiEditor/Models/DataHolders/Document/Document.cs

@@ -61,7 +61,7 @@ namespace PixiEditor.Models.DataHolders
             }
         }
 
-        private Selection selection = new Selection(Array.Empty<Coordinates>());
+        private Selection selection;
 
         public Selection ActiveSelection
         {

+ 2 - 2
PixiEditor/Models/DataHolders/Selection.cs

@@ -17,10 +17,10 @@ namespace PixiEditor.Models.DataHolders
         private readonly SKColor selectionBlue;
         private Layer selectionLayer;
 
-        public Selection(Coordinates[] selectedPoints)
+        public Selection(Coordinates[] selectedPoints, PixelSize maxSize)
         {
             SelectedPoints = new ObservableCollection<Coordinates>(selectedPoints);
-            SelectionLayer = new Layer("_selectionLayer");
+            SelectionLayer = new Layer("_selectionLayer", maxSize.Width, maxSize.Height);
             selectionBlue = new SKColor(142, 202, 255, 255);
         }
 

+ 10 - 6
PixiEditor/Models/Layers/Layer.cs

@@ -32,32 +32,38 @@ namespace PixiEditor.Models.Layers
 
         private string layerHighlightColor = "#666666";
 
-        public Layer(string name)
+        public Layer(string name, int maxWidth, int maxHeight)
         {
             Name = name;
             LayerBitmap = new Surface(1, 1);
             IsReset = true;
             Width = 1;
             Height = 1;
+            MaxWidth = maxWidth;
+            MaxHeight = maxHeight;
             GuidValue = Guid.NewGuid();
         }
 
-        public Layer(string name, int width, int height)
+        public Layer(string name, int width, int height, int maxWidth, int maxHeight)
         {
             Name = name;
             LayerBitmap = new Surface(width, height);
             IsReset = true;
             Width = width;
             Height = height;
+            MaxWidth = maxWidth;
+            MaxHeight = maxHeight;
             GuidValue = Guid.NewGuid();
         }
 
-        public Layer(string name, Surface layerBitmap)
+        public Layer(string name, Surface layerBitmap, int maxWidth, int maxHeight)
         {
             Name = name;
             LayerBitmap = layerBitmap;
             Width = layerBitmap.Width;
             Height = layerBitmap.Height;
+            MaxWidth = maxWidth;
+            MaxHeight = maxHeight;
             GuidValue = Guid.NewGuid();
         }
 
@@ -246,12 +252,10 @@ namespace PixiEditor.Models.Layers
         /// </summary>
         public Layer Clone(bool generateNewGuid = false)
         {
-            return new Layer(Name, new Surface(LayerBitmap))
+            return new Layer(Name, new Surface(LayerBitmap), MaxWidth, MaxHeight)
             {
                 IsVisible = IsVisible,
                 Offset = Offset,
-                MaxHeight = MaxHeight,
-                MaxWidth = MaxWidth,
                 Opacity = Opacity,
                 IsActive = IsActive,
                 IsRenaming = IsRenaming,

+ 1 - 3
PixiEditor/Models/Layers/LayerHelper.cs

@@ -66,11 +66,9 @@ namespace PixiEditor.Models.Layers
 
             Surface mergedBitmap = BitmapUtils.CombineLayers(combined, new Layer[] { thisLayer, otherLayer });
 
-            Layer mergedLayer = new Layer(newName, mergedBitmap)
+            Layer mergedLayer = new Layer(newName, mergedBitmap, documentSize.Width, documentSize.Height)
             {
                 Offset = new Thickness(combined.X, combined.Y, 0, 0),
-                MaxWidth = documentSize.Width,
-                MaxHeight = documentSize.Height
             };
 
             return mergedLayer;

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

@@ -88,7 +88,7 @@ namespace PixiEditor.Models.Tools.Tools
             cachedDocument ??= new Layer("_CombinedLayers", BitmapUtils.CombineLayers(
                 new Int32Rect(0, 0, document.Width, document.Height),
                 document.Layers,
-                document.LayerStructure));
+                document.LayerStructure), document.Width, document.Height);
         }
     }
 }

+ 1 - 3
PixiEditor/Models/Undo/StorageBasedChange.cs

@@ -153,14 +153,12 @@ namespace PixiEditor.Models.Undo
             {
                 UndoLayer storedLayer = StoredLayers[i];
                 var bitmap = Importer.LoadFromGZippedBytes(storedLayer.StoredPngLayerName);
-                layers[i] = new Layer(storedLayer.Name, bitmap)
+                layers[i] = new Layer(storedLayer.Name, bitmap, storedLayer.MaxWidth, storedLayer.MaxHeight)
                 {
                     Width = storedLayer.Width,
                     Height = storedLayer.Height,
                     Offset = new Thickness(storedLayer.OffsetX, storedLayer.OffsetY, 0, 0),
                     Opacity = storedLayer.Opacity,
-                    MaxWidth = storedLayer.MaxWidth,
-                    MaxHeight = storedLayer.MaxHeight,
                     IsVisible = storedLayer.IsVisible,
                     IsActive = storedLayer.IsActive,
                     LayerHighlightColor = storedLayer.LayerHighlightColor

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

@@ -45,7 +45,8 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void Paste(object parameter)
         {
-            ClipboardController.PasteFromClipboard();
+            if (Owner.BitmapManager.ActiveDocument == null) return;
+            ClipboardController.PasteFromClipboard(Owner.BitmapManager.ActiveDocument);
         }
 
         private bool CanPaste(object property)

+ 1 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -362,7 +362,7 @@ namespace PixiEditor.ViewModels
 
         private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
         {
-            BitmapManager.ActiveDocument.ActiveSelection = new Selection(Array.Empty<Coordinates>());
+            BitmapManager.ActiveDocument.ActiveSelection = new Selection(Array.Empty<Coordinates>(), new PixelSize(e.NewWidth, e.NewHeight));
             BitmapManager.ActiveDocument.ChangesSaved = false;
             BitmapManager.ActiveDocument.CenterViewportTrigger.Execute(this, new Size(BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height));
         }

+ 1 - 1
PixiEditor/Views/UserControls/Layers/LayerGroupControl.xaml.cs

@@ -201,7 +201,7 @@ namespace PixiEditor.Views.UserControls.Layers
         private void MoveGroupWithTempLayer(bool above, Models.DataHolders.Document document, Guid group, int indexOfReferenceLayer, bool putItInside) // ¯\_(ツ)_/¯
         {
             // The trick here is to insert a temp layer, assign group to it, then delete it.
-            Layer tempLayer = new("_temp");
+            Layer tempLayer = new("_temp", document.Width, document.Height);
             document.Layers.Insert(indexOfReferenceLayer, tempLayer);
 
             Guid? refGuid = putItInside ? GroupData?.GroupGuid : GroupData?.Parent?.GroupGuid;

+ 1 - 1
PixiEditor/Views/UserControls/Layers/ReferenceLayer.xaml.cs

@@ -37,7 +37,7 @@ namespace PixiEditor.Views.UserControls.Layers
             if (path != null)
             {
                 var bitmap = Importer.ImportSurface(path);
-                Layer = new Layer("_Reference Layer", bitmap);
+                Layer = new Layer("_Reference Layer", bitmap, bitmap.Width, bitmap.Height);
             }
         }