Browse Source

Used DocumentProvider.cs more

CPKreuz 3 years ago
parent
commit
30223d79f4

+ 5 - 3
PixiEditor/Models/Services/DocumentProvider.cs

@@ -1,7 +1,7 @@
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
-using System.Collections.Generic;
+using SkiaSharp;
 
 namespace PixiEditor.Models.Services
 {
@@ -20,7 +20,7 @@ namespace PixiEditor.Models.Services
         /// <summary>
         /// Gets all opened documents
         /// </summary>
-        public IEnumerable<Document> GetDocuments() => _bitmapManager.Documents;
+        public ICollection<Document> GetDocuments() => _bitmapManager.Documents;
 
         /// <summary>
         /// Gets the active document
@@ -30,7 +30,7 @@ namespace PixiEditor.Models.Services
         /// <summary>
         /// Get the layers of the opened document
         /// </summary>
-        public IEnumerable<Layer> GetLayers() => _bitmapManager.ActiveDocument?.Layers;
+        public ICollection<Layer> GetLayers() => _bitmapManager.ActiveDocument?.Layers;
 
         /// <summary>
         /// Gets the layer structure of the opened document
@@ -66,5 +66,7 @@ namespace PixiEditor.Models.Services
         /// Gets the renderer for the reference layer of the active document
         /// </summary>
         public SingleLayerRenderer GetReferenceRenderer() => _bitmapManager.ActiveDocument?.ReferenceLayerRenderer;
+        
+        public ICollection<SKColor> GetSwatches() => _bitmapManager.ActiveDocument?.Swatches;
     }
 }

+ 10 - 7
PixiEditor/ViewModels/SubViewModels/Main/ClipboardViewModel.cs

@@ -7,13 +7,16 @@ using System.Text.RegularExpressions;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Media;
+using PixiEditor.Models.Services;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
     [Command.Group("PixiEditor.Clipboard", "Clipboard")]
     public class ClipboardViewModel : SubViewModel<ViewModelMain>
     {
-        public ClipboardViewModel(ViewModelMain owner)
+        private readonly DocumentProvider _doc;
+        
+        public ClipboardViewModel(ViewModelMain owner, DocumentProvider provider)
             : base(owner)
         {
         }
@@ -30,15 +33,15 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
             Copy();
             Owner.BitmapManager.BitmapOperations.DeletePixels(
-                new[] { Owner.BitmapManager.ActiveDocument.ActiveLayer },
-                Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints.ToArray());
+                new[] { _doc.GetLayer() },
+                _doc.GetDocument().ActiveSelection.SelectedPoints.ToArray());
         }
 
         [Command.Basic("PixiEditor.Clipboard.Paste", "Paste", "Paste from clipboard", CanExecute = "PixiEditor.Clipboard.CanPaste", Key = Key.V, Modifiers = ModifierKeys.Control)]
         public void Paste()
         {
-            if (Owner.BitmapManager.ActiveDocument == null) return;
-            ClipboardController.PasteFromClipboard(Owner.BitmapManager.ActiveDocument);
+            if (_doc.GetDocument() == null) return;
+            ClipboardController.PasteFromClipboard(_doc.GetDocument());
         }
 
         [Command.Basic("PixiEditor.Clipboard.PasteColor", "Paste color", "Paste color from clipboard", CanExecute = "PixiEditor.Clipboard.CanPasteColor", IconEvaluator = "PixiEditor.Clipboard.PasteColorIcon")]
@@ -53,10 +56,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             ClipboardController.CopyToClipboard(Owner.BitmapManager.ActiveDocument);
         }
 
-        [Evaluator.CanExecute("PixiEditor.Clipboard.CanPaste")]
+        [Evaluator.CanExecute("PixiEditor.Clipboard.CanPaste", requires: "PixiEditor.HasDocument")]
         public bool CanPaste()
         {
-            return Owner.DocumentIsNotNull(null) && ClipboardController.IsImageInClipboard();
+            return ClipboardController.IsImageInClipboard();
         }
 
         [Evaluator.CanExecute("PixiEditor.Clipboard.CanPasteColor")]

+ 10 - 5
PixiEditor/ViewModels/SubViewModels/Main/ColorsViewModel.cs

@@ -1,12 +1,14 @@
 using PixiEditor.Models.Commands.Attributes;
 using SkiaSharp;
 using System.Windows.Input;
