Browse Source

No one saw that please

This reverts commit 30223d79f425b84e19480a6b0fff8d7f2688edf2.
CPKreuz 3 years ago
parent
commit
ccad4b99b1

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

@@ -1,7 +1,7 @@
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
-using SkiaSharp;
+using System.Collections.Generic;
 
 namespace PixiEditor.Models.Services
 {
@@ -20,7 +20,7 @@ namespace PixiEditor.Models.Services
         /// <summary>
         /// Gets all opened documents
         /// </summary>
-        public ICollection<Document> GetDocuments() => _bitmapManager.Documents;
+        public IEnumerable<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 ICollection<Layer> GetLayers() => _bitmapManager.ActiveDocument?.Layers;
+        public IEnumerable<Layer> GetLayers() => _bitmapManager.ActiveDocument?.Layers;
 
         /// <summary>
         /// Gets the layer structure of the opened document
@@ -66,7 +66,5 @@ 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;
     }
 }

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

@@ -7,16 +7,13 @@ 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>
     {
-        private readonly DocumentProvider _doc;
-        
-        public ClipboardViewModel(ViewModelMain owner, DocumentProvider provider)
+        public ClipboardViewModel(ViewModelMain owner)
             : base(owner)
         {
         }
@@ -33,15 +30,15 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
             Copy();
             Owner.BitmapManager.BitmapOperations.DeletePixels(
-                new[] { _doc.GetLayer() },
-                _doc.GetDocument().ActiveSelection.SelectedPoints.ToArray());
+                new[] { Owner.BitmapManager.ActiveDocument.ActiveLayer },
+                Owner.BitmapManager.ActiveDocument.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 (_doc.GetDocument() == null) return;
-            ClipboardController.PasteFromClipboard(_doc.GetDocument());
+            if (Owner.BitmapManager.ActiveDocument == null) return;
+            ClipboardController.PasteFromClipboard(Owner.BitmapManager.ActiveDocument);
         }
 
         [Command.Basic("PixiEditor.Clipboard.PasteColor", "Paste color", "Paste color from clipboard", CanExecute = "PixiEditor.Clipboard.CanPasteColor", IconEvaluator = "PixiEditor.Clipboard.PasteColorIcon")]
@@ -56,10 +53,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             ClipboardController.CopyToClipboard(Owner.BitmapManager.ActiveDocument);
         }
 
-        [Evaluator.CanExecute("PixiEditor.Clipboard.CanPaste", requires: "PixiEditor.HasDocument")]
+        [Evaluator.CanExecute("PixiEditor.Clipboard.CanPaste")]
         public bool CanPaste()
         {
-            return ClipboardController.IsImageInClipboard();
+            return Owner.DocumentIsNotNull(null) && ClipboardController.IsImageInClipboard();
         }
 
         [Evaluator.CanExecute("PixiEditor.Clipboard.CanPasteColor")]

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

@@ -1,14 +1,12 @@
 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
         {
@@ -39,10 +37,9 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
         }
 
-        public ColorsViewModel(ViewModelMain owner, DocumentProvider provider)
+        public ColorsViewModel(ViewModelMain owner)
             : base(owner)
         {
-            _doc = provider;
         }
 
         [Command.Basic("PixiEditor.Colors.Swap", "Swap colors", "Swap primary and secondary colors", Key = Key.X)]
@@ -55,20 +52,18 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void AddSwatch(SKColor color)
         {
-            var swatches = _doc.GetSwatches();
-            if (!swatches.Contains(color))
+            if (!Owner.BitmapManager.ActiveDocument.Swatches.Contains(color))
             {
-                swatches.Add(color);
+                Owner.BitmapManager.ActiveDocument.Swatches.Add(color);
             }
         }
 
         [Command.Internal("PixiEditor.Colors.RemoveSwatch")]
         public void RemoveSwatch(SKColor color)
         {
-            var swatches = _doc.GetSwatches();
-            if (swatches.Contains(color))
+            if (Owner.BitmapManager.ActiveDocument.Swatches.Contains(color))
             {
-                swatches.Remove(color);
+                Owner.BitmapManager.ActiveDocument.Swatches.Remove(color);
             }
         }
 

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

@@ -3,7 +3,6 @@ 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
 {
@@ -13,35 +12,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?";
 
-        private readonly DocumentProvider _doc;
-        
-        public DocumentViewModel(ViewModelMain owner, DocumentProvider provider)
+        public DocumentViewModel(ViewModelMain owner)
             : base(owner)
         {
-            _doc = provider;
         }
 
         public void FlipDocument(object parameter)
         {
             if (parameter is "Horizontal")
             {
-                _doc.GetDocument().FlipActiveDocument(FlipType.Horizontal);
+                Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Horizontal);
             }
             else if (parameter is "Vertical")
             {
-                _doc.GetDocument().FlipActiveDocument(FlipType.Vertical);
+                Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Vertical);
             }
         }
 
