Pārlūkot izejas kodu

Changed settings generation

Krzysztof Krysiński 3 gadi atpakaļ
vecāks
revīzija
56987b66e8

+ 0 - 1
PixiEditor/Models/Tools/Tool.cs

@@ -52,7 +52,6 @@ namespace PixiEditor.Models.Tools
         private bool isActive;
         private string actionDisplay = string.Empty;
 
-
         public virtual void OnKeyDown(Key key) { }
 
         public virtual void OnKeyUp(Key key) { }

+ 9 - 5
PixiEditor/Models/Tools/ToolSettings/Settings/BoolSetting.cs

@@ -2,23 +2,22 @@
 using System.Windows.Controls;
 using System.Windows.Controls.Primitives;
 using System.Windows.Data;
-using System.Windows.Media;
-
+using System.Windows.Media;
+
 namespace PixiEditor.Models.Tools.ToolSettings.Settings
 {
     public class BoolSetting : Setting<bool>
     {
-        public BoolSetting(string name, string label = "")
+        public BoolSetting(string name, string label = "")
             : this(name, false, label)
         {
         }
 
-        public BoolSetting(string name, bool isChecked, string label = "")
+        public BoolSetting(string name, bool isChecked, string label = "")
             : base(name)
         {
             Label = label;
             Value = isChecked;
-            SettingControl = GenerateCheckBox();
         }
 
         private Control GenerateCheckBox()
@@ -38,5 +37,10 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
 
             return checkBox;
         }
+
+        public override Control GenerateControl()
+        {
+            return GenerateCheckBox();
+        }
     }
 }

+ 17 - 12
PixiEditor/Models/Tools/ToolSettings/Settings/ColorSetting.cs

@@ -1,32 +1,32 @@
-using System.Windows;
+using System.Windows;
+using System.Windows.Controls;
 using System.Windows.Data;
-using System.Windows.Interactivity;
+using System.Windows.Interactivity;
 using System.Windows.Media;
 using ColorPicker;
-using PixiEditor.Helpers.Behaviours;
-using PixiEditor.Views;
-
+using PixiEditor.Helpers.Behaviours;
+using PixiEditor.Views;
+
 namespace PixiEditor.Models.Tools.ToolSettings.Settings
 {
     public class ColorSetting : Setting<Color>
     {
-        public ColorSetting(string name, string label = "")
+        public ColorSetting(string name, string label = "")
             : base(name)
         {
             Label = label;
-            SettingControl = GenerateColorPicker();
             Value = Color.FromArgb(255, 255, 255, 255);
         }
 
         private ToolSettingColorPicker GenerateColorPicker()
         {
             var resourceDictionary = new ResourceDictionary();
-            resourceDictionary.Source = new System.Uri(
-                "pack://application:,,,/ColorPicker;component/Styles/DefaultColorPickerStyle.xaml",
+            resourceDictionary.Source = new System.Uri(
+                "pack://application:,,,/ColorPicker;component/Styles/DefaultColorPickerStyle.xaml",
                 System.UriKind.RelativeOrAbsolute);
-            ToolSettingColorPicker picker = new ToolSettingColorPicker
-            {
-                Style = (Style)resourceDictionary["DefaultColorPickerStyle"]
+            ToolSettingColorPicker picker = new ToolSettingColorPicker
+            {
+                Style = (Style)resourceDictionary["DefaultColorPickerStyle"]
             };
 
             Binding selectedColorBinding = new Binding("Value")
@@ -39,5 +39,10 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
             picker.SetBinding(ToolSettingColorPicker.SelectedColorProperty, selectedColorBinding);
             return picker;
         }
+
+        public override Control GenerateControl()
+        {
+            return GenerateColorPicker();
+        }
     }
 }

+ 5 - 1
PixiEditor/Models/Tools/ToolSettings/Settings/DropdownSetting.cs

@@ -11,7 +11,6 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
             : base(name)
         {
             Values = values;
-            SettingControl = GenerateDropdown();
             Value = ((ComboBox)SettingControl).Items[0];
             Label = label;
         }
@@ -45,5 +44,10 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
                 comboBox.Items.Add(item);
             }
         }
+
+        public override Control GenerateControl()
+        {
+            return GenerateDropdown();
+        }
     }
 }

+ 5 - 2
PixiEditor/Models/Tools/ToolSettings/Settings/EnumSetting.cs

@@ -51,11 +51,14 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
             }
         }
 
+        public override Control GenerateControl()
+        {
+            return GenerateDropdown();
+        }
+
         public EnumSetting(string name, string label)
             : base(name)
         {
-            SettingControl = GenerateDropdown();
-
             Label = label;
         }
 

+ 7 - 2
PixiEditor/Models/Tools/ToolSettings/Settings/FloatSetting.cs

@@ -1,4 +1,5 @@
-using System.Windows.Data;
+using System.Windows.Controls;
+using System.Windows.Data;
 using PixiEditor.Views;
 
 namespace PixiEditor.Models.Tools.ToolSettings.Settings
@@ -17,7 +18,6 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
             Value = initialValue;
             Min = min;
             Max = max;
-            SettingControl = GenerateNumberInput();
         }
 
         public float Min { get; set; }
@@ -40,5 +40,10 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
             numbrInput.SetBinding(NumberInput.ValueProperty, binding);
             return numbrInput;
         }
+
+        public override Control GenerateControl()
+        {
+            return GenerateNumberInput();
+        }
     }
 }

+ 2 - 0
PixiEditor/Models/Tools/ToolSettings/Settings/Setting.cs

