Fix adding items in Array/List editors if element type is reference (eg. class)

This commit is contained in:
Wojtek Figat
2021-03-01 11:43:46 +01:00
parent dbec1389e8
commit b4e09a9d55
2 changed files with 24 additions and 13 deletions

View File

@@ -43,10 +43,18 @@ namespace FlaxEditor.CustomEditors.Editors
// Copy old values // Copy old values
Array.Copy(array, 0, newValues, 0, sharedCount); Array.Copy(array, 0, newValues, 0, sharedCount);
// Fill new entries with the last value if (elementType.IsValueType)
for (int i = oldSize; i < newSize; i++)
{ {
Array.Copy(array, oldSize - 1, newValues, i, 1); // Fill new entries with the last value
for (int i = oldSize; i < newSize; i++)
Array.Copy(array, oldSize - 1, newValues, i, 1);
}
else
{
// Initialize new entries with default values
var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType));
for (int i = oldSize; i < newSize; i++)
newValues.SetValue(defaultValue, i);
} }
} }
else if (newSize > 0) else if (newSize > 0)
@@ -54,9 +62,7 @@ namespace FlaxEditor.CustomEditors.Editors
// Initialize new entries with default values // Initialize new entries with default values
var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType)); var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType));
for (int i = 0; i < newSize; i++) for (int i = 0; i < newSize; i++)
{
newValues.SetValue(defaultValue, i); newValues.SetValue(defaultValue, i);
}
} }
SetValue(newValues); SetValue(newValues);

View File

@@ -40,30 +40,35 @@ namespace FlaxEditor.CustomEditors.Editors
// Allocate new list // Allocate new list
var listType = Values.Type; var listType = Values.Type;
var newValues = (IList)listType.CreateInstance(); var newValues = (IList)listType.CreateInstance();
var elementType = ElementType;
var sharedCount = Mathf.Min(oldSize, newSize); var sharedCount = Mathf.Min(oldSize, newSize);
if (list != null && sharedCount > 0) if (list != null && sharedCount > 0)
{ {
// Copy old values // Copy old values
for (int i = 0; i < sharedCount; i++) for (int i = 0; i < sharedCount; i++)
{
newValues.Add(list[i]); newValues.Add(list[i]);
}
// Fill new entries with the last value if (elementType.IsValueType)
for (int i = oldSize; i < newSize; i++)
{ {
newValues.Add(list[oldSize - 1]); // Fill new entries with the last value
for (int i = oldSize; i < newSize; i++)
newValues.Add(list[oldSize - 1]);
}
else
{
// Initialize new entries with default values
var defaultValue = Scripting.TypeUtils.GetDefaultValue(elementType);
for (int i = oldSize; i < newSize; i++)
newValues.Add(defaultValue);
} }
} }
else if (newSize > 0) else if (newSize > 0)
{ {
// Fill new entries with default value // Fill new entries with default value
var defaultValue = Scripting.TypeUtils.GetDefaultValue(ElementType); var defaultValue = Scripting.TypeUtils.GetDefaultValue(elementType);
for (int i = oldSize; i < newSize; i++) for (int i = oldSize; i < newSize; i++)
{
newValues.Add(defaultValue); newValues.Add(defaultValue);
}
} }
SetValue(newValues); SetValue(newValues);