-        public void RotateDocument(float angle)
-        { 
-            _doc.GetDocument().RotateActiveDocument(angle);
+        public void RotateDocument(object parameter)
+        {
+            if (parameter is double angle)
+            {
+                Owner.BitmapManager.ActiveDocument?.RotateActiveDocument((float)angle);
+            }
         }
 
         [Command.Basic("PixiEditor.Document.ClipCanvas", "Clip Canvas", "Clip Canvas", CanExecute = "PixiEditor.HasDocument")]
         public void ClipCanvas()
         {
-            _doc.GetDocument().ClipCanvas();
+            Owner.BitmapManager.ActiveDocument?.ClipCanvas();
         }
 
         public void RequestCloseDocument(Document document)
@@ -67,7 +66,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 = _doc.GetDocument();
+            var doc = Owner.BitmapManager.ActiveDocument;
             Owner.BitmapManager.BitmapOperations.DeletePixels(
                 doc.Layers.Where(x => x.IsActive && doc.GetFinalLayerIsVisible(x)).ToArray(),
                 doc.ActiveSelection.SelectedPoints.ToArray());
@@ -78,18 +77,18 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public void OpenResizePopup(bool canvas)
         {
             ResizeDocumentDialog dialog = new ResizeDocumentDialog(
-                _doc.GetDocument().Width,
-                _doc.GetDocument().Height,
+                Owner.BitmapManager.ActiveDocument.Width,
+                Owner.BitmapManager.ActiveDocument.Height,
                 canvas);
             if (dialog.ShowDialog())
             {
                 if (canvas)
                 {
-                    _doc.GetDocument().ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
+                    Owner.BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
                 }
                 else
                 {
-                    _doc.GetDocument().Resize(dialog.Width, dialog.Height);
+                    Owner.BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
                 }
             }
         }
@@ -97,7 +96,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         [Command.Basic("PixiEditor.Document.CenterContent", "Center Content", "Center Content", CanExecute = "PixiEditor.HasDocument")]
         public void CenterContent()
         {
-            _doc.GetDocument().CenterContent();
+            Owner.BitmapManager.ActiveDocument.CenterContent();
         }
     }
 }

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

@@ -13,14 +13,12 @@ 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
@@ -35,7 +33,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public RecentlyOpenedCollection RecentlyOpened { get; set; } = new RecentlyOpenedCollection();
 
-        public FileViewModel(ViewModelMain owner, DocumentProvider provider)
+        public FileViewModel(ViewModelMain owner)
             : base(owner)
         {
             Owner.OnStartupEvent += Owner_OnStartupEvent;
@@ -78,12 +76,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void NewDocument(int width, int height, bool addBaseLayer = true)
         {
-            Document document = new Document(width, height);
-            _doc.GetDocuments().Add(document);
-            Owner.BitmapManager.ActiveDocument = document;
+            Owner.BitmapManager.Documents.Add(new Document(width, height));
+            Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents[^1];
             if (addBaseLayer)
             {
-                document.AddNewLayer("Base Layer");
+                Owner.BitmapManager.ActiveDocument.AddNewLayer("Base Layer");
             }
 
             Owner.ResetProgramStateValues();
@@ -105,11 +102,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             if (dialog.ShowDialog())
             {
                 NewDocument(dialog.FileWidth, dialog.FileHeight, false);
-                _doc.GetDocument().DocumentFilePath = path;
-                _doc.GetDocument().AddNewLayer(
+                Owner.BitmapManager.ActiveDocument.DocumentFilePath = path;
+                Owner.BitmapManager.ActiveDocument.AddNewLayer(
                     "Image",
                     Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight));
-                _doc.GetDocument().UpdatePreviewImage();
+                Owner.BitmapManager.ActiveDocument.UpdatePreviewImage();
             }
         }
 
@@ -178,9 +175,9 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
                 {
                     Open(dialog.FileName);
 
-                    if (_doc.GetDocuments().Count > 0)
+                    if (Owner.BitmapManager.Documents.Count > 0)
                     {
-                        Owner.BitmapManager.ActiveDocument = _doc.GetDocuments().Last();
+                        Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
                     }
                 }
             }
@@ -188,28 +185,30 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         private void OpenDocument(string path)
         {
-            Document document = _doc.GetDocuments().FirstOrDefault(x => x.DocumentFilePath == path);
+            Document document = Importer.ImportDocument(path);
 
-            if (document is null)
+            if (Owner.BitmapManager.Documents.Select(x => x.DocumentFilePath).All(y => y != path))
             {
-                document = Importer.ImportDocument(path);
-                _doc.GetDocuments().Add(document);
+                Owner.BitmapManager.Documents.Add(document);
+                Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
+            }
+            else
+            {
+                Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.First(y => y.DocumentFilePath == path);
             }
-            
-            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(_doc.GetDocument().DocumentFilePath)) 
+            if (asNew || string.IsNullOrEmpty(Owner.BitmapManager.ActiveDocument.DocumentFilePath)) 
             {
-                _doc.GetDocument().SaveWithDialog();
+                Owner.BitmapManager.ActiveDocument.SaveWithDialog();
             }
             else
             {
-                _doc.GetDocument().Save();
+                Owner.BitmapManager.ActiveDocument.Save();
             }
         }
 
