Browse Source

Fixed opacity and isVisible setter abuse and disabled opacity on doc nul

flabbet 4 years ago
parent
commit
7a21759826

+ 26 - 0
PixiEditor/Helpers/Converters/NotNullToBoolConverter.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Globalization;
+using System.Windows.Data;
+
+namespace PixiEditor.Helpers.Converters
+{
+    [ValueConversion(typeof(object), typeof(bool))]
+    public class NotNullToBoolConverter : IValueConverter
+    {
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            bool result = value != null;
+            if (parameter != null)
+            {
+                return !result;
+            }
+
+            return result;
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            return value;
+        }
+    }
+}

+ 42 - 16
PixiEditor/Models/Layers/Layer.cs

@@ -64,7 +64,7 @@ namespace PixiEditor.Models.Layers
             set
             {
                 name = value;
-                RaisePropertyChanged("Name");
+                RaisePropertyChanged(nameof(Name));
             }
         }
 
@@ -74,7 +74,7 @@ namespace PixiEditor.Models.Layers
             set
             {
                 isActive = value;
-                RaisePropertyChanged("IsActive");
+                RaisePropertyChanged(nameof(IsActive));
             }
         }
 
@@ -84,6 +84,20 @@ namespace PixiEditor.Models.Layers
             set
             {
                 if (isVisible != value)
+                {
+                    isVisible = value;
+                    RaisePropertyChanged(nameof(IsVisible));
+                    RaisePropertyChanged(nameof(IsVisibleUndoTriggerable));
+                }
+            }
+        }
+
+        public bool IsVisibleUndoTriggerable
+        {
+            get => IsVisible;
+            set
+            {
+                if (value != IsVisible)
                 {
                     ViewModelMain.Current?.BitmapManager?.ActiveDocument?.UndoManager
                         .AddUndoChange(
@@ -93,9 +107,8 @@ namespace PixiEditor.Models.Layers
                             value,
                             LayerHelper.FindLayerByGuidProcess,
                             new object[] { LayerGuid },
-                            "Change layer visibility"), true);
-                    isVisible = value;
-                    RaisePropertyChanged("IsVisible");
+                            "Change layer visibility"));
+                    IsVisible = value;
                 }
             }
         }
@@ -116,7 +129,7 @@ namespace PixiEditor.Models.Layers
             set
             {
                 layerBitmap = value;
-                RaisePropertyChanged("LayerBitmap");
+                RaisePropertyChanged(nameof(LayerBitmap));
             }
         }
 
@@ -127,17 +140,30 @@ namespace PixiEditor.Models.Layers
             {
                 if (opacity != value)
                 {
-                    ViewModelMain.Current?.BitmapManager?.ActiveDocument?.UndoManager
-                        .AddUndoChange(
-                            new Change(
-                            nameof(Opacity),
-                            opacity,
-                            value,
-                            LayerHelper.FindLayerByGuidProcess,
-                            new object[] { LayerGuid },
-                            "Change layer opacity"), true);
                     opacity = value;
-                    RaisePropertyChanged("Opacity");
+                    RaisePropertyChanged(nameof(Opacity));
+                    RaisePropertyChanged(nameof(OpacityUndoTriggerable));
+                }
+            }
+        }
+
+        public float OpacityUndoTriggerable
+        {
+            get => Opacity;
+            set
+            {
+                if (value != Opacity)
+                {
+                    ViewModelMain.Current?.BitmapManager?.ActiveDocument?.UndoManager
+                    .AddUndoChange(
+                                   new Change(
+                                   nameof(Opacity),
+                                   opacity,
+                                   value,
+                                   LayerHelper.FindLayerByGuidProcess,
+                                   new object[] { LayerGuid },
+                                   "Change layer opacity"));
+                    Opacity = value;
                 }
             }
         }

+ 8 - 10
PixiEditor/Views/MainWindow.xaml

@@ -25,6 +25,7 @@
             <vm:ViewModelMain x:Key="ViewModelMain" />
             <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
             <converters:BoolToIntConverter x:Key="BoolToIntConverter" />
+            <converters:NotNullToBoolConverter x:Key="NotNullToBoolConverter" />
             <converters:FloatNormalizeConverter x:Key="FloatNormalizeConverter" />
             <converters:DoubleToIntConverter x:Key="DoubleToIntConverter"/>
             <ResourceDictionary.MergedDictionaries>
@@ -294,8 +295,13 @@
                                             Style="{StaticResource DarkRoundButton}" />
                                             <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10,0">
                                                 <Label Content="Opacity" Foreground="White" VerticalAlignment="Center"/>
