Skip to content

Commit ee4e02d

Browse files
committed
Add option to show element labels in lists
1 parent 5cb263e commit ee4e02d

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

Editor.Samples/Collections/Collections_ListDrawerSettingsSample.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using TriInspector;
34
using UnityEngine;
45

@@ -12,4 +13,18 @@ public class Collections_ListDrawerSettingsSample : ScriptableObject
1213

1314
[ListDrawerSettings(Draggable = false, AlwaysExpanded = true)]
1415
public Vector3[] vectors;
16+
17+
[ListDrawerSettings(ShowElementLabels = true)]
18+
public MyStruct[] namedStructs = new MyStruct[]
19+
{
20+
new MyStruct {name = "First", value = 1},
21+
new MyStruct {name = "Second", value = 2,},
22+
};
23+
24+
[Serializable]
25+
public struct MyStruct
26+
{
27+
public string name;
28+
public int value;
29+
}
1530
}

Editor/Elements/TriListElement.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class TriListElement : TriElement
1818
private readonly TriProperty _property;
1919
private readonly ReorderableList _reorderableListGui;
2020
private readonly bool _alwaysExpanded;
21+
private readonly bool _showElementLabels;
2122

2223
private float _lastContentWidth;
2324

@@ -29,6 +30,7 @@ public TriListElement(TriProperty property)
2930

3031
_property = property;
3132
_alwaysExpanded = settings?.AlwaysExpanded ?? false;
33+
_showElementLabels = settings?.ShowElementLabels ?? false;
3234
_reorderableListGui = new ReorderableList(null, _property.ArrayElementType)
3335
{
3436
draggable = settings?.Draggable ?? true,
@@ -285,7 +287,7 @@ protected virtual TriElement CreateItemElement(TriProperty property)
285287
{
286288
return new TriPropertyElement(property, new TriPropertyElement.Props
287289
{
288-
forceInline = true,
290+
forceInline = !_showElementLabels,
289291
});
290292
}
291293

@@ -345,7 +347,10 @@ private void DrawElementCallback(Rect rect, int index, bool isActive, bool isFoc
345347
rect.xMin += DraggableAreaExtraWidth;
346348
}
347349

348-
GetChild(index).OnGUI(rect);
350+
using (TriPropertyOverrideContext.BeginOverride(ListPropertyOverrideContext.Instance))
351+
{
352+
GetChild(index).OnGUI(rect);
353+
}
349354
}
350355

351356
private float ElementHeightCallback(int index)
@@ -402,6 +407,28 @@ private bool TryGetDragAndDropObject(Object obj, out Object result)
402407
return false;
403408
}
404409

410+
private class ListPropertyOverrideContext : TriPropertyOverrideContext
411+
{
412+
public static readonly ListPropertyOverrideContext Instance = new ListPropertyOverrideContext();
413+
414+
private readonly GUIContent _noneLabel = GUIContent.none;
415+
416+
public override bool TryGetDisplayName(TriProperty property, out GUIContent displayName)
417+
{
418+
var showLabels = property.TryGetAttribute(out ListDrawerSettingsAttribute settings) &&
419+
settings.ShowElementLabels;
420+
421+
if (!showLabels)
422+
{
423+
displayName = _noneLabel;
424+
return true;
425+
}
426+
427+
displayName = default;
428+
return false;
429+
}
430+
}
431+
405432
private static class Styles
406433
{
407434
public static readonly GUIStyle ItemsCount;

Editor/TriProperty.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public GUIContent DisplayNameContent
127127
{
128128
_displayNameBackingField.text = specialName;
129129
}
130+
else
131+
{
132+
_displayNameBackingField.text = TriUnityInspectorUtilities.GetStandardArrayElementName(this);
133+
}
130134
}
131135
else
132136
{

Editor/TriPropertyOverrideContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public EnterPropertyScope Init()
2828
{
2929
_previousContext = Current;
3030
Current = Override;
31+
Override = null;
3132
return this;
3233
}
3334

Editor/Utilities/TriUnityInspectorUtilities.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
using System.Reflection;
1+
using System.Collections.Generic;
2+
using System.Reflection;
23
using UnityEngine;
34

45
namespace TriInspector.Utilities
56
{
67
public class TriUnityInspectorUtilities
78
{
9+
private static readonly Dictionary<int, string> StandardArrayElementNames = new Dictionary<int, string>();
10+
811
private static readonly FieldInfo GUIStyleNameBackingField = typeof(GUIStyle)
912
.GetField("m_Name", BindingFlags.Instance | BindingFlags.NonPublic);
1013

@@ -19,6 +22,18 @@ public static bool MustDrawWithUnity(TriProperty property)
1922
return !property.IsArray && property.TryGetAttribute(out DrawWithUnityAttribute _);
2023
}
2124

25+
public static string GetStandardArrayElementName(TriProperty property)
26+
{
27+
var index = property.IndexInArray;
28+
29+
if (!StandardArrayElementNames.TryGetValue(index, out var name))
30+
{
31+
StandardArrayElementNames[index] = name = $"Element {index}";
32+
}
33+
34+
return name;
35+
}
36+
2237
public static bool TryGetSpecialArrayElementName(TriProperty property, out string name)
2338
{
2439
if (property.FieldType == typeof(GUIStyle) && property.Value is GUIStyle guiStyle)
@@ -32,7 +47,8 @@ public static bool TryGetSpecialArrayElementName(TriProperty property, out strin
3247
property.ChildrenProperties.Count > 0 &&
3348
property.ChildrenProperties[0] is var firstChild &&
3449
firstChild.ValueType == typeof(string) &&
35-
firstChild.Value is string firstChildValueStr)
50+
firstChild.Value is string firstChildValueStr &&
51+
!string.IsNullOrEmpty(firstChildValueStr))
3652
{
3753
name = firstChildValueStr;
3854
return true;

Runtime/Attributes/ListDrawerSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public class ListDrawerSettingsAttribute : Attribute
1111
public bool HideAddButton { get; set; }
1212
public bool HideRemoveButton { get; set; }
1313
public bool AlwaysExpanded { get; set; }
14+
public bool ShowElementLabels { get; set; }
1415
}
1516
}

0 commit comments

Comments
 (0)