Fix duplicating array values in Editor

#1959
This commit is contained in:
Wojtek Figat
2024-03-04 18:25:57 +01:00
parent 5fdf1789ce
commit bbe08be462
3 changed files with 21 additions and 17 deletions

View File

@@ -747,7 +747,7 @@ namespace FlaxEditor.CustomEditors
/// </summary> /// </summary>
public void SetValueToDefault() public void SetValueToDefault()
{ {
SetValueCloned(Values.DefaultValue); SetValue(Utilities.Utils.CloneValue(Values.DefaultValue));
} }
/// <summary> /// <summary>
@@ -784,19 +784,7 @@ namespace FlaxEditor.CustomEditors
return; return;
} }
SetValueCloned(Values.ReferenceValue); SetValue(Utilities.Utils.CloneValue(Values.ReferenceValue));
}
private void SetValueCloned(object value)
{
// For objects (eg. arrays) we need to clone them to prevent editing default/reference value within editor
if (value != null && !value.GetType().IsValueType)
{
var json = JsonSerializer.Serialize(value);
value = JsonSerializer.Deserialize(json, value.GetType());
}
SetValue(value);
} }
/// <summary> /// <summary>

View File

@@ -2,7 +2,6 @@
using System; using System;
using System.Collections; using System.Collections;
using FlaxEditor.Scripting;
using FlaxEngine; using FlaxEngine;
using FlaxEngine.Utilities; using FlaxEngine.Utilities;
@@ -47,8 +46,9 @@ namespace FlaxEditor.CustomEditors.Editors
if (elementType.IsValueType || NotNullItems) if (elementType.IsValueType || NotNullItems)
{ {
// Fill new entries with the last value // Fill new entries with the last value
var lastValue = array.GetValue(oldSize - 1);
for (int i = oldSize; i < newSize; i++) for (int i = oldSize; i < newSize; i++)
Array.Copy(array, oldSize - 1, newValues, i, 1); newValues.SetValue(Utilities.Utils.CloneValue(lastValue), i);
} }
else else
{ {

View File

@@ -11,7 +11,6 @@ using System.Globalization;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@@ -20,6 +19,7 @@ using FlaxEditor.GUI.Input;
using FlaxEditor.GUI.Tree; using FlaxEditor.GUI.Tree;
using FlaxEditor.SceneGraph; using FlaxEditor.SceneGraph;
using FlaxEngine; using FlaxEngine;
using FlaxEngine.Json;
using FlaxEngine.GUI; using FlaxEngine.GUI;
using FlaxEngine.Utilities; using FlaxEngine.Utilities;
using FlaxEditor.Windows; using FlaxEditor.Windows;
@@ -203,6 +203,22 @@ namespace FlaxEditor.Utilities
} }
} }
/// <summary>
/// Clones the value. handles non-value types (such as arrays) that need deep value cloning.
/// </summary>
/// <param name="value">The source value to clone.</param>
/// <returns>The duplicated value.</returns>
internal static object CloneValue(object value)
{
// For objects (eg. arrays) we need to clone them to prevent editing default/reference value within editor
if (value != null && (!value.GetType().IsValueType || !value.GetType().IsClass))
{
var json = JsonSerializer.Serialize(value);
value = JsonSerializer.Deserialize(json, value.GetType());
}
return value;
}
/// <summary> /// <summary>
/// The colors for the keyframes used by the curve editor. /// The colors for the keyframes used by the curve editor.
/// </summary> /// </summary>