1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 01:06:01 +08:00

Serialized connections

Nodes now also have a reference to their NodeGraph
This commit is contained in:
Thor Brigsted 2017-09-21 10:54:49 +02:00
parent 1606c583f9
commit b7543df012
7 changed files with 74 additions and 22 deletions

View File

@ -3,7 +3,7 @@
[System.Serializable]
public class AddNode : Node {
public string thisIsAddNode = "Add";
public int someValue;
protected override void Init() {
inputs = new NodePort[2];

View File

@ -3,7 +3,7 @@
[System.Serializable]
public class BaseNode : Node {
public string thisIsBaseNode = "ASDF";
public bool concat;
protected override void Init() {
inputs = new NodePort[2];

View File

@ -92,7 +92,7 @@ public partial class NodeEditorWindow {
if (IsDraggingPort) {
//If connection is valid, save it
if (draggedOutputTarget != null) {
draggedOutput.Connect(draggedOutputTarget);
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
}
//Release dragged connection
draggedOutput = null;
@ -129,6 +129,7 @@ public partial class NodeEditorWindow {
/// <summary> Draw a connection as we are dragging it </summary>
public void DrawDraggedConnection() {
if (IsDraggingPort) {
if (!_portConnectionPoints.ContainsKey(draggedOutput)) return;
Vector2 from = _portConnectionPoints[draggedOutput];
Vector2 to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget] : WindowToGridPosition(Event.current.mousePosition);
DrawConnection(from, to);

View File

@ -31,9 +31,11 @@ public partial class NodeEditorWindow : EditorWindow {
public void Save() {
saved = graph.Serialize();
Debug.Log(saved);
}
public void Load() {
Debug.Log(saved);
_graph = NodeGraph.Deserialize(saved);
}
@ -41,12 +43,12 @@ public partial class NodeEditorWindow : EditorWindow {
GUI.DragWindow();
}
public byte[] ProtoSerialize<T>(T value) {
/*public byte[] ProtoSerialize<T>(T value) {
using (var ms = new MemoryStream()) {
ProtoBuf.Serializer.Serialize(ms, value);
return ms.ToArray();
}
}
}*/
public Vector2 WindowToGridPosition(Vector2 windowPosition) {
return (windowPosition - (position.size * 0.5f) - (panOffset / zoom)) * zoom;

View File

@ -7,12 +7,13 @@ using System;
[Serializable]
public abstract class Node {
[NonSerialized] public NodeGraph graph;
public string NodeType { get { return nodeType; } }
[SerializeField] private string nodeType;
public Rect position = new Rect(0,0,200,200);
protected NodePort[] inputs = new NodePort[0];
protected NodePort[] outputs = new NodePort[0];
[SerializeField] public Rect position = new Rect(0,0,200,200);
[SerializeField] protected NodePort[] inputs = new NodePort[0];
[SerializeField] protected NodePort[] outputs = new NodePort[0];
public int InputCount { get { return inputs.Length; } }
public int OutputCount { get { return outputs.Length; } }
@ -54,6 +55,13 @@ public abstract class Node {
return new NodePort(name, type, this, NodePort.IO.Output);
}
public void FinalizeDeserialization() {
for (int i = 0; i < outputs.Length; i++) {
outputs[i].FinalizeDeserialization();
}
}
public void ClearConnections() {
for (int i = 0; i < inputs.Length; i++) {
inputs[i].ClearConnections();

View File

@ -12,30 +12,29 @@ public class NodeGraph {
/// <summary> Serialized nodes. </summary>
[SerializeField] public string[] s_nodes;
public List<string> strings = new List<string>() { "ASDF", "3523" };
public T AddNode<T>() where T : Node {
T node = default(T);
nodes.Add(node);
return node;
return AddNode(node) as T;
}
public Node AddNode(Type type) {
Node node = (Node)Activator.CreateInstance(type);
if (node == null) {
Debug.LogError("Node could node be instanced");
return null;
}
nodes.Add(node);
return node;
return AddNode(node);
}
public Node AddNode(string type) {
Debug.Log(type);
Node node = (Node)Activator.CreateInstance(null,type).Unwrap();
return AddNode(node);
}
public Node AddNode(Node node) {
if (node == null) {
Debug.LogError("Node could node be instanced");
return null;
}
nodes.Add(node);
node.graph = this;
return node;
}
@ -76,8 +75,13 @@ public class NodeGraph {
for (int i = 0; i < nodeGraph.s_nodes.Length; i++) {
NodeTyper tempNode = new NodeTyper();
JsonUtility.FromJsonOverwrite(nodeGraph.s_nodes[i],tempNode);
Node node = nodeGraph.AddNode(tempNode.nodeType);
JsonUtility.FromJsonOverwrite(nodeGraph.s_nodes[i], node);
//Node node = nodeGraph.AddNode(tempNode.nodeType);
Type type = Type.GetType(tempNode.nodeType);
Node node = JsonUtility.FromJson(nodeGraph.s_nodes[i], type) as Node;
nodeGraph.AddNode(node);
}
for (int i = 0; i < nodeGraph.nodes.Count; i++) {
nodeGraph.nodes[i].FinalizeDeserialization();
}
return nodeGraph;
}

View File

@ -3,7 +3,8 @@ using System.Collections.Generic;
using UnityEngine;
using System;
public class NodePort {
[Serializable]
public class NodePort :ISerializationCallbackReceiver{
public enum IO { Input, Output}
public int ConnectionCount { get { return connections.Count; } }
@ -23,13 +24,15 @@ public class NodePort {
public string name { get { return _name; } set { _name = value; } }
public bool enabled { get { return _enabled; } set { _enabled = value; } }
private Type _type;
private List<NodePort> connections = new List<NodePort>();
[NonSerialized] private List<NodePort> connections = new List<NodePort>();
[SerializeField] private Type _type;
[SerializeField] private string _name;
[SerializeField] private bool _enabled = true;
[SerializeField] private IO _direction;
[SerializeField] private PortID[] connectionIDs;
public NodePort(string name, Type type, Node node, IO direction) {
_name = name;
_type = type;
@ -64,4 +67,38 @@ public class NodePort {
}
connections.Clear();
}
public void OnBeforeSerialize() {
if (direction == IO.Output) {
connectionIDs = new PortID[connections.Count];
for (int i = 0; i < connections.Count; i++) {
connectionIDs[i] = new PortID();
connectionIDs[i].nodeID = node.graph.nodes.IndexOf(connections[i].node);
connectionIDs[i].portID = connections[i].node.GetInputId(connections[i]);
}
}
}
public void OnAfterDeserialize() {
}
public void FinalizeDeserialization() {
//Reconnect
if (direction == IO.Output) {
connections = new List<NodePort>();
for (int i = 0; i < connectionIDs.Length; i++) {
Node node = this.node.graph.nodes[connectionIDs[i].nodeID];
NodePort port = node.GetInput(connectionIDs[i].portID);
Connect(port);
}
}
}
[Serializable]
private class PortID {
public int nodeID;
public int portID;
}
}