Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using TriInspector;
using UnityEngine;

Expand All @@ -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;
}
}
31 changes: 29 additions & 2 deletions Editor/Elements/TriListElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class TriListElement : TriElement
private readonly TriProperty _property;
private readonly ReorderableList _reorderableListGui;
private readonly bool _alwaysExpanded;
private readonly bool _showElementLabels;

private float _lastContentWidth;

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

_property = property;
_alwaysExpanded = settings?.AlwaysExpanded ?? false;
_showElementLabels = settings?.ShowElementLabels ?? false;
_reorderableListGui = new ReorderableList(null, _property.ArrayElementType)
{
draggable = settings?.Draggable ?? true,
Expand Down Expand Up @@ -285,7 +287,7 @@ protected virtual TriElement CreateItemElement(TriProperty property)
{
return new TriPropertyElement(property, new TriPropertyElement.Props
{
forceInline = true,
forceInline = !_showElementLabels,
});
}

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

GetChild(index).OnGUI(rect);
using (TriPropertyOverrideContext.BeginOverride(ListPropertyOverrideContext.Instance))
{
GetChild(index).OnGUI(rect);
}
}

private float ElementHeightCallback(int index)
Expand Down Expand Up @@ -402,6 +407,28 @@ private bool TryGetDragAndDropObject(Object obj, out Object result)
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;
Expand Down
4 changes: 4 additions & 0 deletions Editor/TriProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public GUIContent DisplayNameContent
{
_displayNameBackingField.text = specialName;
}
else
{
_displayNameBackingField.text = TriUnityInspectorUtilities.GetStandardArrayElementName(this);
}
}
else
{
Expand Down
1 change: 1 addition & 0 deletions Editor/TriPropertyOverrideContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public EnterPropertyScope Init()
{
_previousContext = Current;
Current = Override;
Override = null;
return this;
}

Expand Down
20 changes: 18 additions & 2 deletions Editor/Utilities/TriUnityInspectorUtilities.cs
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -19,6 +22,18 @@ public static bool MustDrawWithUnity(TriProperty property)
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)
Expand All @@ -32,7 +47,8 @@ public static bool TryGetSpecialArrayElementName(TriProperty property, out strin
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;
Expand Down
1 change: 1 addition & 0 deletions Runtime/Attributes/ListDrawerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class ListDrawerSettingsAttribute : Attribute
public bool HideAddButton { get; set; }
public bool HideRemoveButton { get; set; }
public bool AlwaysExpanded { get; set; }
public bool ShowElementLabels { get; set; }
}
}