Browse Source

Changed look of popups and implemented resize document popup

-Added label to New File popup
-Disabled text boxes in open file popup if path is not correct
-Disabled popups show taskbar
-Popups are not topmost windows
flabbet 5 years ago
parent
commit
860864eb7c

+ 34 - 16
PixiEditor/Models/DataHolders/Document.cs

@@ -12,6 +12,8 @@ namespace PixiEditor.Models.DataHolders
     [Serializable]
     public class Document : NotifyableObject
     {
+        public event EventHandler<DocumentSizeChangedEventArgs> DocumentSizeChanged;
+
         private int _width;
         public int Width
         {
@@ -60,28 +62,28 @@ namespace PixiEditor.Models.DataHolders
             Height = height;
         }
 
-        public Document DeepClone()
-        {
-            Document doc = new Document(Width, Height)
-            {
-                Layers = new ObservableCollection<Layer>(Layers.Select(x => new Layer(x.LayerBitmap.Clone()) 
-                {
-                    Name = x.Name,
-                    Width = x.Width,
-                    Height = x.Height,
-                    IsActive = x.IsActive,
-                    IsVisible = x.IsVisible
-                })),
-            };
-            return doc;
-        }
-
         public void Crop(int x, int y, int width, int height)
         {
             object[] reverseArgs = new object[] { x, y, Width, Height, width, height};
             CropDocument(x, y, width, height);
             UndoManager.AddUndoChange(new Change("BitmapManager.ActiveDocument", ReverseCrop, 
                 reverseArgs, this, "Crop document"));
+            DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(Width, Height, width, height));
+        }
+
+        public void Resize(int newWidth, int newHeight)
+        {
+            int oldWidth = Width;
+            int oldHeight = Height;
+            for (int i = 0; i < Layers.Count; i++)
+            {
+                Layers[i].LayerBitmap = Layers[i].LayerBitmap.Resize(newWidth, newHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
+                Layers[i].Width = newWidth;
+                Layers[i].Height = newHeight;
+            }
+            Height = newHeight;
+            Width = newWidth;
+            DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(oldWidth, oldHeight, newWidth, newHeight));
         }
 
         private void CropDocument(int x, int y, int width, int height)
@@ -175,4 +177,20 @@ namespace PixiEditor.Models.DataHolders
             return biggestPixels;
         }
     }
+
+    public class DocumentSizeChangedEventArgs
+    {
+        public int OldWidth { get; set; }
+        public int OldHeight{ get; set; }
+        public int NewWidth { get; set; }
+        public int NewHeight { get; set; }
+
+        public DocumentSizeChangedEventArgs(int oldWidth, int oldHeight, int newWidth, int newHeight)
+        {
+            OldWidth = oldWidth;
+            OldHeight = oldHeight;
+            NewWidth = newWidth;
+            NewHeight = newHeight;
+        }
+    }
 }

+ 34 - 1
PixiEditor/Models/Dialogs/ResizeDocumentDialog.cs

@@ -7,10 +7,43 @@ namespace PixiEditor.Models.Dialogs
 {
     public class ResizeDocumentDialog : CustomDialog
     {
+
+        private int _width;
+
+        public int Width
+        {
+            get { return _width; }
+            set { if (_width != value) { _width = value; RaisePropertyChanged("Width"); } }
+        }
+
+
+        private int _height;
+
+        public int Height
+        {
+            get { return _height; }
+            set { if (_height != value) { _height = value; RaisePropertyChanged("Height"); } }
+        }
+
+        public ResizeDocumentDialog(int currentWidth, int currentHeight)
+        {
+            Width = currentWidth;
+            Height = currentHeight;
+        }
+
         public override bool ShowDialog()
         {
-            ResizeDocumentPopup popup = new ResizeDocumentPopup();
+            ResizeDocumentPopup popup = new ResizeDocumentPopup(); 
+            ResizeDocumentPopup result = popup.DataContext as ResizeDocumentPopup;
+            result.NewHeight = Height;
+            result.NewWidth = Width;
+
             popup.ShowDialog();
+            if (popup.DialogResult == true)
+            {
+                Width = result.NewWidth;
+                Height = result.NewHeight;
+            }
             return (bool)popup.DialogResult;
         }
     }

+ 0 - 9
PixiEditor/Models/Layers/LayerGenerator.cs

@@ -15,15 +15,6 @@ namespace PixiEditor.Models.Layers
             return new Layer(GenerateBitmap(imageWidth, imageHeight));
         }
 
