Browse Source

Now tabs are unique to documents, they don't depend on active one

flabbet 4 years ago
parent
commit
8996deca41

+ 11 - 31
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -21,7 +21,6 @@ namespace PixiEditor.Models.Controllers
     public class BitmapManager : NotifyableObject
     {
         private Document activeDocument;
-        private Layer previewLayer;
         private Tool selectedTool;
 
         public BitmapManager()
@@ -50,16 +49,6 @@ namespace PixiEditor.Models.Controllers
             }
         }
 
-        public Layer PreviewLayer
-        {
-            get => previewLayer;
-            set
-            {
-                previewLayer = value;
-                RaisePropertyChanged("PreviewLayer");
-            }
-        }
-
         public Layer ActiveLayer => ActiveDocument.ActiveLayer;
 
         public Color PrimaryColor { get; set; }
@@ -132,18 +121,6 @@ namespace PixiEditor.Models.Controllers
             }
         }
 
-        public void GeneratePreviewLayer()
-        {
-            if (ActiveDocument != null)
-            {
-                PreviewLayer = new Layer("_previewLayer")
-                {
-                    MaxWidth = ActiveDocument.Width,
-                    MaxHeight = ActiveDocument.Height
-                };
-            }
-        }
-
         public WriteableBitmap GetCombinedLayersBitmap()
         {
             return BitmapUtils.CombineLayers(ActiveDocument.Layers.Where(x => x.IsVisible).ToArray(), ActiveDocument.Width, ActiveDocument.Height);
@@ -159,7 +136,10 @@ namespace PixiEditor.Models.Controllers
 
         public void SetActiveTool(Tool tool)
         {
-            PreviewLayer = null;
+            if (ActiveDocument != null)
+            {
+                ActiveDocument.PreviewLayer = null;
+            }
             SelectedTool?.Toolbar.SaveToolbarSettings();
             SelectedTool = tool;
             SelectedTool.Toolbar.LoadSharedSettings();
@@ -196,7 +176,7 @@ namespace PixiEditor.Models.Controllers
         private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
         {
             SelectedTool.OnRecordingLeftMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
-            PreviewLayer = null;
+            ActiveDocument.PreviewLayer = null;
         }
 
         private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
@@ -220,16 +200,16 @@ namespace PixiEditor.Models.Controllers
             if (CanChangeHighlightOffset(highlightArea))
             {
                 Coordinates start = highlightArea.First();
-                PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
+                ActiveDocument.PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
             }
             else if (!IsInsideBounds(highlightArea))
             {
-                PreviewLayer = null;
+                ActiveDocument.PreviewLayer = null;
             }
             else
             {
-                GeneratePreviewLayer();
-                PreviewLayer.SetPixels(
+                ActiveDocument.GeneratePreviewLayer();
+                ActiveDocument.PreviewLayer.SetPixels(
                     BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77, 0, 0, 0)));
             }
         }
@@ -237,8 +217,8 @@ namespace PixiEditor.Models.Controllers
         private bool CanChangeHighlightOffset(IEnumerable<Coordinates> highlightArea)
         {
             int count = highlightArea.Count();
-            return count > 0 && PreviewLayer != null &&
-                   IsInsideBounds(highlightArea) && count == PreviewLayer.Width * PreviewLayer.Height;
+            return count > 0 && ActiveDocument.PreviewLayer != null &&
+                   IsInsideBounds(highlightArea) && count == ActiveDocument.PreviewLayer.Width * ActiveDocument.PreviewLayer.Height;
         }
 
         private bool IsInsideBounds(IEnumerable<Coordinates> highlightArea)

+ 3 - 3
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -92,7 +92,7 @@ namespace PixiEditor.Models.Controllers
                     lastModifiedLayers[i].PixelChanges,
                     oldValues,
                     lastModifiedLayers[i].LayerIndex));
-                Manager.PreviewLayer = null;
+                Manager.ActiveDocument.GeneratePreviewLayer();
             }
         }
 
