From c029b57877c048fe094ed21714a86d74cb60988a Mon Sep 17 00:00:00 2001 From: NostraThomas99 <34015422+NostraThomas99@users.noreply.github.com> Date: Fri, 29 Mar 2024 02:13:52 -0500 Subject: [PATCH] Reworked enum value mapping in UI to be more robust --- .../UI/SearchableConfigs/EnumSearch.cs | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/RotationSolver/UI/SearchableConfigs/EnumSearch.cs b/RotationSolver/UI/SearchableConfigs/EnumSearch.cs index 8a8a8f718..33f2dcbd7 100644 --- a/RotationSolver/UI/SearchableConfigs/EnumSearch.cs +++ b/RotationSolver/UI/SearchableConfigs/EnumSearch.cs @@ -14,29 +14,26 @@ protected int Value protected override void DrawMain() { - var value = Value; + var currentValue = Value; - var names = new List(); - foreach (Enum v in Enum.GetValues(_property.PropertyType)) + var enumValueToNameMap = new Dictionary(); + foreach (Enum enumValue in Enum.GetValues(_property.PropertyType)) { - names.Add(v.Local()); + enumValueToNameMap[Convert.ToInt32(enumValue)] = enumValue.Local(); } - var strs = names.ToArray(); - if (strs.Length > 0) + var displayNames = enumValueToNameMap.Values.ToArray(); + + if (displayNames.Length > 0) { - ImGui.SetNextItemWidth(Math.Max(ImGui.CalcTextSize(strs[value % strs.Length]).X + 30, DRAG_WIDTH) * Scale); - if (ImGui.Combo($"##Config_{ID}{GetHashCode()}", ref value, strs, strs.Length)) + ImGui.SetNextItemWidth(Math.Max(displayNames.Max(name => ImGui.CalcTextSize(name).X) + 30, DRAG_WIDTH) * Scale); + + int currentIndex = enumValueToNameMap.Keys.ToList().IndexOf(currentValue); + if (currentIndex == -1) currentIndex = 0; // Default to first item if not found + + if (ImGui.Combo($"##Config_{ID}{GetHashCode()}", ref currentIndex, displayNames, displayNames.Length)) { - int index = 0; - foreach (var v in Enum.GetValues(_property.PropertyType)) - { - if (index++ == value) - { - Value = Convert.ToInt32(v); - break; - } - } + Value = enumValueToNameMap.Keys.ElementAt(currentIndex); } }