Browse Source

Added recenter zoombox on each document size changed

flabbet 5 years ago
parent
commit
ff7cc35747

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

@@ -66,20 +66,37 @@ namespace PixiEditor.Models.DataHolders
             Height = height;
             Height = height;
         }
         }
 
 
-        //Resize methods below can be probably reduced in count
 
 
+        /// <summary>
+        /// Crops canvas at specified x and y offset.
+        /// </summary>
+        /// <param name="x">X offset</param>
+        /// <param name="y">Y offset</param>
+        /// <param name="width">New width</param>
+        /// <param name="height">New height</param>
         public void Crop(int x, int y, int width, int height)
         public void Crop(int x, int y, int width, int height)
         {
         {
+            int oldWidth = Width;
+            int oldHeight = Height;
+
             object[] reverseArgs = new object[] { 0, 0, x, y, Width, Height, width, height};
             object[] reverseArgs = new object[] { 0, 0, x, y, Width, Height, width, height};
             object[] processArgs = new object[] { x, y, 0, 0, width, height };
             object[] processArgs = new object[] { x, y, 0, 0, width, height };
             ResizeDocumentCanvas(processArgs);
             ResizeDocumentCanvas(processArgs);
             UndoManager.AddUndoChange(new Change("BitmapManager.ActiveDocument", ResizeDocumentCanvas, 
             UndoManager.AddUndoChange(new Change("BitmapManager.ActiveDocument", ResizeDocumentCanvas, 
                 reverseArgs, ResizeDocumentCanvas, processArgs, "Crop document"));
                 reverseArgs, ResizeDocumentCanvas, processArgs, "Crop document"));
-            DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(Width, Height, width, height));
         }
         }
 
 
+        /// <summary>
+        /// Resizes canvas to specifid width and height to selected anchor
+        /// </summary>
+        /// <param name="width">New width of canvas</param>
+        /// <param name="height">New height of canvas</param>
+        /// <param name="anchor">Point that will act as "starting position" of resizing. Use pipe to connect horizontal and vertical.</param>
         public void ResizeCanvas(int width, int height, AnchorPoint anchor)
         public void ResizeCanvas(int width, int height, AnchorPoint anchor)
         {
         {
+            int oldWidth = Width;
+            int oldHeight = Height;
+
             int offsetX = GetOffsetXForAnchor(Width, width, anchor);
             int offsetX = GetOffsetXForAnchor(Width, width, anchor);
             int offsetY = GetOffsetYForAnchor(Height, height, anchor);
             int offsetY = GetOffsetYForAnchor(Height, height, anchor);
             int offsetXSrc = 0;
             int offsetXSrc = 0;
@@ -100,6 +117,7 @@ namespace PixiEditor.Models.DataHolders
             ResizeCanvas(offsetX, offsetY, offsetXSrc, offsetYSrc, Width, Height, width, height);
             ResizeCanvas(offsetX, offsetY, offsetXSrc, offsetYSrc, Width, Height, width, height);
             UndoManager.AddUndoChange(new Change("BitmapManager.ActiveDocument", ResizeDocumentCanvas,
             UndoManager.AddUndoChange(new Change("BitmapManager.ActiveDocument", ResizeDocumentCanvas,
                 reverseProcessArgs, ResizeDocumentCanvas, processArgs, "Resize canvas"));
                 reverseProcessArgs, ResizeDocumentCanvas, processArgs, "Resize canvas"));
+            DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(oldWidth, oldHeight, width, height));
         }
         }
 
 
         private int GetOffsetXForAnchor(int srcWidth, int destWidth, AnchorPoint anchor)
         private int GetOffsetXForAnchor(int srcWidth, int destWidth, AnchorPoint anchor)
@@ -128,21 +146,25 @@ namespace PixiEditor.Models.DataHolders
             return 0;
             return 0;
         }
         }
 
 