@@ -184,13 +184,13 @@ namespace PixiEditor.Models.Controllers
             LayerChange[] modifiedLayers;
             if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
             {
-                Manager.GeneratePreviewLayer();
+                Manager.ActiveDocument.GeneratePreviewLayer();
                 modifiedLayers = ((BitmapOperationTool)Manager.SelectedTool).Use(
                     Manager.ActiveDocument.ActiveLayer,
                     mouseMove.ToArray(),
                     Manager.PrimaryColor);
                 BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();
-                Manager.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
+                Manager.ActiveDocument.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
                 lastModifiedLayers = modifiedLayers;
             }
         }

+ 103 - 0
PixiEditor/Models/DataHolders/Document.cs

@@ -28,6 +28,8 @@ namespace PixiEditor.Models.DataHolders
             Height = height;
             RequestCloseDocumentCommand = new RelayCommand(RequestCloseDocument);
             UndoManager = new UndoManager();
+            XamlAccesibleViewModel = ViewModelMain.Current ?? null;
+            GeneratePreviewLayer();
             DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(0, 0, width, height));
         }
 
@@ -37,6 +39,18 @@ namespace PixiEditor.Models.DataHolders
 
         public RelayCommand RequestCloseDocumentCommand { get; set; }
 
+        private ViewModelMain xamlAccesibleViewModel = null;
+
+        public ViewModelMain XamlAccesibleViewModel // Used to access ViewModelMain, without changing DataContext in XAML
+        {
+            get => xamlAccesibleViewModel;
+            set
+            {
+                xamlAccesibleViewModel = value;
+                RaisePropertyChanged(nameof(XamlAccesibleViewModel));
+            }
+        }
+
         private string documentFilePath = string.Empty;
 
         public string DocumentFilePath
@@ -101,6 +115,78 @@ namespace PixiEditor.Models.DataHolders
             }
         }
 
+        private Layer previewLayer;
+
+        public Layer PreviewLayer
+        {
+            get => previewLayer;
+            set
+            {
+                previewLayer = value;
+                RaisePropertyChanged("PreviewLayer");
+            }
+        }
+
+        private double mouseXonCanvas;
+
+        private double mouseYonCanvas;
+
+        public double MouseXOnCanvas // Mouse X coordinate relative to canvas
+        {
+            get => mouseXonCanvas;
+            set
+            {
+                mouseXonCanvas = value;
+                RaisePropertyChanged(nameof(MouseXOnCanvas));
+            }
+        }
+
+        public double MouseYOnCanvas // Mouse Y coordinate relative to canvas
+        {
+            get => mouseYonCanvas;
+            set
+            {
+                mouseYonCanvas = value;
+                RaisePropertyChanged(nameof(MouseYOnCanvas));
+            }
+        }
+
+        private double zoomPercentage = 100;
+
+        public double ZoomPercentage
+        {
+            get => zoomPercentage;
+            set
+            {
+                zoomPercentage = value;
+                RaisePropertyChanged(nameof(ZoomPercentage));
+            }
+        }
+
+        private Point viewPortPosition;
+
+        public Point ViewportPosition
+        {
+            get => viewPortPosition;
+            set
+            {
+                viewPortPosition = value;
+                RaisePropertyChanged(nameof(ViewportPosition));
+            }
+        }
+
+        private bool recenterZoombox = true;
+
+        public bool RecenterZoombox
+        {
+            get => recenterZoombox;
+            set
+            {
+                recenterZoombox = value;
+                RaisePropertyChanged(nameof(RecenterZoombox));
+            }
+        }
+
         public UndoManager UndoManager { get; set; }
 
         public ObservableCollection<Layer> Layers { get; set; } = new ObservableCollection<Layer>();
@@ -118,6 +204,23 @@ namespace PixiEditor.Models.DataHolders
             }
         }
 
