Skip to content

Commit 1921fb0

Browse files
committed
Add arraysize gui field for lists
1 parent 8e9e488 commit 1921fb0

File tree

1 file changed

+67
-6
lines changed

1 file changed

+67
-6
lines changed

Editor/Elements/TriListElement.cs

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,56 @@ private void ReorderCallback(ReorderableList list, int oldIndex, int newIndex)
248248
});
249249
}
250250

251+
private void SetArraySizeCallback(int arraySize)
252+
{
253+
if (arraySize < 0)
254+
{
255+
return;
256+
}
257+
258+
if (_property.TryGetSerializedProperty(out var serializedProperty))
259+
{
260+
serializedProperty.arraySize = arraySize;
261+
_property.NotifyValueChanged();
262+
return;
263+
}
264+
265+
var template = CloneValue(_property);
266+
267+
_property.SetValues(targetIndex =>
268+
{
269+
var value = (IList) _property.GetValue(targetIndex);
270+
271+
if (_property.FieldType.IsArray)
272+
{
273+
var array = Array.CreateInstance(_property.ArrayElementType, arraySize);
274+
Array.Copy(template, array, Math.Min(arraySize, template.Length));
275+
276+
value = array;
277+
}
278+
else
279+
{
280+
if (value == null)
281+
{
282+
value = (IList) Activator.CreateInstance(_property.FieldType);
283+
}
284+
285+
while (value.Count > arraySize)
286+
{
287+
value.RemoveAt(value.Count - 1);
288+
}
289+
290+
while (value.Count < arraySize)
291+
{
292+
var newElement = CreateDefaultElementValue(_property);
293+
value.Add(newElement);
294+
}
295+
}
296+
297+
return value;
298+
});
299+
}
300+
251301
private bool GenerateChildren()
252302
{
253303
var count = _reorderableListGui.count;
@@ -293,10 +343,13 @@ protected virtual TriElement CreateItemElement(TriProperty property)
293343

294344
private void DrawHeaderCallback(Rect rect)
295345
{
296-
var labelRect = new Rect(rect);
346+
var labelRect = new Rect(rect)
347+
{
348+
xMax = rect.xMax - 50,
349+
};
297350
var arraySizeRect = new Rect(rect)
298351
{
299-
xMin = rect.xMax - 100,
352+
xMin = labelRect.xMax,
300353
};
301354

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

311-
var label = _reorderableListGui.count == 0 ? "Empty" : $"{_reorderableListGui.count} items";
312-
GUI.Label(arraySizeRect, label, Styles.ItemsCount);
364+
EditorGUI.BeginChangeCheck();
365+
366+
var newArraySize = EditorGUI.DelayedIntField(arraySizeRect, _reorderableListGui.count);
367+
368+
if (EditorGUI.EndChangeCheck())
369+
{
370+
SetArraySizeCallback(newArraySize);
371+
GUIUtility.ExitGUI();
372+
return;
373+
}
313374

314375
if (Event.current.type == EventType.DragUpdated && rect.Contains(Event.current.mousePosition))
315376
{
@@ -410,7 +471,7 @@ private bool TryGetDragAndDropObject(Object obj, out Object result)
410471
private class ListPropertyOverrideContext : TriPropertyOverrideContext
411472
{
412473
public static readonly ListPropertyOverrideContext Instance = new ListPropertyOverrideContext();
413-
474+
414475
private readonly GUIContent _noneLabel = GUIContent.none;
415476

416477
public override bool TryGetDisplayName(TriProperty property, out GUIContent displayName)
@@ -428,7 +489,7 @@ public override bool TryGetDisplayName(TriProperty property, out GUIContent disp
428489
return false;
429490
}
430491
}
431-
492+
432493
private static class Styles
433494
{
434495
public static readonly GUIStyle ItemsCount;

0 commit comments

Comments
 (0)