-        public static LightLayer GenerateWithByteArray(int width, int height)
-        {
-            WriteableBitmap bitmap = GenerateBitmap(width, height);
-            bitmap.Lock();
-            byte[] byteArray = bitmap.ToByteArray();
-            bitmap.Unlock();
-            return new LightLayer(byteArray, height, width);
-        }
-
         /// <summary>
         /// Generates bitmap ready to work with
         /// </summary>

+ 0 - 48
PixiEditor/Models/Layers/LightLayer.cs

@@ -1,48 +0,0 @@
-using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
-using System;
-
-namespace PixiEditor.Models.Layers
-{
-    [Serializable]
-    public class LightLayer : BasicLayer
-    {
-        private byte[] _layerBytes;
-
-        public byte[] LayerBytes
-        {
-            get => _layerBytes;
-            set
-            {
-                _layerBytes = value;
-                RaisePropertyChanged("LayerBytes");
-            }
-        }
-
-        public LightLayer(int width, int height)
-        {
-            LightLayer layer = LayerGenerator.GenerateWithByteArray(width, height);
-            LayerBytes = layer.LayerBytes;
-            Width = width;
-            Height = height;
-        }
-
-        public LightLayer(byte[] layerBytes, int height, int width)
-        {
-            LayerBytes = layerBytes;
-            Width = height;
-            Height = width;
-        }
-
-        public LightLayer()
-        {
-
-        }
-
-        public static LightLayer Deserialize(object value)
-        {
-            return JsonConvert.DeserializeObject<LightLayer>(((JObject)value).ToString());
-        }
-
-    }
-}

+ 20 - 4
PixiEditor/ViewModels/ViewModelMain.cs

@@ -221,6 +221,8 @@ namespace PixiEditor.ViewModels
                     new Shortcut(Key.J, DuplicateCommand, modifier: ModifierKeys.Control),
                     new Shortcut(Key.X, CutCommand, modifier: ModifierKeys.Control),
                     new Shortcut(Key.Delete, DeletePixelsCommand),
+                    new Shortcut(Key.I, OpenResizePopupCommand, modifier: ModifierKeys.Control | ModifierKeys.Shift),
+                    new Shortcut(Key.C, OpenResizePopupCommand, "canvas" ,ModifierKeys.Control | ModifierKeys.Shift),
                 }
             };
             UndoManager.SetMainRoot(this);
@@ -230,11 +232,25 @@ namespace PixiEditor.ViewModels
             Current = this;
         }
 
+        private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
+        {
+            ActiveSelection = new Selection(Array.Empty<Coordinates>());
+        }
+
         private void OpenResizePopup(object parameter)
         {
-            ResizeDocumentDialog dialog = new ResizeDocumentDialog();
-            dialog.ShowDialog();
-            Console.WriteLine();
+            ResizeDocumentDialog dialog = new ResizeDocumentDialog(BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height);
+            if (dialog.ShowDialog())
+            {
+                if ((string)parameter == "canvas")
+                {
+                    //Todo resize canvas
+                }
+                else
+                {
+                    BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
+                }
+            }           
         }
 
         private void DeletePixels(object parameter)
@@ -248,7 +264,6 @@ namespace PixiEditor.ViewModels
             if (BitmapManager.ActiveDocument != null)
             {
                 BitmapManager.ActiveDocument.ClipCanvas();
-                ActiveSelection = new Selection(Array.Empty<Coordinates>());
             }
         }
 
