diff --git a/Editor/ClusterView.cs b/Editor/ClusterView.cs index b6a73a2..214c7f8 100644 --- a/Editor/ClusterView.cs +++ b/Editor/ClusterView.cs @@ -213,12 +213,12 @@ namespace NanoBrain.Unity { #region Full Graph protected void DrawFullGraph() { - if (this.currentNucleus == null) { - Vector3 position = new(150, 210, 0); - DrawAllOutputs(position); - DrawOutputs(position); - return; - } + // if (this.currentNucleus == null) { + // Vector3 position = new(150, 210, 0); + // DrawAllOutputs(position); + // DrawOutputs(position); + // return; + // } Dag dag = GenerateGraph(this.selectedOutput); Dag.ComputeLayout(dag); @@ -271,8 +271,8 @@ namespace NanoBrain.Unity { public Dag GenerateGraph(Nucleus rootNucleus) { Dag dag = new(); - if (rootNucleus == null) - return dag; + // if (rootNucleus == null) + // return dag; int ix = 0; Dag.Node receiver = new() { @@ -281,18 +281,41 @@ namespace NanoBrain.Unity { }; dag.nodes.Add(receiver); ix++; - DescendGraph(receiver, ref ix, dag); + if (rootNucleus == null) + DescendGraphFromRoot(ref ix, dag); + else + DescendGraph(receiver, ref ix, dag); return dag; } + private void DescendGraphFromRoot(ref int ix, Dag dag) { + foreach (Nucleus nucleus in this.currentCluster.outputs) { + string nucleusName = nucleus.name; + Dag.Node node = dag.FindNode(nucleusName); + if (node == null) { + node = new() { + id = ix, + nucleus = nucleus + }; + dag.nodes.Add(node); + } + Dag.Edge edge = new() { + fromId = node.id, + toId = 0 + }; + dag.edges.Add(edge); + ix++; + DescendGraph(node, ref ix, dag); + } + } + private void DescendGraph(Dag.Node receiver, ref int ix, Dag dag) { - Neuron receiverNeuron = receiver.nucleus as Neuron; - if (receiverNeuron == null) + if (receiver.nucleus is not Neuron receiverNeuron) return; foreach (Synapse synapse in receiverNeuron.synapses) { Nucleus nucleus = synapse.neuron; - if (nucleus.parent != null && nucleus.parent != currentNucleus.parent) { + if (nucleus.parent != null && currentNucleus != null && nucleus.parent != currentNucleus.parent) { nucleus = nucleus.parent; } string nucleusName = nucleus.name; diff --git a/Editor/Cluster_Drawer.cs b/Editor/Cluster_Drawer.cs index d5110df..f31c3d6 100644 --- a/Editor/Cluster_Drawer.cs +++ b/Editor/Cluster_Drawer.cs @@ -18,13 +18,12 @@ namespace NanoBrain.Unity { currentClusterView.initialized = false; } - private const float padding = 0f;//4f; private const float graphHeight = 500f; // height reserved for the VisualElement private static ClusterView currentClusterView; private static UnityEngine.Object selectedTarget; public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { - float height = EditorGUIUtility.singleLineHeight + padding; + float height = EditorGUIUtility.singleLineHeight; if (Cluster_Drawer.currentClusterView == null) // When no cluster is viewed return height; @@ -32,6 +31,10 @@ namespace NanoBrain.Unity { SerializedProperty prefabProp = property.FindPropertyRelative(nameof(Cluster.prefab)); if (prefabProp.objectReferenceValue != null && Cluster_Drawer.currentClusterView.isOpen) { height = graphHeight; + } else { + // Unclear why this is necessary, + // but without this, expanding the graph foldout is not possible + height += EditorGUIUtility.singleLineHeight; } return height; } @@ -73,7 +76,7 @@ namespace NanoBrain.Unity { SerializedProperty prefabProp = property.FindPropertyRelative(nameof(Cluster.prefab)); // Draw the object field on the top line - Rect fieldRect = new(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight + padding); + Rect fieldRect = new(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight); EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(fieldRect, prefabProp, label); // If a new prefab has been selected diff --git a/Editor/Dag.cs b/Editor/Dag.cs index 7b12f52..b183e26 100644 --- a/Editor/Dag.cs +++ b/Editor/Dag.cs @@ -28,6 +28,9 @@ namespace NanoBrain.Unity { name = name[..colonPos]; } foreach (Node node in this.nodes) { + if (node.nucleus == null) + continue; + string nodeName = node.nucleus.name; if (justBaseName) { int colonPos = nodeName.IndexOf(":");