Browse Source

Fixed shared settings bug

flabbet 4 years ago
parent
commit
f223582425

+ 2 - 2
PixiEditor/Models/Tools/ToolSettings/Settings/DropdownSetting.cs

@@ -5,14 +5,14 @@ using System.Windows.Data;
 
 namespace PixiEditor.Models.Tools.ToolSettings.Settings
 {
-    public class DropdownSetting : Setting<object>
+    public class DropdownSetting : Setting<ComboBoxItem>
     {
         public DropdownSetting(string name, string[] values, string label)
             : base(name)
         {
             Values = values;
             SettingControl = GenerateDropdown();
-            Value = ((ComboBox)SettingControl).Items[0];
+            Value = (ComboBoxItem)((ComboBox)SettingControl).Items[0];
             Label = label;
         }
 

+ 39 - 39
PixiEditor/Models/Tools/ToolSettings/Settings/Setting.cs

@@ -1,45 +1,45 @@
-using System.Windows.Controls;
-using PixiEditor.Helpers;
-
-namespace PixiEditor.Models.Tools.ToolSettings.Settings
+using System.Windows.Controls;
+using PixiEditor.Helpers;
+
+namespace PixiEditor.Models.Tools.ToolSettings.Settings
 {
     [System.Diagnostics.CodeAnalysis.SuppressMessage(
         "StyleCop.CSharp.MaintainabilityRules",
         "SA1402:File may only contain a single type",
         Justification = "Same class with generic value")]
-    public abstract class Setting<T> : Setting
-    {
-        private T value;
-
-        protected Setting(string name)
-            : base(name)
-        {
-        }
-
-        public T Value
-        {
-            get => value;
-            set
-            {
-                this.value = value;
-                RaisePropertyChanged("Value");
-            }
-        }
-    }
-
-    public abstract class Setting : NotifyableObject
-    {
-        protected Setting(string name)
-        {
-            Name = name;
-        }
-
-        public string Name { get; }
-
-        public string Label { get; set; }
-
-        public bool HasLabel => !string.IsNullOrEmpty(Label);
-
-        public Control SettingControl { get; set; }
-    }
+    public abstract class Setting<T> : Setting
+    {
+        protected Setting(string name)
+            : base(name)
+        {
+        }
+
+        public new T Value
+        {
+            get => (T)base.Value;
+            set
+            {
+                base.Value = value;
+                RaisePropertyChanged("Value");
+            }
+        }
+    }
+
+    public abstract class Setting : NotifyableObject
+    {
+        protected Setting(string name)
+        {
+            Name = name;
+        }
+
+        public object Value { get; set; }
+
+        public string Name { get; }
+
+        public string Label { get; set; }
+
+        public bool HasLabel => !string.IsNullOrEmpty(Label);
+
+        public Control SettingControl { get; set; }
+    }
 }

+ 1 - 1
PixiEditor/Models/Tools/ToolSettings/Toolbars/BrightnessToolToolbar.cs

@@ -9,7 +9,7 @@ namespace PixiEditor.Models.Tools.ToolSettings.Toolbars
         public BrightnessToolToolbar(float initialValue)
         {
             Settings.Add(new FloatSetting("CorrectionFactor", initialValue, "Strength:", 0f, 100f));
-            Settings.Add(new DropdownSetting("Mode", Enum.GetNames(typeof(BrightnessMode)), "Mode"));
+            Settings.Add(new DropdownSetting("BrightnessMode", Enum.GetNames(typeof(BrightnessMode)), "Mode"));
         }
     }
 }

+ 1 - 1
PixiEditor/Models/Tools/ToolSettings/Toolbars/SelectToolToolbar.cs

@@ -6,7 +6,7 @@ namespace PixiEditor.Models.Tools.ToolSettings.Toolbars
     {
         public SelectToolToolbar()
         {
-            Settings.Add(new DropdownSetting("Mode", new[] { "New", "Add", "Subtract" }, "Selection type"));
+            Settings.Add(new DropdownSetting("SelectMode", new[] { "New", "Add", "Subtract" }, "Selection type"));
         }
     }
 }

+ 64 - 65
PixiEditor/Models/Tools/ToolSettings/Toolbars/Toolbar.cs

