Skip to content
Merged
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
73 changes: 67 additions & 6 deletions Editor/Elements/TriListElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,56 @@ private void ReorderCallback(ReorderableList list, int oldIndex, int newIndex)
});
}

private void SetArraySizeCallback(int arraySize)
{
if (arraySize < 0)
{
return;
}

if (_property.TryGetSerializedProperty(out var serializedProperty))
{
serializedProperty.arraySize = arraySize;
_property.NotifyValueChanged();
return;
}

var template = CloneValue(_property);

_property.SetValues(targetIndex =>
{
var value = (IList) _property.GetValue(targetIndex);

if (_property.FieldType.IsArray)
{
var array = Array.CreateInstance(_property.ArrayElementType, arraySize);
Array.Copy(template, array, Math.Min(arraySize, template.Length));

value = array;
}
else
{
if (value == null)
{
value = (IList) Activator.CreateInstance(_property.FieldType);
}

while (value.Count > arraySize)
{
value.RemoveAt(value.Count - 1);
}

while (value.Count < arraySize)
{
var newElement = CreateDefaultElementValue(_property);
value.Add(newElement);
}
}

return value;
});
}

private bool GenerateChildren()
{
var count = _reorderableListGui.count;
Expand Down Expand Up @@ -293,10 +343,13 @@ protected virtual TriElement CreateItemElement(TriProperty property)

private void DrawHeaderCallback(Rect rect)
{
var labelRect = new Rect(rect);
var labelRect = new Rect(rect)
{
xMax = rect.xMax - 50,
};
var arraySizeRect = new Rect(rect)
{
xMin = rect.xMax - 100,
xMin = labelRect.xMax,
};

if (_alwaysExpanded)
Expand All @@ -308,8 +361,16 @@ private void DrawHeaderCallback(Rect rect)
TriEditorGUI.Foldout(labelRect, _property);
}

var label = _reorderableListGui.count == 0 ? "Empty" : $"{_reorderableListGui.count} items";
GUI.Label(arraySizeRect, label, Styles.ItemsCount);
EditorGUI.BeginChangeCheck();

var newArraySize = EditorGUI.DelayedIntField(arraySizeRect, _reorderableListGui.count);

if (EditorGUI.EndChangeCheck())
{
SetArraySizeCallback(newArraySize);
GUIUtility.ExitGUI();
return;
}

if (Event.current.type == EventType.DragUpdated && rect.Contains(Event.current.mousePosition))
{
Expand Down Expand Up @@ -410,7 +471,7 @@ private bool TryGetDragAndDropObject(Object obj, out Object result)
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)
Expand All @@ -428,7 +489,7 @@ public override bool TryGetDisplayName(TriProperty property, out GUIContent disp
return false;
}
}

private static class Styles
{
public static readonly GUIStyle ItemsCount;
Expand Down