Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions Editor/Builders/AssetsItemBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using TNRD.Items;
using TNRD.Utilities;
using UnityEditor;
Expand Down Expand Up @@ -46,7 +47,60 @@ public AdvancedDropdownItem Build()
}
}

return root;
return Collapse(root);
}

private AdvancedDropdownItem Collapse(AdvancedDropdownItem root)
{
AdvancedDropdownItem[] rootChildren = root.children.ToArray();

if (root is IDropdownItem)
{
return root;
}

if (rootChildren.Length == 0)
{
return null;
}
AdvancedDropdownItem newRoot = new(root.name)
{
icon = root.icon,
enabled = root.enabled,
id = root.id,
};

while (rootChildren.Length == 1 && rootChildren[0] is {} singleChild and not IDropdownItem)
{
newRoot = new AdvancedDropdownItem(CollapseName($"{newRoot.name}/{singleChild.name}"))
{
icon = singleChild.icon,
id = singleChild.id,
};
rootChildren = singleChild.children.ToArray();
}
bool addedChildren = false;
foreach (var child in rootChildren)
{
AdvancedDropdownItem newChild = Collapse(child);
if (newChild != null)
{
newRoot.AddChild(newChild);
addedChildren = true;
}
}
return addedChildren ? newRoot : null;
}

private const int MAX_NAME_LENGTH = 90;
private const int HALF_LENGTH = (MAX_NAME_LENGTH - 4) / 2;
private string CollapseName(string name)
{
if (name.Length > MAX_NAME_LENGTH)
{
return $"{name[..HALF_LENGTH]}...{name[^HALF_LENGTH..]}";
}
return name;
}

private void CreateItemForPath(AdvancedDropdownItem root, string path)
Expand Down
7 changes: 6 additions & 1 deletion Editor/Builders/ClassesItemBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using TNRD.Items;
using TNRD.Utilities;
using UnityEditor;
Expand Down Expand Up @@ -35,6 +36,10 @@ public AdvancedDropdownItem Build()
AdvancedDropdownItem parent = GetOrCreateParentItem(type, root);
parent.AddChild(new ClassDropdownItem(type));
}
if (!root.children.Any())
{
return null;
}

return root;
}
Expand Down
11 changes: 6 additions & 5 deletions Editor/Builders/SceneItemBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using TNRD.Items;
using TNRD.Utilities;
Expand Down Expand Up @@ -26,10 +26,7 @@ public AdvancedDropdownItem Build()
{
if (scene == null || !scene.Value.IsValid())
{
return new AdvancedDropdownItem("Scene")
{
enabled = false
};
return null;
}

AdvancedDropdownItem root = new AdvancedDropdownItemWrapper("Scene");
Expand All @@ -40,6 +37,10 @@ public AdvancedDropdownItem Build()
{
CreateItemsRecursive(rootGameObject.transform, root);
}
if (!root.children.Any())
{
return null;
}

return root;
}
Expand Down
6 changes: 4 additions & 2 deletions Editor/Items/ClassDropdownItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using TNRD.Utilities;
using UnityEditor.IMGUI.Controls;
Expand All @@ -14,7 +14,9 @@ public ClassDropdownItem(Type type)
: base(type.Name)
{
this.type = type;
enabled = type.GetConstructors().Any(x => x.GetParameters().Length == 0);
// Needs a parameterless constructor or be a value type
enabled = type.GetConstructors().Any(x => x.GetParameters().Length == 0)
|| type.IsValueType;
icon = IconUtility.ScriptIcon;
}

Expand Down
7 changes: 5 additions & 2 deletions Editor/Utilities/AdvancedDropdownItemWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using UnityEditor.IMGUI.Controls;
using UnityEditor.IMGUI.Controls;

namespace TNRD.Utilities
{
Expand All @@ -12,7 +12,10 @@ public AdvancedDropdownItemWrapper(string name)

public new AdvancedDropdownItemWrapper AddChild(AdvancedDropdownItem child)
{
base.AddChild(child);
if (child != null)
{
base.AddChild(child);
}
return this;
}

Expand Down
7 changes: 2 additions & 5 deletions Editor/Utilities/SerializableInterfaceAdvancedDropdown.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Reflection;
using TNRD.Builders;
Expand Down Expand Up @@ -60,10 +60,7 @@ protected override AdvancedDropdownItem BuildRoot()
item.AddChild(new SceneItemBuilder(interfaceType, relevantScene).Build());
}

foreach (AdvancedDropdownItem dropdownItem in item.children)
{
dropdownItem.AddChild(new NoneDropdownItem());
}
item.AddChild(new NoneDropdownItem());

if (canSort)
{
Expand Down