mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
Serialized connections
Nodes now also have a reference to their NodeGraph
This commit is contained in:
parent
1606c583f9
commit
b7543df012
@ -3,7 +3,7 @@
|
|||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class AddNode : Node {
|
public class AddNode : Node {
|
||||||
|
|
||||||
public string thisIsAddNode = "Add";
|
public int someValue;
|
||||||
|
|
||||||
protected override void Init() {
|
protected override void Init() {
|
||||||
inputs = new NodePort[2];
|
inputs = new NodePort[2];
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class BaseNode : Node {
|
public class BaseNode : Node {
|
||||||
|
|
||||||
public string thisIsBaseNode = "ASDF";
|
public bool concat;
|
||||||
|
|
||||||
protected override void Init() {
|
protected override void Init() {
|
||||||
inputs = new NodePort[2];
|
inputs = new NodePort[2];
|
||||||
|
|||||||
@ -92,7 +92,7 @@ public partial class NodeEditorWindow {
|
|||||||
if (IsDraggingPort) {
|
if (IsDraggingPort) {
|
||||||
//If connection is valid, save it
|
//If connection is valid, save it
|
||||||
if (draggedOutputTarget != null) {
|
if (draggedOutputTarget != null) {
|
||||||
draggedOutput.Connect(draggedOutputTarget);
|
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
|
||||||
}
|
}
|
||||||
//Release dragged connection
|
//Release dragged connection
|
||||||
draggedOutput = null;
|
draggedOutput = null;
|
||||||
@ -129,6 +129,7 @@ public partial class NodeEditorWindow {
|
|||||||
/// <summary> Draw a connection as we are dragging it </summary>
|
/// <summary> Draw a connection as we are dragging it </summary>
|
||||||
public void DrawDraggedConnection() {
|
public void DrawDraggedConnection() {
|
||||||
if (IsDraggingPort) {
|
if (IsDraggingPort) {
|
||||||
|
if (!_portConnectionPoints.ContainsKey(draggedOutput)) return;
|
||||||
Vector2 from = _portConnectionPoints[draggedOutput];
|
Vector2 from = _portConnectionPoints[draggedOutput];
|
||||||
Vector2 to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget] : WindowToGridPosition(Event.current.mousePosition);
|
Vector2 to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget] : WindowToGridPosition(Event.current.mousePosition);
|
||||||
DrawConnection(from, to);
|
DrawConnection(from, to);
|
||||||
|
|||||||
@ -31,9 +31,11 @@ public partial class NodeEditorWindow : EditorWindow {
|
|||||||
|
|
||||||
public void Save() {
|
public void Save() {
|
||||||
saved = graph.Serialize();
|
saved = graph.Serialize();
|
||||||
|
Debug.Log(saved);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Load() {
|
public void Load() {
|
||||||
|
Debug.Log(saved);
|
||||||
_graph = NodeGraph.Deserialize(saved);
|
_graph = NodeGraph.Deserialize(saved);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,12 +43,12 @@ public partial class NodeEditorWindow : EditorWindow {
|
|||||||
GUI.DragWindow();
|
GUI.DragWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] ProtoSerialize<T>(T value) {
|
/*public byte[] ProtoSerialize<T>(T value) {
|
||||||
using (var ms = new MemoryStream()) {
|
using (var ms = new MemoryStream()) {
|
||||||
ProtoBuf.Serializer.Serialize(ms, value);
|
ProtoBuf.Serializer.Serialize(ms, value);
|
||||||
return ms.ToArray();
|
return ms.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public Vector2 WindowToGridPosition(Vector2 windowPosition) {
|
public Vector2 WindowToGridPosition(Vector2 windowPosition) {
|
||||||
return (windowPosition - (position.size * 0.5f) - (panOffset / zoom)) * zoom;
|
return (windowPosition - (position.size * 0.5f) - (panOffset / zoom)) * zoom;
|
||||||
|
|||||||
@ -7,12 +7,13 @@ using System;
|
|||||||
[Serializable]
|
[Serializable]
|
||||||
public abstract class Node {
|
public abstract class Node {
|
||||||
|
|
||||||
|
[NonSerialized] public NodeGraph graph;
|
||||||
public string NodeType { get { return nodeType; } }
|
public string NodeType { get { return nodeType; } }
|
||||||
[SerializeField] private string nodeType;
|
[SerializeField] private string nodeType;
|
||||||
|
|
||||||
public Rect position = new Rect(0,0,200,200);
|
[SerializeField] public Rect position = new Rect(0,0,200,200);
|
||||||
protected NodePort[] inputs = new NodePort[0];
|
[SerializeField] protected NodePort[] inputs = new NodePort[0];
|
||||||
protected NodePort[] outputs = new NodePort[0];
|
[SerializeField] protected NodePort[] outputs = new NodePort[0];
|
||||||
|
|
||||||
public int InputCount { get { return inputs.Length; } }
|
public int InputCount { get { return inputs.Length; } }
|
||||||
public int OutputCount { get { return outputs.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);
|
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() {
|
public void ClearConnections() {
|
||||||
for (int i = 0; i < inputs.Length; i++) {
|
for (int i = 0; i < inputs.Length; i++) {
|
||||||
inputs[i].ClearConnections();
|
inputs[i].ClearConnections();
|
||||||
|
|||||||
@ -12,30 +12,29 @@ public class NodeGraph {
|
|||||||
/// <summary> Serialized nodes. </summary>
|
/// <summary> Serialized nodes. </summary>
|
||||||
[SerializeField] public string[] s_nodes;
|
[SerializeField] public string[] s_nodes;
|
||||||
|
|
||||||
public List<string> strings = new List<string>() { "ASDF", "3523" };
|
|
||||||
public T AddNode<T>() where T : Node {
|
public T AddNode<T>() where T : Node {
|
||||||
T node = default(T);
|
T node = default(T);
|
||||||
nodes.Add(node);
|
return AddNode(node) as T;
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node AddNode(Type type) {
|
public Node AddNode(Type type) {
|
||||||
Node node = (Node)Activator.CreateInstance(type);
|
Node node = (Node)Activator.CreateInstance(type);
|
||||||
if (node == null) {
|
return AddNode(node);
|
||||||
Debug.LogError("Node could node be instanced");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
nodes.Add(node);
|
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node AddNode(string type) {
|
public Node AddNode(string type) {
|
||||||
|
Debug.Log(type);
|
||||||
Node node = (Node)Activator.CreateInstance(null,type).Unwrap();
|
Node node = (Node)Activator.CreateInstance(null,type).Unwrap();
|
||||||
|
return AddNode(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node AddNode(Node node) {
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
Debug.LogError("Node could node be instanced");
|
Debug.LogError("Node could node be instanced");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
nodes.Add(node);
|
nodes.Add(node);
|
||||||
|
node.graph = this;
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,8 +75,13 @@ public class NodeGraph {
|
|||||||
for (int i = 0; i < nodeGraph.s_nodes.Length; i++) {
|
for (int i = 0; i < nodeGraph.s_nodes.Length; i++) {
|
||||||
NodeTyper tempNode = new NodeTyper();
|
NodeTyper tempNode = new NodeTyper();
|
||||||
JsonUtility.FromJsonOverwrite(nodeGraph.s_nodes[i],tempNode);
|
JsonUtility.FromJsonOverwrite(nodeGraph.s_nodes[i],tempNode);
|
||||||
Node node = nodeGraph.AddNode(tempNode.nodeType);
|
//Node node = nodeGraph.AddNode(tempNode.nodeType);
|
||||||
JsonUtility.FromJsonOverwrite(nodeGraph.s_nodes[i], node);
|
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;
|
return nodeGraph;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,8 @@ using System.Collections.Generic;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
public class NodePort {
|
[Serializable]
|
||||||
|
public class NodePort :ISerializationCallbackReceiver{
|
||||||
public enum IO { Input, Output}
|
public enum IO { Input, Output}
|
||||||
|
|
||||||
public int ConnectionCount { get { return connections.Count; } }
|
public int ConnectionCount { get { return connections.Count; } }
|
||||||
@ -23,13 +24,15 @@ public class NodePort {
|
|||||||
public string name { get { return _name; } set { _name = value; } }
|
public string name { get { return _name; } set { _name = value; } }
|
||||||
public bool enabled { get { return _enabled; } set { _enabled = value; } }
|
public bool enabled { get { return _enabled; } set { _enabled = value; } }
|
||||||
|
|
||||||
private Type _type;
|
[NonSerialized] private List<NodePort> connections = new List<NodePort>();
|
||||||
private List<NodePort> connections = new List<NodePort>();
|
|
||||||
|
|
||||||
|
[SerializeField] private Type _type;
|
||||||
[SerializeField] private string _name;
|
[SerializeField] private string _name;
|
||||||
[SerializeField] private bool _enabled = true;
|
[SerializeField] private bool _enabled = true;
|
||||||
[SerializeField] private IO _direction;
|
[SerializeField] private IO _direction;
|
||||||
|
|
||||||
|
[SerializeField] private PortID[] connectionIDs;
|
||||||
|
|
||||||
public NodePort(string name, Type type, Node node, IO direction) {
|
public NodePort(string name, Type type, Node node, IO direction) {
|
||||||
_name = name;
|
_name = name;
|
||||||
_type = type;
|
_type = type;
|
||||||
@ -64,4 +67,38 @@ public class NodePort {
|
|||||||
}
|
}
|
||||||
connections.Clear();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user