Browse Source

Added EnumSetting<TEnum>

CPKreuz 4 years ago
parent
commit
6947e97727

+ 19 - 0
PixiEditor/Helpers/Extensions/ToolbarHelpers.cs

@@ -0,0 +1,19 @@
+using PixiEditor.Models.Tools.ToolSettings.Settings;
+using PixiEditor.Models.Tools.ToolSettings.Toolbars;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PixiEditor.Helpers.Extensions
+{
+    public static class ToolbarHelpers
+    {
+        public static EnumSetting<TEnum> GetEnumSetting<TEnum>(this Toolbar toolbar, string name)
+            where TEnum : struct, Enum
+        {
+            return toolbar.GetSetting<EnumSetting<TEnum>>(name);
+        }
+    }
+}

+ 96 - 0
PixiEditor/Models/Tools/ToolSettings/Settings/EnumSetting.cs

@@ -0,0 +1,96 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Controls.Primitives;
+using System.Windows.Data;
+
+namespace PixiEditor.Models.Tools.ToolSettings.Settings
+{
+    public class EnumSetting<TEnum> : Setting<TEnum, ComboBox>
+        where TEnum : struct, Enum
+    {
+        private int selectedIndex = 0;
+
+        /// <summary>
+        /// Gets or sets the selected Index of the <see cref="ComboBox"/>.
+        /// </summary>
+        public int SelectedIndex
+        {
+            get => selectedIndex;
+            set
+            {
+                if (SetProperty(ref selectedIndex, value))
+                {
+                    RaisePropertyChanged(nameof(Value));
+                }
+            }
+        }
+
+        /// <summary>
+        /// Gets or sets the selected value of the <see cref="ComboBox"/>.
+        /// </summary>
+        public new TEnum Value
+        {
+            get => (TEnum)(SettingControl.SelectedItem as ComboBoxItem).Tag;
+            set
+            {
+                SettingControl.SelectedItem = SettingControl.Items.Cast<ComboBoxItem>().First(x => x.Tag == (object)value);
+                RaisePropertyChanged(nameof(Value));
+            }
+        }
+
+        public EnumSetting(string name, string label)
+            : base(name)
+        {
+            SettingControl = GenerateDropdown();
+
+            Label = label;
+        }
+
+        public EnumSetting(string name, string label, TEnum defaultValue)
+            : this(name, label)
+        {
+            Value = defaultValue;
+        }
+
+        private static ComboBox GenerateDropdown()
+        {
+            ComboBox combobox = new ComboBox
+            {
+                VerticalAlignment = VerticalAlignment.Center
+            };
+
+            GenerateItems(combobox);
+
+            Binding binding = new Binding(nameof(SelectedIndex))
+            {
+                Mode = BindingMode.TwoWay
+            };
+
+            combobox.SetBinding(Selector.SelectedIndexProperty, binding);
+
+            return combobox;
+        }
+
+        private static void GenerateItems(ComboBox comboBox)
+        {
+            string[] names = Enum.GetNames<TEnum>();
+            TEnum[] values = Enum.GetValues<TEnum>();
+
+            for (int i = 0; i < names.Length; i++)
+            {
+                ComboBoxItem item = new ComboBoxItem
+                {
+                    Content = names[i],
+                    Tag = values[i]
+                };
+
+                comboBox.Items.Add(item);
+            }
+        }
+    }
+}

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

@@ -4,6 +4,25 @@ 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, TControl> : Setting<T>
+        where TControl : Control
+    {
+        protected Setting(string name)
+            : base(name)
+        {
+        }
+
+        public new TControl SettingControl
+        {
+            get => (TControl)base.SettingControl;
+            set => base.SettingControl = value;
+        }
+    }
+
     [System.Diagnostics.CodeAnalysis.SuppressMessage(
         "StyleCop.CSharp.MaintainabilityRules",
         "SA1402:File may only contain a single type",