Browse Source

Undo group visibility works

flabbet 4 years ago
parent
commit
c4501968d2

+ 7 - 10
PixiEditor/Models/Position/CoordinatesCalculator.cs

@@ -123,40 +123,37 @@ namespace PixiEditor.Models.Position
         public static int FindMaxYNonTransparent(WriteableBitmap bitmap)
         {
             Color transparent = Color.FromArgb(0, 0, 0, 0);
-            bitmap.Lock();
-            for (int y = (int)bitmap.Height - 1; y >= 0; y--)
+            using BitmapContext ctx = bitmap.GetBitmapContext(ReadWriteMode.ReadOnly);
+            for (int y = ctx.Height - 1; y >= 0; y--)
             {
-                for (int x = (int)bitmap.Width - 1; x >= 0; x--)
+                for (int x = ctx.Width - 1; x >= 0; x--)
                 {
                     if (bitmap.GetPixel(x, y) != transparent)
                     {
-                        bitmap.Unlock();
                         return y;
                     }
                 }
             }
 
-            bitmap.Unlock();
             return -1;
         }
 
         public static int FindMaxXNonTransparent(WriteableBitmap bitmap)
         {
             Color transparent = Color.FromArgb(0, 0, 0, 0);
-            bitmap.Lock();
-            for (int x = (int)bitmap.Width - 1; x >= 0; x--)
+
+            using BitmapContext ctx = bitmap.GetBitmapContext(ReadWriteMode.ReadOnly);
+            for (int x = ctx.Width - 1; x >= 0; x--)
             {
-                for (int y = (int)bitmap.Height - 1; y >= 0; y--)
+                for (int y = ctx.Height - 1; y >= 0; y--)
                 {
                     if (bitmap.GetPixel(x, y) != transparent)
                     {
-                        bitmap.Unlock();
                         return x;
                     }
                 }
             }
 
-            bitmap.Unlock();
             return -1;
         }
     }

+ 1 - 1
PixiEditor/Styles/TreeViewStyle.xaml

@@ -124,7 +124,7 @@
                 <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
             </Trigger>
         </Style.Triggers>
-        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
+        <!--<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />-->
         <Setter Property="HorizontalContentAlignment" Value="Stretch" />
     </Style>
 

+ 18 - 17
PixiEditor/ViewModels/SubViewModels/Main/UpdateViewModel.cs

@@ -87,27 +87,28 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 #if RELEASE
             if (IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true))
             {
-            string dir = AppDomain.CurrentDomain.BaseDirectory;
-            UpdateDownloader.CreateTempDirectory();
-            bool updateZipExists = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.zip").Length > 0;
-            string[] updateExeFiles = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.exe");
-            bool updateExeExists = updateExeFiles.Length > 0;
+                string dir = AppDomain.CurrentDomain.BaseDirectory;
+                UpdateDownloader.CreateTempDirectory();
+                bool updateZipExists = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.zip").Length > 0;
+                string[] updateExeFiles = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.exe");
+                bool updateExeExists = updateExeFiles.Length > 0;
 
-            string updaterPath = Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
+                string updaterPath = Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
 
-            if (updateZipExists || updateExeExists)
-            {
-                ViewModelMain.Current.UpdateSubViewModel.UpdateReadyToInstall = true;
-                var result = ConfirmationDialog.Show("Update is ready to install. Do you want to install it now?");
-                if (result == Models.Enums.ConfirmationType.Yes)
+                if (updateZipExists || updateExeExists)
                 {
-                    if (updateZipExists && File.Exists(updaterPath))
-                    {
-                        InstallHeadless(updaterPath);
-                    }
-                    else if (updateExeExists)
+                    ViewModelMain.Current.UpdateSubViewModel.UpdateReadyToInstall = true;
+                    var result = ConfirmationDialog.Show("Update is ready to install. Do you want to install it now?");
+                    if (result == Models.Enums.ConfirmationType.Yes)
                     {
-                        OpenExeInstaller(updateExeFiles[0]);
+                        if (updateZipExists && File.Exists(updaterPath))
+                        {
+                            InstallHeadless(updaterPath);
+                        }
+                        else if (updateExeExists)
+                        {
+                            OpenExeInstaller(updateExeFiles[0]);
+                        }
                     }
                 }
             }

+ 32 - 2
PixiEditor/Views/UserControls/LayersManager.xaml.cs

@@ -5,6 +5,7 @@ using System.Diagnostics;
 using System.Windows;
 using System.Windows.Controls;
 using PixiEditor.Models.Layers;
+using PixiEditor.Models.Undo;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels.SubViewModels.Main;
 
@@ -89,6 +90,35 @@ namespace PixiEditor.Views.UserControls
             }
         }
 
+        private void HandleGroupOpacityChange(GuidStructureItem group, float value)
+        {
+            if (LayerCommandsViewModel.Owner?.BitmapManager?.ActiveDocument != null)
+            {
+                var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
+
+                var processArgs = new object[] { group.GroupGuid, value };
+                var reverseProcessArgs = new object[] { group.GroupGuid, group.Opacity };
+
+                ChangeGroupOpacityProcess(processArgs);
+
+                doc.UndoManager.AddUndoChange(
+                new Change(
+                    ChangeGroupOpacityProcess,
+                    reverseProcessArgs,
+                    ChangeGroupOpacityProcess,
+                    processArgs,
+                    $"Change {group.Name} opacity"), false);
+            }
+        }
+
+        private void ChangeGroupOpacityProcess(object[] processArgs)
+        {
+            if (processArgs.Length > 0 && processArgs[0] is Guid groupGuid && processArgs[1] is float opacity)
+            {
+                LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.GetGroupByGuid(groupGuid).Opacity = opacity;
+            }
+        }
+
         private void LayerGroup_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
         {
             if (sender is LayerGroupControl container && e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
@@ -110,13 +140,13 @@ namespace PixiEditor.Views.UserControls
 
             if (item is Layer layer)
             {
-                layer.Opacity = val;
+                layer.OpacityUndoTriggerable = val;
             }
             else if(item is LayerGroup group)
             {
                 LayerStructure structure = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure;
                 var groupData = structure.GetGroupByGuid(group.GroupGuid);
-                groupData.Opacity = val;
+                HandleGroupOpacityChange(groupData, val);
 
                 var layers = structure.GetGroupLayers(groupData);
                 layers.ForEach(x => x.Opacity = x.Opacity); // This might seems stupid, but it raises property changed, without setting any value. This is used to trigger converters that use group opacity