+using PixiEditor.Models.Services;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
     public class ColorsViewModel : SubViewModel<ViewModelMain>
     {
         private SKColor primaryColor = SKColors.Black;
+        private DocumentProvider _doc;
 
         public SKColor PrimaryColor // Primary color, hooked with left mouse button
         {
@@ -37,9 +39,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
         }
 
-        public ColorsViewModel(ViewModelMain owner)
+        public ColorsViewModel(ViewModelMain owner, DocumentProvider provider)
             : base(owner)
         {
+            _doc = provider;
         }
 
         [Command.Basic("PixiEditor.Colors.Swap", "Swap colors", "Swap primary and secondary colors", Key = Key.X)]
@@ -52,18 +55,20 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void AddSwatch(SKColor color)
         {
-            if (!Owner.BitmapManager.ActiveDocument.Swatches.Contains(color))
+            var swatches = _doc.GetSwatches();
+            if (!swatches.Contains(color))
             {
-                Owner.BitmapManager.ActiveDocument.Swatches.Add(color);
+                swatches.Add(color);
             }
         }
 
         [Command.Internal("PixiEditor.Colors.RemoveSwatch")]
         public void RemoveSwatch(SKColor color)
         {
-            if (Owner.BitmapManager.ActiveDocument.Swatches.Contains(color))
+            var swatches = _doc.GetSwatches();
+            if (swatches.Contains(color))
             {
-                Owner.BitmapManager.ActiveDocument.Swatches.Remove(color);
+                swatches.Remove(color);
             }
         }
 

+ 17 - 16
PixiEditor/ViewModels/SubViewModels/Main/DocumentViewModel.cs

@@ -3,6 +3,7 @@ using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Enums;
 using System.Windows.Input;
+using PixiEditor.Models.Services;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
@@ -12,35 +13,35 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public const string ConfirmationDialogTitle = "Unsaved changes";
         public const string ConfirmationDialogMessage = "The document has been modified. Do you want to save changes?";
 
-        public DocumentViewModel(ViewModelMain owner)
+        private readonly DocumentProvider _doc;
+        
+        public DocumentViewModel(ViewModelMain owner, DocumentProvider provider)
             : base(owner)
         {
+            _doc = provider;
         }
 
         public void FlipDocument(object parameter)
         {
             if (parameter is "Horizontal")
             {
-                Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Horizontal);
+                _doc.GetDocument().FlipActiveDocument(FlipType.Horizontal);
             }
             else if (parameter is "Vertical")
             {
-                Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Vertical);
+                _doc.GetDocument().FlipActiveDocument(FlipType.Vertical);
             }
         }
 
-        public void RotateDocument(object parameter)
-        {
-            if (parameter is double angle)
-            {
-                Owner.BitmapManager.ActiveDocument?.RotateActiveDocument((float)angle);
-            }
+        public void RotateDocument(float angle)
+        { 
+            _doc.GetDocument().RotateActiveDocument(angle);
         }
 
         [Command.Basic("PixiEditor.Document.ClipCanvas", "Clip Canvas", "Clip Canvas", CanExecute = "PixiEditor.HasDocument")]
         public void ClipCanvas()
         {
-            Owner.BitmapManager.ActiveDocument?.ClipCanvas();
+            _doc.GetDocument().ClipCanvas();
         }
 
         public void RequestCloseDocument(Document document)
@@ -66,7 +67,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         [Command.Basic("PixiEditor.Document.DeletePixels", "Delete pixels", "Delete selected pixels", CanExecute = "PixiEditor.Selection.IsNotEmpty", Key = Key.Delete, Icon = "Tools/EraserImage.png")]
         public void DeletePixels()
         {
-            var doc = Owner.BitmapManager.ActiveDocument;
+            var doc = _doc.GetDocument();
             Owner.BitmapManager.BitmapOperations.DeletePixels(
                 doc.Layers.Where(x => x.IsActive && doc.GetFinalLayerIsVisible(x)).ToArray(),
                 doc.ActiveSelection.SelectedPoints.ToArray());
@@ -77,18 +78,18 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public void OpenResizePopup(bool canvas)
         {
             ResizeDocumentDialog dialog = new ResizeDocumentDialog(
-                Owner.BitmapManager.ActiveDocument.Width,
-                Owner.BitmapManager.ActiveDocument.Height,
+                _doc.GetDocument().Width,
+                _doc.GetDocument().Height,
                 canvas);
             if (dialog.ShowDialog())
             {
                 if (canvas)
                 {
-                    Owner.BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
+                    _doc.GetDocument().ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
                 }
                 else
                 {
-                    Owner.BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
+                    _doc.GetDocument().Resize(dialog.Width, dialog.Height);
                 }
             }
         }
@@ -96,7 +97,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         [Command.Basic("PixiEditor.Document.CenterContent", "Center Content", "Center Content", CanExecute = "PixiEditor.HasDocument")]
         public void CenterContent()
         {
-            Owner.BitmapManager.ActiveDocument.CenterContent();
+            _doc.GetDocument().CenterContent();
         }
     }
 }

