Browse Source

Palette is mostly finished

flabbet 3 years ago
parent
commit
44285ce5f2

+ 0 - 1
PixiEditor/PixiEditor.csproj

@@ -275,5 +275,4 @@
 			<LastGenOutput>Settings.Designer.cs</LastGenOutput>
 		</None>
 	</ItemGroup>
-
 </Project>

+ 12 - 0
PixiEditor/ViewModels/SubViewModels/Main/ColorsViewModel.cs

@@ -12,6 +12,8 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public RelayCommand RemoveSwatchCommand { get; set; }
 
+        public RelayCommand<int> SelectPaletteColorCommand { get; set; }
+
         private SKColor primaryColor = SKColors.Black;
 
         public SKColor PrimaryColor // Primary color, hooked with left mouse button
@@ -49,6 +51,16 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             SelectColorCommand = new RelayCommand(SelectColor);
             RemoveSwatchCommand = new RelayCommand(RemoveSwatch);
             SwapColorsCommand = new RelayCommand(SwapColors);
+            SelectPaletteColorCommand = new RelayCommand<int>(SelectPaletteColor);
+        }
+
+        private void SelectPaletteColor(int number)
+        {
+            var document = Owner.BitmapManager.ActiveDocument;
+            if(document.Palette != null && document.Palette.Count >= number)
+            {
+                PrimaryColor = document.Palette[number - 1];
+            }
         }
 
         public void SwapColors(object parameter)

+ 9 - 0
PixiEditor/ViewModels/ViewModelMain.cs

@@ -209,6 +209,15 @@ namespace PixiEditor.ViewModels
                         "View",
                         new Shortcut(Key.OemTilde, ViewportSubViewModel.ToggleGridLinesCommand, "Toggle gridlines", modifier: ModifierKeys.Control)));
 
+            Shortcut[] colorShortcuts = new Shortcut[9];
+            for (int i = 0; i < colorShortcuts.Length; i++)
+            {
+                //34 is a D1 key integer value
+                colorShortcuts[i] = new Shortcut((Key)34 + i, ColorsSubViewModel.SelectPaletteColorCommand, i);
+            }
+
+            ShortcutController.ShortcutGroups.Add(new ShortcutGroup("Palette Colors", colorShortcuts));
+
             MiscSubViewModel = services.GetService<MiscViewModel>();
 
             // Add F1 shortcut after MiscSubViewModel is constructed

+ 2 - 1
PixiEditor/Views/MainWindow.xaml

@@ -343,7 +343,8 @@
                                     <avalondock:LayoutAnchorable ContentId="palette" Title="Palette" CanHide="False"
                                                                  CanClose="False" CanAutoHide="False"
                                                                  CanDockAsTabbedDocument="False" CanFloat="True">
-                                        <usercontrols:Palette Colors="{Binding BitmapManager.ActiveDocument.Palette}"/>
+                                        <usercontrols:Palette Colors="{Binding BitmapManager.ActiveDocument.Palette}"
+                                                              SelectColorCommand="{Binding ColorsSubViewModel.SelectColorCommand}"/>
                                     </avalondock:LayoutAnchorable>
                                     <avalondock:LayoutAnchorable ContentId="swatches" Title="Swatches" CanHide="False"
                                                                  CanClose="False" CanAutoHide="False"

+ 8 - 2
PixiEditor/Views/MainWindow.xaml.cs

@@ -1,6 +1,7 @@
 using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.IO;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels;
 using PixiEditor.Views.Dialogs;
@@ -179,8 +180,13 @@ namespace PixiEditor
             if (e.Data.GetDataPresent(DataFormats.FileDrop))
             {
                 string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
-
-                DataContext.FileSubViewModel.Open(files[0]);
+                if(files != null && files.Length > 0)
+                {
+                    if (Importer.IsSupportedFile(files[0]))
+                    {
+                        DataContext.FileSubViewModel.Open(files[0]);
+                    }
+                }
             }
         }
     }

+ 35 - 11
PixiEditor/Views/UserControls/Palette.xaml

@@ -8,20 +8,37 @@
              xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
              mc:Ignorable="d" 
              d:DesignHeight="450" d:DesignWidth="300" Name="paletteControl">
-    <Grid>
+    <Grid AllowDrop="True" PreviewDragEnter="Grid_PreviewDragEnter" PreviewDragLeave="Grid_PreviewDragLeave"
+          Drop="Grid_Drop">
         <Grid.RowDefinitions>
