Browse Source

Added opacity preview

flabbet 4 years ago
parent
commit
3a39b3f3d8

+ 40 - 0
PixiEditor/Helpers/Converters/LayerToFinalOpacityConverter.cs

@@ -0,0 +1,40 @@
+using System;
+using System.Globalization;
+using System.Windows.Data;
+using System.Windows.Markup;
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Layers.Utils;
+using PixiEditor.ViewModels;
+
+namespace PixiEditor.Helpers.Converters
+{
+    public class LayerToFinalOpacityConverter : MarkupExtension, IMultiValueConverter
+    {
+        private static LayerToFinalOpacityConverter converter;
+
+        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
+        {
+            if (values.Length > 0 && values[0] is Layer layer && ViewModelMain.Current?.BitmapManager?.ActiveDocument != null)
+            {
+                return (double)LayerStructureUtils.GetFinalLayerOpacity(layer, ViewModelMain.Current.BitmapManager.ActiveDocument.LayerStructure);
+            }
+
+            return null;
+        }
+
+        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
+        {
+            return null;
+        }
+
+        public override object ProvideValue(IServiceProvider serviceProvider)
+        {
+            if(converter == null)
+            {
+                converter = new LayerToFinalOpacityConverter();
+            }
+
+            return converter;
+        }
+    }
+}

+ 2 - 2
PixiEditor/Models/Layers/Layer.cs

@@ -154,9 +154,9 @@ namespace PixiEditor.Models.Layers
                 if (opacity != value)
                 {
                     opacity = value;
-                    RaisePropertyChanged(nameof(Opacity));
-                    RaisePropertyChanged(nameof(OpacityUndoTriggerable));
                 }
+                RaisePropertyChanged(nameof(Opacity));
+                RaisePropertyChanged(nameof(OpacityUndoTriggerable));
             }
         }
 

+ 7 - 2
PixiEditor/Views/UserControls/DrawingViewPort.xaml

@@ -5,7 +5,7 @@
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:local="clr-namespace:PixiEditor.Views.UserControls" 
              xmlns:tools="clr-namespace:PixiEditor.Models.Tools.Tools"
-             xmlns:vws="clr-namespace:PixiEditor.Views" 
+             xmlns:vws="clr-namespace:PixiEditor.Views"
              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
              xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
              xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:sys="clr-namespace:System;assembly=System.Runtime" x:Class="PixiEditor.Views.UserControls.DrawingViewPort"
@@ -67,7 +67,6 @@
                         <DataTemplate>
                             <Image VerticalAlignment="Top" HorizontalAlignment="Left" Source="{Binding LayerBitmap}"
                                                RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform"
-                                               Opacity="{Binding Opacity}"
                                                Width="{Binding Width}" Height="{Binding Height}" Margin="{Binding Offset}">
                                 <Image.Visibility>
                                     <MultiBinding Converter="{converters:FinalIsVisibleToVisiblityConverter}">
@@ -75,6 +74,12 @@
                                         <Binding Path="IsVisible"/>
                                     </MultiBinding>
                                 </Image.Visibility>
+                                <Image.Opacity>
+                                    <MultiBinding Converter="{converters:LayerToFinalOpacityConverter}">
+                                        <Binding Path="."/>
+                                        <Binding Path="Opacity"/>
+                                    </MultiBinding>
+                                </Image.Opacity>
                             </Image>
                         </DataTemplate>
                     </ItemsControl.ItemTemplate>

+ 6 - 1
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.ViewModels;
 using PixiEditor.ViewModels.SubViewModels.Main;
 
 namespace PixiEditor.Views.UserControls
@@ -113,8 +114,12 @@ namespace PixiEditor.Views.UserControls
             }
             else if(item is LayerGroup group)
             {
-                var groupData = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure.GetGroupByGuid(group.GroupGuid);
+                LayerStructure structure = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument.LayerStructure;
+                var groupData = structure.GetGroupByGuid(group.GroupGuid);
                 groupData.Opacity = 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
             }
         }