From 1dc01cd0238142c3e4336a824f8b96f0e8c7b0d2 Mon Sep 17 00:00:00 2001 From: Nils Hausfeld Date: Sat, 30 Sep 2023 20:07:33 +0200 Subject: [PATCH] - Added compatibility checks to packing structures nodes - Added compatibility checks to more function nodes --- Source/Editor/Surface/Archetypes/Function.cs | 135 ++++++++++++++++++- Source/Editor/Surface/Archetypes/Packing.cs | 82 +++++++++++ 2 files changed, 212 insertions(+), 5 deletions(-) diff --git a/Source/Editor/Surface/Archetypes/Function.cs b/Source/Editor/Surface/Archetypes/Function.cs index 975267ad2..cf680c06e 100644 --- a/Source/Editor/Surface/Archetypes/Function.cs +++ b/Source/Editor/Surface/Archetypes/Function.cs @@ -1156,14 +1156,19 @@ namespace FlaxEditor.Surface.Archetypes { if (nodeArch.Tag is not ScriptMemberInfo memberInfo) return false; - if(memberInfo.ValueType.IsVoid && outputType.IsVoid) - return true; + if (!memberInfo.IsStatic) { if (VisjectSurface.FullCastCheck(memberInfo.DeclaringType, outputType, hint)) return true; } - foreach (var param in memberInfo.GetParameters()) + + var parameters = memberInfo.GetParameters(); + bool isPure = (parameters.Length == 0 && !memberInfo.ValueType.IsVoid); + if (outputType.IsVoid) + return !isPure; + + foreach (var param in parameters) { if(param.IsOut) continue; @@ -1177,10 +1182,14 @@ namespace FlaxEditor.Surface.Archetypes { if (nodeArch.Tag is not ScriptMemberInfo memberInfo) return false; - if(memberInfo.ValueType.IsVoid && inputType.IsVoid) - return true; if (VisjectSurface.FullCastCheck(memberInfo.ValueType, inputType, hint)) return true; + + var parameters = memberInfo.GetParameters(); + bool isPure = (parameters.Length == 0 && !memberInfo.ValueType.IsVoid); + if (inputType.IsVoid) + return !isPure; + foreach (var param in memberInfo.GetParameters()) { if(!param.IsOut) @@ -1816,6 +1825,16 @@ namespace FlaxEditor.Surface.Archetypes base.OnDestroy(); } + + internal static bool IsInputCompatible(NodeArchetype nodeArch, ScriptType outputType, ConnectionsHint hint) + { + return false; + } + + internal static bool IsOutputCompatible(NodeArchetype nodeArch, ScriptType inputType, ConnectionsHint hint) + { + return inputType.IsVoid; + } } private abstract class FieldNodeBase : SurfaceNode @@ -1952,6 +1971,64 @@ namespace FlaxEditor.Surface.Archetypes Title = "Get " + SurfaceUtils.GetMethodDisplayName((string)Values[1]); UpdateSignature(); } + + internal static bool IsInputCompatible(NodeArchetype nodeArch, ScriptType outputType, ConnectionsHint hint) + { + var scriptType = TypeUtils.GetType((string)nodeArch.DefaultValues[0]); + if (scriptType == ScriptType.Null) + return false; + + var members = scriptType.GetMembers((string)nodeArch.DefaultValues[1], MemberTypes.Field, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly); + foreach (var member in members) + { + if (!SurfaceUtils.IsValidVisualScriptField(member)) + continue; + + if (member) + { + if (!member.IsStatic && VisjectSurface.FullCastCheck(scriptType, outputType, hint)) + return true; + } + else + { + var isStatic = (bool)nodeArch.DefaultValues[3]; + if (!isStatic && VisjectSurface.FullCastCheck(scriptType, outputType, hint)) + return true; + } + break; + } + + return false; + } + + internal static bool IsOutputCompatible(NodeArchetype nodeArch, ScriptType inputType, ConnectionsHint hint) + { + var scriptType = TypeUtils.GetType((string)nodeArch.DefaultValues[0]); + if (scriptType == ScriptType.Null) + return false; + + var members = scriptType.GetMembers((string)nodeArch.DefaultValues[1], MemberTypes.Field, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly); + foreach (var member in members) + { + if (!SurfaceUtils.IsValidVisualScriptField(member)) + continue; + + if (member) + { + if (VisjectSurface.FullCastCheck(member.ValueType, inputType, hint)) + return true; + } + else + { + var typeName = (string)nodeArch.DefaultValues[2]; + if (VisjectSurface.FullCastCheck(TypeUtils.GetType(typeName), inputType, hint)) + return true; + } + break; + } + + return false; + } } private sealed class SetFieldNode : FieldNodeBase @@ -2005,6 +2082,48 @@ namespace FlaxEditor.Surface.Archetypes Title = "Set " + SurfaceUtils.GetMethodDisplayName((string)Values[1]); UpdateSignature(); } + + internal static bool IsInputCompatible(NodeArchetype nodeArch, ScriptType outputType, ConnectionsHint hint) + { + if(outputType.IsVoid) + return true; + + var scriptType = TypeUtils.GetType((string)nodeArch.DefaultValues[0]); + if (scriptType == ScriptType.Null) + return false; + + var members = scriptType.GetMembers((string)nodeArch.DefaultValues[1], MemberTypes.Field, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly); + foreach (var member in members) + { + if (!SurfaceUtils.IsValidVisualScriptField(member)) + continue; + + if (member) + { + if (VisjectSurface.FullCastCheck(member.ValueType, outputType, hint)) + return true; + if (!member.IsStatic && VisjectSurface.FullCastCheck(scriptType, outputType, hint)) + return true; + } + else + { + var typeName = (string)nodeArch.DefaultValues[2]; + if (VisjectSurface.FullCastCheck(TypeUtils.GetType(typeName), outputType, hint)) + return true; + var isStatic = (bool)nodeArch.DefaultValues[3]; + if (!isStatic && VisjectSurface.FullCastCheck(scriptType, outputType, hint)) + return true; + } + break; + } + + return false; + } + + internal static bool IsOutputCompatible(NodeArchetype nodeArch, ScriptType inputType, ConnectionsHint hint) + { + return inputType.IsVoid; + } } private abstract class EventBaseNode : SurfaceNode, IFunctionsDependantNode @@ -2395,6 +2514,8 @@ namespace FlaxEditor.Surface.Archetypes { TypeID = 6, Create = (id, context, arch, groupArch) => new VisualScriptFunctionNode(id, context, arch, groupArch), + IsInputCompatible = VisualScriptFunctionNode.IsInputCompatible, + IsOutputCompatible = VisualScriptFunctionNode.IsOutputCompatible, Title = "New Function", Description = "Adds a new function to the script", Flags = NodeFlags.VisualScriptGraph, @@ -2408,6 +2529,8 @@ namespace FlaxEditor.Surface.Archetypes { TypeID = 7, Create = (id, context, arch, groupArch) => new GetFieldNode(id, context, arch, groupArch), + IsInputCompatible = GetFieldNode.IsInputCompatible, + IsOutputCompatible = GetFieldNode.IsOutputCompatible, Title = string.Empty, Flags = NodeFlags.VisualScriptGraph | NodeFlags.NoSpawnViaGUI, Size = new Float2(240, 60), @@ -2423,6 +2546,8 @@ namespace FlaxEditor.Surface.Archetypes { TypeID = 8, Create = (id, context, arch, groupArch) => new SetFieldNode(id, context, arch, groupArch), + IsInputCompatible = SetFieldNode.IsInputCompatible, + IsOutputCompatible = SetFieldNode.IsOutputCompatible, Title = string.Empty, Flags = NodeFlags.VisualScriptGraph | NodeFlags.NoSpawnViaGUI, Size = new Float2(240, 60), diff --git a/Source/Editor/Surface/Archetypes/Packing.cs b/Source/Editor/Surface/Archetypes/Packing.cs index 5a5e9cd5b..ccabaaa6d 100644 --- a/Source/Editor/Surface/Archetypes/Packing.cs +++ b/Source/Editor/Surface/Archetypes/Packing.cs @@ -207,6 +207,26 @@ namespace FlaxEditor.Surface.Archetypes AddElement(box); } } + + protected static bool IsInputCompatible(NodeArchetype nodeArch, ScriptType outputType, ConnectionsHint hint) + { + // Event based nodes always have a pulse input, so it's always compatible with void + if (outputType.IsVoid) + return true; + + var eventName = (string)nodeArch.DefaultValues[1]; + var eventType = TypeUtils.GetType((string)nodeArch.DefaultValues[0]); + var member = eventType.GetMember(eventName, MemberTypes.Event, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); + if (member && SurfaceUtils.IsValidVisualScriptEvent(member)) + { + if (!member.IsStatic) + { + if (VisjectSurface.FullCastCheck(eventType, outputType, hint)) + return true; + } + } + return false; + } } private sealed class PackStructureNode : StructureNode @@ -216,6 +236,35 @@ namespace FlaxEditor.Surface.Archetypes : base(id, context, nodeArch, groupArch, false) { } + + internal static bool IsInputCompatible(NodeArchetype nodeArch, ScriptType outputType, ConnectionsHint hint) + { + var typeName = (string)nodeArch.DefaultValues[0]; + var type = TypeUtils.GetType(typeName); + if (type) + { + var fields = type.GetMembers(BindingFlags.Public | BindingFlags.Instance).Where(x => x.IsField).ToArray(); + var fieldsLength = fields.Length; + for (var i = 0; i < fieldsLength; i++) + { + if (VisjectSurface.FullCastCheck(fields[i].ValueType, outputType, hint)) + return true; + } + } + return false; + } + + internal static bool IsOutputCompatible(NodeArchetype nodeArch, ScriptType inputType, ConnectionsHint hint) + { + var typeName = (string)nodeArch.DefaultValues[0]; + var type = TypeUtils.GetType(typeName); + if (type) + { + if (VisjectSurface.FullCastCheck(type, inputType, hint)) + return true; + } + return false; + } } private sealed class UnpackStructureNode : StructureNode @@ -225,6 +274,35 @@ namespace FlaxEditor.Surface.Archetypes : base(id, context, nodeArch, groupArch, true) { } + + internal static bool IsInputCompatible(NodeArchetype nodeArch, ScriptType outputType, ConnectionsHint hint) + { + var typeName = (string)nodeArch.DefaultValues[0]; + var type = TypeUtils.GetType(typeName); + if (type) + { + if (VisjectSurface.FullCastCheck(type, outputType, hint)) + return true; + } + return false; + } + + internal static bool IsOutputCompatible(NodeArchetype nodeArch, ScriptType inputType, ConnectionsHint hint) + { + var typeName = (string)nodeArch.DefaultValues[0]; + var type = TypeUtils.GetType(typeName); + if (type) + { + var fields = type.GetMembers(BindingFlags.Public | BindingFlags.Instance).Where(x => x.IsField).ToArray(); + var fieldsLength = fields.Length; + for (var i = 0; i < fieldsLength; i++) + { + if (VisjectSurface.FullCastCheck(fields[i].ValueType, inputType, hint)) + return true; + } + } + return false; + } } /// @@ -351,6 +429,8 @@ namespace FlaxEditor.Surface.Archetypes TypeID = 26, Title = "Pack Structure", Create = (id, context, arch, groupArch) => new PackStructureNode(id, context, arch, groupArch), + IsInputCompatible = PackStructureNode.IsInputCompatible, + IsOutputCompatible = PackStructureNode.IsOutputCompatible, Description = "Makes the structure data to from the components.", Flags = NodeFlags.VisualScriptGraph | NodeFlags.AnimGraph | NodeFlags.NoSpawnViaGUI, Size = new Float2(180, 20), @@ -461,6 +541,8 @@ namespace FlaxEditor.Surface.Archetypes TypeID = 36, Title = "Unpack Structure", Create = (id, context, arch, groupArch) => new UnpackStructureNode(id, context, arch, groupArch), + IsInputCompatible = UnpackStructureNode.IsInputCompatible, + IsOutputCompatible = UnpackStructureNode.IsOutputCompatible, Description = "Breaks the structure data to allow extracting components from it.", Flags = NodeFlags.VisualScriptGraph | NodeFlags.AnimGraph | NodeFlags.NoSpawnViaGUI, Size = new Float2(180, 20),