+ 22 - 21
PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -13,12 +13,14 @@ using System.IO;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Media.Imaging;
+using PixiEditor.Models.Services;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
     [Command.Group("PixiEditor.File", "File")]
     public class FileViewModel : SubViewModel<ViewModelMain>
     {
+        private readonly DocumentProvider _doc;
         private bool hasRecent;
 
         public bool HasRecent
@@ -33,7 +35,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public RecentlyOpenedCollection RecentlyOpened { get; set; } = new RecentlyOpenedCollection();
 
-        public FileViewModel(ViewModelMain owner)
+        public FileViewModel(ViewModelMain owner, DocumentProvider provider)
             : base(owner)
         {
             Owner.OnStartupEvent += Owner_OnStartupEvent;
@@ -76,11 +78,12 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void NewDocument(int width, int height, bool addBaseLayer = true)
         {
-            Owner.BitmapManager.Documents.Add(new Document(width, height));
-            Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents[^1];
+            Document document = new Document(width, height);
+            _doc.GetDocuments().Add(document);
+            Owner.BitmapManager.ActiveDocument = document;
             if (addBaseLayer)
             {
-                Owner.BitmapManager.ActiveDocument.AddNewLayer("Base Layer");
+                document.AddNewLayer("Base Layer");
             }
 
             Owner.ResetProgramStateValues();
@@ -102,11 +105,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             if (dialog.ShowDialog())
             {
                 NewDocument(dialog.FileWidth, dialog.FileHeight, false);
-                Owner.BitmapManager.ActiveDocument.DocumentFilePath = path;
-                Owner.BitmapManager.ActiveDocument.AddNewLayer(
+                _doc.GetDocument().DocumentFilePath = path;
+                _doc.GetDocument().AddNewLayer(
                     "Image",
                     Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight));
-                Owner.BitmapManager.ActiveDocument.UpdatePreviewImage();
+                _doc.GetDocument().UpdatePreviewImage();
             }
         }
 
@@ -175,9 +178,9 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
                 {
                     Open(dialog.FileName);
 
-                    if (Owner.BitmapManager.Documents.Count > 0)
+                    if (_doc.GetDocuments().Count > 0)
                     {
-                        Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
+                        Owner.BitmapManager.ActiveDocument = _doc.GetDocuments().Last();
                     }
                 }
             }
@@ -185,30 +188,28 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         private void OpenDocument(string path)
         {
-            Document document = Importer.ImportDocument(path);
+            Document document = _doc.GetDocuments().FirstOrDefault(x => x.DocumentFilePath == path);
 
-            if (Owner.BitmapManager.Documents.Select(x => x.DocumentFilePath).All(y => y != path))
+            if (document is null)
             {
-                Owner.BitmapManager.Documents.Add(document);
-                Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
-            }
-            else
-            {
-                Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.First(y => y.DocumentFilePath == path);
+                document = Importer.ImportDocument(path);
+                _doc.GetDocuments().Add(document);
             }
+            
+            Owner.BitmapManager.ActiveDocument = document;
         }
 
         [Command.Basic("PixiEditor.File.Save", false, "Save", "Save image", CanExecute = "PixiEditor.HasDocument", Key = Key.S, Modifiers = ModifierKeys.Control)]
         [Command.Basic("PixiEditor.File.SaveAsNew", true, "Save as...", "Save image as new", CanExecute = "PixiEditor.HasDocument", Key = Key.S, Modifiers = ModifierKeys.Control | ModifierKeys.Shift)]
         public void SaveDocument(bool asNew)
         {
-            if (asNew || string.IsNullOrEmpty(Owner.BitmapManager.ActiveDocument.DocumentFilePath)) 
+            if (asNew || string.IsNullOrEmpty(_doc.GetDocument().DocumentFilePath)) 
             {
-                Owner.BitmapManager.ActiveDocument.SaveWithDialog();
+                _doc.GetDocument().SaveWithDialog();
             }
             else
             {
-                Owner.BitmapManager.ActiveDocument.Save();
+                _doc.GetDocument().Save();
             }
         }
 
@@ -220,7 +221,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public void ExportFile()
         {
             ViewModelMain.Current.ActionDisplay = "";
-            WriteableBitmap bitmap = Owner.BitmapManager.ActiveDocument.Renderer.FinalBitmap;
+            WriteableBitmap bitmap = _doc.GetRenderer().FinalBitmap;
             Exporter.Export(bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
         }
 

+ 9 - 7
PixiEditor/ViewModels/SubViewModels/Main/SelectionViewModel.cs

@@ -4,6 +4,7 @@ using PixiEditor.Models.Enums;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.Tools;
 using System.Windows.Input;
+using PixiEditor.Models.Services;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
@@ -11,8 +12,9 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
     public class SelectionViewModel : SubViewModel<ViewModelMain>
     {
         private readonly SelectTool selectTool;
+        private readonly DocumentProvider _doc;
 
-        public SelectionViewModel(ViewModelMain owner)
+        public SelectionViewModel(ViewModelMain owner, DocumentProvider provider)
             : base(owner)
         {
             selectTool = new SelectTool(Owner.BitmapManager);
@@ -23,24 +25,24 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
             var oldSelection = new List<Coordinates>(Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints);
 
-            Owner.BitmapManager.ActiveDocument.ActiveSelection.SetSelection(selectTool.GetAllSelection(), SelectionType.New);
-            SelectionHelpers.AddSelectionUndoStep(Owner.BitmapManager.ActiveDocument, oldSelection, SelectionType.New);
+            _doc.GetDocument().ActiveSelection.SetSelection(selectTool.GetAllSelection(), SelectionType.New);
+            SelectionHelpers.AddSelectionUndoStep(_doc.GetDocument(), oldSelection, SelectionType.New);
         }
 
         [Command.Basic("PixiEditor.Selection.Clear", "Clear selection", "Clear selection", CanExecute = "PixiEditor.Selection.IsNotEmpty", Key = Key.D, Modifiers = ModifierKeys.Control)]
         public void Deselect()
         {
-            var oldSelection = new List<Coordinates>(Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints);
+            var oldSelection = new List<Coordinates>(_doc.GetDocument().ActiveSelection.SelectedPoints);
 
-            Owner.BitmapManager.ActiveDocument.ActiveSelection?.Clear();
+            _doc.GetDocument().ActiveSelection?.Clear();
 
-            SelectionHelpers.AddSelectionUndoStep(Owner.BitmapManager.ActiveDocument, oldSelection, SelectionType.New);
+            SelectionHelpers.AddSelectionUndoStep(_doc.GetDocument(), oldSelection, SelectionType.New);
         }
 
         [Evaluator.CanExecute("PixiEditor.Selection.IsNotEmpty")]
         public bool SelectionIsNotEmpty()
         {
-            var selectedPoints = Owner.BitmapManager.ActiveDocument?.ActiveSelection.SelectedPoints;
+            var selectedPoints = _doc.GetDocument().ActiveSelection.SelectedPoints;
             return selectedPoints != null && selectedPoints.Count > 0;
         }
     }

+ 11 - 8
PixiEditor/ViewModels/SubViewModels/Main/UndoViewModel.cs

@@ -2,20 +2,23 @@
 using PixiEditor.Models.Undo;
 using System.IO;
 using System.Windows.Input;
+using PixiEditor.Models.Services;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
     [Command.Group("PixiEditor.Undo", "Undo")]
     public class UndoViewModel : SubViewModel<ViewModelMain>
     {
+        private readonly DocumentProvider _doc;
+        
         public event EventHandler UndoRedoCalled;
 
-        public UndoViewModel(ViewModelMain owner)
+        public UndoViewModel(ViewModelMain owner, DocumentProvider provider)
             : base(owner)
         {
             var result = Directory.CreateDirectory(StorageBasedChange.DefaultUndoChangeLocation);
 
-            //ClearUndoTempDirectory();
+            _doc = provider;
         }
 
         /// <summary>
@@ -47,8 +50,8 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             //sometimes CanUndo gets changed after UndoRedoCalled invoke, so check again (normally this is checked by the relaycommand)
             if (CanUndo())
             {
-                Owner.BitmapManager.ActiveDocument.UndoManager.Undo();
-                Owner.BitmapManager.ActiveDocument.ChangesSaved = false;
+                _doc.GetDocument().UndoManager.Undo();
+                _doc.GetDocument().ChangesSaved = false;
             }
         }
 
@@ -69,10 +72,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         /// </summary>
         /// <param name="property">CommandParameter.</param>
         /// <returns>True if can undo.</returns>
-        [Evaluator.CanExecute("PixiEditor.Undo.CanUndo")]
+        [Evaluator.CanExecute("PixiEditor.Undo.CanUndo", requires: "PixiEditor.HasDocument")]
         public bool CanUndo()
         {
-            return Owner.BitmapManager.ActiveDocument?.UndoManager.CanUndo ?? false;
+            return _doc.GetDocument().UndoManager.CanUndo;
         }
 
         /// <summary>
@@ -80,10 +83,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         /// </summary>
         /// <param name="property">CommandProperty.</param>
         /// <returns>True if can redo.</returns>
-        [Evaluator.CanExecute("PixiEditor.Undo.CanRedo")]
+        [Evaluator.CanExecute("PixiEditor.Undo.CanRedo", requires: "PixiEditor.HasDocument")]
         public bool CanRedo()
         {
-            return Owner.BitmapManager.ActiveDocument?.UndoManager.CanRedo ?? false;
+            return _doc.GetDocument().UndoManager.CanRedo;
         }
     }
 }