@@ -563,6 +578,7 @@ namespace PixiEditor.ViewModels
             UndoManager.UndoStack.Clear();
             UndoManager.RedoStack.Clear();
             ActiveSelection = new Selection(Array.Empty<Coordinates>());
+            BitmapManager.ActiveDocument.DocumentSizeChanged += ActiveDocument_DocumentSizeChanged;
         }
 
         /// <summary>

+ 13 - 13
PixiEditor/Views/ImportFilePopup.xaml

@@ -9,7 +9,7 @@
         xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
         xmlns:helpers="clr-namespace:PixiEditor.Helpers"
         mc:Ignorable="d"
-        Title="ImportFilePopup" Height="350" Width="300" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Name="importFilePopup" DataContext="{DynamicResource ImportFilePopupViewModel}">
+        Title="ImportFilePopup" Topmost="True" ShowInTaskbar="False" Height="350" Width="300" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Name="importFilePopup" DataContext="{DynamicResource ImportFilePopupViewModel}">
     <Window.Resources>
         <vm:ImportFilePopupViewModel x:Key="ImportFilePopupViewModel"/>
         <helpers:ToolSizeToIntConverter x:Key="ToolSizeToIntConverter"/>
@@ -33,27 +33,27 @@
             </Button>
         </Grid>
         <StackPanel Grid.Row="1" >
-            <Label Height="40" Width="120" VerticalAlignment="Top" Content="Import" Foreground="Snow" HorizontalContentAlignment="Center" FontSize="24" Margin="0,10,0,0"/>
-        <StackPanel Background="{StaticResource MainColor}" Height="85" Width="200" Margin="0,10,0,0">
+            <Label Height="40" Width="120" VerticalAlignment="Top" Content="Open" Foreground="Snow" HorizontalContentAlignment="Center" FontSize="24" Margin="0,10,0,0"/>
+                <Button Grid.Row="1" BorderThickness="1" Foreground="Snow" Height="40" Width="160" Margin="0,30,0,0" Content="File Path" Background="#303030" BorderBrush="{Binding PathButtonBorder}" Command="{Binding ChoosePathCommand}"/>
+                <StackPanel Background="{StaticResource MainColor}" Height="85" Width="200" Margin="0,30,0,0">
                 <DockPanel Height="20" Margin="0,15,0,0" Width="150">
                     <TextBlock Width="40" HorizontalAlignment="Left" Text="Width:" Foreground="Snow" FontSize="14"/>
-                    <TextBox Style="{StaticResource DarkTextBoxStyle}" Width="75" Margin="-10,0,0,0" Text="{Binding ImportWidth, Converter={StaticResource ToolSizeToIntConverter}, Mode=TwoWay}">
+                    <TextBox IsEnabled="{Binding PathIsCorrect}" Style="{StaticResource DarkTextBoxStyle}" Width="75" Margin="-10,0,0,0" Text="{Binding ImportWidth, Converter={StaticResource ToolSizeToIntConverter}, Mode=TwoWay}">
                         <i:Interaction.Behaviors>
                                 <behaviors:TextBoxFocusBehavior FillSize="True"/>
                             </i:Interaction.Behaviors>
                     </TextBox>
                 </DockPanel>
-                <DockPanel Height="20" Margin="0,15,0,0" Width="150">
-                    <TextBlock Width="45" HorizontalAlignment="Left" Text="Height:" Foreground="Snow" FontSize="14"/>
-                    <TextBox Style="{StaticResource DarkTextBoxStyle}" Width="75" Margin="-15,0,0,0" Text="{Binding ImportHeight, Converter={StaticResource ToolSizeToIntConverter}, Mode=TwoWay}">
-                        <i:Interaction.Behaviors>
+         
+                    <DockPanel Height="20" Margin="0,15,0,0" Width="150">
+                        <TextBlock Width="45" HorizontalAlignment="Left" Text="Height:" Foreground="Snow" FontSize="14"/>
+                        <TextBox IsEnabled="{Binding PathIsCorrect}" Style="{StaticResource DarkTextBoxStyle}" Width="75" Margin="-15,0,0,0" Text="{Binding ImportHeight, Converter={StaticResource ToolSizeToIntConverter}, Mode=TwoWay}">
+                            <i:Interaction.Behaviors>
                                 <behaviors:TextBoxFocusBehavior FillSize="True"/>
                             </i:Interaction.Behaviors>
