Equbuxu преди 3 години
родител
ревизия
37e792063f

+ 0 - 1
src/PixiEditor/Helpers/Extensions/ServiceCollectionHelpers.cs

@@ -35,7 +35,6 @@ internal static class ServiceCollectionHelpers
         .AddSingleton<SelectionViewModel>()
         .AddSingleton<ViewportViewModel>()
         .AddSingleton<ColorsViewModel>()
-        .AddSingleton<DocumentViewModel>()
         .AddSingleton<RegistryViewModel>()
         .AddSingleton(static x => new DiscordViewModel(x.GetService<ViewModelMain>(), "764168193685979138"))
         .AddSingleton<DebugViewModel>()

+ 16 - 0
src/PixiEditor/Models/Events/DocumentChangedEventArgs.cs

@@ -0,0 +1,16 @@
+using PixiEditor.ViewModels.SubViewModels.Document;
+
+namespace PixiEditor.Models.Events;
+
+internal class DocumentChangedEventArgs
+{
+    public DocumentChangedEventArgs(DocumentViewModel newDocument, DocumentViewModel oldDocument)
+    {
+        NewDocument = newDocument;
+        OldDocument = oldDocument;
+    }
+
+    public DocumentViewModel OldDocument { get; set; }
+
+    public DocumentViewModel NewDocument { get; set; }
+}

+ 96 - 15
src/PixiEditor/ViewModels/SubViewModels/Document/DocumentManagerViewModel.cs

@@ -1,46 +1,53 @@
 using System.Collections.ObjectModel;
+using System.Windows.Input;
 using ChunkyImageLib.DataHolders;
 using PixiEditor.Models.Commands.Attributes;
 using PixiEditor.Models.Controllers;
+using PixiEditor.Models.Events;
 using PixiEditor.Models.Tools;
-using PixiEditor.ViewModels.SubViewModels.Main;
 
 namespace PixiEditor.ViewModels.SubViewModels.Document;
+#nullable enable
+[Command.Group("PixiEditor.Document", "Image")]
 internal class DocumentManagerViewModel : SubViewModel<ViewModelMain>
 {
     public DocumentManagerViewModel(ViewModelMain owner) : base(owner)
     {
-        ToolSessionController = new ToolSessionController();
+        /*ToolSessionController = new ToolSessionController();
         ToolSessionController.SessionStarted += OnSessionStart;
         ToolSessionController.SessionEnded += OnSessionEnd;
         ToolSessionController.PixelMousePositionChanged += OnPixelMousePositionChange;
         ToolSessionController.PreciseMousePositionChanged += OnPreciseMousePositionChange;
-        ToolSessionController.KeyStateChanged += (_, _) => UpdateActionDisplay(_tools.ActiveTool);
+        ToolSessionController.KeyStateChanged += (_, _) => UpdateActionDisplay(_tools.ActiveTool);*/
 
         //undo.UndoRedoCalled += (_, _) => ToolSessionController.ForceStopActiveSessionIfAny();
     }
 
-    private ToolSessionController ToolSessionController { get; set; }
-    public ICanvasInputTarget InputTarget => ToolSessionController;
+    //private ToolSessionController ToolSessionController { get; set; }
+    //public ICanvasInputTarget InputTarget => ToolSessionController;
 
     public ObservableCollection<DocumentViewModel> Documents { get; set; } = new ObservableCollection<DocumentViewModel>();
+    public event EventHandler<DocumentChangedEventArgs>? ActiveDocumentChanged;
 
-    private DocumentViewModel activeDocument;
-    public DocumentViewModel ActiveDocument
+
+    private DocumentViewModel? activeDocument;
+    public DocumentViewModel? ActiveDocument
     {
         get => activeDocument;
         set
         {
             if (activeDocument == value)
                 return;
+            var prevDoc = activeDocument;
             activeDocument = value;
             RaisePropertyChanged(nameof(ActiveDocument));
+            ActiveDocumentChanged?.Invoke(this, new(value, prevDoc));
             ActiveWindow = value;
         }
     }
 
-    private object activeWindow;
-    public object ActiveWindow
+    private object? activeWindow;
+    public object? ActiveWindow
     {
         get => activeWindow;
         set
@@ -54,11 +61,11 @@ internal class DocumentManagerViewModel : SubViewModel<ViewModelMain>
         }
     }
 
