Browse Source

Drag drop color palette

flabbet 3 years ago
parent
commit
f654755650

+ 3 - 2
PixiEditor/Views/UserControls/Palette.xaml

@@ -59,11 +59,12 @@
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                     <DataTemplate>
-                        <local:PaletteColor Color="{Binding}" Margin="0 5 5 5"
+                        <local:PaletteColor AllowDrop="True" Color="{Binding}" Margin="0 5 5 5" MouseMove="PaletteColor_MouseMove"
+                                            Drop="PaletteColor_Drop"
                                             AssociatedKey="{Binding Path=(ItemsControl.AlternationIndex), 
                 RelativeSource={RelativeSource TemplatedParent}, Converter={converters:IndexToAssociatedKeyConverter}}">
                             <b:Interaction.Triggers>
-                                <b:EventTrigger EventName="MouseDown">
+                                <b:EventTrigger EventName="MouseUp">
                                     <b:InvokeCommandAction
                                     Command="{Binding SelectColorCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Palette}}}"
                                     CommandParameter="{Binding}" />

+ 27 - 1
PixiEditor/Views/UserControls/Palette.xaml.cs

@@ -14,6 +14,8 @@ namespace PixiEditor.Views.UserControls
     /// </summary>
     public partial class Palette : UserControl
     {
+        public const string PaletteColorDaoFormat = "PixiEditor.PaletteColor";
+
         public static readonly DependencyProperty ColorsProperty = DependencyProperty.Register(
             "Colors", typeof(ObservableCollection<SKColor>), typeof(Palette));
 
@@ -118,9 +120,33 @@ namespace PixiEditor.Views.UserControls
             return false;
         }
 
-        private void PaletteColor_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
+        private void PaletteColor_MouseMove(object sender, MouseEventArgs e)
         {
+            PaletteColor color = sender as PaletteColor;
+            if (color != null && e.LeftButton == MouseButtonState.Pressed) 
+            {
+                DataObject data = new DataObject();
+                data.SetData(PaletteColorDaoFormat, color.Color.ToString());
+                DragDrop.DoDragDrop(color, data, DragDropEffects.Move);
+                e.Handled = true;
+            }
+        }
 
+        private void PaletteColor_Drop(object sender, DragEventArgs e)
+        {
+            if(e.Data.GetDataPresent(PaletteColorDaoFormat))
+            {
+                string data = (string)e.Data.GetData(PaletteColorDaoFormat);
+                SKColor color = SKColor.Parse(data);
+                if(Colors.Contains(color))
+                {
+                    PaletteColor paletteColor = sender as PaletteColor;
+                    int currIndex = Colors.IndexOf(color);
+                    int newIndex = Colors.IndexOf(paletteColor.Color);
+                    Colors.RemoveAt(currIndex);
+                    Colors.Insert(newIndex, color);
+                }
+            }
         }
     }
 }