-                                                <vws:NumberInput Min="0" Max="100" Width="40" Height="20" VerticalAlignment="Center"
-                                                         Value="{Binding BitmapManager.ActiveDocument.ActiveLayer.Opacity, Mode=TwoWay, 
+                                                <vws:NumberInput 
+                                                    Min="0" Max="100"
+                                                    IsEnabled="{Binding Path=BitmapManager.ActiveDocument, 
+                                                    Converter={StaticResource NotNullToBoolConverter}}" 
+                                                    Width="40" Height="20"
+                                                    VerticalAlignment="Center"
+                                                   Value="{Binding BitmapManager.ActiveDocument.ActiveLayer.OpacityUndoTriggerable, Mode=TwoWay, 
                                             Converter={StaticResource FloatNormalizeConverter}}" />
                                                 <Label Content="%" Foreground="White" VerticalAlignment="Center"/>
                                             </StackPanel>
@@ -381,14 +387,6 @@
             </ItemsControl>
         </StackPanel>
 
-        <!--<Grid Grid.Column="2" Background="{StaticResource AccentColor}" Grid.Row="2" Grid.RowSpan="1">
-            <avalondock:DockingManager Foreground="White" Background="{StaticResource AccentColor}" BorderThickness="0">
-
-                <avalondock:DockingManager.Theme>
-                    <avalonDockTheme:PixiEditorDockTheme />
-                </avalondock:DockingManager.Theme>
-            </avalondock:DockingManager>
-        </Grid>-->
         <Grid Grid.Row="3" Grid.Column="1">
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="*"/>

+ 6 - 3
PixiEditor/Views/UserControls/LayerItem.xaml

@@ -5,7 +5,7 @@
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:local="clr-namespace:PixiEditor.Views"
              xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
-             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
+             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
              mc:Ignorable="d" Focusable="True"
              d:DesignHeight="60" d:DesignWidth="250" Name="uc"
              MouseLeave="LayerItem_OnMouseLeave" MouseEnter="LayerItem_OnMouseEnter">
@@ -21,15 +21,18 @@
                                        CommandParameter="{Binding Path=LayerIndex, ElementName=uc}"/>
             </i:EventTrigger>
         </i:Interaction.Triggers>
-        <Grid>
+        <Grid Focusable="True">
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="30"/>
                 <ColumnDefinition Width="199*"/>
                 <ColumnDefinition Width="20"/>
             </Grid.ColumnDefinitions>
+            <i:Interaction.Behaviors>
+                <behaviors:ClearFocusOnClickBehavior/>
+            </i:Interaction.Behaviors>
             <CheckBox Style="{StaticResource ImageCheckBox}" VerticalAlignment="Center"
                       IsThreeState="False" HorizontalAlignment="Center" 
-                      IsChecked="{Binding Path=IsVisible, Mode=TwoWay}" Grid.Column="0" Height="16" />
+                      IsChecked="{Binding Path=IsVisibleUndoTriggerable, Mode=TwoWay}" Grid.Column="0" Height="16" />
             <StackPanel Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Left" Margin="5,0,0,0">
                 <Image Source="{Binding PreviewImage,ElementName=uc}" Stretch="Uniform" Width="50" Height="20" Margin="0,0,20,0"
                        RenderOptions.BitmapScalingMode="NearestNeighbor"/>

+ 1 - 2
PixiEditor/Views/UserControls/LayerItem.xaml.cs

@@ -107,7 +107,6 @@ namespace PixiEditor.Views
             set { SetValue(MoveToFrontCommandProperty, value); }
         }
 
-
         private void LayerItem_OnMouseEnter(object sender, MouseEventArgs e)
         {
             ControlButtonsVisible = Visibility.Visible;
@@ -119,4 +118,4 @@ namespace PixiEditor.Views
 
         }
     }
-}
+}

+ 2 - 4
PixiEditor/Views/UserControls/NumberInput.xaml

@@ -3,15 +3,13 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-             xmlns:local="clr-namespace:PixiEditor.Views"
              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
              xmlns:behaviours="clr-namespace:PixiEditor.Helpers.Behaviours"
-             xmlns:ui="clr-namespace:PixiEditor.Helpers.UI"
-             mc:Ignorable="d" Focusable="True"
+             mc:Ignorable="d"
              d:DesignHeight="20" d:DesignWidth="40" x:Name="numberInput">
     <TextBox TextAlignment="Center" Style="{StaticResource DarkTextBoxStyle}" Focusable="True"
                
-             PreviewTextInput="TextBox_PreviewTextInput" Text="{Binding ElementName=numberInput, Path=Value, UpdateSourceTrigger=PropertyChanged}">
+             PreviewTextInput="TextBox_PreviewTextInput" Text="{Binding ElementName=numberInput, Path=Value}">
         <i:Interaction.Behaviors>
             <behaviours:TextBoxFocusBehavior/>
             <behaviours:GlobalShortcutFocusBehavior/>