-            <RowDefinition Height="35"/>
+            <RowDefinition Height="37.5"/>
+            <RowDefinition Height="5"/>
             <RowDefinition Height="*"/>
         </Grid.RowDefinitions>
-        <StackPanel Orientation="Vertical" Grid.Row="0">
-            <StackPanel Orientation="Horizontal">
-                <local:PaletteColorAdder Colors="{Binding ElementName=paletteControl, Path=Colors}"/>
-                <Button Style="{StaticResource DarkRoundButton}" Content="Import Palette" Click="ImportPalette_OnClick"/>
-                <Button Style="{StaticResource DarkRoundButton}" Content="Save Palette" Click="SavePalette_OnClick"/>
-            </StackPanel>
-            <Separator/>
+        <StackPanel Orientation="Vertical" Grid.Row="0" Background="{StaticResource MainColor}">
+            <DockPanel VerticalAlignment="Center" Margin="0 5 0 0">
+                <local:PaletteColorAdder DockPanel.Dock="Left" Margin="5 0 0 0" Colors="{Binding ElementName=paletteControl, Path=Colors}"/>
+                <StackPanel Margin="0, 0, 5, 0" HorizontalAlignment="Right" Width="55" VerticalAlignment="Center" Orientation="Horizontal">
+                <Button Margin="0, 0, 5, 0" Style="{StaticResource ToolButtonStyle}" 
+                Cursor="Hand" Height="24" Width="24"  ToolTip="Load Palette" Click="ImportPalette_OnClick">
+                    <Button.Background>
+                        <ImageBrush ImageSource="/Images/Load.png"/>
+                    </Button.Background>
+                </Button>
+                <Button Height="24" Width="24" Margin="0" Style="{StaticResource ToolButtonStyle}" 
+                Cursor="Hand" ToolTip="Save Palette" Click="SavePalette_OnClick">
+                    <Button.Background>
+                        <ImageBrush ImageSource="/Images/Save.png"/>
+                    </Button.Background>
+                </Button>
+                </StackPanel>
+            </DockPanel>
     </StackPanel>
-        <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
+        <Separator Grid.Row="1" Margin="0, 0, 0, 0" BorderBrush="{StaticResource DarkerAccentColor}" BorderThickness="2" />
+        <Grid Visibility="Hidden" Background="{StaticResource AccentColor}" Opacity="0.7" Grid.Row="2" Name="dragDropGrid">
+            <TextBlock Text="Drop palette here" Foreground="White" FontSize="32" HorizontalAlignment="Center" VerticalAlignment="Center"/>
+        </Grid>
+        <ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
            <ItemsControl ItemsSource="{Binding Colors, ElementName=paletteControl}">
                <d:ItemsControl.ItemsSource>
                    <x:Array Type="{x:Type Color}">
@@ -43,7 +60,14 @@
                <ItemsControl.ItemTemplate>
                     <DataTemplate>
                         <local:PaletteColor Color="{Binding}" Margin="0 5 5 5">
-                                    <local:PaletteColor.ContextMenu>
+                            <b:Interaction.Triggers>
+                                <b:EventTrigger EventName="MouseDown">
+                                    <b:InvokeCommandAction
+                                    Command="{Binding SelectColorCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Palette}}}"
+                                    CommandParameter="{Binding}" />
+                                </b:EventTrigger>
+                            </b:Interaction.Triggers>
+                            <local:PaletteColor.ContextMenu>
                                         <ContextMenu>
                                             <MenuItem Header="Remove" Foreground="White"
                                               Click="RemoveColorMenuItem_OnClick"

+ 62 - 4
PixiEditor/Views/UserControls/Palette.xaml.cs

@@ -1,6 +1,7 @@
 using System.Linq;
 using System.Windows;
 using System.Windows.Controls;
+using System.Windows.Input;
 using Microsoft.Win32;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.IO.JascPalFile;
@@ -22,6 +23,16 @@ namespace PixiEditor.Views.UserControls
             set { SetValue(ColorsProperty, value); }
         }
 
