Skip to content

Commit 51664da

Browse files
committed
Add alternating background in lists
1 parent 54bba08 commit 51664da

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Editor/Elements/TriListElement.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class TriListElement : TriElement
2121
private readonly ReorderableList _reorderableListGui;
2222
private readonly bool _alwaysExpanded;
2323
private readonly bool _showElementLabels;
24+
private readonly bool _showAlternatingBackground;
2425

2526
private float _lastContentWidth;
2627
private int? _lastInvisibleElement;
@@ -35,6 +36,7 @@ public TriListElement(TriProperty property)
3536
_property = property;
3637
_alwaysExpanded = settings?.AlwaysExpanded ?? false;
3738
_showElementLabels = settings?.ShowElementLabels ?? false;
39+
_showAlternatingBackground = settings?.ShowAlternatingBackground ?? true;
3840
_reorderableListGui = new ReorderableList(null, _property.ArrayElementType)
3941
{
4042
showDefaultBackground = settings?.ShowDefaultBackground ?? true,
@@ -43,6 +45,7 @@ public TriListElement(TriProperty property)
4345
displayRemove = settings == null || !settings.HideRemoveButton,
4446
drawHeaderCallback = DrawHeaderCallback,
4547
elementHeightCallback = ElementHeightCallback,
48+
drawElementBackgroundCallback = DrawElementBackgroundCallback,
4649
drawElementCallback = DrawElementCallback,
4750
onAddCallback = AddElementCallback,
4851
onRemoveCallback = RemoveElementCallback,
@@ -409,6 +412,23 @@ private void DrawHeaderCallback(Rect rect)
409412
Event.current.Use();
410413
}
411414
}
415+
416+
private void DrawElementBackgroundCallback(Rect rect, int index, bool isActive, bool isFocused)
417+
{
418+
if (_lastInvisibleElement.HasValue && index + 1 < _lastInvisibleElement.Value ||
419+
_lastVisibleElement.HasValue && index - 1 > _lastVisibleElement.Value)
420+
{
421+
return;
422+
}
423+
424+
if (_showAlternatingBackground && index % 2 != 0)
425+
{
426+
EditorGUI.DrawRect(rect, new Color(0.1f, 0.1f, 0.1f, 0.15f));
427+
}
428+
429+
ReorderableList.defaultBehaviours.DrawElementBackground(rect, index, isActive, isFocused,
430+
_reorderableListGui.draggable);
431+
}
412432

413433
private void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
414434
{

Runtime/Attributes/Collections/ListDrawerSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public class ListDrawerSettingsAttribute : Attribute
1313
public bool AlwaysExpanded { get; set; }
1414
public bool ShowElementLabels { get; set; }
1515
public bool ShowDefaultBackground { get; set; } = true;
16+
public bool ShowAlternatingBackground { get; set; } = true;
1617
}
1718
}

0 commit comments

Comments
 (0)