@@ -1,76 +1,75 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.Linq;
-using PixiEditor.Models.Tools.ToolSettings.Settings;
-
-namespace PixiEditor.Models.Tools.ToolSettings.Toolbars
-{
-    public abstract class Toolbar
-    {
-        private static readonly List<Setting> SharedSettings = new List<Setting>();
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using PixiEditor.Models.Tools.ToolSettings.Settings;
+
+namespace PixiEditor.Models.Tools.ToolSettings.Toolbars
+{
+    public abstract class Toolbar
+    {
+        private static readonly List<Setting> SharedSettings = new List<Setting>();
+
+        public ObservableCollection<Setting> Settings { get; set; } = new ObservableCollection<Setting>();
+
+        /// <summary>
+        ///     Gets setting in toolbar by name.
+        /// </summary>
+        /// <param name="name">Setting name, non case sensitive.</param>
+        /// <returns></returns>
+        public virtual Setting GetSetting(string name)
+        {
+            return Settings.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.CurrentCultureIgnoreCase));
+        }
+
+        /// <summary>
+        ///     Gets setting of given type T in toolbar by name.
+        /// </summary>
+        /// <param name="name">Setting name, non case sensitive.</param>
+        /// <returns></returns>
+        public T GetSetting<T>(string name)
+            where T : Setting
+        {
+            Setting setting = Settings.FirstOrDefault(currentSetting => string.Equals(currentSetting.Name, name, StringComparison.CurrentCultureIgnoreCase));
 
-        public ObservableCollection<Setting> Settings { get; set; } = new ObservableCollection<Setting>();
-
-        /// <summary>
-        ///     Gets setting in toolbar by name.
-        /// </summary>
-        /// <param name="name">Setting name, non case sensitive.</param>
-        /// <returns></returns>
-        public virtual Setting GetSetting(string name)
-        {
-            return Settings.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.CurrentCultureIgnoreCase));
-        }
-
-        /// <summary>
-        ///     Gets setting of given type T in toolbar by name.
-        /// </summary>
-        /// <param name="name">Setting name, non case sensitive.</param>
-        /// <returns></returns>
-        public T GetSetting<T>(string name)
-            where T : Setting
-        {
-            Setting setting = Settings.FirstOrDefault(currentSetting => string.Equals(currentSetting.Name, name, StringComparison.CurrentCultureIgnoreCase));
-
             if (setting == null || !(setting is T convertedSetting))
             {
                 return null;
             }
 
-            return convertedSetting;
-        }
-
-        /// <summary>
-        ///     Saves current toolbar state, so other toolbars with common settings can load them.
-        /// </summary>
-        public void SaveToolbarSettings()
-        {
-            foreach (Setting setting in Settings)
-            {
-                AddSettingToCollection(SharedSettings, setting);
-            }
-        }
-
-        /// <summary>
-        ///     Loads common settings saved from previous tools to current one.
-        /// </summary>
-        public void LoadSharedSettings()
-        {
-            foreach (Setting sharedSetting in SharedSettings)
+            return convertedSetting;
+        }
+
+        /// <summary>
+        ///     Saves current toolbar state, so other toolbars with common settings can load them.
+        /// </summary>
+        public void SaveToolbarSettings()
+        {
+            for (int i = 0; i < Settings.Count; i++)
             {
-                AddSettingToCollection(Settings, sharedSetting);
+                if (SharedSettings.Any(x => x.Name == Settings[i].Name))
+                {
+                    SharedSettings.First(x => x.Name == Settings[i].Name).Value = Settings[i].Value;
+                }
+                else
+                {
+                    SharedSettings.Add(Settings[i]);
+                }
             }
-        }
-
-        private static void AddSettingToCollection(ICollection<Setting> collection, Setting setting)
-        {
-            Setting storedSetting = collection.FirstOrDefault(currentSetting => currentSetting.Name == setting.Name);
-            if (storedSetting != null)
+        }
+
+        /// <summary>
+        ///     Loads common settings saved from previous tools to current one.
+        /// </summary>
+        public void LoadSharedSettings()
+        {
+            for (int i = 0; i < SharedSettings.Count; i++)
             {
-                collection.Remove(storedSetting);
+                if (Settings.Any(x => x.Name == SharedSettings[i].Name))
+                {
+                    Settings.First(x => x.Name == SharedSettings[i].Name).Value = SharedSettings[i].Value;
+                }
             }
-
-            collection.Add(setting);
-        }
-    }
+        }
+    }
 }