Skip to content

Commit a95256a

Browse files
committed
Add naming of the elements by index
1 parent 7749ac0 commit a95256a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Runtime/Extensions/NodeExtensions.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public static (T1, T2, T3, T4) Find<T1, T2, T3, T4>(this VisualElement value, st
186186
}
187187
public static VisualElement Find(this VisualElement value, string path, string className = default, bool throwException = true, bool silent = true) => Find<VisualElement>(value, path, className, throwException, silent);
188188

189-
public static T Clone<T>(this T value, VisualElement parent = default) where T : VisualElement
189+
public static T Clone<T>(this T value, VisualElement parent = default, int index = -1) where T : VisualElement
190190
{
191191
parent ??= value.parent;
192192

@@ -215,6 +215,7 @@ public static T Clone<T>(this T value, VisualElement parent = default) where T :
215215

216216
parent.Add(elementClone);
217217
if (value.parent == elementClone.parent) elementClone.PlaceBehind(value);
218+
if (index >= 0) elementClone.name = $"{value.name} {nameof(VisualElement)}:{index}";
218219
parent.MarkDirtyRepaint();
219220

220221
if (elementClone is ISubElement subElement)
@@ -239,7 +240,7 @@ public static T Clone<T>(this T value, VisualElement parent = default) where T :
239240
documenRoot.MarkDirtyRepaint();
240241
}
241242
}
242-
public static VisualElement Clone(this VisualElement value, VisualElement parent = default) => Clone<VisualElement>(value, parent);
243+
public static VisualElement Clone(this VisualElement value, VisualElement parent = default, int index = -1) => Clone<VisualElement>(value, parent, index);
243244

244245
public static T Replace<T>(this VisualElement value, VisualElement prefab) where T : VisualElement
245246
{
@@ -579,8 +580,12 @@ static VisualElement FindRoot(VisualElement value)
579580
static (VisualElement value, string path) FindRoot(VisualElement value, string path)
580581
{
581582
if (rootMetadata.ContainsKey(value)) return (value, path);
582-
else if (value.parent is not null) return FindRoot(value.parent, path.NotNullOrEmpty() ? Path.Combine(value.name, path) : value.name);
583-
else throw new ArgumentException();
583+
if (value.parent is not null)
584+
{
585+
string name = value.name.Split($" {nameof(VisualElement)}:", StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? value.name;
586+
return FindRoot(value.parent, path.NotNullOrEmpty() ? Path.Combine(name, path) : name);
587+
}
588+
throw new ArgumentException();
584589
}
585590

586591
static void Initialize(object target, Type targetType, VisualElement targetRoot, bool throwException = false, bool silent = false)

0 commit comments

Comments
 (0)