|
| 1 | +using System; |
| 2 | +using UnityEditor.IMGUI.Controls; |
| 3 | +using UnityEngine; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace TarasK8.UI.Editor.Animations |
| 7 | +{ |
| 8 | + public class AnimatedPropertiesDropdown : AdvancedDropdown |
| 9 | + { |
| 10 | + private readonly string[] _properties; |
| 11 | + private readonly Dictionary<AdvancedDropdownItem, string> _itemToPathMap; |
| 12 | + private readonly Dictionary<string, int> _propertyIndexMap; |
| 13 | + |
| 14 | + public Action<int> OnItemSelected { get; set; } |
| 15 | + |
| 16 | + public AnimatedPropertiesDropdown(AdvancedDropdownState state, string[] properties) : base(state) |
| 17 | + { |
| 18 | + _properties = properties; |
| 19 | + minimumSize = new Vector2(200f, 300f); |
| 20 | + |
| 21 | + // Initialize lookup dictionaries |
| 22 | + _itemToPathMap = new Dictionary<AdvancedDropdownItem, string>(); |
| 23 | + _propertyIndexMap = BuildPropertyIndexMap(properties); |
| 24 | + } |
| 25 | + |
| 26 | + protected override AdvancedDropdownItem BuildRoot() |
| 27 | + { |
| 28 | + var root = new AdvancedDropdownItem("Properties"); |
| 29 | + |
| 30 | + // Build the tree from the string array |
| 31 | + var categoryTree = BuildCategoryTree(_properties); |
| 32 | + |
| 33 | + // Convert the tree into AdvancedDropdownItems |
| 34 | + foreach (var category in categoryTree) |
| 35 | + { |
| 36 | + AddCategoryToDropdown(root, category.Key, category.Value, category.Key); |
| 37 | + } |
| 38 | + |
| 39 | + return root; |
| 40 | + } |
| 41 | + |
| 42 | + private Dictionary<string, object> BuildCategoryTree(string[] properties) |
| 43 | + { |
| 44 | + var root = new Dictionary<string, object>(); |
| 45 | + |
| 46 | + foreach (var property in properties) |
| 47 | + { |
| 48 | + var parts = property.Split('/'); |
| 49 | + var currentNode = root; |
| 50 | + |
| 51 | + for (int i = 0; i < parts.Length; i++) |
| 52 | + { |
| 53 | + if (!currentNode.ContainsKey(parts[i])) |
| 54 | + { |
| 55 | + currentNode[parts[i]] = new Dictionary<string, object>(); |
| 56 | + } |
| 57 | + |
| 58 | + // Navigate to the next level |
| 59 | + currentNode = (Dictionary<string, object>)currentNode[parts[i]]; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return root; |
| 64 | + } |
| 65 | + |
| 66 | + private void AddCategoryToDropdown(AdvancedDropdownItem parent, string categoryName, object subcategories, string fullPath) |
| 67 | + { |
| 68 | + var categoryItem = new AdvancedDropdownItem(categoryName); |
| 69 | + |
| 70 | + // Store the full path for this item |
| 71 | + _itemToPathMap[categoryItem] = fullPath; |
| 72 | + |
| 73 | + parent.AddChild(categoryItem); |
| 74 | + |
| 75 | + if (subcategories is Dictionary<string, object> subcategoryDict) |
| 76 | + { |
| 77 | + foreach (var subcategory in subcategoryDict) |
| 78 | + { |
| 79 | + var childFullPath = $"{fullPath}/{subcategory.Key}"; |
| 80 | + AddCategoryToDropdown(categoryItem, subcategory.Key, subcategory.Value, childFullPath); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private Dictionary<string, int> BuildPropertyIndexMap(string[] properties) |
| 86 | + { |
| 87 | + var map = new Dictionary<string, int>(); |
| 88 | + for (int i = 0; i < properties.Length; i++) |
| 89 | + { |
| 90 | + map[properties[i]] = i; |
| 91 | + } |
| 92 | + return map; |
| 93 | + } |
| 94 | + |
| 95 | + protected override void ItemSelected(AdvancedDropdownItem item) |
| 96 | + { |
| 97 | + base.ItemSelected(item); |
| 98 | + |
| 99 | + // Lookup the full path of the selected item |
| 100 | + if (_itemToPathMap.TryGetValue(item, out string selectedProperty)) |
| 101 | + { |
| 102 | + if (_propertyIndexMap.TryGetValue(selectedProperty, out int index)) |
| 103 | + { |
| 104 | + OnItemSelected?.Invoke(index); // Raise the event with the index |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + Debug.LogWarning($"Selected item '{selectedProperty}' not found in the properties array."); |
| 109 | + } |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + Debug.LogWarning("Selected item not mapped to a property."); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments