mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Add option to show element labels in lists
This commit is contained in:
parent
5cb263e277
commit
ee4e02d7ce
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
@ -12,4 +13,18 @@ public class Collections_ListDrawerSettingsSample : ScriptableObject
|
||||
|
||||
[ListDrawerSettings(Draggable = false, AlwaysExpanded = true)]
|
||||
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;
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ namespace TriInspector.Elements
|
||||
private readonly TriProperty _property;
|
||||
private readonly ReorderableList _reorderableListGui;
|
||||
private readonly bool _alwaysExpanded;
|
||||
private readonly bool _showElementLabels;
|
||||
|
||||
private float _lastContentWidth;
|
||||
|
||||
@ -29,6 +30,7 @@ namespace TriInspector.Elements
|
||||
|
||||
_property = property;
|
||||
_alwaysExpanded = settings?.AlwaysExpanded ?? false;
|
||||
_showElementLabels = settings?.ShowElementLabels ?? false;
|
||||
_reorderableListGui = new ReorderableList(null, _property.ArrayElementType)
|
||||
{
|
||||
draggable = settings?.Draggable ?? true,
|
||||
@ -285,7 +287,7 @@ namespace TriInspector.Elements
|
||||
{
|
||||
return new TriPropertyElement(property, new TriPropertyElement.Props
|
||||
{
|
||||
forceInline = true,
|
||||
forceInline = !_showElementLabels,
|
||||
});
|
||||
}
|
||||
|
||||
@ -345,7 +347,10 @@ namespace TriInspector.Elements
|
||||
rect.xMin += DraggableAreaExtraWidth;
|
||||
}
|
||||
|
||||
GetChild(index).OnGUI(rect);
|
||||
using (TriPropertyOverrideContext.BeginOverride(ListPropertyOverrideContext.Instance))
|
||||
{
|
||||
GetChild(index).OnGUI(rect);
|
||||
}
|
||||
}
|
||||
|
||||
private float ElementHeightCallback(int index)
|
||||
@ -402,6 +407,28 @@ namespace TriInspector.Elements
|
||||
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
|
||||
{
|
||||
public static readonly GUIStyle ItemsCount;
|
||||
|
@ -127,6 +127,10 @@ namespace TriInspector
|
||||
{
|
||||
_displayNameBackingField.text = specialName;
|
||||
}
|
||||
else
|
||||
{
|
||||
_displayNameBackingField.text = TriUnityInspectorUtilities.GetStandardArrayElementName(this);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -28,6 +28,7 @@ namespace TriInspector
|
||||
{
|
||||
_previousContext = Current;
|
||||
Current = Override;
|
||||
Override = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,13 @@
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TriInspector.Utilities
|
||||
{
|
||||
public class TriUnityInspectorUtilities
|
||||
{
|
||||
private static readonly Dictionary<int, string> StandardArrayElementNames = new Dictionary<int, string>();
|
||||
|
||||
private static readonly FieldInfo GUIStyleNameBackingField = typeof(GUIStyle)
|
||||
.GetField("m_Name", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
|
||||
@ -19,6 +22,18 @@ namespace TriInspector.Utilities
|
||||
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)
|
||||
{
|
||||
if (property.FieldType == typeof(GUIStyle) && property.Value is GUIStyle guiStyle)
|
||||
@ -32,7 +47,8 @@ namespace TriInspector.Utilities
|
||||
property.ChildrenProperties.Count > 0 &&
|
||||
property.ChildrenProperties[0] is var firstChild &&
|
||||
firstChild.ValueType == typeof(string) &&
|
||||
firstChild.Value is string firstChildValueStr)
|
||||
firstChild.Value is string firstChildValueStr &&
|
||||
!string.IsNullOrEmpty(firstChildValueStr))
|
||||
{
|
||||
name = firstChildValueStr;
|
||||
return true;
|
||||
|
@ -11,5 +11,6 @@ namespace TriInspector
|
||||
public bool HideAddButton { get; set; }
|
||||
public bool HideRemoveButton { get; set; }
|
||||
public bool AlwaysExpanded { get; set; }
|
||||
public bool ShowElementLabels { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user