Add option to show element labels in lists

This commit is contained in:
VladV 2023-08-18 10:35:16 +04:00
parent 5cb263e277
commit ee4e02d7ce
6 changed files with 69 additions and 5 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using TriInspector; using TriInspector;
using UnityEngine; using UnityEngine;
@ -12,4 +13,18 @@ public class Collections_ListDrawerSettingsSample : ScriptableObject
[ListDrawerSettings(Draggable = false, AlwaysExpanded = true)] [ListDrawerSettings(Draggable = false, AlwaysExpanded = true)]
public Vector3[] vectors; public Vector3[] vectors;
[ListDrawerSettings(ShowElementLabels = true)]
public MyStruct[] namedStructs = new MyStruct[]
{
new MyStruct {name = "First", value = 1},
new MyStruct {name = "Second", value = 2,},
};
[Serializable]
public struct MyStruct
{
public string name;
public int value;
}
} }

View File

@ -18,6 +18,7 @@ namespace TriInspector.Elements
private readonly TriProperty _property; private readonly TriProperty _property;
private readonly ReorderableList _reorderableListGui; private readonly ReorderableList _reorderableListGui;
private readonly bool _alwaysExpanded; private readonly bool _alwaysExpanded;
private readonly bool _showElementLabels;
private float _lastContentWidth; private float _lastContentWidth;
@ -29,6 +30,7 @@ namespace TriInspector.Elements
_property = property; _property = property;
_alwaysExpanded = settings?.AlwaysExpanded ?? false; _alwaysExpanded = settings?.AlwaysExpanded ?? false;
_showElementLabels = settings?.ShowElementLabels ?? false;
_reorderableListGui = new ReorderableList(null, _property.ArrayElementType) _reorderableListGui = new ReorderableList(null, _property.ArrayElementType)
{ {
draggable = settings?.Draggable ?? true, draggable = settings?.Draggable ?? true,
@ -285,7 +287,7 @@ namespace TriInspector.Elements
{ {
return new TriPropertyElement(property, new TriPropertyElement.Props return new TriPropertyElement(property, new TriPropertyElement.Props
{ {
forceInline = true, forceInline = !_showElementLabels,
}); });
} }
@ -345,7 +347,10 @@ namespace TriInspector.Elements
rect.xMin += DraggableAreaExtraWidth; rect.xMin += DraggableAreaExtraWidth;
} }
GetChild(index).OnGUI(rect); using (TriPropertyOverrideContext.BeginOverride(ListPropertyOverrideContext.Instance))
{
GetChild(index).OnGUI(rect);
}
} }
private float ElementHeightCallback(int index) private float ElementHeightCallback(int index)
@ -402,6 +407,28 @@ namespace TriInspector.Elements
return false; return false;
} }
private class ListPropertyOverrideContext : TriPropertyOverrideContext
{
public static readonly ListPropertyOverrideContext Instance = new ListPropertyOverrideContext();
private readonly GUIContent _noneLabel = GUIContent.none;
public override bool TryGetDisplayName(TriProperty property, out GUIContent displayName)
{
var showLabels = property.TryGetAttribute(out ListDrawerSettingsAttribute settings) &&
settings.ShowElementLabels;
if (!showLabels)
{
displayName = _noneLabel;
return true;
}
displayName = default;
return false;
}
}
private static class Styles private static class Styles
{ {
public static readonly GUIStyle ItemsCount; public static readonly GUIStyle ItemsCount;

View File

@ -127,6 +127,10 @@ namespace TriInspector
{ {
_displayNameBackingField.text = specialName; _displayNameBackingField.text = specialName;
} }
else
{
_displayNameBackingField.text = TriUnityInspectorUtilities.GetStandardArrayElementName(this);
}
} }
else else
{ {

View File

@ -28,6 +28,7 @@ namespace TriInspector
{ {
_previousContext = Current; _previousContext = Current;
Current = Override; Current = Override;
Override = null;
return this; return this;
} }

View File

@ -1,10 +1,13 @@
using System.Reflection; using System.Collections.Generic;
using System.Reflection;
using UnityEngine; using UnityEngine;
namespace TriInspector.Utilities namespace TriInspector.Utilities
{ {
public class TriUnityInspectorUtilities public class TriUnityInspectorUtilities
{ {
private static readonly Dictionary<int, string> StandardArrayElementNames = new Dictionary<int, string>();
private static readonly FieldInfo GUIStyleNameBackingField = typeof(GUIStyle) private static readonly FieldInfo GUIStyleNameBackingField = typeof(GUIStyle)
.GetField("m_Name", BindingFlags.Instance | BindingFlags.NonPublic); .GetField("m_Name", BindingFlags.Instance | BindingFlags.NonPublic);
@ -19,6 +22,18 @@ namespace TriInspector.Utilities
return !property.IsArray && property.TryGetAttribute(out DrawWithUnityAttribute _); return !property.IsArray && property.TryGetAttribute(out DrawWithUnityAttribute _);
} }
public static string GetStandardArrayElementName(TriProperty property)
{
var index = property.IndexInArray;
if (!StandardArrayElementNames.TryGetValue(index, out var name))
{
StandardArrayElementNames[index] = name = $"Element {index}";
}
return name;
}
public static bool TryGetSpecialArrayElementName(TriProperty property, out string name) public static bool TryGetSpecialArrayElementName(TriProperty property, out string name)
{ {
if (property.FieldType == typeof(GUIStyle) && property.Value is GUIStyle guiStyle) if (property.FieldType == typeof(GUIStyle) && property.Value is GUIStyle guiStyle)
@ -32,7 +47,8 @@ namespace TriInspector.Utilities
property.ChildrenProperties.Count > 0 && property.ChildrenProperties.Count > 0 &&
property.ChildrenProperties[0] is var firstChild && property.ChildrenProperties[0] is var firstChild &&
firstChild.ValueType == typeof(string) && firstChild.ValueType == typeof(string) &&
firstChild.Value is string firstChildValueStr) firstChild.Value is string firstChildValueStr &&
!string.IsNullOrEmpty(firstChildValueStr))
{ {
name = firstChildValueStr; name = firstChildValueStr;
return true; return true;

View File

@ -11,5 +11,6 @@ namespace TriInspector
public bool HideAddButton { get; set; } public bool HideAddButton { get; set; }
public bool HideRemoveButton { get; set; } public bool HideRemoveButton { get; set; }
public bool AlwaysExpanded { get; set; } public bool AlwaysExpanded { get; set; }
public bool ShowElementLabels { get; set; }
} }
} }