-    public event EventHandler StopUsingTool;
+    public event EventHandler? StopUsingTool;
 
-    private readonly ToolsViewModel _tools;
+    //private readonly ToolsViewModel _tools;
 
-    private ToolSession activeSession = null;
+    private ToolSession? activeSession = null;
 
     [Evaluator.CanExecute("PixiEditor.HasDocument")]
     public bool DocumentNotNull() => ActiveDocument != null;
@@ -80,7 +87,7 @@ internal class DocumentManagerViewModel : SubViewModel<ViewModelMain>
 
     public void UpdateActionDisplay(Tool tool)
     {
-        tool?.UpdateActionDisplay(ToolSessionController.IsCtrlDown, ToolSessionController.IsShiftDown, ToolSessionController.IsAltDown);
+        //tool?.UpdateActionDisplay(ToolSessionController.IsCtrlDown, ToolSessionController.IsShiftDown, ToolSessionController.IsAltDown);
     }
 
     private void OnSessionStart(object sender, ToolSession e)
@@ -94,7 +101,7 @@ internal class DocumentManagerViewModel : SubViewModel<ViewModelMain>
     {
         activeSession = null;
 
-        HighlightPixels(ToolSessionController.LastPixelPosition);
+        //HighlightPixels(ToolSessionController.LastPixelPosition);
         StopUsingTool?.Invoke(this, EventArgs.Empty);
     }
 
@@ -152,11 +159,85 @@ internal class DocumentManagerViewModel : SubViewModel<ViewModelMain>
         if (activeSession != null)
             return;
 
-        HighlightPixels(forceHide ? new(-1, -1) : ToolSessionController.LastPixelPosition);
+        //HighlightPixels(forceHide ? new(-1, -1) : ToolSessionController.LastPixelPosition);
     }
 
     private void HighlightPixels(VecI position)
     {
 
     }
+
+
+    [Command.Basic("PixiEditor.Document.ClipCanvas", "Clip Canvas", "Clip Canvas", CanExecute = "PixiEditor.HasDocument")]
+    public void ClipCanvas()
+    {
+        //Owner.BitmapManager.ActiveDocument?.ClipCanvas();
+    }
+
+    /*
+    public void RequestCloseDocument(Document document)
+    {
+        /*
+        if (!document.ChangesSaved)
+        {
+            ConfirmationType result = ConfirmationDialog.Show(ConfirmationDialogMessage, ConfirmationDialogTitle);
+            if (result == ConfirmationType.Yes)
+            {
+                Owner.FileSubViewModel.SaveDocument(false);
+                if (!document.ChangesSaved)
+                    return;
+            }
+            else if (result == ConfirmationType.Canceled)
+            {
+                return;
+            }
+        }
+
+        Owner.BitmapManager.CloseDocument(document);
+        
+    }
+*/
+
+    [Command.Basic("PixiEditor.Document.DeletePixels", "Delete pixels", "Delete selected pixels", CanExecute = "PixiEditor.Selection.IsNotEmpty", Key = Key.Delete, IconPath = "Tools/EraserImage.png")]
+    public void DeletePixels()
+    {
+        /*
+        var doc = Owner.BitmapManager.ActiveDocument;
+        Owner.BitmapManager.BitmapOperations.DeletePixels(
+            doc.Layers.Where(x => x.IsActive && doc.GetFinalLayerIsVisible(x)).ToArray(),
+            doc.ActiveSelection.SelectedPoints.ToArray());
+        */
+    }
+
+
+    [Command.Basic("PixiEditor.Document.ResizeDocument", false, "Resize Document", "Resize Document", CanExecute = "PixiEditor.HasDocument", Key = Key.I, Modifiers = ModifierKeys.Control | ModifierKeys.Shift)]
+    [Command.Basic("PixiEditor.Document.ResizeCanvas", true, "Resize Canvas", "Resize Canvas", CanExecute = "PixiEditor.HasDocument", Key = Key.C, Modifiers = ModifierKeys.Control | ModifierKeys.Shift)]
+    public void OpenResizePopup(bool canvas)
+    {
+        /*
+        ResizeDocumentDialog dialog = new ResizeDocumentDialog(
+            Owner.BitmapManager.ActiveDocument.Width,
+            Owner.BitmapManager.ActiveDocument.Height,
+            canvas);
+        if (dialog.ShowDialog())
+        {
+            if (canvas)
+            {
+                Owner.BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
+            }
+            else
+            {
+                Owner.BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
+            }
+        }
+        
+*/
+    }
+
+    [Command.Basic("PixiEditor.Document.CenterContent", "Center Content", "Center Content", CanExecute = "PixiEditor.HasDocument")]
+    public void CenterContent()
+    {
+        //Owner.BitmapManager.ActiveDocument.CenterContent();
+    }
+
 }