+        public ICommand SelectColorCommand
+        {
+            get { return (ICommand)GetValue(SelectColorCommandProperty); }
+            set { SetValue(SelectColorCommandProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for SelectColorCommand.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty SelectColorCommandProperty =
+            DependencyProperty.Register("SelectColorCommand", typeof(ICommand), typeof(Palette));
+
         public Palette()
         {
             InitializeComponent();
@@ -45,13 +56,17 @@ namespace PixiEditor.Views.UserControls
             };
             if (openFileDialog.ShowDialog() == true)
             {
-                string fileName = openFileDialog.FileName;
-                var jascData = JascFileParser.Parse(fileName);
-                Colors.Clear();
-                Colors.AddRange(jascData.Colors);
+                ImportPallete(openFileDialog.FileName);
             }
         }
 
+        private void ImportPallete(string fileName)
+        {
+            var jascData = JascFileParser.Parse(fileName);
+            Colors.Clear();
+            Colors.AddRange(jascData.Colors);
+        }
+
         private void SavePalette_OnClick(object sender, RoutedEventArgs e)
         {
             SaveFileDialog saveFileDialog = new SaveFileDialog
@@ -64,5 +79,48 @@ namespace PixiEditor.Views.UserControls
                 JascFileParser.Save(fileName, new JascFileData(Colors.ToArray()));
             }
         }
+
+        private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
+        {
+            if(IsPalFilePresent(e, out _))
+            {
+                dragDropGrid.Visibility = Visibility.Visible;
+            }
+        }
+
+        private void Grid_PreviewDragLeave(object sender, DragEventArgs e)
+        {
+            dragDropGrid.Visibility = Visibility.Hidden;
+        }
+
+        private void Grid_Drop(object sender, DragEventArgs e)
+        {
+            if(IsPalFilePresent(e, out string filePath))
+            {
+                ImportPallete(filePath);
+                dragDropGrid.Visibility = Visibility.Hidden;
+            }
+        }
+
+        private bool IsPalFilePresent(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"))
+                {
+                    filePath = files[0];
+                    return true;
+                }
+            }
+
+            filePath = null;
+            return false;
+        }
+
+        private void PaletteColor_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
+        {
+
+        }
     }
 }

+ 3 - 1
PixiEditor/Views/UserControls/PaletteColorAdder.xaml

@@ -13,7 +13,9 @@
             SelectedColor="{Binding SelectedColor, ElementName=paletteColorAdder, Mode=TwoWay}"
             Style="{StaticResource DefaultColorPickerStyle}" Margin="0 0 10 0"
             ShowAlpha="False"/>
-        <Button Name="AddButton" Style="{StaticResource ToolButtonStyle}" Cursor="Hand" Width="24" Height="24" Click="Button_Click">
+        <Button Name="AddButton" Margin="0" Width="24" Height="24" 
+                Style="{StaticResource ToolButtonStyle}" 
+                Cursor="Hand"  Click="Button_Click">
             <Button.Background>
                 <ImageBrush ImageSource="/Images/Plus-square.png"/>
             </Button.Background>

+ 19 - 4
PixiEditor/Views/UserControls/PaletteColorAdder.xaml.cs

@@ -28,8 +28,6 @@ namespace PixiEditor.Views.UserControls
             set { SetValue(ColorsProperty, value); }
         }
 
-
-
         public Color SelectedColor
         {
             get { return (Color)GetValue(SelectedColorProperty); }
@@ -44,8 +42,25 @@ namespace PixiEditor.Views.UserControls
 
         // Using a DependencyProperty as the backing store for Colors.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty ColorsProperty =
-            DependencyProperty.Register("Colors", typeof(ObservableCollection<SKColor>), typeof(PaletteColorAdder), new PropertyMetadata(default(ObservableCollection<SKColor>)));
+            DependencyProperty.Register("Colors", typeof(ObservableCollection<SKColor>), typeof(PaletteColorAdder), new PropertyMetadata(default(ObservableCollection<SKColor>), OnColorsChanged));
+
+        private static void OnColorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        {
+            PaletteColorAdder adder = (PaletteColorAdder)d;
+            if (e.NewValue != null)
+            {
+                adder.Colors.CollectionChanged += adder.Colors_CollectionChanged;
+            }
+            else if(e.OldValue != null)
+            {
+                adder.Colors.CollectionChanged -= adder.Colors_CollectionChanged;
+            }
+        }
 
+        private void Colors_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
+        {
+            AddButton.IsEnabled = !Colors.Contains(ToSKColor(SelectedColor));
+        }
 
         public PaletteColorAdder()
         {
@@ -65,6 +80,6 @@ namespace PixiEditor.Views.UserControls
         private void PortableColorPicker_ColorChanged(object sender, RoutedEventArgs e) => 
             AddButton.IsEnabled = !Colors.Contains(ToSKColor(SelectedColor));
 
-        private SKColor ToSKColor(Color color) => new SKColor(color.R, color.G, color.B, color.A);
+        private static SKColor ToSKColor(Color color) => new SKColor(color.R, color.G, color.B, color.A);
     }
 }