Skip to content

Commit

Permalink
🚧 Render InputSocket arrays in the BehaviourEditor for #21
Browse files Browse the repository at this point in the history
🚧 Deserialize arrays of InputSockets properly
🐛 Misc. fixes to the BehaviourManifest
  • Loading branch information
Fydar committed Mar 8, 2020
1 parent a3c57eb commit aa14fb0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 10 deletions.
61 changes: 60 additions & 1 deletion src/RPGCore.Behaviour/Core/SerializedNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public NodeTemplate UnpackNodeAndInputs(Type nodeType, LocalId id, HashSet<Local
var jsonSerializer = new JsonSerializer();

jsonSerializer.Converters.Add(new InputSocketConverter(validOutputs, connectionIds));
jsonSerializer.Converters.Add(new InputSocketArrayConverter(validOutputs, connectionIds));
jsonSerializer.Converters.Add(new LocalIdJsonConverter());

object nodeObject = Data.ToObject(nodeType, jsonSerializer);
Expand Down Expand Up @@ -58,7 +59,7 @@ internal sealed class InputSocketConverter : JsonConverter

public override bool CanConvert(Type objectType)
{
return (objectType == typeof(InputSocket));
return objectType == typeof(InputSocket);
}

public InputSocketConverter(HashSet<LocalPropertyId> validOutputs, List<LocalPropertyId> mappedInputs)
Expand Down Expand Up @@ -97,4 +98,62 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
return new InputSocket(connectionId);
}
}

internal sealed class InputSocketArrayConverter : JsonConverter
{
private readonly HashSet<LocalPropertyId> validOutputs;
private readonly List<LocalPropertyId> mappedInputs;

public override bool CanWrite => false;

public override bool CanConvert(Type objectType)
{
if (!objectType.IsArray)
{
return false;
}

return objectType.IsArray && objectType.GetElementType() == typeof(InputSocket);
}

public InputSocketArrayConverter(HashSet<LocalPropertyId> validOutputs, List<LocalPropertyId> mappedInputs)
{
this.validOutputs = validOutputs;
this.mappedInputs = mappedInputs;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var array = JArray.Load(reader);

var inputSockets = new InputSocket[array.Count];
for (int i = 0; i < array.Count; i++)
{
var arrayElement = array[i];

var inputSource = new LocalPropertyId(arrayElement.ToObject<string>());

if (!validOutputs.Contains(inputSource))
{
Console.WriteLine($"Ignoring desired input of \"{inputSource}\" as it is not valid.");
return new InputSocket[0];
}

int connectionId = mappedInputs.IndexOf(inputSource);
if (connectionId == -1)
{
connectionId = mappedInputs.Count;
mappedInputs.Add(inputSource);
}
inputSockets[i] = new InputSocket(connectionId);
}

return inputSockets;
}
}
}
4 changes: 2 additions & 2 deletions src/RPGCore.Behaviour/Manifest/BehaviourManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public sealed class BehaviourManifest
private static IEnumerable<Assembly> GetDependentAssemblies(AppDomain appDomain, Assembly analyzedAssembly)
{
return appDomain.GetAssemblies()
.Where(assembly => GetNamesOfAssembliesReferencedBy(assembly)
.Where(assembly => assembly == analyzedAssembly || GetNamesOfAssembliesReferencedBy(assembly)
.Contains(analyzedAssembly.FullName));
}

Expand Down Expand Up @@ -75,7 +75,7 @@ public static BehaviourManifest CreateFromAppDomain(AppDomain appDomain)

if (typeof(NodeTemplate).IsAssignableFrom(type))
{
nodeTypes.Add(type.Name, NodeInformation.Construct(type));
nodeTypes.Add(type.FullName, NodeInformation.Construct(type));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/RPGCore.Demo.Inventory/Nodes/AddNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public class AddInstance : Instance

public override InputMap[] Inputs(ConnectionMapper connections) => new[]
{
connections.Connect (ref Template.ValueA, ref ValueA),
connections.Connect (ref Template.ValueB, ref ValueB)
connections.Connect(ref Template.ValueA, ref ValueA),
connections.Connect(ref Template.ValueB, ref ValueB)
};

public override OutputMap[] Outputs(ConnectionMapper connections) => new[]
{
connections.Connect (ref Template.Output, ref Output)
connections.Connect(ref Template.Output, ref Output)
};

public override void Setup()
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified src/RPGCoreUnity/Assets/RPGCore/Plugins/RPGCore.Behaviour.dll
Binary file not shown.
Binary file modified src/RPGCoreUnity/Assets/RPGCore/Plugins/RPGCore.Behaviour.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private void DrawNodes()
{
return;
}

var graphEditorNodes = View.GraphField["Nodes"];

// Draw Nodes
Expand Down Expand Up @@ -223,13 +224,35 @@ public void DrawConnections()
}
}

// Foreach Input
foreach (var childField in nodeData)
IEnumerable<EditorField> InputSocketFields(EditorField nodeDataField)
{
if (childField.Field.Type != "InputSocket")
foreach (var childField in nodeDataField)
{
continue;
if (childField.Field.Type != "InputSocket")
{
continue;
}
if (childField.Field.Format == FieldFormat.List)
{
foreach (var listElement in childField)
{
yield return listElement;
}
}
else if (childField.Field.Format == FieldFormat.Object)
{
yield return childField;
}
else
{
Debug.LogError($"Unknown InputSocket format. InputSockets cannot be a \"{childField.Field.Format}\".");
}
}
}

// Foreach Input
foreach (var childField in InputSocketFields(nodeData))
{
var inputFieldFeature = childField.GetOrCreateFeature<FieldFeature>();

inputFieldFeature.GlobalRenderedPosition = new Rect(
Expand Down

0 comments on commit aa14fb0

Please sign in to comment.