+ 2 - 99
src/PixiEditor/ViewModels/SubViewModels/Document/DocumentViewModel.cs

@@ -1,109 +1,12 @@
-using System.Windows.Input;
-using PixiEditor.Models.Commands.Attributes;
+namespace PixiEditor.ViewModels.SubViewModels.Document;
 
-
-namespace PixiEditor.ViewModels.SubViewModels.Document;
-
-[Command.Group("PixiEditor.Document", "Image")]
 internal class DocumentViewModel : SubViewModel<ViewModelMain>
 {
     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)
+    public DocumentViewModel(ViewModelMain owner, string name)
         : base(owner)
     {
     }
-
-    public void FlipDocument(object parameter)
-    {
-        /*
-        if (parameter is "Horizontal")
-        {
-            Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Horizontal);
-        }
-        else if (parameter is "Vertical")
-        {
-            Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Vertical);
-        }
-        */
-    }
-
-    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()
-    {
-        //Owner.BitmapManager.ActiveDocument?.ClipCanvas();
-    }
-    /*
-    public void RequestCloseDocument(Document document)
-    {
-        /*
-        if (!document.ChangesSaved)
-        {
-            ConfirmationType result = ConfirmationDialog.Show(ConfirmationDialogMessage, ConfirmationDialogTitle);
-            if (result == ConfirmationType.Yes)
-            {
-                Owner.FileSubViewModel.SaveDocument(false);
-                if (!document.ChangesSaved)
-                    return;
-            }
-            else if (result == ConfirmationType.Canceled)
-            {
-                return;
-            }
-        }
-
-        Owner.BitmapManager.CloseDocument(document);
-        
-    }
-*/
-    [Command.Basic("PixiEditor.Document.DeletePixels", "Delete pixels", "Delete selected pixels", CanExecute = "PixiEditor.Selection.IsNotEmpty", Key = Key.Delete, IconPath = "Tools/EraserImage.png")]
-    public void DeletePixels()
-    {
-        /*
-        var doc = Owner.BitmapManager.ActiveDocument;
-        Owner.BitmapManager.BitmapOperations.DeletePixels(
-            doc.Layers.Where(x => x.IsActive && doc.GetFinalLayerIsVisible(x)).ToArray(),
-            doc.ActiveSelection.SelectedPoints.ToArray());
-        */
-    }
-
-    [Command.Basic("PixiEditor.Document.ResizeDocument", false, "Resize Document", "Resize Document", CanExecute = "PixiEditor.HasDocument", Key = Key.I, Modifiers = ModifierKeys.Control | ModifierKeys.Shift)]
-    [Command.Basic("PixiEditor.Document.ResizeCanvas", true, "Resize Canvas", "Resize Canvas", CanExecute = "PixiEditor.HasDocument", Key = Key.C, Modifiers = ModifierKeys.Control | ModifierKeys.Shift)]
-    public void OpenResizePopup(bool canvas)
-    {
-        /*
-        ResizeDocumentDialog dialog = new ResizeDocumentDialog(
-            Owner.BitmapManager.ActiveDocument.Width,
-            Owner.BitmapManager.ActiveDocument.Height,
-            canvas);
-        if (dialog.ShowDialog())
-        {
-            if (canvas)
-            {
-                Owner.BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
-            }
-            else
-            {
-                Owner.BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
-            }
-        }
-        */
-    }
-
-    [Command.Basic("PixiEditor.Document.CenterContent", "Center Content", "Center Content", CanExecute = "PixiEditor.HasDocument")]
-    public void CenterContent()
-    {
-        //Owner.BitmapManager.ActiveDocument.CenterContent();
-    }
 }

+ 4 - 3
src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -4,6 +4,7 @@ using PixiEditor.Models.Commands.Attributes;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.UserPreferences;
+using PixiEditor.ViewModels.SubViewModels.Document;
 using PixiEditor.Views.Dialogs;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main;
