Browse Source

Palette dropping improvements

CPKreuz 2 years ago
parent
commit
c004cf754f

+ 9 - 3
src/PixiEditor/Views/UserControls/Layers/ReferenceLayer.xaml.cs

@@ -3,6 +3,7 @@ using System.Windows.Controls;
 using System.Windows.Input;
 using Microsoft.Win32;
 using PixiEditor.Models.Commands;
+using PixiEditor.Models.Commands.Commands;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.IO;
 using PixiEditor.ViewModels.SubViewModels.Document;
@@ -12,6 +13,8 @@ namespace PixiEditor.Views.UserControls.Layers;
 #nullable enable
 internal partial class ReferenceLayer : UserControl
 {
+    private Command command;
+    
     public static readonly DependencyProperty DocumentProperty =
         DependencyProperty.Register(nameof(Document), typeof(DocumentViewModel), typeof(ReferenceLayer), new(null));
 
@@ -23,11 +26,17 @@ internal partial class ReferenceLayer : UserControl
 
     public ReferenceLayer()
     {
+        command = CommandController.Current.Commands["PixiEditor.Layer.PasteReferenceLayer"];
         InitializeComponent();
     }
 
     private void ReferenceLayer_DragEnter(object sender, DragEventArgs e)
     {
+        if (!command.Methods.CanExecute(e.Data))
+        {
+            return;
+        }
+
         ViewModelMain.Current.ActionDisplays[nameof(ReferenceLayer_Drop)] = "Import as reference layer";
         e.Handled = true;
     }
@@ -35,13 +44,10 @@ internal partial class ReferenceLayer : UserControl
     private void ReferenceLayer_DragLeave(object sender, DragEventArgs e)
     {
         ViewModelMain.Current.ActionDisplays[nameof(ReferenceLayer_Drop)] = null;
-        e.Handled = true;
     }
 
     private void ReferenceLayer_Drop(object sender, DragEventArgs e)
     {
-        var command = CommandController.Current.Commands["PixiEditor.Layer.PasteReferenceLayer"];
-
         if (!command.Methods.CanExecute(e.Data))
         {
             return;

+ 17 - 8
src/PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml.cs

@@ -144,35 +144,44 @@ internal partial class PaletteViewer : UserControl
 
     private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
     {
-        if (IsPalFilePresent(e, out _))
+        if (IsSupportedFilePresent(e, out _))
         {
             dragDropGrid.Visibility = Visibility.Visible;
+            ViewModelMain.Current.ActionDisplays[nameof(PaletteViewer)] = "Import palette file";
         }
     }
 
     private void Grid_PreviewDragLeave(object sender, DragEventArgs e)
     {
         dragDropGrid.Visibility = Visibility.Hidden;
+        ViewModelMain.Current.ActionDisplays[nameof(PaletteViewer)] = null;
     }
 
     private async void Grid_Drop(object sender, DragEventArgs e)
     {
-        if (IsPalFilePresent(e, out string filePath))
+        if (!IsSupportedFilePresent(e, out string filePath))
         {
-            await ImportPalette(filePath);
-            dragDropGrid.Visibility = Visibility.Hidden;
+            return;
         }
+
+        await ImportPalette(filePath);
+        dragDropGrid.Visibility = Visibility.Hidden;
     }
 
-    private bool IsPalFilePresent(DragEventArgs e, out string filePath)
+    private bool IsSupportedFilePresent(DragEventArgs e, out string filePath)
     {
         if (e.Data.GetDataPresent(DataFormats.FileDrop))
         {
             string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
-            if (files != null && files.Length > 0 && files[0].EndsWith(".pal"))
+            if (files is { Length: > 0 })
             {
-                filePath = files[0];
-                return true;
+                var fileName = files[0];
+                var foundParser = FileParsers.FirstOrDefault(x => x.SupportedFileExtensions.Contains(Path.GetExtension(fileName)));
+                if (foundParser != null)
+                {
+                    filePath = fileName;
+                    return true;
+                }
             }
         }