Fix UI with list of null items when layout gets rebuilt at item level

This commit is contained in:
Wojtek Figat
2021-08-12 11:47:38 +02:00
parent f6eef82864
commit 718ae78036
6 changed files with 60 additions and 19 deletions

View File

@@ -154,6 +154,10 @@ namespace FlaxEditor.CustomEditors.Editors
var panel = layout.VerticalPanel();
panel.Panel.BackgroundColor = _background;
var elementType = ElementType;
// Use separate layout cells for each collection items to improve layout updates for them in separation
var useSharedLayout = elementType.IsPrimitive || elementType.IsEnum;
if (_canReorderItems)
{
for (int i = 0; i < size; i++)
@@ -178,7 +182,9 @@ namespace FlaxEditor.CustomEditors.Editors
}
var overrideEditor = overrideEditorType != null ? (CustomEditor)Activator.CreateInstance(overrideEditorType) : null;
panel.Object(new CollectionItemLabel(this, i), new ListValueContainer(elementType, i, Values), overrideEditor);
var property = panel.AddPropertyItem(new CollectionItemLabel(this, i));
var itemLayout = useSharedLayout ? (LayoutElementsContainer)property : property.VerticalPanel();
itemLayout.Object(new ListValueContainer(elementType, i, Values), overrideEditor);
}
}
else
@@ -194,7 +200,9 @@ namespace FlaxEditor.CustomEditors.Editors
}
var overrideEditor = overrideEditorType != null ? (CustomEditor)Activator.CreateInstance(overrideEditorType) : null;
panel.Object("Element " + i, new ListValueContainer(elementType, i, Values), overrideEditor);
var property = panel.AddPropertyItem("Element " + i);
var itemLayout = useSharedLayout ? (LayoutElementsContainer)property : property.VerticalPanel();
itemLayout.Object(new ListValueContainer(elementType, i, Values), overrideEditor);
}
}
}

View File

@@ -216,6 +216,11 @@ namespace FlaxEditor.CustomEditors.Editors
panel.Panel.BackgroundColor = _background;
var keysEnumerable = ((IDictionary)Values[0]).Keys.OfType<object>();
var keys = keysEnumerable as object[] ?? keysEnumerable.ToArray();
var valuesType = new ScriptType(valueType);
// Use separate layout cells for each collection items to improve layout updates for them in separation
var useSharedLayout = valueType.IsPrimitive || valueType.IsEnum;
for (int i = 0; i < size; i++)
{
if (i != 0 && spacing > 0f)
@@ -239,7 +244,9 @@ namespace FlaxEditor.CustomEditors.Editors
var key = keys.ElementAt(i);
var overrideEditor = overrideEditorType != null ? (CustomEditor)Activator.CreateInstance(overrideEditorType) : null;
panel.Object(new DictionaryItemLabel(this, key), new DictionaryValueContainer(new ScriptType(valueType), key, Values), overrideEditor);
var property = panel.AddPropertyItem(new DictionaryItemLabel(this, key));
var itemLayout = useSharedLayout ? (LayoutElementsContainer)property : property.VerticalPanel();
itemLayout.Object(new DictionaryValueContainer(valuesType, key, Values), overrideEditor);
}
}
_elementsCount = size;

View File

@@ -228,6 +228,7 @@ namespace FlaxEditor.CustomEditors.Editors
}
private VisibleIfCache[] _visibleIfCaches;
private bool _isNull;
/// <summary>
/// Gets the items for the type
@@ -264,7 +265,7 @@ namespace FlaxEditor.CustomEditors.Editors
// Skip properties without getter or setter
if (!p.HasGet || (!p.HasSet && !showInEditor))
continue;
// Skip hidden fields, handle special attributes
if ((!p.IsPublic && !showInEditor) || attributes.Any(x => x is HideInEditorAttribute))
continue;
@@ -421,7 +422,9 @@ namespace FlaxEditor.CustomEditors.Editors
/// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout)
{
var values = Values;
_visibleIfCaches = null;
_isNull = values != null && values.IsNull;
// Collect items to edit
List<ItemInfo> items;
@@ -446,12 +449,7 @@ namespace FlaxEditor.CustomEditors.Editors
Parent = layout.ContainerControl,
Location = new Vector2(layout.ContainerControl.Width - ButtonSize - 4, (layout.ContainerControl.Height - ButtonSize) * 0.5f),
};
button.Clicked += () =>
{
var newType = Values.Type;
SetValue(newType.CreateInstance());
RebuildLayoutOnRefresh();
};
button.Clicked += () => SetValue(Values.Type.CreateInstance());
}
layout.Label("<null>");
@@ -558,6 +556,13 @@ namespace FlaxEditor.CustomEditors.Editors
/// <inheritdoc />
public override void Refresh()
{
// Automatic refresh when value nullability changed
if (_isNull != Values.IsNull)
{
RebuildLayout();
return;
}
if (_visibleIfCaches != null)
{
try