-                    </TextBox>
-                </DockPanel>
-
-            </StackPanel>
-            <Button Grid.Row="1" BorderThickness="1" Foreground="Snow" Height="40" Width="160" Margin="0,30,0,0" Content="File Path" Background="#303030" BorderBrush="{Binding PathButtonBorder}" Command="{Binding ChoosePathCommand}"/>
+                        </TextBox>
+                    </DockPanel>
+                </StackPanel>
         </StackPanel>
         <Button Grid.Row="1" Height="30" Width="60" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="10" Style="{StaticResource DarkRoundButton}" Content="OK" Command="{Binding OkCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
     </Grid>

+ 4 - 3
PixiEditor/Views/MainWindow.xaml

@@ -63,7 +63,7 @@
                     <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource menuItemStyle}"/>
                 </Menu.Resources>
                 <MenuItem Header="_File">
-                    <MenuItem InputGestureText="CTRL+N" Header="_New" Command="{Binding GenerateDrawAreaCommand}"/>
+                    <MenuItem InputGestureText="CTRL+N" Header="_New" Command="{Binding OpenNewFilePopupCommand}"/>
                     <MenuItem Header="_Open" InputGestureText="Ctrl+O" Command="{Binding OpenFileCommand}"/>
                     <MenuItem Header="_Export" InputGestureText="Ctrl+S" Command="{Binding SaveFileCommand}"/>
                     <MenuItem Header="_Export As..." InputGestureText="Ctrl+Shift+S" Command="{Binding SaveFileCommand}" CommandParameter="AsNew"/>
@@ -84,8 +84,9 @@
                     <MenuItem Header="_Deselect" Command="{Binding DeselectCommand}" InputGestureText="Ctrl+D"/>
                 </MenuItem>
                 <MenuItem Header="_Document">
-                    <MenuItem Header="Resize document" Command="{Binding OpenResizePopupCommand}"/>
-                    <MenuItem Header="Clip canvas" Command="{Binding ClipCanvasCommand}"/>
+                    <MenuItem Header="Resize Document..." Command="{Binding OpenResizePopupCommand}" InputGestureText="Ctrl+Shift+I"/>
+                    <MenuItem Header="Resize Canvas..." Command="{Binding OpenResizePopupCommand}" CommandParameter="canvas" InputGestureText="Ctrl+Shift+C"/>
+                    <MenuItem Header="Clip Canvas" Command="{Binding ClipCanvasCommand}"/>
                 </MenuItem>
             </Menu>
             <StackPanel DockPanel.Dock="Right" VerticalAlignment="Top" Orientation="Horizontal" HorizontalAlignment="Right" WindowChrome.IsHitTestVisibleInChrome="True">

+ 3 - 2
PixiEditor/Views/NewFilePopup.xaml

@@ -9,7 +9,7 @@
              xmlns:helpers="clr-namespace:PixiEditor.Helpers.Behaviours" 
              xmlns:converters="clr-namespace:PixiEditor.Helpers"
         mc:Ignorable="d" 
-             d:DesignHeight="600" d:DesignWidth="450" DataContext="{DynamicResource NewFileMenuViewModel}" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Height="600" Width="450" Name="newFilePopup">
+             d:DesignHeight="600" Topmost="True" ShowInTaskbar="False" d:DesignWidth="450" DataContext="{DynamicResource NewFileMenuViewModel}" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Height="600" Width="450" Name="newFilePopup">
     <Window.Resources>
         <vm:NewFileMenuViewModel x:Key="NewFileMenuViewModel"/>
         <converters:ToolSizeToIntConverter x:Key="ToolSizeToIntConverter"/>
