Fix issues on Visject nodes copy pasting due to incorrect enum values serialization

This commit is contained in:
Wojtek Figat
2020-12-15 12:34:18 +01:00
parent 696df47cec
commit 3c3e0b93f0
2 changed files with 32 additions and 8 deletions

View File

@@ -19,13 +19,11 @@ namespace FlaxEditor.Surface.Archetypes
{ {
private EnumComboBox _picker; private EnumComboBox _picker;
/// <inheritdoc />
public EnumNode(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch) public EnumNode(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch)
: base(id, context, nodeArch, groupArch) : base(id, context, nodeArch, groupArch)
{ {
} }
/// <inheritdoc />
public override void OnValuesChanged() public override void OnValuesChanged()
{ {
base.OnValuesChanged(); base.OnValuesChanged();
@@ -36,7 +34,6 @@ namespace FlaxEditor.Surface.Archetypes
box.CurrentType = new ScriptType(Values[0].GetType()); box.CurrentType = new ScriptType(Values[0].GetType());
} }
/// <inheritdoc />
public override void OnLoaded() public override void OnLoaded()
{ {
base.OnLoaded(); base.OnLoaded();
@@ -66,7 +63,6 @@ namespace FlaxEditor.Surface.Archetypes
box.CurrentType = new ScriptType(type); box.CurrentType = new ScriptType(type);
} }
/// <inheritdoc />
public override void OnDestroy() public override void OnDestroy()
{ {
_picker = null; _picker = null;

View File

@@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using FlaxEditor.Scripting;
using FlaxEditor.Surface.Elements; using FlaxEditor.Surface.Elements;
using FlaxEditor.Surface.Undo; using FlaxEditor.Surface.Undo;
using FlaxEngine; using FlaxEngine;
@@ -15,6 +16,12 @@ namespace FlaxEditor.Surface
{ {
public partial class VisjectSurface public partial class VisjectSurface
{ {
private struct DataModelValue
{
public string EnumTypeName;
public object Value;
}
private class DataModelBox private class DataModelBox
{ {
public int ID; public int ID;
@@ -29,7 +36,7 @@ namespace FlaxEditor.Surface
public uint ID; public uint ID;
public float X; public float X;
public float Y; public float Y;
public object[] Values; public DataModelValue[] Values;
public DataModelBox[] Boxes; public DataModelBox[] Boxes;
} }
@@ -76,8 +83,21 @@ namespace FlaxEditor.Surface
ID = node.ID, ID = node.ID,
X = node.Location.X, X = node.Location.X,
Y = node.Location.Y, Y = node.Location.Y,
Values = node.Values,
}; };
if (node.Values != null)
{
dataModelNode.Values = new DataModelValue[node.Values.Length];
for (int j = 0; j < node.Values.Length; j++)
{
var value = new DataModelValue
{
Value = node.Values[j],
};
if (value.Value != null && value.Value.GetType().IsEnum)
value.EnumTypeName = value.Value.GetType().FullName;
dataModelNode.Values[j] = value;
}
}
if (node.Elements != null && node.Elements.Count > 0) if (node.Elements != null && node.Elements.Count > 0)
{ {
@@ -252,10 +272,18 @@ namespace FlaxEditor.Surface
// Copy and fix values (Json deserializes may output them in a different format) // Copy and fix values (Json deserializes may output them in a different format)
for (int l = 0; l < node.Values.Length; l++) for (int l = 0; l < node.Values.Length; l++)
{ {
var src = nodeData.Values[l]; var src = nodeData.Values[l].Value;
var dst = node.Values[l]; var dst = node.Values[l];
if (src is JToken token) if (nodeData.Values[l].EnumTypeName != null)
{
var enumType = TypeUtils.GetManagedType(nodeData.Values[l].EnumTypeName);
if (enumType != null)
{
src = Enum.ToObject(enumType, src);
}
}
else if (src is JToken token)
{ {
if (dst is Vector2) if (dst is Vector2)
{ {