@@ -221,7 +220,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public void ExportFile()
         {
             ViewModelMain.Current.ActionDisplay = "";
-            WriteableBitmap bitmap = _doc.GetRenderer().FinalBitmap;
+            WriteableBitmap bitmap = Owner.BitmapManager.ActiveDocument.Renderer.FinalBitmap;
             Exporter.Export(bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
         }
 

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

@@ -4,7 +4,6 @@ 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
 {
@@ -12,9 +11,8 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
     public class SelectionViewModel : SubViewModel<ViewModelMain>
     {
         private readonly SelectTool selectTool;
-        private readonly DocumentProvider _doc;
 
-        public SelectionViewModel(ViewModelMain owner, DocumentProvider provider)
+        public SelectionViewModel(ViewModelMain owner)
             : base(owner)
         {
             selectTool = new SelectTool(Owner.BitmapManager);
@@ -25,24 +23,24 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
             var oldSelection = new List<Coordinates>(Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints);
 
-            _doc.GetDocument().ActiveSelection.SetSelection(selectTool.GetAllSelection(), SelectionType.New);
-            SelectionHelpers.AddSelectionUndoStep(_doc.GetDocument(), oldSelection, SelectionType.New);
+            Owner.BitmapManager.ActiveDocument.ActiveSelection.SetSelection(selectTool.GetAllSelection(), SelectionType.New);
+            SelectionHelpers.AddSelectionUndoStep(Owner.BitmapManager.ActiveDocument, 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>(_doc.GetDocument().ActiveSelection.SelectedPoints);
+            var oldSelection = new List<Coordinates>(Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints);
 
-            _doc.GetDocument().ActiveSelection?.Clear();
+            Owner.BitmapManager.ActiveDocument.ActiveSelection?.Clear();
 
-            SelectionHelpers.AddSelectionUndoStep(_doc.GetDocument(), oldSelection, SelectionType.New);
+            SelectionHelpers.AddSelectionUndoStep(Owner.BitmapManager.ActiveDocument, oldSelection, SelectionType.New);
         }
 
         [Evaluator.CanExecute("PixiEditor.Selection.IsNotEmpty")]
         public bool SelectionIsNotEmpty()
         {
-            var selectedPoints = _doc.GetDocument().ActiveSelection.SelectedPoints;
+            var selectedPoints = Owner.BitmapManager.ActiveDocument?.ActiveSelection.SelectedPoints;
             return selectedPoints != null && selectedPoints.Count > 0;
         }
     }

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

@@ -2,23 +2,20 @@
 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, DocumentProvider provider)
+        public UndoViewModel(ViewModelMain owner)
             : base(owner)
         {
             var result = Directory.CreateDirectory(StorageBasedChange.DefaultUndoChangeLocation);
 
-            _doc = provider;
+            //ClearUndoTempDirectory();
         }
 
         /// <summary>
@@ -50,8 +47,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())
             {
-                _doc.GetDocument().UndoManager.Undo();
-                _doc.GetDocument().ChangesSaved = false;
+                Owner.BitmapManager.ActiveDocument.UndoManager.Undo();
+                Owner.BitmapManager.ActiveDocument.ChangesSaved = false;
             }
         }
 
@@ -72,10 +69,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         /// </summary>
         /// <param name="property">CommandParameter.</param>
         /// <returns>True if can undo.</returns>
-        [Evaluator.CanExecute("PixiEditor.Undo.CanUndo", requires: "PixiEditor.HasDocument")]
+        [Evaluator.CanExecute("PixiEditor.Undo.CanUndo")]
         public bool CanUndo()
         {
-            return _doc.GetDocument().UndoManager.CanUndo;
+            return Owner.BitmapManager.ActiveDocument?.UndoManager.CanUndo ?? false;
         }
 
         /// <summary>
@@ -83,10 +80,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         /// </summary>
         /// <param name="property">CommandProperty.</param>
         /// <returns>True if can redo.</returns>
-        [Evaluator.CanExecute("PixiEditor.Undo.CanRedo", requires: "PixiEditor.HasDocument")]
+        [Evaluator.CanExecute("PixiEditor.Undo.CanRedo")]
         public bool CanRedo()
         {
-            return _doc.GetDocument().UndoManager.CanRedo;
+            return Owner.BitmapManager.ActiveDocument?.UndoManager.CanRedo ?? false;
         }
     }
 }

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

@@ -1,12 +1,10 @@
 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
@@ -15,26 +13,25 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             set => SetProperty(ref gridLinesEnabled, value);
         }
 
-        public ViewportViewModel(ViewModelMain owner, DocumentProvider provider)
+        public ViewportViewModel(ViewModelMain owner)
             : 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)
         {
-            _doc.GetDocument().ZoomViewportTrigger.Execute(this, zoom);
+            if (Owner.BitmapManager.ActiveDocument is not null)
+            {
+                Owner.BitmapManager.ActiveDocument.ZoomViewportTrigger.Execute(this, zoom);
+            }
         }
     }
-}
+}