Custom background color for collections

This commit is contained in:
Crawcik
2021-06-03 13:45:07 +02:00
parent a1f985bc7d
commit e459707b0d
5 changed files with 50 additions and 28 deletions

View File

@@ -52,7 +52,7 @@ namespace FlaxEditor.CustomEditors.Editors
else
{
// Initialize new entries with default values
var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType));
var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType), NotNullItems);
for (int i = oldSize; i < newSize; i++)
newValues.SetValue(defaultValue, i);
}
@@ -60,7 +60,7 @@ namespace FlaxEditor.CustomEditors.Editors
else if (newSize > 0)
{
// Initialize new entries with default values
var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType));
var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType), NotNullItems);
for (int i = 0; i < newSize; i++)
newValues.SetValue(defaultValue, i);
}

View File

@@ -77,11 +77,15 @@ namespace FlaxEditor.CustomEditors.Editors
}
}
/// <summary>
/// Determines if value of collection can be null.
/// </summary>
protected bool NotNullItems;
private IntegerValueElement _size;
private Color _background;
private int _elementsCount;
private bool _readOnly;
private bool _canReorderItems;
private bool _notNullItems;
/// <summary>
/// Gets the length of the collection.
@@ -105,7 +109,7 @@ namespace FlaxEditor.CustomEditors.Editors
{
_readOnly = false;
_canReorderItems = true;
_notNullItems = false;
NotNullItems = false;
// No support for different collections for now
if (HasDifferentValues || HasDifferentTypes)
@@ -118,13 +122,19 @@ namespace FlaxEditor.CustomEditors.Editors
Type overrideEditorType = null;
float spacing = 10.0f;
var collection = (CollectionAttribute)attributes?.FirstOrDefault(x => x is CollectionAttribute);
if (collection != null)
if (collection is null)
{
_readOnly = false;
NotNullItems = false;
_background = new Color(1f, 1f, 1f, 0.08f);
}
else
{
// TODO: handle NotNullItems by filtering child editors SetValue
_readOnly = collection.ReadOnly;
_canReorderItems = collection.CanReorderItems;
_notNullItems = collection.NotNullItems;
NotNullItems = collection.NotNullItems;
_background = collection.BackgroundColor;
overrideEditorType = TypeUtils.GetType(collection.OverrideEditorTypeName).Type;
spacing = collection.Spacing;
}
@@ -146,6 +156,8 @@ namespace FlaxEditor.CustomEditors.Editors
// Elements
if (size > 0)
{
var panel = layout.VerticalPanel();
panel.Panel.BackgroundColor = _background;
var elementType = ElementType;
if (_canReorderItems)
{
@@ -153,7 +165,7 @@ namespace FlaxEditor.CustomEditors.Editors
{
if (i != 0 && spacing > 0f)
{
if (layout.Children.Count > 0 && layout.Children[layout.Children.Count - 1] is PropertiesListElement propertiesListElement)
if (panel.Children.Count > 0 && panel.Children[panel.Children.Count - 1] is PropertiesListElement propertiesListElement)
{
if (propertiesListElement.Labels.Count > 0)
{
@@ -166,12 +178,12 @@ namespace FlaxEditor.CustomEditors.Editors
}
else
{
layout.Space(spacing);
panel.Space(spacing);
}
}
var overrideEditor = overrideEditorType != null ? (CustomEditor)Activator.CreateInstance(overrideEditorType) : null;
layout.Object(new CollectionItemLabel(this, i), new ListValueContainer(elementType, i, Values), overrideEditor);
panel.Object(new CollectionItemLabel(this, i), new ListValueContainer(elementType, i, Values), overrideEditor);
}
}
else
@@ -180,14 +192,14 @@ namespace FlaxEditor.CustomEditors.Editors
{
if (i != 0 && spacing > 0f)
{
if (layout.Children.Count > 0 && layout.Children[layout.Children.Count - 1] is PropertiesListElement propertiesListElement)
if (panel.Children.Count > 0 && panel.Children[panel.Children.Count - 1] is PropertiesListElement propertiesListElement)
propertiesListElement.Space(spacing);
else
layout.Space(spacing);
panel.Space(spacing);
}
var overrideEditor = overrideEditorType != null ? (CustomEditor)Activator.CreateInstance(overrideEditorType) : null;
layout.Object("Element " + i, new ListValueContainer(elementType, i, Values), overrideEditor);
panel.Object("Element " + i, new ListValueContainer(elementType, i, Values), overrideEditor);
}
}
}

View File

@@ -135,6 +135,7 @@ namespace FlaxEditor.CustomEditors.Editors
}
private IntegerValueElement _size;
private Color _background;
private int _elementsCount;
private bool _readOnly;
private bool _notNullItems;
@@ -164,8 +165,6 @@ namespace FlaxEditor.CustomEditors.Editors
/// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout)
{
_readOnly = false;
_notNullItems = false;
// No support for different collections for now
if (HasDifferentValues || HasDifferentTypes)
@@ -185,12 +184,20 @@ namespace FlaxEditor.CustomEditors.Editors
if (attributes != null)
{
var collection = (CollectionAttribute)attributes.FirstOrDefault(x => x is CollectionAttribute);
if (collection != null)
if (collection is null)
{
// TODO: handle ReadOnly and NotNullItems by filtering child editors SetValue
_readOnly = false;
_notNullItems = false;
_background = new Color(1f, 1f, 1f, 0.08f);
}
else
{
_readOnly = collection.ReadOnly;
_notNullItems = collection.NotNullItems;
_background = collection.BackgroundColor;
overrideEditorType = TypeUtils.GetType(collection.OverrideEditorTypeName).Type;
spacing = collection.Spacing;
}
@@ -213,13 +220,15 @@ namespace FlaxEditor.CustomEditors.Editors
// Elements
if (size > 0)
{
var panel = layout.VerticalPanel();
panel.Panel.BackgroundColor = _background;
var keysEnumerable = ((IDictionary)Values[0]).Keys.OfType<object>();
var keys = keysEnumerable as object[] ?? keysEnumerable.ToArray();
for (int i = 0; i < size; i++)
{
if (i != 0 && spacing > 0f)
{
if (layout.Children.Count > 0 && layout.Children[layout.Children.Count - 1] is PropertiesListElement propertiesListElement)
if (panel.Children.Count > 0 && panel.Children[panel.Children.Count - 1] is PropertiesListElement propertiesListElement)
{
if (propertiesListElement.Labels.Count > 0)
{
@@ -232,13 +241,13 @@ namespace FlaxEditor.CustomEditors.Editors
}
else
{
layout.Space(spacing);
panel.Space(spacing);
}
}
var key = keys.ElementAt(i);
var overrideEditor = overrideEditorType != null ? (CustomEditor)Activator.CreateInstance(overrideEditorType) : null;
layout.Object(new DictionaryItemLabel(this, key), new DictionaryValueContainer(new ScriptType(valueType), key, Values), overrideEditor);
panel.Object(new DictionaryItemLabel(this, key), new DictionaryValueContainer(new ScriptType(valueType), key, Values), overrideEditor);
}
}
_elementsCount = size;
@@ -407,7 +416,7 @@ namespace FlaxEditor.CustomEditors.Editors
}
} while (!isUnique);
newValues[Convert.ChangeType(uniqueKey, keyType)] = TypeUtils.GetDefaultValue(new ScriptType(valueType));
newValues[Convert.ChangeType(uniqueKey, keyType)] = TypeUtils.GetDefaultValue(new ScriptType(valueType), _notNullItems);
}
else if (keyType.IsEnum)
{
@@ -428,7 +437,7 @@ namespace FlaxEditor.CustomEditors.Editors
}
} while (!isUnique && uniqueKeyIndex < enumValues.Length);
newValues[enumValues.GetValue(uniqueKeyIndex)] = TypeUtils.GetDefaultValue(new ScriptType(valueType));
newValues[enumValues.GetValue(uniqueKeyIndex)] = TypeUtils.GetDefaultValue(new ScriptType(valueType), _notNullItems);
}
else if (keyType == typeof(string))
{
@@ -448,7 +457,7 @@ namespace FlaxEditor.CustomEditors.Editors
}
} while (!isUnique);
newValues[uniqueKey] = TypeUtils.GetDefaultValue(new ScriptType(valueType));
newValues[uniqueKey] = TypeUtils.GetDefaultValue(new ScriptType(valueType), _notNullItems);
}
else
{

View File

@@ -22,9 +22,7 @@ namespace FlaxEditor.CustomEditors.Editors
var list = (IList)listType.CreateInstance();
var defaultValue = Scripting.TypeUtils.GetDefaultValue(ElementType);
for (int i = 0; i < size; i++)
{
list.Add(defaultValue);
}
return list;
}
@@ -57,7 +55,7 @@ namespace FlaxEditor.CustomEditors.Editors
else
{
// Initialize new entries with default values
var defaultValue = Scripting.TypeUtils.GetDefaultValue(elementType);
var defaultValue = Scripting.TypeUtils.GetDefaultValue(elementType, NotNullItems);
for (int i = oldSize; i < newSize; i++)
newValues.Add(defaultValue);
}
@@ -65,7 +63,7 @@ namespace FlaxEditor.CustomEditors.Editors
else if (newSize > 0)
{
// Fill new entries with default value
var defaultValue = Scripting.TypeUtils.GetDefaultValue(elementType);
var defaultValue = Scripting.TypeUtils.GetDefaultValue(elementType, NotNullItems);
for (int i = oldSize; i < newSize; i++)
newValues.Add(defaultValue);
}
@@ -86,9 +84,7 @@ namespace FlaxEditor.CustomEditors.Editors
var cloned = (IList)listType.CreateInstance();
for (int i = 0; i < size; i++)
{
cloned.Add(list[i]);
}
return cloned;
}

View File

@@ -34,5 +34,10 @@ namespace FlaxEngine
/// The spacing amount between collection items in the UI.
/// </summary>
public float Spacing;
/// <summary>
/// Background color of the collection.
/// </summary>
public Color BackgroundColor = new Color(1f, 1f, 1f, 0.08f);
}
}