+        /// <summary>
+        /// Resizes all document layers using NearestNeighbor interpolation.
+        /// </summary>
+        /// <param name="newWidth">New document width</param>
+        /// <param name="newHeight">New document height</param>
         public void Resize(int newWidth, int newHeight)
         public void Resize(int newWidth, int newHeight)
         {
         {
-            int oldWidth = Width;
-            int oldHeight = Height;
-
-            object[] reverseArgs = new object[] { oldWidth, oldHeight};
+            object[] reverseArgs = new object[] { Width, Height};
             object[] args = new object[] { newWidth, newHeight };
             object[] args = new object[] { newWidth, newHeight };
             ResizeDocument(args);
             ResizeDocument(args);
             UndoManager.AddUndoChange(new Change("BitmapManager.ActiveDocument", ResizeDocument, reverseArgs,
             UndoManager.AddUndoChange(new Change("BitmapManager.ActiveDocument", ResizeDocument, reverseArgs,
                 ResizeDocument, args, "Resize document"));
                 ResizeDocument, args, "Resize document"));
-            DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(oldWidth, oldHeight, newWidth, newHeight));
         }
         }
 
 
         private void ResizeDocument(object[] arguments)
         private void ResizeDocument(object[] arguments)
         {
         {
+            int oldWidth = Width;
+            int oldHeight = Height;
+
             int newWidth = (int)arguments[0];
             int newWidth = (int)arguments[0];
             int newHeight = (int)arguments[1];
             int newHeight = (int)arguments[1];
             for (int i = 0; i < Layers.Count; i++)
             for (int i = 0; i < Layers.Count; i++)
@@ -153,10 +175,14 @@ namespace PixiEditor.Models.DataHolders
             }
             }
             Height = newHeight;
             Height = newHeight;
             Width = newWidth;
             Width = newWidth;
+            DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(oldWidth, oldHeight, newWidth, newHeight));
         }
         }
 
 
         private void ResizeDocumentCanvas(object[] arguments)
         private void ResizeDocumentCanvas(object[] arguments)
         {
         {
+            int oldWidth = Width;
+            int oldHeight = Height;
+
             int x = (int)arguments[0];
             int x = (int)arguments[0];
             int y = (int)arguments[1];
             int y = (int)arguments[1];
             int destX = (int)arguments[2];
             int destX = (int)arguments[2];
@@ -166,6 +192,7 @@ namespace PixiEditor.Models.DataHolders
             ResizeCanvas(destX, destY, x, y, Width, Height, width, height);
             ResizeCanvas(destX, destY, x, y, Width, Height, width, height);
             Height = height;
             Height = height;
             Width = width;
             Width = width;
+            DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(oldWidth, oldHeight, width, height));
         }
         }
 
 
         private void ResizeCanvas(int offsetX, int offsetY, int offsetXSrc, int offsetYSrc, int oldWidth, int oldHeight, int newWidth, int newHeight)
         private void ResizeCanvas(int offsetX, int offsetY, int offsetXSrc, int offsetYSrc, int oldWidth, int oldHeight, int newWidth, int newHeight)

+ 10 - 12
PixiEditor/ViewModels/ViewModelMain.cs

@@ -32,7 +32,6 @@ namespace PixiEditor.ViewModels
         public RelayCommand UndoCommand { get; set; }
         public RelayCommand UndoCommand { get; set; }
         public RelayCommand RedoCommand { get; set; }
         public RelayCommand RedoCommand { get; set; }
         public RelayCommand MouseUpCommand { get; set; }
         public RelayCommand MouseUpCommand { get; set; }
-        public RelayCommand RecenterZoomboxCommand { get; set; }
         public RelayCommand OpenFileCommand { get; set; }
         public RelayCommand OpenFileCommand { get; set; }
         public RelayCommand SetActiveLayerCommand { get; set; }
         public RelayCommand SetActiveLayerCommand { get; set; }
         public RelayCommand NewLayerCommand { get; set; }
         public RelayCommand NewLayerCommand { get; set; }
@@ -69,7 +68,15 @@ namespace PixiEditor.ViewModels
             set { _mouseYonCanvas = value; RaisePropertyChanged("MouseYonCanvas"); }
             set { _mouseYonCanvas = value; RaisePropertyChanged("MouseYonCanvas"); }
         }
         }
 
 
-
+        private bool _recenterZoombox;
+
+        public bool RecenterZoombox
+        {
+            get => _recenterZoombox;
+            set { _recenterZoombox = value; RaisePropertyChanged("RecenterZoombox"); }
+        }        
+
+
         private Color _primaryColor = Colors.Black;
         private Color _primaryColor = Colors.Black;
 
 
         public Color PrimaryColor //Primary color, hooked with left mouse button
         public Color PrimaryColor //Primary color, hooked with left mouse button
@@ -173,7 +180,6 @@ namespace PixiEditor.ViewModels
             UndoCommand = new RelayCommand(Undo, CanUndo);
             UndoCommand = new RelayCommand(Undo, CanUndo);
             RedoCommand = new RelayCommand(Redo, CanRedo);
             RedoCommand = new RelayCommand(Redo, CanRedo);
             MouseUpCommand = new RelayCommand(MouseUp);
             MouseUpCommand = new RelayCommand(MouseUp);
