Browse Source

Added simple caching for converter and fixed group visiblity button

flabbet 4 years ago
parent
commit
8026f304d5

+ 69 - 1
PixiEditor/Helpers/Converters/LayersToStructuredLayersConverter.cs

@@ -1,23 +1,91 @@
 using System;
 using System;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Collections.ObjectModel;
+using System.Diagnostics;
 using System.Globalization;
 using System.Globalization;
+using System.Linq;
 using System.Windows.Data;
 using System.Windows.Data;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 
 
 namespace PixiEditor.Helpers.Converters
 namespace PixiEditor.Helpers.Converters
 {
 {
+    //TODO: Implement rebuilding only changed items instead whole tree
     public class LayersToStructuredLayersConverter : IMultiValueConverter
     public class LayersToStructuredLayersConverter : IMultiValueConverter
     {
     {
+        private static StructuredLayerTree cachedTree;
+        private List<Guid> lastLayers = new List<Guid>();
+        private ObservableCollection<GuidStructureItem> lastStructure = new ObservableCollection<GuidStructureItem>();
+
         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
         {
         {
             if (values[0] is ObservableCollection<Layer> layers && values[1] is LayerStructure structure)
             if (values[0] is ObservableCollection<Layer> layers && values[1] is LayerStructure structure)
             {
             {
-                return new StructuredLayerTree(layers, structure).RootDirectoryItems;
+                if (cachedTree == null)
+                {
+                    cachedTree = new StructuredLayerTree(layers, structure);
+                }
+
+                if (TryFindStructureDifferences(structure) || lastLayers.Count != layers.Count || LayerOrderIsDifferent(layers))
+                {
+                    cachedTree = new StructuredLayerTree(layers, structure);
+
+                    lastLayers = layers.Select(x => x.LayerGuid).ToList();
+                    lastStructure = structure.CloneGroups();
+                }
+
+                return cachedTree.RootDirectoryItems;
             }
             }
 
 
             return new StructuredLayerTree(null, null);
             return new StructuredLayerTree(null, null);
         }
         }
 
 
+        private bool LayerOrderIsDifferent(IList<Layer> layers)
+        {
+            var guids = layers.Select(x => x.LayerGuid).ToArray();
+            return !guids.SequenceEqual(lastLayers);
+        }
+
+        private bool TryFindStructureDifferences(LayerStructure structure)
+        {
+            bool structureModified = false;
+
+            if (lastStructure.Count != structure.Groups.Count)
+            {
+                return true;
+            }
+
+
+            foreach (GuidStructureItem treeItem in lastStructure)
+            {
+                var matchingGroup = structure.Groups.FirstOrDefault(x => x.GroupGuid == treeItem.GroupGuid);
+                List<GuidStructureItem> changedGroups = new List<GuidStructureItem>();
+                if(matchingGroup == null || StructureMismatch(treeItem, matchingGroup))
+                {
+                    structureModified = true;
+                }
+
+            }
+
+            return structureModified;
+        }
+
+        private bool StructureMismatch(GuidStructureItem first, GuidStructureItem second)
+        {
+            bool rootMismatch = first.EndLayerGuid != second.EndLayerGuid || first.StartLayerGuid != second.StartLayerGuid || first.IsVisible != second.IsVisible || first.IsExpanded != second.IsExpanded || first.Opacity != second.Opacity || first.Subgroups.Count != second.Subgroups.Count || second.Name != first.Name;
+
+            if (!rootMismatch && first.Subgroups.Count > 0)
+            {
+                for (int i = 0; i < first.Subgroups.Count; i++)
+                {
+                    if (StructureMismatch(first.Subgroups[i], second.Subgroups[i]))
+                    {
+                        return true;
+                    }
+                }
+            }
+            return rootMismatch;
+        }
+
         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
         {
         {
             throw new ArgumentException("Value is not a StructuredLayerTree");
             throw new ArgumentException("Value is not a StructuredLayerTree");

+ 11 - 1
PixiEditor/Models/Layers/GuidStructureItem.cs

@@ -114,7 +114,17 @@ namespace PixiEditor.Models.Layers
 
 
         public override int GetHashCode()
         public override int GetHashCode()
         {
         {
-            return GroupGuid.GetHashCode();
+            HashCode hc = default;
+            hc.Add(GroupGuid);
+            hc.Add(EndLayerGuid);
+            hc.Add(StartLayerGuid);
+            hc.Add(Opacity);
+            hc.Add(IsVisible);
+            hc.Add(IsExpanded);
+            hc.Add(IsRenaming);
+            hc.Add(Parent);
+            hc.Add(Subgroups);
+            return hc.ToHashCode();
         }
         }
 
 
         public GuidStructureItem CloneGroup()
         public GuidStructureItem CloneGroup()

+ 4 - 2
PixiEditor/Views/MainWindow.xaml

@@ -328,8 +328,9 @@
             </Grid>
             </Grid>
         </Grid>
         </Grid>
 
 
-        <StackPanel Orientation="Vertical" Cursor="Arrow" Grid.Row="2" Grid.Column="0"
-                    Background="{StaticResource AccentColor}" Grid.RowSpan="2">
+        <Border Grid.Row="2" Grid.Column="0"
+                    Background="{StaticResource AccentColor}" Grid.RowSpan="2" CornerRadius="5">
+        <StackPanel Orientation="Vertical" Cursor="Arrow" >
 
 
             <ItemsControl ItemsSource="{Binding ToolsSubViewModel.ToolSet}">
             <ItemsControl ItemsSource="{Binding ToolsSubViewModel.ToolSet}">
                 <ItemsControl.ItemTemplate>
                 <ItemsControl.ItemTemplate>
@@ -348,6 +349,7 @@
                 </ItemsControl.ItemTemplate>
                 </ItemsControl.ItemTemplate>
             </ItemsControl>
             </ItemsControl>
         </StackPanel>
         </StackPanel>
+        </Border>
 
 
         <Grid Grid.Row="3" Grid.Column="1">
         <Grid Grid.Row="3" Grid.Column="1">
             <Grid.ColumnDefinitions>
             <Grid.ColumnDefinitions>

+ 2 - 2
PixiEditor/Views/UserControls/LayerGroupControl.xaml

@@ -5,7 +5,7 @@
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours" xmlns:userControls="clr-namespace:PixiEditor.Views" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters" xmlns:helpers="clr-namespace:PixiEditor.Helpers.UI"
              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours" xmlns:userControls="clr-namespace:PixiEditor.Views" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters" xmlns:helpers="clr-namespace:PixiEditor.Helpers.UI"
              mc:Ignorable="d" Focusable="True"
              mc:Ignorable="d" Focusable="True"
-             d:DesignHeight="60" d:DesignWidth="250" Name="groupControl">
+             d:DesignHeight="60" d:DesignWidth="250" Name="groupControl" DragEnter="GroupControl_DragEnter" DragLeave="GroupControl_DragLeave" DragDrop.Drop="GroupControl_DragLeave">
     <UserControl.Resources>
     <UserControl.Resources>
         <converters:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
         <converters:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
         <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
         <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
@@ -21,7 +21,7 @@
                 <RowDefinition Height="10"/>
                 <RowDefinition Height="10"/>
             </Grid.RowDefinitions>
             </Grid.RowDefinitions>
             <Grid AllowDrop="True" DragEnter="Grid_DragEnter" Drop="Grid_Drop_Top" DragLeave="Grid_DragLeave" Grid.Row="0" Grid.ColumnSpan="3" Background="Transparent" Panel.ZIndex="3"/>
             <Grid AllowDrop="True" DragEnter="Grid_DragEnter" Drop="Grid_Drop_Top" DragLeave="Grid_DragLeave" Grid.Row="0" Grid.ColumnSpan="3" Background="Transparent" Panel.ZIndex="3"/>
-            <Grid x:Name="middleDropGrid" Grid.Row="1" AllowDrop="True" Panel.ZIndex="2" Background="Transparent" DragEnter="Grid_CenterEnter" Drop="Grid_Drop_Center" DragLeave="Grid_CenterLeave"></Grid>
+            <Grid Visibility="Collapsed" x:Name="middleDropGrid" Grid.Row="1" AllowDrop="True" Panel.ZIndex="2" Background="Transparent" DragEnter="Grid_CenterEnter" Drop="Grid_Drop_Center" DragLeave="Grid_CenterLeave"></Grid>
             <Grid x:Name="centerGrid" Grid.Row="0" Grid.RowSpan="3" Background="Transparent">
             <Grid x:Name="centerGrid" Grid.Row="0" Grid.RowSpan="3" Background="Transparent">
                 <Grid.ColumnDefinitions>
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="30"/>
                     <ColumnDefinition Width="30"/>

+ 11 - 1
PixiEditor/Views/UserControls/LayerGroupControl.xaml.cs

@@ -251,7 +251,7 @@ namespace PixiEditor.Views.UserControls
         {
         {
             var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
             var doc = LayersViewModel.Owner.BitmapManager.ActiveDocument;
             var layer = doc.Layers.First(x => x.LayerGuid == GroupData.EndLayerGuid);
             var layer = doc.Layers.First(x => x.LayerGuid == GroupData.EndLayerGuid);
-            if (GroupData.IsExpanded && doc.ActiveLayerGuid != layer.LayerGuid)
+            if (doc.ActiveLayerGuid != layer.LayerGuid)
             {
             {
                 doc.SetMainActiveLayer(doc.Layers.IndexOf(layer));
                 doc.SetMainActiveLayer(doc.Layers.IndexOf(layer));
             }
             }
@@ -306,5 +306,15 @@ namespace PixiEditor.Views.UserControls
                 IsVisibleUndoTriggerable = value;
                 IsVisibleUndoTriggerable = value;
             }
             }
         }
         }
+
+        private void GroupControl_DragEnter(object sender, DragEventArgs e)
+        {
+            middleDropGrid.Visibility = Visibility.Visible;
+        }
+
+        private void GroupControl_DragLeave(object sender, DragEventArgs e)
+        {
+            middleDropGrid.Visibility = Visibility.Collapsed;
+        }
     }
     }
 }
 }