@@ -64,5 +64,7 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
         public bool HasLabel => !string.IsNullOrEmpty(Label);
 
         public Control SettingControl { get; set; }
+
+        public abstract Control GenerateControl();
     }
 }

+ 14 - 9
PixiEditor/Models/Tools/ToolSettings/Settings/SizeSetting.cs

@@ -1,16 +1,16 @@
-using PixiEditor.Views;
+using PixiEditor.Views;
 using System.Windows;
+using System.Windows.Controls;
 using System.Windows.Data;
-
+
 namespace PixiEditor.Models.Tools.ToolSettings.Settings
 {
     public class SizeSetting : Setting<int>
     {
-        public SizeSetting(string name, string label = null)
+        public SizeSetting(string name, string label = null)
             : base(name)
         {
             Value = 1;
-            SettingControl = GenerateTextBox();
             Label = label;
         }
 
@@ -23,14 +23,19 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
                 VerticalAlignment = VerticalAlignment.Center,
                 MaxSize = 9999,
                 IsEnabled = true
-            };
-
-            Binding binding = new Binding("Value")
-            {
-                Mode = BindingMode.TwoWay,
+            };
+
+            Binding binding = new Binding("Value")
+            {
+                Mode = BindingMode.TwoWay,
             };
             tb.SetBinding(SizeInput.SizeProperty, binding);
             return tb;
         }
+
+        public override Control GenerateControl()
+        {
+            return GenerateTextBox();
+        }
     }
 }

+ 11 - 0
PixiEditor/Models/Tools/ToolSettings/Toolbars/Toolbar.cs

@@ -11,6 +11,17 @@ namespace PixiEditor.Models.Tools.ToolSettings.Toolbars
         private static readonly List<Setting> SharedSettings = new List<Setting>();
 
         public ObservableCollection<Setting> Settings { get; set; } = new ObservableCollection<Setting>();
+        public bool SettingsGenerated { get; private set; }
+
+        public void GenerateSettings()
+        {
+            foreach (var setting in Settings)
+            {
+                setting.SettingControl = setting.GenerateControl();
+            }
+
+            SettingsGenerated = true;
+        }
 
         /// <summary>
         ///     Gets setting in toolbar by name.

+ 6 - 0
PixiEditor/ViewModels/SubViewModels/Main/ToolsViewModel.cs

@@ -94,6 +94,12 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public void SetActiveTool(Tool tool)
         {
             if (ActiveTool == tool) return;
+
+            if (!tool.Toolbar.SettingsGenerated)
+            {
+                tool.Toolbar.GenerateSettings();
+            }
+
             ActiveToolIsTransient = false;
             bool shareToolbar = IPreferences.Current.GetPreference<bool>("EnableSharedToolbar");
             if (ActiveTool != null)

+ 2 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -144,7 +144,6 @@ namespace PixiEditor.ViewModels
             FileSubViewModel = services.GetService<FileViewModel>();
             ToolsSubViewModel = services.GetService<ToolsViewModel>();
             ToolsSubViewModel.SelectedToolChanged += BitmapManager_SelectedToolChanged;
-            ToolsSubViewModel?.SetupTools(services);
 
             IoSubViewModel = services.GetService<IoViewModel>();
             LayersSubViewModel = services.GetService<LayersViewModel>();
@@ -154,6 +153,8 @@ namespace PixiEditor.ViewModels
             ColorsSubViewModel = services.GetService<ColorsViewModel>();
             ColorsSubViewModel?.SetupPaletteParsers(services);
 
+            ToolsSubViewModel?.SetupTools(services);
+
             DocumentSubViewModel = services.GetService<DocumentViewModel>();
             DiscordViewModel = services.GetService<DiscordViewModel>();
             UpdateSubViewModel = services.GetService<UpdateViewModel>();

+ 5 - 2
PixiEditor/Views/MainWindow.xaml.cs

@@ -32,8 +32,12 @@ namespace PixiEditor
 
         public new ViewModelMain DataContext { get => (ViewModelMain)base.DataContext; set => base.DataContext = value; }
 
+        public event Action OnDataContextInitialized;
+
         public MainWindow()
         {
+            Current = this;
+
             services = new ServiceCollection()
                 .AddPixiEditor()
                 .BuildServiceProvider();
@@ -44,6 +48,7 @@ namespace PixiEditor
 
             InitializeComponent();
 
+            OnDataContextInitialized?.Invoke();
             pixiEditorLogo = BitmapFactory.FromResource(@"/Images/PixiEditorLogo.png");
 
             UpdateWindowChromeBorderThickness();
@@ -58,8 +63,6 @@ namespace PixiEditor
                 UpdateTaskbarIcon(x ? DataContext.BitmapManager.ActiveDocument : null);
             });
 
-            Current = this;
-
             OnReleaseBuild();
         }
 

+ 1 - 1
PixiEditor/Views/UserControls/ToolSettingColorPicker.xaml

@@ -12,7 +12,7 @@
              d:Background="{StaticResource AccentColor}">
     <colorpicker:PortableColorPicker Width="40" Height="20" x:Name="ColorPicker" UseHintColor="True"
                                      HintColor="{Binding DataContext.ColorsSubViewModel.PrimaryColor, 
-        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type PixiEditor:MainWindow}, AncestorLevel=0}, 
+        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type PixiEditor:MainWindow}, AncestorLevel=1},
         Converter={converters:SKColorToMediaColorConverter}}"
                                      SelectedColor="{Binding SelectedColor, ElementName=uc, Mode=TwoWay}"/>
 </UserControl>