-            RecenterZoomboxCommand = new RelayCommand(RecenterZoombox);
             OpenFileCommand = new RelayCommand(OpenFile);
             OpenFileCommand = new RelayCommand(OpenFile);
             SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
             SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
             NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
             NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
@@ -235,6 +241,7 @@ namespace PixiEditor.ViewModels
         private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
         private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
         {
         {
             ActiveSelection = new Selection(Array.Empty<Coordinates>());
             ActiveSelection = new Selection(Array.Empty<Coordinates>());
+            RecenterZoombox = true;
         }
         }
 
 
         private void OpenResizePopup(object parameter)
         private void OpenResizePopup(object parameter)
@@ -582,15 +589,6 @@ namespace PixiEditor.ViewModels
             BitmapManager.ActiveDocument.DocumentSizeChanged += ActiveDocument_DocumentSizeChanged;
             BitmapManager.ActiveDocument.DocumentSizeChanged += ActiveDocument_DocumentSizeChanged;
         }
         }
 
 
-        /// <summary>
-        /// For now, shows not implemented info, lol.
-        /// </summary>
-        /// <param name="parameter"></param>
-        public void RecenterZoombox(object parameter)
-        {
-            MessageBox.Show("This feature is not implemented yet.", "Feature not implemented", MessageBoxButton.OK, MessageBoxImage.Information);
-        }
-
         public void NewLayer(object parameter)
         public void NewLayer(object parameter)
         {
         {
             BitmapManager.AddNewLayer($"New Layer {BitmapManager.ActiveDocument.Layers.Count}", BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height);
             BitmapManager.AddNewLayer($"New Layer {BitmapManager.ActiveDocument.Layers.Count}", BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height);

+ 20 - 2
PixiEditor/Views/MainDrawingPanel.xaml.cs

@@ -1,4 +1,5 @@
-using System.Windows;
+using System;
+using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Controls;
 using System.Windows.Input;
 using System.Windows.Input;
 using Xceed.Wpf.Toolkit.Zoombox;
 using Xceed.Wpf.Toolkit.Zoombox;
@@ -16,6 +17,23 @@ namespace PixiEditor.Views
         }
         }
 
 
 
 
+
+        public bool Center
+        {
+            get { return (bool)GetValue(CenterProperty); }
+            set { SetValue(CenterProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for Center.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty CenterProperty =
+            DependencyProperty.Register("Center", typeof(bool), typeof(MainDrawingPanel), new PropertyMetadata(true, OnCenterChanged));
+
+        private static void OnCenterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        {
+            MainDrawingPanel panel = (MainDrawingPanel)d;
+            panel.Zoombox.CenterContent();
+        }
+
         public double MouseX
         public double MouseX
         {
         {
             get { return (double)GetValue(MouseXProperty); }
             get { return (double)GetValue(MouseXProperty); }
@@ -80,6 +98,6 @@ namespace PixiEditor.Views
             {
             {
                 ((Zoombox)sender).CenterContent();
                 ((Zoombox)sender).CenterContent();
             }
             }
-        }
+        }
     }
     }
 }
 }

+ 1 - 1
PixiEditor/Views/MainWindow.xaml

@@ -119,7 +119,7 @@
         </StackPanel>
         </StackPanel>
         <Grid Grid.Column="1" Grid.Row="2" Background="#303030" Margin="0,7,5,0">
         <Grid Grid.Column="1" Grid.Row="2" Background="#303030" Margin="0,7,5,0">
             <Grid>
             <Grid>
-                <vws:MainDrawingPanel x:Name="DrawingPanel" CenterOnStart="True" Cursor="{Binding ToolCursor}">
+                <vws:MainDrawingPanel Center="{Binding RecenterZoombox, Mode=TwoWay}" x:Name="DrawingPanel" CenterOnStart="True" Cursor="{Binding ToolCursor}">
                     <i:Interaction.Triggers>
                     <i:Interaction.Triggers>
                         <i:EventTrigger EventName="MouseMove">
                         <i:EventTrigger EventName="MouseMove">
                             <i:InvokeCommandAction Command="{Binding MouseMoveCommand}"/>
                             <i:InvokeCommandAction Command="{Binding MouseMoveCommand}"/>