Sfoglia il codice sorgente

Added raw text drag n drop into palette viewer

CPKreuz 2 anni fa
parent
commit
76a30cd0da

+ 40 - 0
src/PixiEditor/Helpers/ColorHelper.cs

@@ -9,6 +9,9 @@ public class ColorHelper
     public static bool ParseAnyFormat(IDataObject data, [NotNullWhen(true)] out DrawingApi.Core.ColorsImpl.Color? result) => 
         ParseAnyFormat(((DataObject)data).GetText().Trim(), out result);
     
+    public static bool ParseAnyFormatList(IDataObject data, [NotNullWhen(true)] out List<DrawingApi.Core.ColorsImpl.Color> result) => 
+        ParseAnyFormatList(((DataObject)data).GetText().Trim(), out result);
+
     public static bool ParseAnyFormat(string value, [NotNullWhen(true)] out DrawingApi.Core.ColorsImpl.Color? result)
     {
         bool hex = Regex.IsMatch(value, "^#?([a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})$");
@@ -34,6 +37,43 @@ public class ColorHelper
 
         result = new DrawingApi.Core.ColorsImpl.Color(r, g, b, a);
         return true;
+    }
+    
+    public static bool ParseAnyFormatList(string value, [NotNullWhen(true)] out List<DrawingApi.Core.ColorsImpl.Color> result)
+    {
+        result = new List<DrawingApi.Core.ColorsImpl.Color>();
 
+        // Regex patterns for hex and RGB(A) formats
+        const string hexPattern = @"#?([a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})";
+        const string rgbaPattern = @"(?:rgba?\(?)? *(?<r>\d{1,3})(?:, *| +)(?<g>\d{1,3})(?:, *| +)(?<b>\d{1,3})(?:(?:, *| +)(?<a>\d{0,3}))?\)?";
+
+        // Combined pattern for both hex and RGB(A) formats
+        const string combinedPattern = $@"({hexPattern})|({rgbaPattern})";
+        var matches = Regex.Matches(value, combinedPattern);
+
+        if (matches.Count == 0)
+        {
+            return false;
+        }
+
+        foreach (Match match in matches)
+        {
+            if (Regex.IsMatch(match.Value, $"^{hexPattern}$"))
+            {
+                result.Add(DrawingApi.Core.ColorsImpl.Color.Parse(match.Value));
+            }
+            else if (match.Groups["r"].Success && match.Groups["g"].Success && match.Groups["b"].Success)
+            {
+                byte r = byte.Parse(match.Groups["r"].ValueSpan);
+                byte g = byte.Parse(match.Groups["g"].ValueSpan);
+                byte b = byte.Parse(match.Groups["b"].ValueSpan);
+                byte a = match.Groups["a"].Success ? byte.Parse(match.Groups["a"].ValueSpan) : (byte)255;
+
+                result.Add(new DrawingApi.Core.ColorsImpl.Color(r, g, b, a));
+            }
+        }
+
+        return result.Count > 0;
     }
+
 }

+ 2 - 0
src/PixiEditor/Views/MainWindow.xaml.cs

@@ -202,6 +202,7 @@ internal partial class MainWindow : Window
                 return;
             }
 
+            e.Effects = DragDropEffects.Copy;
             DataContext.ColorsSubViewModel.PrimaryColor = color.Value;
             return;
         }
@@ -222,6 +223,7 @@ internal partial class MainWindow : Window
             if (ColorHelper.ParseAnyFormat(e.Data, out _))
             {
                 DataContext.ActionDisplays[nameof(MainWindow_Drop)] = "Paste as primary color";
+                e.Effects = DragDropEffects.Copy;
                 return;
             }
             

+ 14 - 0
src/PixiEditor/Views/UserControls/Palettes/PaletteViewer.xaml.cs

@@ -149,6 +149,12 @@ internal partial class PaletteViewer : UserControl
             dragDropGrid.Visibility = Visibility.Visible;
             ViewModelMain.Current.ActionDisplays[nameof(PaletteViewer)] = "Import palette file";
         }
+        else if (ColorHelper.ParseAnyFormatList(e.Data, out var list))
+        {
+            e.Effects = DragDropEffects.Copy;
+            ViewModelMain.Current.ActionDisplays[nameof(PaletteViewer)] = list.Count > 1 ? "Import colors into palette" : "Import color into palette";
+            e.Handled = true;
+        }
     }
 
     private void Grid_PreviewDragLeave(object sender, DragEventArgs e)
@@ -163,6 +169,14 @@ internal partial class PaletteViewer : UserControl
         
         if (!IsSupportedFilePresent(e, out string filePath))
         {
+            if (!ColorHelper.ParseAnyFormatList(e.Data, out var colors))
+            {
+                return;
+            }
+            
+            e.Effects = DragDropEffects.Copy;
+            Colors.AddRange(colors.Where(x => !Colors.Contains(x)).ToList());
+            e.Handled = true;
             return;
         }