Browse Source

Added file search result for pasting as reference layer

CPKreuz 2 years ago
parent
commit
f29a733d01

+ 19 - 4
src/PixiEditor/Models/Commands/Search/FileSearchResult.cs

@@ -7,28 +7,43 @@ namespace PixiEditor.Models.Commands.Search;
 internal class FileSearchResult : SearchResult
 {
     private readonly DrawingImage icon;
+    private readonly bool asReferenceLayer;
 
     public string FilePath { get; }
 
-    public override string Text => $"...\\{Path.GetFileName(FilePath)}";
+    public override string Text => asReferenceLayer ? $"As reference: ...\\{Path.GetFileName(FilePath)}" : $"...\\{Path.GetFileName(FilePath)}";
 
     public override string Description => FilePath;
 
-    public override bool CanExecute => true;
+    public override bool CanExecute => !asReferenceLayer ||
+                CommandController.Current.Commands["PixiEditor.Layer.PasteReferenceLayerFromPath"].Methods.CanExecute(FilePath);
 
     public override ImageSource Icon => icon;
 
-    public FileSearchResult(string path)
+    public FileSearchResult(string path, bool asReferenceLayer = false)
     {
         FilePath = path;
         var drawing = new GeometryDrawing() { Brush = FileExtensionToColorConverter.GetBrush(FilePath) };
         var geometry = new RectangleGeometry(new(0, 0, 10, 10), 3, 3) { };
         drawing.Geometry = geometry;
         icon = new DrawingImage(drawing);
+        this.asReferenceLayer = asReferenceLayer;
     }
 
     public override void Execute()
     {
-        CommandController.Current.Commands["PixiEditor.File.OpenRecent"].Methods.Execute(FilePath);
+        if (!asReferenceLayer)
+        {
+            CommandController.Current.Commands["PixiEditor.File.OpenRecent"].Methods.Execute(FilePath);
+        }
+        else
+        {
+            var command = CommandController.Current.Commands["PixiEditor.Layer.PasteReferenceLayerFromPath"];
+            if (command.Methods.CanExecute(FilePath))
+            {
+                CommandController.Current.Commands["PixiEditor.Layer.PasteReferenceLayerFromPath"].Methods
+                    .Execute(FilePath);
+            }
+        }
     }
 }

+ 23 - 6
src/PixiEditor/Models/IO/Importer.cs

@@ -53,11 +53,11 @@ internal class Importer : NotifyableObject
         }
         catch (NotSupportedException)
         {
-            throw new CorruptedFileException();
+            throw new CorruptedFileException($"The file type '{Path.GetExtension(path)}' is not supported");
         }
         catch (FileFormatException)
         {
-            throw new CorruptedFileException();
+            throw new CorruptedFileException("The file appears to be corrupted");
         }
     }
 
@@ -77,9 +77,9 @@ internal class Importer : NotifyableObject
                 doc.FullFilePath = path;
                 return doc;
             }
-            catch (InvalidFileException)
+            catch (InvalidFileException e)
             {
-                throw new CorruptedFileException();
+                throw new CorruptedFileException("The given file seems to be corrupted or from a newer version of PixiEditor", e);
             }
         }
     }
@@ -100,13 +100,30 @@ internal class Importer : NotifyableObject
                 doc.FullFilePath = originalFilePath;
                 return doc;
             }
-            catch (InvalidFileException)
+            catch (InvalidFileException e)
             {
-                throw new CorruptedFileException();
+                throw new CorruptedFileException("The given file seems to be corrupted or from a newer version of PixiEditor", e);
             }
         }
     }
 
+    public static WriteableBitmap GetPreviewBitmap(string path)
+    {
+        if (!IsSupportedFile(path))
+        {
+            throw new ArgumentException($"The file type '{Path.GetExtension(path)}' is not supported");
+        }
+        
+        try
+        {
+            return Path.GetExtension(path) != ".pixi" ? ImportWriteableBitmap(path) : PixiParser.Deserialize(path).ToDocument().PreviewBitmap;
+        }
+        catch (InvalidFileException)
+        {
+            throw new CorruptedFileException();
+        }
+    }
+
     public static bool IsSupportedFile(string path)
     {
         return SupportedFilesHelper.IsSupportedFile(path);

+ 14 - 0
src/PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -397,6 +397,20 @@ internal class LayersViewModel : SubViewModel<ViewModelMain>
             surface.image.Size);
     }
     
+    [Command.Internal("PixiEditor.Layer.PasteReferenceLayerFromPath", CanExecute = "PixiEditor.Layer.ReferenceLayerDoesntExist")]
+    public void PasteReferenceLayer(string path)
+    {
+        var doc = Owner.DocumentManagerSubViewModel.ActiveDocument;
+
+        var bitmap = Importer.GetPreviewBitmap(path);
+        byte[] pixels = new byte[bitmap.PixelWidth * bitmap.PixelHeight * 4];
+        bitmap.CopyPixels(pixels, bitmap.PixelWidth * 4, 0);
+
+        doc.Operations.ImportReferenceLayer(
+            pixels.ToImmutableArray(),
+            new VecI(bitmap.PixelWidth, bitmap.PixelHeight));
+    }
+
     private string OpenReferenceLayerFilePicker()
     {
         var imagesFilter = new FileTypeDialogDataSet(FileTypeDialogDataSet.SetKind.Images).GetFormattedTypes();

+ 22 - 6
src/PixiEditor/Views/UserControls/CommandSearch/CommandSearchControlHelper.cs

@@ -103,13 +103,29 @@ internal static class CommandSearchControlHelper
             files = files.Where(x => x.Contains(name, StringComparison.OrdinalIgnoreCase));
         }
 
-        return files
-            .Select(static file => Path.GetFullPath(file))
-            .Select(path => new FileSearchResult(path)
+        var array = files as string[] ?? files.ToArray();
+        
+        if (array.Length != 1)
+        {
+            return array
+                .Select(static file => Path.GetFullPath(file))
+                .Select(path => new FileSearchResult(path)
+                {
+                    SearchTerm = name, Match = Match($".../{Path.GetFileName(path)}", name ?? "")
+                });
+        }
+        else if (array.Length < 1)
+        {
+            return ArraySegment<SearchResult>.Empty;
+        }
+        else
+        {
+            return new[]
             {
-                SearchTerm = name,
-                Match = Match($".../{Path.GetFileName(path)}", name ?? "")
-            });
+                new FileSearchResult(array[0]),
+                new FileSearchResult(array[0], true)
+            };
+        }
     }
 
     private static bool GetDirectory(string path, out string directory, out string file)