@@ -33,7 +33,8 @@
                     </Button.Background>
                 </Button>
             </Grid>
-            <StackPanel HorizontalAlignment="Center" Margin="0,50,0,0" Background="{StaticResource MainColor}" VerticalAlignment="Top" Grid.Row="1" Width="350" Height="150">
+            <Label Content="New File" Grid.Row="1" Margin="0,10,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="White" FontSize="24"/>
+            <StackPanel HorizontalAlignment="Center" Margin="0,60,0,0" Background="{StaticResource MainColor}" VerticalAlignment="Top" Grid.Row="1" Width="350" Height="150">
                 <DockPanel Margin="50,35,0,0">
                     <TextBlock Height="30" Foreground="Snow" Text="Height:" TextAlignment="Center" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                     <TextBox Height="30" Width="150" Style="{StaticResource DarkTextBoxStyle}" FontSize="16"

+ 3 - 4
PixiEditor/Views/ResizeDocumentPopup.xaml

@@ -6,7 +6,7 @@
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:local="clr-namespace:PixiEditor.Views" xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours" xmlns:converters="clr-namespace:PixiEditor.Helpers"
         mc:Ignorable="d" Name="window"
-        Title="ResizeDocumentPopup"  WindowStartupLocation="CenterScreen" Height="300" Width="400" WindowStyle="None">
+        Title="ResizeDocumentPopup" Topmost="True" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" Height="300" Width="400" WindowStyle="None">
 
     <Window.Resources>
         <converters:ToolSizeToIntConverter x:Key="ToolSizeToIntConverter"/>
@@ -30,7 +30,7 @@
             <Button DockPanel.Dock="Right" HorizontalAlignment="Right" Style="{StaticResource CloseButtonStyle}" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close"
                             Command="{x:Static SystemCommands.CloseWindowCommand}"/>
         </DockPanel>
-        <Label Grid.Row="1" Foreground="White" FontSize="20" HorizontalAlignment="Center" Content="Resize document"/>
+        <Label Grid.Row="1" VerticalAlignment="Top" Foreground="White" FontSize="24" HorizontalAlignment="Center" Content="Resize document"/>
         <StackPanel HorizontalAlignment="Center" Margin="0,50,0,0" Background="{StaticResource MainColor}" VerticalAlignment="Top" Grid.Row="1" Width="350" Height="150">
             <DockPanel Margin="50,35,0,0">
                 <TextBlock Height="30" Foreground="Snow" Text="Height:" TextAlignment="Center" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
@@ -52,7 +52,6 @@
                 </TextBox>
             </DockPanel>
         </StackPanel>
-        <Button Grid.Row="1" Height="30" Width="60" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="10" Style="{StaticResource DarkRoundButton}" Content="OK" Command="{Binding OkCommand}" 
-                CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
+        <Button Grid.Row="1" Height="30" Width="60" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="10" Style="{StaticResource DarkRoundButton}" Content="OK" Click="Button_Click"/>
     </Grid>
 </Window>

+ 8 - 3
PixiEditor/Views/ResizeDocumentPopup.xaml.cs

@@ -12,6 +12,7 @@ namespace PixiEditor.Views
         public ResizeDocumentPopup()
         {
             InitializeComponent();
+            DataContext = this;
         }
 
 
@@ -36,9 +37,7 @@ namespace PixiEditor.Views
 
         // Using a DependencyProperty as the backing store for NewWidth.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty NewWidthProperty =
-            DependencyProperty.Register("NewWidth", typeof(int), typeof(ResizeDocumentPopup), new PropertyMetadata(0));
-
-
+            DependencyProperty.Register("NewWidth", typeof(int), typeof(ResizeDocumentPopup), new PropertyMetadata(0));   
 
 
 
@@ -51,5 +50,11 @@ namespace PixiEditor.Views
         {
             SystemCommands.CloseWindow(this);
         }
+
+        private void Button_Click(object sender, RoutedEventArgs e)
+        {
+            DialogResult = true;
+            Close();
+        }
     }
 }