Skip to content

Commit d42fba7

Browse files
authored
Merge pull request #17 from TarasK8/dev
Update 0.2.3
2 parents 6a60b42 + 140ae8f commit d42fba7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+618
-349
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

Editor/Animations/AnimatedPropertiesDropdown.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Linq;
2+
using TarasK8.UI.Animations;
3+
using TarasK8.UI.Editor.Animations;
4+
using TarasK8.UI.Editor.Utils;
5+
using UnityEditor;
6+
using UnityEngine;
7+
8+
namespace TarasK8.UI.Editor
9+
{
10+
public static class StateListDrawer
11+
{
12+
public const string NameFieldName = "<Name>k__BackingField";
13+
14+
public static void Draw(SerializedProperty property)
15+
{
16+
var listProperty = property.FindPropertyRelative("_states");
17+
18+
for (int i = 0; i < listProperty.arraySize; i++)
19+
{
20+
DrawState(listProperty, i);
21+
}
22+
}
23+
24+
public static string GetUniqueName(StateList stateList)
25+
{
26+
int nameIndex = 0;
27+
while (stateList.ContainsName($"State {nameIndex}"))
28+
nameIndex++;
29+
return $"State {nameIndex}";
30+
}
31+
32+
private static void DrawState(SerializedProperty listProperty, int index)
33+
{
34+
var property = listProperty.GetArrayElementAtIndex(index);
35+
EditorGUILayout.BeginVertical(GUI.skin.box);
36+
37+
var nameProperty = property.FindPropertyRelative(NameFieldName);
38+
var dataListProperty = property.FindPropertyRelative("_dataList");
39+
40+
EditorGUILayout.BeginHorizontal();
41+
EditorGUILayout.PropertyField(nameProperty, GUIContent.none);
42+
bool remove = MyGuiUtility.DrawRemoveButton();
43+
EditorGUILayout.EndHorizontal();
44+
45+
for (int i = 0; i < dataListProperty.arraySize; i++)
46+
{
47+
var dataProperty = dataListProperty.GetArrayElementAtIndex(i);
48+
var dataChildProperties = dataProperty.GetChildProperties();
49+
foreach (var prop in dataChildProperties)
50+
{
51+
EditorGUILayout.PropertyField(prop);
52+
}
53+
}
54+
55+
EditorGUILayout.EndVertical();
56+
57+
if(remove)
58+
listProperty.DeleteArrayElementAtIndex(index);
59+
}
60+
}
61+
}

Editor/Animations/StateListDrawer.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)