Browse Source

Added description attribute support to enum setting

CPKreuz 4 years ago
parent
commit
59949df5a0

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

@@ -1,5 +1,6 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
@@ -13,5 +14,23 @@ namespace PixiEditor.Helpers.Extensions
         {
         {
             return Enum.GetValues(e.GetType()).Cast<T>().Where(x => e.HasFlag(x));
             return Enum.GetValues(e.GetType()).Cast<T>().Where(x => e.HasFlag(x));
         }
         }
+
+        public static string GetDescription<T>(this T enumValue)
+            where T : struct, Enum
+        {
+            var description = enumValue.ToString();
+            var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
+
+            if (fieldInfo != null)
+            {
+                var attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
+                if (attrs != null && attrs.Length > 0)
+                {
+                    description = ((DescriptionAttribute)attrs[0]).Description;
+                }
+            }
+
+            return description;
+        }
     }
     }
 }
 }

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

@@ -1,4 +1,5 @@
-using System;
+using PixiEditor.Helpers.Extensions;
+using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
@@ -85,15 +86,14 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
 
 
         private static void GenerateItems(ComboBox comboBox)
         private static void GenerateItems(ComboBox comboBox)
         {
         {
-            string[] names = Enum.GetNames<TEnum>();
             TEnum[] values = Enum.GetValues<TEnum>();
             TEnum[] values = Enum.GetValues<TEnum>();
 
 
-            for (int i = 0; i < names.Length; i++)
+            foreach (TEnum value in values)
             {
             {
                 ComboBoxItem item = new ComboBoxItem
                 ComboBoxItem item = new ComboBoxItem
                 {
                 {
-                    Content = names[i],
-                    Tag = values[i]
+                    Content = value.GetDescription(),
+                    Tag = value
                 };
                 };
 
 
                 comboBox.Items.Add(item);
                 comboBox.Items.Add(item);

+ 1 - 53
PixiEditor/Models/Tools/Tools/MagicWandTool.cs

@@ -6,8 +6,6 @@ using PixiEditor.Models.Enums;
 using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
-using PixiEditor.Models.Tools.ToolSettings;
-using PixiEditor.Models.Tools.ToolSettings.Settings;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;
 using System.Collections.Generic;
 using System.Collections.Generic;
@@ -73,61 +71,11 @@ namespace PixiEditor.Models.Tools.Tools
 
 
             Toolbar = new MagicWandToolbar();
             Toolbar = new MagicWandToolbar();
 
 
-            var selectionTypeSetting = Toolbar.GetEnumSetting<SelectionType>("SelectMode");
-
-            selectionTypeSetting.ValueChanged += MagicWandTool_ValueChanged;
-
-            UpdateActionDisplay(selectionTypeSetting);
+            ActionDisplay = "Click to flood the selection.";
         }
         }
 
 
         public override void Use(List<Coordinates> pixels)
         public override void Use(List<Coordinates> pixels)
         {
         {
-            return;
-        }
-
-        public override void OnKeyDown(KeyEventArgs e)
-        {
-            if (e.Key != Key.LeftShift || e.IsRepeat)
-            {
-                return;
-            }
-
-            EnumSetting<SelectionType> enumSetting = Toolbar.GetEnumSetting<SelectionType>("SelectMode");
-            previousSelectionType = enumSetting.Value;
-            enumSetting.Value = SelectionType.Add;
-        }
-
-        public override void OnKeyUp(KeyEventArgs e)
-        {
-            if (e.Key != Key.LeftShift)
-            {
-                return;
-            }
-
-            Toolbar.GetEnumSetting<SelectionType>("SelectMode").Value = previousSelectionType;
-        }
-
-        private void MagicWandTool_ValueChanged(object sender, SettingValueChangedEventArgs<SelectionType> e)
-        {
-            UpdateActionDisplay(sender as EnumSetting<SelectionType>);
-        }
-
-        private void UpdateActionDisplay(EnumSetting<SelectionType> setting)
-        {
-            if (setting.Value == SelectionType.Add)
-            {
-                if (Keyboard.IsKeyDown(Key.LeftShift))
-                {
-                    ActionDisplay = $"Click to flood the selection. Release shift to revert the selection type to {previousSelectionType}";
-                    return;
-                }
-
-                ActionDisplay = "Click to flood the selection";
-            }
-            else
-            {
-                ActionDisplay = $"Click to flood the selection. Hold shift to set the selection type to {nameof(SelectionType.Add)}";
-            }
         }
         }
     }
     }
 }
 }