Added full graph view mode
This commit is contained in:
parent
249e88850a
commit
830e3e7265
@ -12,6 +12,7 @@ namespace NanoBrain {
|
|||||||
public string title;
|
public string title;
|
||||||
public Vector2 position;
|
public Vector2 position;
|
||||||
public float radius = 20f; // circle radius
|
public float radius = 20f; // circle radius
|
||||||
|
public Nucleus nucleus;
|
||||||
}
|
}
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
@ -19,15 +20,15 @@ namespace NanoBrain {
|
|||||||
public int fromId;
|
public int fromId;
|
||||||
public int toId;
|
public int toId;
|
||||||
}
|
}
|
||||||
|
public class Dag {
|
||||||
|
public List<DagNode> nodes = new();
|
||||||
|
public List<DagEdge> edges = new();
|
||||||
|
}
|
||||||
|
|
||||||
public class BrainEditorWindow : EditorWindow {
|
public class BrainEditorWindow : EditorWindow {
|
||||||
readonly List<DagNode> nodes = new();
|
Dag dag = new();
|
||||||
readonly List<DagEdge> edges = new();
|
|
||||||
|
|
||||||
Vector2 pan = Vector2.zero;
|
Vector2 pan = Vector2.zero;
|
||||||
float zoom = 1.0f;
|
|
||||||
const float minZoom = 0.5f;
|
|
||||||
const float maxZoom = 2.0f;
|
|
||||||
|
|
||||||
private readonly System.Type acceptedType = typeof(ClusterPrefab);
|
private readonly System.Type acceptedType = typeof(ClusterPrefab);
|
||||||
|
|
||||||
@ -40,8 +41,9 @@ namespace NanoBrain {
|
|||||||
void OnEnable() {
|
void OnEnable() {
|
||||||
// Register callback so window updates when selection changes
|
// Register callback so window updates when selection changes
|
||||||
Selection.selectionChanged += OnSelectionChanged;
|
Selection.selectionChanged += OnSelectionChanged;
|
||||||
RefreshSelection();
|
dag = RefreshSelection();
|
||||||
ComputeLayout();
|
ComputeLayout(dag);
|
||||||
|
Repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDisable() {
|
private void OnDisable() {
|
||||||
@ -49,33 +51,41 @@ namespace NanoBrain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void OnSelectionChanged() {
|
private void OnSelectionChanged() {
|
||||||
RefreshSelection();
|
dag = RefreshSelection();
|
||||||
ComputeLayout();
|
ComputeLayout(dag);
|
||||||
Repaint();
|
Repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RefreshSelection() {
|
private Dag RefreshSelection() {
|
||||||
ClusterPrefab prefab = Selection.activeObject as ClusterPrefab;
|
ClusterPrefab prefab = Selection.activeObject as ClusterPrefab;
|
||||||
if (prefab != null && acceptedType.IsAssignableFrom(prefab.GetType())) {
|
if (prefab != null && acceptedType.IsAssignableFrom(prefab.GetType()))
|
||||||
GenerateGraph(prefab);
|
return GenerateGraph(prefab);
|
||||||
}
|
else
|
||||||
|
return new Dag();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateGraph(ClusterPrefab prefab) {
|
public Dag GenerateGraph(ClusterPrefab prefab) {
|
||||||
nodes.Clear();
|
Dag dag = new();
|
||||||
edges.Clear();
|
|
||||||
|
|
||||||
int ix = 0;
|
int ix = 0;
|
||||||
foreach (Nucleus nucleus in prefab.nuclei) {
|
foreach (Nucleus nucleus in prefab.nuclei) {
|
||||||
nodes.Add(new DagNode() { id = ix, title = nucleus.name });
|
DagNode node = new() {
|
||||||
|
id = ix,
|
||||||
|
title = nucleus.name
|
||||||
|
};
|
||||||
|
dag.nodes.Add(node);
|
||||||
if (nucleus is Neuron neuron) {
|
if (nucleus is Neuron neuron) {
|
||||||
foreach (Nucleus receiver in neuron.receivers) {
|
foreach (Nucleus receiver in neuron.receivers) {
|
||||||
int receiverIx = prefab.GetNucleusIndex(receiver);
|
DagEdge edge = new() {
|
||||||
edges.Add(new DagEdge() { fromId = ix, toId = receiverIx });
|
fromId = ix,
|
||||||
|
toId = prefab.GetNucleusIndex(receiver)
|
||||||
|
};
|
||||||
|
dag.edges.Add(edge);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ix++;
|
ix++;
|
||||||
}
|
}
|
||||||
|
return dag;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnGUI() {
|
void OnGUI() {
|
||||||
@ -88,28 +98,28 @@ namespace NanoBrain {
|
|||||||
Vector2 windowCenter = new(position.width / 2f, position.height / 2f);
|
Vector2 windowCenter = new(position.width / 2f, position.height / 2f);
|
||||||
|
|
||||||
// compute graph bounds center (in graph space)
|
// compute graph bounds center (in graph space)
|
||||||
Rect bounds = GetGraphBounds();
|
Rect bounds = GetGraphBounds(dag);
|
||||||
Vector2 graphCenter = bounds.center;
|
Vector2 graphCenter = bounds.center;
|
||||||
|
|
||||||
// compute autoPan that recenters the graph (does not modify node positions)
|
// compute autoPan that recenters the graph (does not modify node positions)
|
||||||
Vector2 autoPan = -graphCenter; // moves graph center to origin
|
Vector2 autoPan = -graphCenter; // moves graph center to origin
|
||||||
// total translation = windowCenter + autoPan + user pan
|
// total translation = windowCenter + autoPan + user pan
|
||||||
Matrix4x4 oldMatrix = GUI.matrix;
|
Matrix4x4 oldMatrix = GUI.matrix;
|
||||||
GUI.matrix = Matrix4x4.TRS(windowCenter + autoPan + pan, Quaternion.identity, Vector3.one * zoom) *
|
GUI.matrix = Matrix4x4.TRS(windowCenter + autoPan + pan, Quaternion.identity, Vector3.one) *
|
||||||
Matrix4x4.TRS(-windowCenter, Quaternion.identity, Vector3.one);
|
Matrix4x4.TRS(-windowCenter, Quaternion.identity, Vector3.one);
|
||||||
|
|
||||||
|
|
||||||
// Draw edges first
|
// Draw edges first
|
||||||
foreach (DagEdge e in edges) {
|
foreach (DagEdge e in dag.edges) {
|
||||||
DagNode from = GetNodeById(e.fromId);
|
DagNode from = GetNodeById(dag, e.fromId);
|
||||||
DagNode to = GetNodeById(e.toId);
|
DagNode to = GetNodeById(dag, e.toId);
|
||||||
if (from == null || to == null)
|
if (from == null || to == null)
|
||||||
continue;
|
continue;
|
||||||
DrawEdgeCircleNodes(from, to);
|
DrawEdgeCircleNodes(from, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw nodes (circles)
|
// Draw nodes (circles)
|
||||||
foreach (DagNode n in nodes)
|
foreach (DagNode n in dag.nodes)
|
||||||
DrawNucleus(n);
|
DrawNucleus(n);
|
||||||
|
|
||||||
GUI.matrix = oldMatrix;
|
GUI.matrix = oldMatrix;
|
||||||
@ -118,16 +128,6 @@ namespace NanoBrain {
|
|||||||
void HandleInput() {
|
void HandleInput() {
|
||||||
Event e = Event.current;
|
Event e = Event.current;
|
||||||
|
|
||||||
// Zoom with scroll
|
|
||||||
if (e.type == EventType.ScrollWheel) {
|
|
||||||
float oldZoom = zoom;
|
|
||||||
float delta = -e.delta.y * 0.01f;
|
|
||||||
zoom = Mathf.Clamp(zoom + delta, minZoom, maxZoom);
|
|
||||||
Vector2 mouse = e.mousePosition;
|
|
||||||
pan += (mouse - new Vector2(position.width / 2, position.height / 2)) * (1 - zoom / oldZoom);
|
|
||||||
e.Use();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pan with middle or right+ctrl drag
|
// Pan with middle or right+ctrl drag
|
||||||
if (e.type == EventType.MouseDrag && (e.button == 2 || (e.button == 1 && e.control))) {
|
if (e.type == EventType.MouseDrag && (e.button == 2 || (e.button == 1 && e.control))) {
|
||||||
pan += e.delta;
|
pan += e.delta;
|
||||||
@ -135,12 +135,12 @@ namespace NanoBrain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DagNode GetNodeById(int id) => nodes.FirstOrDefault(x => x.id == id);
|
public static DagNode GetNodeById(Dag dag, int id) => dag.nodes.FirstOrDefault(x => x.id == id);
|
||||||
|
|
||||||
void DrawNucleus(DagNode n) {
|
public static void DrawNucleus(DagNode n) {
|
||||||
Vector3 position = n.position;
|
Vector3 position = n.position;
|
||||||
|
|
||||||
Handles.color = Color.white * 0.9f;
|
Handles.color = Color.black * 0.9f;
|
||||||
Handles.DrawSolidDisc(n.position, Vector3.forward, n.radius);
|
Handles.DrawSolidDisc(n.position, Vector3.forward, n.radius);
|
||||||
|
|
||||||
Handles.color = Color.white;
|
Handles.color = Color.white;
|
||||||
@ -153,7 +153,7 @@ namespace NanoBrain {
|
|||||||
Handles.Label(labelPos, n.title, style);
|
Handles.Label(labelPos, n.title, style);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawEdgeCircleNodes(DagNode from, DagNode to) {
|
public static void DrawEdgeCircleNodes(DagNode from, DagNode to) {
|
||||||
Vector2 a = from.position;
|
Vector2 a = from.position;
|
||||||
Vector2 b = to.position;
|
Vector2 b = to.position;
|
||||||
if (a == b) return;
|
if (a == b) return;
|
||||||
@ -162,19 +162,10 @@ namespace NanoBrain {
|
|||||||
Handles.DrawLine(from.position, to.position);
|
Handles.DrawLine(from.position, to.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Right-to-left layered layout (sources on the right, sinks on the left)
|
public static void ComputeLayout(Dag dag) {
|
||||||
void ComputeLayout() {
|
Dictionary<int, List<int>> adjacency = dag.nodes.ToDictionary(n => n.id, n => new List<int>());
|
||||||
// build adjacency and indegree
|
Dictionary<int, int> outdegree = dag.nodes.ToDictionary(node => node.id, n => 0);
|
||||||
Dictionary<int, List<int>> adjacency = nodes.ToDictionary(n => n.id, n => new List<int>());
|
foreach (DagEdge edge in dag.edges) {
|
||||||
Dictionary<int, int> indegree = nodes.ToDictionary(n => n.id, n => 0);
|
|
||||||
foreach (DagEdge edge in edges) {
|
|
||||||
if (!adjacency.ContainsKey(edge.fromId) || !adjacency.ContainsKey(edge.toId))
|
|
||||||
continue;
|
|
||||||
adjacency[edge.fromId].Add(edge.toId);
|
|
||||||
indegree[edge.toId]++;
|
|
||||||
}
|
|
||||||
Dictionary<int, int> outdegree = nodes.ToDictionary(node => node.id, n => 0);
|
|
||||||
foreach (DagEdge edge in edges) {
|
|
||||||
if (!adjacency.ContainsKey(edge.fromId) || !adjacency.ContainsKey(edge.toId))
|
if (!adjacency.ContainsKey(edge.fromId) || !adjacency.ContainsKey(edge.toId))
|
||||||
continue;
|
continue;
|
||||||
adjacency[edge.fromId].Add(edge.toId);
|
adjacency[edge.fromId].Add(edge.toId);
|
||||||
@ -183,9 +174,10 @@ namespace NanoBrain {
|
|||||||
|
|
||||||
// Kahn's algorithm to compute topological layers (horizontal layers)
|
// Kahn's algorithm to compute topological layers (horizontal layers)
|
||||||
// build parent list (reverse adjacency) and parentIndegree = number of children each parent has
|
// build parent list (reverse adjacency) and parentIndegree = number of children each parent has
|
||||||
Dictionary<int, List<int>> parents = nodes.ToDictionary(n => n.id, _ => new List<int>());
|
Dictionary<int, List<int>> parents = dag.nodes.ToDictionary(n => n.id, _ => new List<int>());
|
||||||
Dictionary<int, int> childCount = nodes.ToDictionary(n => n.id, _ => 0);
|
Dictionary<int, int> childCount = dag.nodes.ToDictionary(n => n.id, _ => 0);
|
||||||
foreach (DagEdge edge in edges) {
|
|
||||||
|
foreach (DagEdge edge in dag.edges) {
|
||||||
if (!adjacency.ContainsKey(edge.fromId) || !adjacency.ContainsKey(edge.toId)) continue;
|
if (!adjacency.ContainsKey(edge.fromId) || !adjacency.ContainsKey(edge.toId)) continue;
|
||||||
adjacency[edge.fromId].Add(edge.toId);
|
adjacency[edge.fromId].Add(edge.toId);
|
||||||
parents[edge.toId].Add(edge.fromId); // parent of 'to' is 'from'
|
parents[edge.toId].Add(edge.fromId); // parent of 'to' is 'from'
|
||||||
@ -210,10 +202,9 @@ namespace NanoBrain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Any unreachable nodes -> assign next layers
|
// Any unreachable nodes -> assign next layers
|
||||||
int maxLayer = layer.Count > 0 ? layer.Values.Max() : 0;
|
int maxLayer = layer.Count > 0 ? layer.Values.Max() : 0;
|
||||||
foreach (DagNode node in nodes) {
|
foreach (DagNode node in dag.nodes) {
|
||||||
if (!layer.ContainsKey(node.id)) {
|
if (!layer.ContainsKey(node.id)) {
|
||||||
maxLayer++;
|
maxLayer++;
|
||||||
layer[node.id] = maxLayer;
|
layer[node.id] = maxLayer;
|
||||||
@ -221,7 +212,12 @@ namespace NanoBrain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Group nodes by layer (left to right)
|
// Group nodes by layer (left to right)
|
||||||
List<List<int>> layers = layer.GroupBy(kv => kv.Value).OrderBy(g => g.Key).Select(g => g.Select(x => x.Key).ToList()).ToList();
|
List<List<int>> layers =
|
||||||
|
layer.
|
||||||
|
GroupBy(kv => kv.Value).
|
||||||
|
OrderBy(g => g.Key).
|
||||||
|
Select(g => g.Select(x => x.Key).ToList()).
|
||||||
|
ToList();
|
||||||
|
|
||||||
// Same code without using Linq
|
// Same code without using Linq
|
||||||
// Build layers dictionary: layerIndex -> List<int> nodeIds
|
// Build layers dictionary: layerIndex -> List<int> nodeIds
|
||||||
@ -247,25 +243,27 @@ namespace NanoBrain {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
float hSpacing = 100f;
|
float hSpacing = 100f;
|
||||||
float vSpacing = 100f;
|
float totalHeight = 400f;
|
||||||
|
|
||||||
// Place nodes: x increases with layer index, y spaced within layer
|
// Place nodes: x increases with layer index, y spaced within layer
|
||||||
for (int layerIx = 0; layerIx < layers.Count; layerIx++) {
|
for (int layerIx = 0; layerIx < layers.Count; layerIx++) {
|
||||||
List<int> nodeList = layers[layerIx];
|
List<int> nodeList = layers[layerIx];
|
||||||
float totalHeight = (nodeList.Count - 1) * vSpacing;
|
float spacing = totalHeight / nodeList.Count;
|
||||||
|
float margin = 10 + spacing / 2;
|
||||||
for (int i = 0; i < nodeList.Count; i++) {
|
for (int i = 0; i < nodeList.Count; i++) {
|
||||||
int index = nodeList[i];
|
int index = nodeList[i];
|
||||||
DagNode node = GetNodeById(index);
|
DagNode node = GetNodeById(dag, index);
|
||||||
if (node == null)
|
if (node == null)
|
||||||
continue;
|
continue;
|
||||||
float x = hSpacing + layerIx * hSpacing;
|
float x = hSpacing + layerIx * hSpacing;
|
||||||
float y = 400 - totalHeight / 2f + i * vSpacing;
|
//float y = 400 - totalHeight / 2f + i * vSpacing;
|
||||||
|
float y = margin + i * spacing;
|
||||||
// Debug.Log($"({li}, {i}) -> {x}, {y}");
|
// Debug.Log($"({li}, {i}) -> {x}, {y}");
|
||||||
node.position = new Vector2(x, y);
|
node.position = new Vector2(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Repaint();
|
//Repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Rect RectUnion(Rect a, Rect b) {
|
static Rect RectUnion(Rect a, Rect b) {
|
||||||
@ -276,12 +274,13 @@ namespace NanoBrain {
|
|||||||
return Rect.MinMaxRect(xMin, yMin, xMax, yMax);
|
return Rect.MinMaxRect(xMin, yMin, xMax, yMax);
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect GetGraphBounds() {
|
Rect GetGraphBounds(Dag dag) {
|
||||||
if (nodes == null || nodes.Count == 0) return new Rect(Vector2.zero, Vector2.one);
|
if (dag.nodes == null || dag.nodes.Count == 0)
|
||||||
|
return new Rect(Vector2.zero, Vector2.one);
|
||||||
Rect bounds = new(
|
Rect bounds = new(
|
||||||
nodes[0].position - Vector2.one * nodes[0].radius,
|
dag.nodes[0].position - Vector2.one * dag.nodes[0].radius,
|
||||||
2f * nodes[0].radius * Vector2.one);
|
2f * dag.nodes[0].radius * Vector2.one);
|
||||||
foreach (var n in nodes)
|
foreach (var n in dag.nodes)
|
||||||
bounds = RectUnion(bounds,
|
bounds = RectUnion(bounds,
|
||||||
new Rect(n.position - Vector2.one * n.radius, 2f * n.radius * Vector2.one));
|
new Rect(n.position - Vector2.one * n.radius, 2f * n.radius * Vector2.one));
|
||||||
return bounds;
|
return bounds;
|
||||||
|
|||||||
@ -62,8 +62,18 @@ namespace NanoBrain {
|
|||||||
|
|
||||||
public class GraphEditor : GraphView {
|
public class GraphEditor : GraphView {
|
||||||
|
|
||||||
public GraphEditor(ClusterPrefab prefab) : base(prefab) {
|
public enum Mode {
|
||||||
|
Focus,
|
||||||
|
Full
|
||||||
|
}
|
||||||
|
public Mode mode = Mode.Focus;
|
||||||
|
|
||||||
|
public GraphEditor(ClusterPrefab prefab) : base(prefab) {
|
||||||
|
// create an EnumField for Mode
|
||||||
|
EnumField enumField = new(mode);
|
||||||
|
enumField.style.width = 80;
|
||||||
|
enumField.RegisterValueChangedCallback(OnModeChange);
|
||||||
|
outputContainer.Insert(0, enumField);
|
||||||
|
|
||||||
Button addButton = new(() => OnAddClusterOutput()) {
|
Button addButton = new(() => OnAddClusterOutput()) {
|
||||||
text = "Add"
|
text = "Add"
|
||||||
@ -71,7 +81,22 @@ namespace NanoBrain {
|
|||||||
outputContainer.Add(addButton);
|
outputContainer.Add(addButton);
|
||||||
|
|
||||||
Add(outputContainer);
|
Add(outputContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnModeChange(ChangeEvent<System.Enum> evt) {
|
||||||
|
mode = (Mode)evt.newValue;
|
||||||
|
|
||||||
|
Debug.Log("Mode changed to: " + mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
Nucleus selectedOutput;
|
||||||
|
protected override void OnOutputChanged(string outputName) {
|
||||||
|
if (this.currentNucleus.parent != null)
|
||||||
|
// Get nucleus in the parent instance
|
||||||
|
this.selectedOutput = this.currentNucleus.parent.GetNucleus(outputName);
|
||||||
|
else
|
||||||
|
// Get nucleus in the prefab
|
||||||
|
this.selectedOutput = this.prefab.GetNucleus(outputName);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnAddClusterOutput() {
|
void OnAddClusterOutput() {
|
||||||
@ -83,7 +108,6 @@ namespace NanoBrain {
|
|||||||
this.currentNucleus = newOutput;
|
this.currentNucleus = newOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void SetGraph(GameObject gameObject, Nucleus nucleus, VisualElement inspectorContainer) {
|
public void SetGraph(GameObject gameObject, Nucleus nucleus, VisualElement inspectorContainer) {
|
||||||
this.gameObject = gameObject;
|
this.gameObject = gameObject;
|
||||||
//this.cluster = brain;
|
//this.cluster = brain;
|
||||||
@ -123,6 +147,58 @@ namespace NanoBrain {
|
|||||||
inspectorContainer.Add(inspectorIMGUIContainer);
|
inspectorContainer.Add(inspectorIMGUIContainer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void DrawGraph() {
|
||||||
|
if (mode == Mode.Focus)
|
||||||
|
DrawFocusGraph();
|
||||||
|
else
|
||||||
|
DrawFullGraph();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void DrawFullGraph() {
|
||||||
|
Dag dag = GenerateGraph(this.prefab);
|
||||||
|
BrainEditorWindow.ComputeLayout(dag);
|
||||||
|
// Draw edges
|
||||||
|
foreach (DagEdge e in dag.edges) {
|
||||||
|
DagNode from = dag.nodes.FirstOrDefault(x => x.id == e.fromId);
|
||||||
|
DagNode to = dag.nodes.FirstOrDefault(x => x.id == e.toId);
|
||||||
|
if (from == null || to == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Vector2 fromPosition = from.position;
|
||||||
|
Vector2 toPosition = to.position;
|
||||||
|
DrawEdge(fromPosition, toPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw nodes
|
||||||
|
foreach (DagNode n in dag.nodes)
|
||||||
|
DrawNucleus(n.nucleus, n.position, 1, n.radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dag GenerateGraph(ClusterPrefab prefab) {
|
||||||
|
Dag dag = new();
|
||||||
|
|
||||||
|
int ix = 0;
|
||||||
|
foreach (Nucleus nucleus in prefab.nuclei) {
|
||||||
|
DagNode node = new() {
|
||||||
|
id = ix,
|
||||||
|
title = nucleus.name,
|
||||||
|
nucleus = nucleus
|
||||||
|
};
|
||||||
|
dag.nodes.Add(node);
|
||||||
|
if (nucleus is Neuron neuron) {
|
||||||
|
foreach (Nucleus receiver in neuron.receivers) {
|
||||||
|
DagEdge edge = new() {
|
||||||
|
fromId = ix,
|
||||||
|
toId = prefab.GetNucleusIndex(receiver)
|
||||||
|
};
|
||||||
|
dag.edges.Add(edge);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ix++;
|
||||||
|
}
|
||||||
|
return dag;
|
||||||
|
}
|
||||||
|
|
||||||
#region Inspector
|
#region Inspector
|
||||||
|
|
||||||
private VisualElement inspectorIMGUIContainer;
|
private VisualElement inspectorIMGUIContainer;
|
||||||
|
|||||||
@ -38,9 +38,9 @@ namespace NanoBrain {
|
|||||||
|
|
||||||
outputContainer = new() {
|
outputContainer = new() {
|
||||||
style = {
|
style = {
|
||||||
flexDirection = FlexDirection.Row,
|
flexDirection = FlexDirection.Row,
|
||||||
alignItems = Align.Center,
|
alignItems = Align.Center,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
List<string> names = this.prefab.outputs.Select(output => output.name).ToList();
|
List<string> names = this.prefab.outputs.Select(output => output.name).ToList();
|
||||||
@ -59,7 +59,7 @@ namespace NanoBrain {
|
|||||||
RegisterCallback<DetachFromPanelEvent>(evt => Unsubscribe());
|
RegisterCallback<DetachFromPanelEvent>(evt => Unsubscribe());
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnOutputChanged(string outputName) {
|
protected virtual void OnOutputChanged(string outputName) {
|
||||||
if (this.currentNucleus.parent != null)
|
if (this.currentNucleus.parent != null)
|
||||||
// Get nucleus in the parent instance
|
// Get nucleus in the parent instance
|
||||||
this.currentNucleus = this.currentNucleus.parent.GetNucleus(outputName);
|
this.currentNucleus = this.currentNucleus.parent.GetNucleus(outputName);
|
||||||
@ -177,7 +177,11 @@ namespace NanoBrain {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawGraph() {
|
protected virtual void DrawGraph() {
|
||||||
|
DrawFocusGraph();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void DrawFocusGraph() {
|
||||||
float size = 20;
|
float size = 20;
|
||||||
Vector3 position = new(150, 210, 0);
|
Vector3 position = new(150, 210, 0);
|
||||||
|
|
||||||
@ -235,9 +239,6 @@ namespace NanoBrain {
|
|||||||
// Handles.Label(labelPos, receptorName, style);
|
// Handles.Label(labelPos, receptorName, style);
|
||||||
// }
|
// }
|
||||||
// else {
|
// else {
|
||||||
Handles.color = Color.white;
|
|
||||||
// The selected nucleus highlight ring
|
|
||||||
Handles.DrawSolidDisc(position, Vector3.forward, size + 2);
|
|
||||||
float maxValue = 1;
|
float maxValue = 1;
|
||||||
if (this.currentNucleus is Neuron neuron)
|
if (this.currentNucleus is Neuron neuron)
|
||||||
maxValue = neuron.outputMagnitude;
|
maxValue = neuron.outputMagnitude;
|
||||||
@ -249,9 +250,6 @@ namespace NanoBrain {
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Handles.color = Color.white;
|
|
||||||
// The selected nucleus highlight ring
|
|
||||||
Handles.DrawSolidDisc(position, Vector3.forward, size + 2);
|
|
||||||
float maxValue = 1;
|
float maxValue = 1;
|
||||||
if (this.currentNucleus is Neuron neuron)
|
if (this.currentNucleus is Neuron neuron)
|
||||||
maxValue = neuron.outputMagnitude;
|
maxValue = neuron.outputMagnitude;
|
||||||
@ -301,8 +299,7 @@ namespace NanoBrain {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
Vector3 pos = new(50, margin + row * spacing, 0.0f);
|
Vector3 pos = new(50, margin + row * spacing, 0.0f);
|
||||||
Handles.color = Color.white;
|
DrawEdge(parentPos, pos);
|
||||||
Handles.DrawLine(parentPos, pos);
|
|
||||||
|
|
||||||
DrawNucleus(receiverNucleus, pos, maxValue, size);
|
DrawNucleus(receiverNucleus, pos, maxValue, size);
|
||||||
row++;
|
row++;
|
||||||
@ -321,7 +318,7 @@ namespace NanoBrain {
|
|||||||
if (synapse.neuron == null)
|
if (synapse.neuron == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (synapse.neuron.parent is Cluster cluster &&
|
if (synapse.neuron.parent is Cluster cluster &&
|
||||||
cluster.siblingClusters != null &&
|
cluster.siblingClusters != null &&
|
||||||
synapse.neuron.parent != nucleus.parent) {
|
synapse.neuron.parent != nucleus.parent) {
|
||||||
if (drawnArrays.Contains(cluster.siblingClusters))
|
if (drawnArrays.Contains(cluster.siblingClusters))
|
||||||
@ -372,16 +369,16 @@ namespace NanoBrain {
|
|||||||
if (synapse.neuron.parent != null && synapse.neuron.parent != this.currentNucleus.parent) {
|
if (synapse.neuron.parent != null && synapse.neuron.parent != this.currentNucleus.parent) {
|
||||||
// the synapse nucleus is part of a subcluster
|
// the synapse nucleus is part of a subcluster
|
||||||
//DrawNucleus(synapse.neuron.parent, pos, maxValue, size, color);
|
//DrawNucleus(synapse.neuron.parent, pos, maxValue, size, color);
|
||||||
DrawNucleus(synapse.neuron, pos, maxValue, size, color);
|
DrawNucleus(synapse.neuron, pos, size, color);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
DrawNucleus(synapse.neuron, pos, maxValue, size, color);
|
DrawNucleus(synapse.neuron, pos, size, color);
|
||||||
}
|
}
|
||||||
row++;
|
row++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawNucleus(Nucleus nucleus, Vector3 position, float maxValue, float size) {
|
protected void DrawNucleus(Nucleus nucleus, Vector3 position, float maxValue, float size) {
|
||||||
Color color;
|
Color color;
|
||||||
if (Application.isPlaying) {
|
if (Application.isPlaying) {
|
||||||
float brightness = 0;
|
float brightness = 0;
|
||||||
@ -391,10 +388,16 @@ namespace NanoBrain {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
color = Color.black;
|
color = Color.black;
|
||||||
DrawNucleus(nucleus, position, maxValue, size, color);
|
DrawNucleus(nucleus, position, size, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawNucleus(Nucleus nucleus, Vector3 position, float maxValue, float size, Color color) {
|
protected void DrawNucleus(Nucleus nucleus, Vector3 position, float size, Color color) {
|
||||||
|
if (nucleus == this.currentNucleus) {
|
||||||
|
// The selected nucleus highlight ring
|
||||||
|
Handles.color = Color.white;
|
||||||
|
Handles.DrawSolidDisc(position, Vector3.forward, size + 2);
|
||||||
|
}
|
||||||
|
|
||||||
if (nucleus is MemoryCell) {
|
if (nucleus is MemoryCell) {
|
||||||
Handles.color = Color.white;
|
Handles.color = Color.white;
|
||||||
Handles.DrawWireDisc(position + Vector3.right * 10, Vector3.forward, size);
|
Handles.DrawWireDisc(position + Vector3.right * 10, Vector3.forward, size);
|
||||||
@ -469,18 +472,18 @@ namespace NanoBrain {
|
|||||||
// This neuron is part of another cluster
|
// This neuron is part of another cluster
|
||||||
parentCluster1.name ??= "";
|
parentCluster1.name ??= "";
|
||||||
string baseName = "";
|
string baseName = "";
|
||||||
int colonPos = parentCluster1.name.IndexOf(":");
|
int colonPos = parentCluster1.name.IndexOf(":");
|
||||||
if (colonPos > 0 && colonPos < parentCluster1.name.Length - 2)
|
if (colonPos > 0 && colonPos < parentCluster1.name.Length - 2)
|
||||||
baseName = parentCluster1.name[..colonPos] + ".";
|
baseName = parentCluster1.name[..colonPos] + ".";
|
||||||
else
|
else
|
||||||
baseName = parentCluster1.name + ".";
|
baseName = parentCluster1.name + ".";
|
||||||
// if (colonPos > 0 && colonPos < parentCluster1.name.Length - 2) {
|
// if (colonPos > 0 && colonPos < parentCluster1.name.Length - 2) {
|
||||||
// // if it is an array, we should not show the :0 of the first element
|
// // if it is an array, we should not show the :0 of the first element
|
||||||
// //baseName = baseName[..colonPos];
|
// //baseName = baseName[..colonPos];
|
||||||
// Handles.Label(labelPos, baseName + nucleus.name, style);
|
// Handles.Label(labelPos, baseName + nucleus.name, style);
|
||||||
// }
|
// }
|
||||||
// else
|
// else
|
||||||
Handles.Label(labelPos, baseName + nucleus.name, style);
|
Handles.Label(labelPos, baseName + nucleus.name, style);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
nucleus.name ??= "";
|
nucleus.name ??= "";
|
||||||
@ -562,6 +565,25 @@ namespace NanoBrain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void DrawEdge(Vector2 from, Vector2 to) {
|
||||||
|
Handles.color = Color.white;
|
||||||
|
Handles.DrawLine(from, to);
|
||||||
|
}
|
||||||
|
|
||||||
|
// protected void DrawNode(Vector2 position, float size) {
|
||||||
|
// Handles.color = Color.black * 0.9f;
|
||||||
|
// Handles.DrawSolidDisc(position, Vector3.forward, size);
|
||||||
|
|
||||||
|
// Handles.color = Color.white;
|
||||||
|
// GUIStyle style = new(EditorStyles.label) {
|
||||||
|
// alignment = TextAnchor.UpperCenter,
|
||||||
|
// normal = { textColor = Color.white },
|
||||||
|
// fontStyle = FontStyle.Bold,
|
||||||
|
// };
|
||||||
|
// Vector3 labelPos = position - Vector3.down * (size + 10f); // below disc along up axis
|
||||||
|
// Handles.Label(labelPos, n.title, style);
|
||||||
|
// }
|
||||||
|
|
||||||
void OnSceneGUI(SceneView sceneView) {
|
void OnSceneGUI(SceneView sceneView) {
|
||||||
if (this.gameObject != null) {
|
if (this.gameObject != null) {
|
||||||
// if (this.currentNucleus is IReceptor receptor) {
|
// if (this.currentNucleus is IReceptor receptor) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user