Explorar el Código

Implemented caching

CPKreuz hace 4 años
padre
commit
f52ef7169b

+ 15 - 2
PixiEditor/Models/Layers/GuidStructureItem.cs

@@ -4,6 +4,7 @@ using System.Collections.ObjectModel;
 using System.Diagnostics;
 using System.Linq;
 using PixiEditor.Helpers;
+using PixiEditor.ViewModels;
 
 namespace PixiEditor.Models.Layers
 {
@@ -75,7 +76,13 @@ namespace PixiEditor.Models.Layers
         public bool IsVisible
         {
             get => isVisible;
-            set => SetProperty(ref isVisible, value);
+            set
+            {
+                if (SetProperty(ref isVisible, value))
+                {
+                    ViewModelMain.Current.ToolsSubViewModel.TriggerCacheOutdated();
+                }
+            }
         }
 
         private float opacity = 1;
@@ -83,7 +90,13 @@ namespace PixiEditor.Models.Layers
         public float Opacity
         {
             get => opacity;
-            set => SetProperty(ref opacity, value);
+            set
+            {
+                if (SetProperty(ref opacity, value))
+                {
+                    ViewModelMain.Current.ToolsSubViewModel.TriggerCacheOutdated();
+                }
+            }
         }
 
         public GuidStructureItem(

+ 5 - 7
PixiEditor/Models/Layers/Layer.cs

@@ -96,11 +96,10 @@ namespace PixiEditor.Models.Layers
             get => isVisible;
             set
             {
-                if (isVisible != value)
+                if (SetProperty(ref isVisible, value))
                 {
-                    isVisible = value;
-                    RaisePropertyChanged(nameof(IsVisible));
                     RaisePropertyChanged(nameof(IsVisibleUndoTriggerable));
+                    ViewModelMain.Current.ToolsSubViewModel.TriggerCacheOutdated();
                 }
             }
         }
@@ -151,12 +150,11 @@ namespace PixiEditor.Models.Layers
             get => opacity;
             set
             {
-                if (opacity != value)
+                if (SetProperty(ref opacity, value))
                 {
-                    opacity = value;
+                    RaisePropertyChanged(nameof(OpacityUndoTriggerable));
+                    ViewModelMain.Current.ToolsSubViewModel.TriggerCacheOutdated();
                 }
-                RaisePropertyChanged(nameof(Opacity));
-                RaisePropertyChanged(nameof(OpacityUndoTriggerable));
             }
         }
 

+ 7 - 0
PixiEditor/Models/Tools/ICachedDocumentTool.cs

@@ -0,0 +1,7 @@
+namespace PixiEditor.Models.Tools
+{
+    public interface ICachedDocumentTool
+    {
+        public void DocumentChanged();
+    }
+}

+ 16 - 2
PixiEditor/Models/Tools/Tools/MagicWandTool.cs

@@ -8,13 +8,14 @@ using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
 using PixiEditor.ViewModels;
+using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Windows.Input;
 
 namespace PixiEditor.Models.Tools.Tools
 {
-    public class MagicWandTool : ReadonlyTool
+    public class MagicWandTool : ReadonlyTool, ICachedDocumentTool
     {
         private readonly FloodFill floodFill;
 
@@ -24,6 +25,8 @@ namespace PixiEditor.Models.Tools.Tools
 
         private IEnumerable<Coordinates> oldSelection;
 
+        private Layer cachedDocument;
+
         public override void OnRecordingLeftMouseDown(MouseEventArgs e)
         {
             if (e.LeftButton != MouseButtonState.Pressed)
@@ -45,7 +48,8 @@ namespace PixiEditor.Models.Tools.Tools
             }
             else
             {
-                layer = new Layer("_CombinedLayers", BitmapUtils.CombineLayers(document.Width, document.Height, document.Layers, document.LayerStructure));
+                ValidateCache(document);
+                layer = cachedDocument;
             }
 
             Selection selection = BitmapManager.ActiveDocument.ActiveSelection;
@@ -75,5 +79,15 @@ namespace PixiEditor.Models.Tools.Tools
         public override void Use(List<Coordinates> pixels)
         {
         }
+
+        public void DocumentChanged()
+        {
+            cachedDocument = null;
+        }
+
+        private void ValidateCache(Document document)
+        {
+            cachedDocument ??= new Layer("_CombinedLayers", BitmapUtils.CombineLayers(document.Width, document.Height, document.Layers, document.LayerStructure));
+        }
     }
 }

+ 36 - 10
PixiEditor/ViewModels/SubViewModels/Main/ToolsViewModel.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Reflection;
 using System.Windows.Input;
 using PixiEditor.Helpers;
+using PixiEditor.Models.Enums;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
 
@@ -36,6 +37,9 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         {
             SelectToolCommand = new RelayCommand(SetTool, Owner.DocumentIsNotNull);
             ChangeToolSizeCommand = new RelayCommand(ChangeToolSize);
+
+            Owner.BitmapManager.BitmapOperations.BitmapChanged += (_, _) => TriggerCacheOutdated();
+            Owner.BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
         }
 
         public void SetupTools(IServiceProvider services)
@@ -87,23 +91,45 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             SetActiveTool(tool.GetType());
         }
 
-        private static T CreateTool<T>(IServiceProvider provider)
-            where T : new()
+        public void TriggerCacheOutdated()
         {
-            T tool = default;
-            Type toolType = typeof(T);
-
-            foreach (PropertyInfo info in toolType.GetProperties(BindingFlags.Public))
+            foreach (Tool tool in ToolSet)
             {
-                if (!info.CanWrite)
+                if (tool is ICachedDocumentTool cachedTool)
                 {
-                    continue;
+                    cachedTool.DocumentChanged();
                 }
+            }
+        }
 
-                info.SetValue(tool, provider.GetService(info.PropertyType));
+        private void BitmapManager_DocumentChanged(object sender, Models.Events.DocumentChangedEventArgs e)
+        {
+            if (e.OldDocument != null)
+            {
+                e.OldDocument.DocumentSizeChanged -= Document_DocumentSizeChanged;
+                e.NewDocument.LayersChanged -= Document_LayersChanged;
+            }
+
+            if (e.NewDocument != null)
+            {
+                e.NewDocument.DocumentSizeChanged += Document_DocumentSizeChanged;
+                e.NewDocument.LayersChanged += Document_LayersChanged;
             }
 
-            return tool;
+            TriggerCacheOutdated();
+
+            void Document_DocumentSizeChanged(object sender, Models.DataHolders.DocumentSizeChangedEventArgs e)
+            {
+                TriggerCacheOutdated();
+            }
+
+            void Document_LayersChanged(object sender, Models.Controllers.LayersChangedEventArgs e)
+            {
+                if (e.LayerChangeType is LayerAction.Add or LayerAction.Remove or LayerAction.Move)
+                {
+                    TriggerCacheOutdated();
+                }
+            }
         }
 
         private void ChangeToolSize(object parameter)