+ 12 - 9
PixiEditor/ViewModels/SubViewModels/Main/ViewportViewModel.cs

@@ -1,10 +1,12 @@
 using PixiEditor.Models.Commands.Attributes;
 using System.Windows.Input;
+using PixiEditor.Models.Services;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
     public class ViewportViewModel : SubViewModel<ViewModelMain>
     {
+        private readonly DocumentProvider _doc;
         private bool gridLinesEnabled;
 
         public bool GridLinesEnabled
@@ -13,25 +15,26 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             set => SetProperty(ref gridLinesEnabled, value);
         }
 
-        public ViewportViewModel(ViewModelMain owner)
+        public ViewportViewModel(ViewModelMain owner, DocumentProvider provider)
             : base(owner)
         {
+            _doc = provider;
         }
 
-        [Command.Basic("PixiEditor.View.ToggleGrid", "Toggle gridlines", "Toggle gridlines", Key = Key.OemTilde, Modifiers = ModifierKeys.Control)]
+        [Command.Basic("PixiEditor.View.ToggleGrid", "Toggle gridlines", "Toggle gridlines", Key = Key.OemTilde,
+            Modifiers = ModifierKeys.Control)]
         public void ToggleGridLines()
         {
             GridLinesEnabled = !GridLinesEnabled;
         }
 
-        [Command.Basic("PixiEditor.View.ZoomIn", 1, "Zoom in", "Zoom in", CanExecute = "PixiEditor.HasDocument", Key = Key.OemPlus)]
-        [Command.Basic("PixiEditor.View.Zoomout", -1, "Zoom out", "Zoom out", CanExecute = "PixiEditor.HasDocument", Key = Key.OemMinus)]
+        [Command.Basic("PixiEditor.View.ZoomIn", 1, "Zoom in", "Zoom in", CanExecute = "PixiEditor.HasDocument",
+            Key = Key.OemPlus)]
+        [Command.Basic("PixiEditor.View.Zoomout", -1, "Zoom out", "Zoom out", CanExecute = "PixiEditor.HasDocument",
+            Key = Key.OemMinus)]
         public void ZoomViewport(double zoom)
         {
-            if (Owner.BitmapManager.ActiveDocument is not null)
-            {
-                Owner.BitmapManager.ActiveDocument.ZoomViewportTrigger.Execute(this, zoom);
-            }
+            _doc.GetDocument().ZoomViewportTrigger.Execute(this, zoom);
         }
     }
-}
+}