ReorderableRectsScope
Usefull to reorder lists in a custom editor.
Visual and events
using(var scope = new ReorderableRectsScope(Color.red)) {
scope.OnDrop = (int from, int to) => Debug.Log("drop event");
// example: use begin and end events for optimization (each frame calls Draw method during the drag)
scope.OnDragBegin = () => _refreshData = false;
scope.OnDragEnd = () => _refreshData = true;
var orderedList = li.Value.ToList();
if (reorderable) {
orderedList = orderedList.OrderBy(comp =>(comp.Key as IIndexabled).Index).ToList();
}
foreach (var component in orderedList) {
using(new GUILayout.HorizontalScope()) {
using(new GUILayout.VerticalScope(Style.TabContainer)) {
if (_refreshData) updateData();
// draw item
scope.PushItem(); // take the last rect as line
}
GUILayout.Space(12);
}
EditorGUILayout.Space();
}
}
Reorder example
void Draw() {
// components can be reordered
bool reorderable = (MyList.Count > 1 && (MyList.First().Key as IIndexabled) != null);
using(var scope = new ReorderableRectsScope(Color.red, reorderable ? ReorderableRectsScope.EStyle.Arrow : ReorderableRectsScope.EStyle.Nothing)) {
scope.OnDrop = (int from, int to) => DropComponent(MyList, from, to);
var orderedList = li.Value.ToList();
if (reorderable) {
orderedList = orderedList.OrderBy(comp =>(comp.Key as IIndexabled).Index).ToList();
}
foreach (var component in orderedList) {
...
}
}
}
protected virtual void DropComponent(Dictionary<Component, ManagedBehaviourEditor> data, int from, int to) {
if (from == to) return;
var list = data.Keys.Select(component => component as IIndexabled).OrderBy(indexabled => indexabled.Index).ToList();
list = ReorderableRectsScope.ReorderList<IIndexabled>(list, from, to); // helper to reorder list
// reorder components
int i = 0;
foreach (var indexabled in list) {
indexabled.Index = i++;
}
}
Reorderable scope type
new ReorderableRectsScope(Color, ReorderableRectsScope.EStyle)
ReorderableRectsScope.EStyle.Object
: Event on the entire rectReorderableRectsScope.EStyle.Arrow
: Append an arrow at rightReorderableRectsScope.EStyle.Nothing
: Not reorderable