+        public void GeneratePreviewLayer()
+        {
+            PreviewLayer = new Layer("_previewLayer")
+            {
+                MaxWidth = Width,
+                MaxHeight = Height
+            };
+        }
+
+        public void CenterViewport()
+        {
+            RecenterZoombox = false; // It's a trick to trigger change in UserControl
+            RecenterZoombox = true;
+            ViewportPosition = default;
+            ZoomPercentage = default;
+        }
+
         public void SaveWithDialog()
         {
             bool savedSuccessfully = Exporter.SaveAsEditableFileWithDialog(this, out string path);

+ 1 - 0
PixiEditor/Models/Layers/Layer.cs

@@ -333,6 +333,7 @@ namespace PixiEditor.Models.Layers
         public void Clear()
         {
             LayerBitmap.Clear();
+            ClipCanvas();
         }
 
         /// <summary>

+ 1 - 1
PixiEditor/Models/Tools/Tools/MoveViewportTool.cs

@@ -32,7 +32,7 @@ namespace PixiEditor.Models.Tools.Tools
             if (e.LeftButton == MouseButtonState.Pressed || e.MiddleButton == MouseButtonState.Pressed)
             {
                 var point = MousePositionConverter.GetCursorPosition();
-                ViewModelMain.Current.ViewportSubViewModel.ViewportPosition = new System.Windows.Point(
+                ViewModelMain.Current.BitmapManager.ActiveDocument.ViewportPosition = new System.Windows.Point(
                     point.X - clickPoint.X,
                     point.Y - clickPoint.Y);
             }

+ 2 - 2
PixiEditor/Models/Tools/Tools/ZoomTool.cs

@@ -45,7 +45,7 @@ namespace PixiEditor.Models.Tools.Tools
         public override void OnRecordingLeftMouseDown(MouseEventArgs e)
         {
             startingX = MousePositionConverter.GetCursorPosition().X;
-            ViewModelMain.Current.ViewportSubViewModel.ZoomPercentage = 100; // This resest the value, so callback in MainDrawingPanel can fire again later
+            ViewModelMain.Current.BitmapManager.ActiveDocument.ZoomPercentage = 100; // This resest the value, so callback in MainDrawingPanel can fire again later
         }
 
         public override void OnMouseMove(MouseEventArgs e)
@@ -78,7 +78,7 @@ namespace PixiEditor.Models.Tools.Tools
 
         public void Zoom(double percentage)
         {
-            ViewModelMain.Current.ViewportSubViewModel.ZoomPercentage = percentage;
+            ViewModelMain.Current.BitmapManager.ActiveDocument.ZoomPercentage = percentage;
         }
 
         public override void Use(Coordinates[] pixels)

+ 7 - 29
PixiEditor/ViewModels/SubViewModels/Main/IoViewModel.cs

@@ -17,30 +17,6 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public RelayCommand KeyUpCommand { get; set; }
 
-        private double mouseXonCanvas;
-
-        private double mouseYonCanvas;
-
-        public double MouseXOnCanvas // Mouse X coordinate relative to canvas
-        {
-            get => mouseXonCanvas;
-            set
-            {
-                mouseXonCanvas = value;
-                RaisePropertyChanged(nameof(MouseXOnCanvas));
-            }
-        }
-
-        public double MouseYOnCanvas // Mouse Y coordinate relative to canvas
-        {
-            get => mouseYonCanvas;
-            set
-            {
-                mouseYonCanvas = value;
-                RaisePropertyChanged(nameof(MouseYOnCanvas));
-            }
-        }
-
         private bool restoreToolOnKeyUp = false;
 
         public IoViewModel(ViewModelMain owner)
@@ -90,10 +66,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             {
                 if (!Owner.BitmapManager.MouseController.IsRecordingChanges)
                 {
-                    bool clickedOnCanvas = MouseXOnCanvas >= 0 &&
-                        MouseXOnCanvas <= Owner.BitmapManager.ActiveDocument.Width &&
-                        MouseYOnCanvas >= 0 &&
-                        MouseYOnCanvas <= Owner.BitmapManager.ActiveDocument.Height;
+                    bool clickedOnCanvas = Owner.BitmapManager.ActiveDocument.MouseXOnCanvas >= 0 &&
+                        Owner.BitmapManager.ActiveDocument.MouseXOnCanvas <= Owner.BitmapManager.ActiveDocument.Width &&
+                        Owner.BitmapManager.ActiveDocument.MouseYOnCanvas >= 0 &&
+                        Owner.BitmapManager.ActiveDocument.MouseYOnCanvas <= Owner.BitmapManager.ActiveDocument.Height;
                     Owner.BitmapManager.MouseController.StartRecordingMouseMovementChanges(clickedOnCanvas);
                     Owner.BitmapManager.MouseController.RecordMouseMovementChange(MousePositionConverter.CurrentCoordinates);
                 }
@@ -115,7 +91,9 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         /// <param name="parameter">CommandParameter.</param>
         private void MouseMove(object parameter)
         {
-            Coordinates cords = new Coordinates((int)MouseXOnCanvas, (int)MouseYOnCanvas);
+            Coordinates cords = new Coordinates(
+                (int)Owner.BitmapManager.ActiveDocument.MouseXOnCanvas,
+                (int)Owner.BitmapManager.ActiveDocument.MouseYOnCanvas);
             MousePositionConverter.CurrentCoordinates = cords;
 
             if (Owner.BitmapManager.MouseController.IsRecordingChanges && Mouse.LeftButton == MouseButtonState.Pressed)

+ 2 - 46
PixiEditor/ViewModels/SubViewModels/Main/ViewportViewModel.cs

@@ -7,61 +7,17 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
     {
         public RelayCommand ZoomCommand { get; set; }
 
-        private double zoomPercentage = 100;
-
-        public double ZoomPercentage
-        {
-            get => zoomPercentage;
-            set
-            {
-                zoomPercentage = value;
-                RaisePropertyChanged(nameof(ZoomPercentage));
-            }
-        }
-
-        private Point viewPortPosition;
-
-        public Point ViewportPosition
-        {
-            get => viewPortPosition;
-            set
-            {
-                viewPortPosition = value;
-                RaisePropertyChanged(nameof(ViewportPosition));
-            }
-        }
-
-        private bool recenterZoombox;
-
-        public bool RecenterZoombox
-        {
-            get => recenterZoombox;
-            set
-            {
-                recenterZoombox = value;
-                RaisePropertyChanged(nameof(RecenterZoombox));
-            }
-        }
-
         public ViewportViewModel(ViewModelMain owner)
             : base(owner)
         {
             ZoomCommand = new RelayCommand(ZoomViewport);
         }
 
-        public void CenterViewport()
-        {
-            RecenterZoombox = false; // It's a trick to trigger change in UserControl
-            RecenterZoombox = true;
-            ViewportPosition = default;
-            ZoomPercentage = default;
-        }
-
         private void ZoomViewport(object parameter)
         {
             double zoom = (int)parameter;
-            ZoomPercentage = zoom;
-            ZoomPercentage = 100;
+            Owner.BitmapManager.ActiveDocument.ZoomPercentage = zoom;
+            Owner.BitmapManager.ActiveDocument.ZoomPercentage = 100;
         }
     }
 }

+ 7 - 3
PixiEditor/ViewModels/ViewModelMain.cs

@@ -143,8 +143,12 @@ namespace PixiEditor.ViewModels
         /// </summary>
         public void ResetProgramStateValues()
         {
-            BitmapManager.PreviewLayer = null;
-            ViewportSubViewModel.CenterViewport();
+            foreach (var document in BitmapManager.Documents)
+            {
+                document.PreviewLayer = null;
+            }
+
+            BitmapManager.ActiveDocument.CenterViewport();
         }
 
         public bool DocumentIsNotNull(object property)
@@ -228,7 +232,7 @@ namespace PixiEditor.ViewModels
         private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
         {
             BitmapManager.ActiveDocument.ActiveSelection = new Selection(Array.Empty<Coordinates>());
-            ViewportSubViewModel.CenterViewport();
+            BitmapManager.ActiveDocument.CenterViewport();
             BitmapManager.ActiveDocument.ChangesSaved = false;
         }
 

+ 15 - 15
PixiEditor/Views/MainWindow.xaml

@@ -162,7 +162,8 @@
         </StackPanel>
         <Grid Grid.Column="1" Grid.Row="2" Background="#303030" Margin="0,7,5,0">
             <Grid>
-                <avalondock:DockingManager ActiveContent="{Binding BitmapManager.ActiveDocument, Mode=TwoWay}" DocumentsSource="{Binding BitmapManager.Documents}">
+                <avalondock:DockingManager ActiveContent="{Binding BitmapManager.ActiveDocument, Mode=TwoWay}" 
+                                           DocumentsSource="{Binding BitmapManager.Documents}">
                     <avalondock:DockingManager.Theme>
                         <avalondock:Vs2013DarkTheme/>
                     </avalondock:DockingManager.Theme>
@@ -170,22 +171,21 @@
                         <Style TargetType="{x:Type avalondock:LayoutItem}">
                             <Setter Property="Title" Value="{Binding Model.Name}" />
                             <Setter Property="CloseCommand" Value="{Binding Model.RequestCloseDocumentCommand}" />
-                            <Setter Property="CanClose" Value="{Binding Model.CanClose}" />
                         </Style>
                     </avalondock:DockingManager.LayoutItemContainerStyle>
                     <avalondock:DockingManager.LayoutItemTemplate>
                         <DataTemplate DataType="{x:Type vm:ViewModelMain}">
-                            <usercontrols:DrawingViewPort DataContext="{Binding Path=DataContext, 
-                                RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
-                                        ZoomPercentage="{Binding ViewportSubViewModel.ZoomPercentage}"
-                                        RecenterZoombox="{Binding ViewportSubViewModel.RecenterZoombox}"
-                                        Cursor="{Binding ToolsSubViewModel.ToolCursor}"
-                                        MiddleMouseClickedCommand="{Binding ToolsSubViewModel.SelectToolCommand}"
-                                        ViewportPosition="{Binding ViewportSubViewModel.ViewportPosition}"
-                                        MouseMoveCommand="{Binding IoSubViewModel.MouseMoveCommand}"
-                                        MouseDownCommand="{Binding IoSubViewModel.MouseDownCommand}"
-                                        MouseXOnCanvas="{Binding IoSubViewModel.MouseXOnCanvas, Mode=TwoWay}"
-                                        MouseYOnCanvas="{Binding IoSubViewModel.MouseYOnCanvas, Mode=TwoWay}"/>
+                            <usercontrols:DrawingViewPort
+                                        ZoomPercentage="{Binding ZoomPercentage}"
+                                        RecenterZoombox="{Binding RecenterZoombox}"
+                                        Cursor="{Binding XamlAccesibleViewModel.ToolsSubViewModel.ToolCursor}"
+                                        MiddleMouseClickedCommand="{Binding XamlAccesibleViewModel.ToolsSubViewModel.SelectToolCommand}"
+                                        ViewportPosition="{Binding ViewportPosition}"
+                                        MouseMoveCommand="{Binding XamlAccesibleViewModel.IoSubViewModel.MouseMoveCommand}"
+                                        MouseDownCommand="{Binding XamlAccesibleViewModel.IoSubViewModel.MouseDownCommand}"
+                                        MouseXOnCanvas="{Binding MouseXOnCanvas, Mode=TwoWay}"
+                                        MouseYOnCanvas="{Binding MouseYOnCanvas, Mode=TwoWay}">
+                            </usercontrols:DrawingViewPort>
                         </DataTemplate>
                     </avalondock:DockingManager.LayoutItemTemplate>
                 </avalondock:DockingManager>
@@ -358,9 +358,9 @@
             <TextBlock Text="{Binding BitmapManager.SelectedTool.ActionDisplay}" Foreground="White" FontSize="15"  VerticalAlignment="Center"/>
             <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
                 <TextBlock Text="X:" Foreground="White" FontSize="16"/>
-                <TextBlock Margin="4,0,10,0" Text="{Binding IoSubViewModel.MouseXOnCanvas, Converter={StaticResource DoubleToIntConverter}}" Foreground="White" FontSize="16"/>
+                <TextBlock Margin="4,0,10,0" Text="{Binding BitmapManager.ActiveDocument.MouseXOnCanvas, Converter={StaticResource DoubleToIntConverter}}" Foreground="White" FontSize="16"/>
                 <TextBlock Text="Y:" Foreground="White" FontSize="16"/>
-                <TextBlock Margin="4,0,10,0" Text="{Binding IoSubViewModel.MouseYOnCanvas, Converter={StaticResource DoubleToIntConverter}}" Foreground="White" FontSize="16"/>
+                <TextBlock Margin="4,0,10,0" Text="{Binding BitmapManager.ActiveDocument.MouseYOnCanvas, Converter={StaticResource DoubleToIntConverter}}" Foreground="White" FontSize="16"/>
             </StackPanel>
         </DockPanel>
         <StackPanel Margin="10,0,0,0" VerticalAlignment="Center" Grid.Row="3"

+ 13 - 13
PixiEditor/Views/UserControls/DrawingViewPort.xaml

@@ -35,19 +35,19 @@
                                                   MouseY="{Binding MouseYOnCanvas, Mode=TwoWay, ElementName=uc}" />
             </i:Interaction.Behaviors>
             <vws:MainDrawingPanel.Item>
-                <Canvas Width="{Binding BitmapManager.ActiveDocument.Width}"
-                                Height="{Binding BitmapManager.ActiveDocument.Height}" VerticalAlignment="Center"
+                <Canvas Width="{Binding Width}"
+                                Height="{Binding Height}" VerticalAlignment="Center"
                                 HorizontalAlignment="Center">
                     <Image Source="/Images/transparentbg.png"
-                                   Height="{Binding BitmapManager.ActiveDocument.Height}"
-                                   Width="{Binding BitmapManager.ActiveDocument.Width}" Opacity="0.9"
+                                   Height="{Binding Height}"
+                                   Width="{Binding Width}" Opacity="0.9"
                                    Stretch="UniformToFill" />
-                <Image Source="{Binding BitmapManager.PreviewLayer.LayerBitmap}" Panel.ZIndex="2"
+                <Image Source="{Binding PreviewLayer.LayerBitmap}" Panel.ZIndex="2"
                                    RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform"
-                                   Width="{Binding BitmapManager.PreviewLayer.Width}"
-                                   Height="{Binding BitmapManager.PreviewLayer.Height}" 
-                                   Margin="{Binding BitmapManager.PreviewLayer.Offset}"/>
-                <ItemsControl ItemsSource="{Binding BitmapManager.ActiveDocument.Layers}">
+                                   Width="{Binding PreviewLayer.Width}"
+                                   Height="{Binding PreviewLayer.Height}" 
+                                   Margin="{Binding PreviewLayer.Offset}"/>
+                <ItemsControl ItemsSource="{Binding Layers}">
                         <ItemsControl.ItemsPanel>
                             <ItemsPanelTemplate>
                                 <Grid />
@@ -64,11 +64,11 @@
                         </ItemsControl.ItemTemplate>
                     </ItemsControl>
                     <Image VerticalAlignment="Top" HorizontalAlignment="Left" 
-                           Source="{Binding BitmapManager.ActiveDocument.ActiveSelection.SelectionLayer.LayerBitmap}"
+                           Source="{Binding ActiveSelection.SelectionLayer.LayerBitmap}"
                                    RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform"
-                                   Width="{Binding BitmapManager.ActiveDocument.ActiveSelection.SelectionLayer.Width}"
-                                   Height="{Binding BitmapManager.ActiveDocument.ActiveSelection.SelectionLayer.Height}" 
-                                   Margin="{Binding BitmapManager.ActiveDocument.ActiveSelection.SelectionLayer.Offset}" />
+                                   Width="{Binding ActiveSelection.SelectionLayer.Width}"
+                                   Height="{Binding ActiveSelection.SelectionLayer.Height}" 
+                                   Margin="{Binding ActiveSelection.SelectionLayer.Offset}" />
                 </Canvas>
             </vws:MainDrawingPanel.Item>
         </vws:MainDrawingPanel>

+ 33 - 23
PixiEditor/Views/UserControls/MainDrawingPanel.xaml.cs

@@ -68,42 +68,44 @@ namespace PixiEditor.Views
 
         // Using a DependencyProperty as the backing store for ViewportPosition.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty ViewportPositionProperty =
-            DependencyProperty.Register("ViewportPosition", typeof(Point),
-                typeof(MainDrawingPanel), new PropertyMetadata(default(Point), ViewportPosCallback));
+            DependencyProperty.Register(
+                "ViewportPosition",
+                typeof(Point),
+                typeof(MainDrawingPanel),
+                new PropertyMetadata(
+                    default(Point),
+                    ViewportPosCallback));
 
         public bool Center
         {
-            get => (bool) GetValue(CenterProperty);
+            get => (bool)GetValue(CenterProperty);
             set => SetValue(CenterProperty, value);
         }
 
         public double MouseX
         {
-            get => (double) GetValue(MouseXProperty);
+            get => (double)GetValue(MouseXProperty);
             set => SetValue(MouseXProperty, value);
         }
 
         public double MouseY
         {
-            get => (double) GetValue(MouseYProperty);
+            get => (double)GetValue(MouseYProperty);
             set => SetValue(MouseYProperty, value);
         }
 
-
         public ICommand MouseMoveCommand
         {
-            get => (ICommand) GetValue(MouseMoveCommandProperty);
+            get => (ICommand)GetValue(MouseMoveCommandProperty);
             set => SetValue(MouseMoveCommandProperty, value);
         }
 
-
         public bool CenterOnStart
         {
-            get => (bool) GetValue(CenterOnStartProperty);
+            get => (bool)GetValue(CenterOnStartProperty);
             set => SetValue(CenterOnStartProperty, value);
         }
 
-
         public object Item
         {
             get => GetValue(ItemProperty);
@@ -126,8 +128,6 @@ namespace PixiEditor.Views
         public static readonly DependencyProperty MiddleMouseClickedCommandProperty =
             DependencyProperty.Register("MiddleMouseClickedCommand", typeof(ICommand), typeof(MainDrawingPanel), new PropertyMetadata(default(ICommand)));
 
-
-
         public object MiddleMouseClickedCommandParameter
         {
             get { return (object)GetValue(MiddleMouseClickedCommandParameterProperty); }
@@ -136,10 +136,12 @@ namespace PixiEditor.Views
 
         // Using a DependencyProperty as the backing store for MiddleMouseClickedCommandParameter.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty MiddleMouseClickedCommandParameterProperty =
-            DependencyProperty.Register("MiddleMouseClickedCommandParameter", typeof(object), typeof(MainDrawingPanel), 
-                new PropertyMetadata(default(object)));
-
-
+            DependencyProperty.Register(
+                "MiddleMouseClickedCommandParameter",
+                typeof(object),
+                typeof(MainDrawingPanel),
+                new PropertyMetadata(
+                    default(object)));
 
         private static void ZoomPercentegeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
@@ -168,13 +170,14 @@ namespace PixiEditor.Views
             {
                 panel.Zoombox.Position = default;
                 return;
-            }           
+            }
             TranslateZoombox(panel, (Point)e.NewValue);
         }
 
         private static void TranslateZoombox(MainDrawingPanel panel, Point vector)
         {
-            var newPos = new Point(panel.ClickPosition.X + vector.X,
+            var newPos = new Point(
+                panel.ClickPosition.X + vector.X,
                 panel.ClickPosition.Y + vector.Y);
             panel.Zoombox.Position = newPos;
         }
@@ -195,7 +198,11 @@ namespace PixiEditor.Views
 
         private void SetClickValues()
         {
-            if (!IsUsingZoomTool) return;
+            if (!IsUsingZoomTool)
+            {
+                return;
+            }
+
             ClickScale = Zoombox.Scale;
             SetZoomOrigin();
         }
@@ -211,13 +218,16 @@ namespace PixiEditor.Views
         private static void OnCenterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             MainDrawingPanel panel = (MainDrawingPanel) d;
-            panel.Zoombox.CenterContent();
+            panel.Zoombox.FitToBounds();
         }
 
-
         private void Zoombox_Loaded(object sender, RoutedEventArgs e)
         {
-            if (CenterOnStart) ((Zoombox) sender).CenterContent();
+            if (CenterOnStart)
+            {
+                ((Zoombox)sender).FitToBounds();
+            }
+
             ClickScale = Zoombox.Scale;
         }
 
@@ -241,7 +251,7 @@ namespace PixiEditor.Views
 
         private void Zoombox_MouseDown(object sender, MouseButtonEventArgs e)
         {
-            if (e.MiddleButton == MouseButtonState.Pressed && 
+            if (e.MiddleButton == MouseButtonState.Pressed &&
                 MiddleMouseClickedCommand.CanExecute(MiddleMouseClickedCommandParameter))
             {
                 MiddleMouseClickedCommand.Execute(MiddleMouseClickedCommandParameter);