Browse Source

Fixed cross-document copying

flabbet 9 months ago
parent
commit
eb364bf6e9

+ 1 - 0
src/PixiEditor.ChangeableDocument/Changeables/Document.cs

@@ -15,6 +15,7 @@ namespace PixiEditor.ChangeableDocument.Changeables;
 
 internal class Document : IChangeable, IReadOnlyDocument
 {
+    public Guid DocumentId { get; } = Guid.NewGuid();
     IReadOnlyNodeGraph IReadOnlyDocument.NodeGraph => NodeGraph;
     IReadOnlySelection IReadOnlyDocument.Selection => Selection;
     IReadOnlyAnimationData IReadOnlyDocument.AnimationData => AnimationData;

+ 1 - 0
src/PixiEditor.ChangeableDocument/Changeables/Interfaces/IReadOnlyDocument.cs

@@ -10,6 +10,7 @@ namespace PixiEditor.ChangeableDocument.Changeables.Interfaces;
 
 public interface IReadOnlyDocument : IDisposable
 {    
+    public Guid DocumentId { get; }
     /// <summary>
     /// The root folder of the document
     /// </summary>

+ 1 - 0
src/PixiEditor/Helpers/Constants/ClipboardDataFormats.cs

@@ -6,4 +6,5 @@ public static class ClipboardDataFormats
     public const string LayerIdList = "PixiEditor.LayerIdList";
     public const string PositionFormat = "PixiEditor.Position";
     public const string ImageSlashPng = "image/png";
+    public const string DocumentFormat = "PixiEditor.Document"; 
 }

+ 22 - 0
src/PixiEditor/Models/Controllers/ClipboardController.cs

@@ -116,6 +116,7 @@ internal static class ClipboardController
         byte[] layerIdsBytes = System.Text.Encoding.UTF8.GetBytes(layerIdsString);
 
         data.Set(ClipboardDataFormats.LayerIdList, layerIdsBytes);
+        data.Set(ClipboardDataFormats.DocumentFormat, document.Id);
 
         await Clipboard.SetDataObjectAsync(data);
     }
@@ -144,7 +145,13 @@ internal static class ClipboardController
     /// </summary>
     public static bool TryPaste(DocumentViewModel document, IEnumerable<IDataObject> data, bool pasteAsNew = false)
     {
+        Guid sourceDocument = GetSourceDocument(data); 
         Guid[] layerIds = GetLayerIds(data);
+        
+        if (sourceDocument != document.Id)
+        {
+            layerIds = [];
+        }
 
         bool hasPos = data.Any(x => x.Contains(ClipboardDataFormats.PositionFormat));
 
@@ -235,6 +242,21 @@ internal static class ClipboardController
 
         return [];
     }
+    
+    private static Guid GetSourceDocument(IEnumerable<IDataObject> data)
+    {
+        foreach (var dataObject in data)
+        {
+            if (dataObject.Contains(ClipboardDataFormats.DocumentFormat))
+            {
+                byte[] guidBytes = (byte[])dataObject.Get(ClipboardDataFormats.DocumentFormat);
+                string guidString = System.Text.Encoding.UTF8.GetString(guidBytes);
+                return Guid.Parse(guidString);
+            }
+        }
+
+        return Guid.Empty;
+    }
 
     /// <summary>
     ///     Pastes image from clipboard into new layer.

+ 1 - 0
src/PixiEditor/Models/Handlers/IDocument.cs

@@ -20,6 +20,7 @@ namespace PixiEditor.Models.Handlers;
 
 internal interface IDocument : IHandler
 {
+    public Guid Id { get; }
     public ObservableRangeCollection<PaletteColor> Palette { get; set; }
     public VecI SizeBindable { get; }
     public IStructureMemberHandler? SelectedStructureMember { get; }

+ 3 - 2
src/PixiEditor/ViewModels/Document/DocumentViewModel.cs

@@ -195,6 +195,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
     private VectorPath selectionPath = new VectorPath();
     public VectorPath SelectionPathBindable => selectionPath;
     public ObservableCollection<PaletteColor> Swatches { get; set; } = new();
+    public Guid Id => Internals.Tracker.Document.DocumentId;
     public ObservableRangeCollection<PaletteColor> Palette { get; set; } = new();
     public SnappingViewModel SnappingViewModel { get; }
     ISnappingHandler IDocument.SnappingHandler => SnappingViewModel;
@@ -620,7 +621,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
 
                     Renderer.RenderLayer(toPaintOn.DrawingSurface, layerVm.Id, ChunkResolution.Full,
                         AnimationDataViewModel.ActiveFrameTime);
-                    using Image snapshot = toPaintOn.DrawingSurface.Snapshot(bounds);
+                    using Image snapshot = toPaintOn.DrawingSurface.Snapshot(finalBounds);
                     output.DrawingSurface.Canvas.DrawImage(snapshot, 0, 0, paint);
                 });
             }
@@ -632,7 +633,7 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
         }
 
         output.DrawingSurface.Canvas.Restore();
-        return (output, bounds);
+        return (output, finalBounds);
     }
 
     /// <summary>