@@ -68,9 +69,9 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
 
     public void NewDocument(int width, int height, bool addBaseLayer = true)
     {
+        Owner.DocumentManagerSubViewModel.Documents.Add(new DocumentViewModel(Owner, "Unnamed"));
+        Owner.DocumentManagerSubViewModel.ActiveDocument = Owner.DocumentManagerSubViewModel.Documents[^1];
         /*
-        Owner.BitmapManager.Documents.Add(new Document(width, height));
-        Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents[^1];
         if (addBaseLayer)
         {
             Owner.BitmapManager.ActiveDocument.AddNewLayer("Base Layer");
@@ -221,7 +222,7 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
 
         if (Owner.BitmapManager.Documents.Select(x => x.DocumentFilePath).All(y => y != path))
         {
-          j  Owner.BitmapManager.Documents.Add(document);
+            Owner.BitmapManager.Documents.Add(document);
             Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
         }
         else

+ 2 - 5
src/PixiEditor/ViewModels/ViewModelMain.cs

@@ -50,15 +50,13 @@ internal class ViewModelMain : ViewModelBase
 
     public ColorsViewModel ColorsSubViewModel { get; set; }
 
-    public DocumentViewModel DocumentSubViewModel { get; set; }
-
     public MiscViewModel MiscSubViewModel { get; set; }
 
     public DiscordViewModel DiscordViewModel { get; set; }
 
     public DebugViewModel DebugSubViewModel { get; set; }
 
-    public DocumentManagerViewModel DocumentManagerViewModel { get; set; }
+    public DocumentManagerViewModel DocumentManagerSubViewModel { get; set; }
 
     public CommandController CommandController { get; set; }
 
@@ -116,7 +114,7 @@ internal class ViewModelMain : ViewModelBase
         Preferences = services.GetRequiredService<IPreferences>();
 
         Preferences.Init();
-        DocumentManagerViewModel = services.GetRequiredService<DocumentManagerViewModel>();
+        DocumentManagerSubViewModel = services.GetRequiredService<DocumentManagerViewModel>();
 
         SelectionSubViewModel = services.GetService<SelectionViewModel>();
 
@@ -137,7 +135,6 @@ internal class ViewModelMain : ViewModelBase
 
         ToolsSubViewModel?.SetupTools(services);
 
-        DocumentSubViewModel = services.GetService<DocumentViewModel>();
         DiscordViewModel = services.GetService<DiscordViewModel>();
         UpdateSubViewModel = services.GetService<UpdateViewModel>();
         DebugSubViewModel = services.GetService<DebugViewModel>();

+ 4 - 3
src/PixiEditor/Views/MainWindow.xaml

@@ -10,6 +10,7 @@
     xmlns:local="clr-namespace:PixiEditor"
     xmlns:tools="clr-namespace:PixiEditor.Models.Tools.Tools"
     xmlns:vm="clr-namespace:PixiEditor.ViewModels"
+    xmlns:doc="clr-namespace:PixiEditor.ViewModels.SubViewModels.Document"
     xmlns:dataHolders="clr-namespace:PixiEditor.Models.DataHolders"
     xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
@@ -480,8 +481,8 @@
             </StackPanel>
             <Grid Grid.Column="1" Grid.Row="2" Background="#303030" >
                 <Grid AllowDrop="True" Drop="MainWindow_Drop">
-                    <DockingManager ActiveContent="{Binding BitmapManager.ActiveWindow, Mode=TwoWay}"
-                                           DocumentsSource="{Binding BitmapManager.Documents}">
+                    <DockingManager ActiveContent="{Binding DocumentManagerSubViewModel.ActiveWindow, Mode=TwoWay}"
+                                           DocumentsSource="{Binding DocumentManagerSubViewModel.Documents}">
                         <DockingManager.Theme>
                             <avalonDockTheme:PixiEditorDockTheme />
                         </DockingManager.Theme>
@@ -499,7 +500,7 @@
                         <DockingManager.LayoutItemTemplateSelector>
                             <ui:DocumentsTemplateSelector>
                                 <ui:DocumentsTemplateSelector.DocumentsViewTemplate>
-                                    <DataTemplate DataType="{x:Type dataHolders:Document}">
+                                    <DataTemplate DataType="{x:Type doc:DocumentViewModel}">
                                         <usercontrols:DrawingViewPort
                                         CenterViewportTrigger="{Binding CenterViewportTrigger}"
                                         ZoomViewportTrigger="{Binding ZoomViewportTrigger}"