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

Removed more JSON serialization

Also changed Rect "position" to Rect "rect"
This commit is contained in:
Thor Brigsted 2017-09-27 22:33:01 +02:00
parent ae9943fae1
commit 2b0cf61435
7 changed files with 25 additions and 60 deletions

View File

@ -53,7 +53,7 @@ public partial class NodeEditorWindow {
Repaint(); Repaint();
} }
else if (IsDraggingNode) { else if (IsDraggingNode) {
draggedNode.position.position = WindowToGridPosition(e.mousePosition) + dragOffset; draggedNode.rect.position = WindowToGridPosition(e.mousePosition) + dragOffset;
Repaint(); Repaint();
} }
} }
@ -84,7 +84,7 @@ public partial class NodeEditorWindow {
} }
else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) { else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
draggedNode = hoveredNode; draggedNode = hoveredNode;
dragOffset = hoveredNode.position.position - WindowToGridPosition(e.mousePosition); dragOffset = hoveredNode.rect.position - WindowToGridPosition(e.mousePosition);
} }
break; break;
case EventType.MouseUp: case EventType.MouseUp:
@ -121,7 +121,7 @@ public partial class NodeEditorWindow {
public void CreateNode(Type type, Vector2 position) { public void CreateNode(Type type, Vector2 position) {
Node node = graph.AddNode(type); Node node = graph.AddNode(type);
node.position.position = position; node.rect.position = position;
Repaint(); Repaint();
} }
@ -129,8 +129,8 @@ public partial class NodeEditorWindow {
public void DrawDraggedConnection() { public void DrawDraggedConnection() {
if (IsDraggingPort) { if (IsDraggingPort) {
if (!_portConnectionPoints.ContainsKey(draggedOutput)) return; if (!_portConnectionPoints.ContainsKey(draggedOutput)) return;
Vector2 from = draggedOutput.node.position.position + _portConnectionPoints[draggedOutput].center; Vector2 from = draggedOutput.node.rect.position + _portConnectionPoints[draggedOutput].center;
Vector2 to = draggedOutputTarget != null ? draggedOutputTarget.node.position.position + portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition); Vector2 to = draggedOutputTarget != null ? draggedOutputTarget.node.rect.position + portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition);
Color col = NodeEditorUtilities.GetTypeColor(draggedOutput.type); Color col = NodeEditorUtilities.GetTypeColor(draggedOutput.type);
col.a = 0.6f; col.a = 0.6f;
DrawConnection(from, to, col); DrawConnection(from, to, col);
@ -143,8 +143,8 @@ public partial class NodeEditorWindow {
Node newHoverNode = null; Node newHoverNode = null;
foreach (Node node in graph.nodes) { foreach (Node node in graph.nodes) {
//Get node position //Get node position
Vector2 nodePos = GridToWindowPosition(node.position.position); Vector2 nodePos = GridToWindowPosition(node.rect.position);
Rect windowRect = new Rect(nodePos, new Vector2(node.position.size.x / zoom, node.position.size.y / zoom)); Rect windowRect = new Rect(nodePos, new Vector2(node.rect.size.x / zoom, node.rect.size.y / zoom));
if (windowRect.Contains(mousePos)) { if (windowRect.Contains(mousePos)) {
newHoverNode = node; newHoverNode = node;
} }
@ -162,7 +162,7 @@ public partial class NodeEditorWindow {
//Check if port rect is available //Check if port rect is available
if (!portConnectionPoints.ContainsKey(port)) continue; if (!portConnectionPoints.ContainsKey(port)) continue;
Rect r = portConnectionPoints[port]; Rect r = portConnectionPoints[port];
r.position = GridToWindowPosition(r.position + hoveredNode.position.position); r.position = GridToWindowPosition(r.position + hoveredNode.rect.position);
r.size /= zoom; r.size /= zoom;
if (r.Contains(mousePos)) newHoverPort = port; if (r.Contains(mousePos)) newHoverPort = port;
} }
@ -172,7 +172,7 @@ public partial class NodeEditorWindow {
//Check if port rect is available //Check if port rect is available
if (!portConnectionPoints.ContainsKey(port)) continue; if (!portConnectionPoints.ContainsKey(port)) continue;
Rect r = portConnectionPoints[port]; Rect r = portConnectionPoints[port];
r.position = GridToWindowPosition(r.position + hoveredNode.position.position); r.position = GridToWindowPosition(r.position + hoveredNode.rect.position);
r.size /= zoom; r.size /= zoom;
if (r.Contains(mousePos)) newHoverPort = port; if (r.Contains(mousePos)) newHoverPort = port;
} }
@ -186,8 +186,8 @@ public partial class NodeEditorWindow {
bool IsHoveringTitle(Node node) { bool IsHoveringTitle(Node node) {
Vector2 mousePos = Event.current.mousePosition; Vector2 mousePos = Event.current.mousePosition;
//Get node position //Get node position
Vector2 nodePos = GridToWindowPosition(node.position.position); Vector2 nodePos = GridToWindowPosition(node.rect.position);
Rect windowRect = new Rect(nodePos, new Vector2(node.position.size.x / zoom, 30 / zoom)); Rect windowRect = new Rect(nodePos, new Vector2(node.rect.size.x / zoom, 30 / zoom));
return windowRect.Contains(mousePos); return windowRect.Contains(mousePos);
} }
} }

View File

@ -124,10 +124,10 @@ public partial class NodeEditorWindow {
//Needs cleanup. Null checks are ugly //Needs cleanup. Null checks are ugly
if (!portConnectionPoints.ContainsKey(output)) continue; if (!portConnectionPoints.ContainsKey(output)) continue;
Vector2 from = _portConnectionPoints[output].center + node.position.position; Vector2 from = _portConnectionPoints[output].center + node.rect.position;
for (int k = 0; k < output.ConnectionCount; k++) { for (int k = 0; k < output.ConnectionCount; k++) {
NodePort input = output.GetConnection(k); NodePort input = output.GetConnection(k);
Vector2 to = input.node.position.position + _portConnectionPoints[input].center; Vector2 to = input.node.rect.position + _portConnectionPoints[input].center;
DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type)); DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type));
} }
} }
@ -151,7 +151,7 @@ public partial class NodeEditorWindow {
foreach (Node node in graph.nodes) { foreach (Node node in graph.nodes) {
//Get node position //Get node position
Vector2 nodePos = GridToWindowPositionNoClipped(node.position.position); Vector2 nodePos = GridToWindowPositionNoClipped(node.rect.position);
GUIStyle style = (node == selectedNode) ? (GUIStyle)"flow node 0 on" : (GUIStyle)"flow node 0"; GUIStyle style = (node == selectedNode) ? (GUIStyle)"flow node 0 on" : (GUIStyle)"flow node 0";
GUILayout.BeginArea(new Rect(nodePos,new Vector2(240,4000))); GUILayout.BeginArea(new Rect(nodePos,new Vector2(240,4000)));
@ -176,7 +176,7 @@ public partial class NodeEditorWindow {
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
if (e.type == EventType.Repaint) node.position.size = GUILayoutUtility.GetLastRect().size; if (e.type == EventType.Repaint) node.rect.size = GUILayoutUtility.GetLastRect().size;
GUILayout.EndArea(); GUILayout.EndArea();
} }
EndZoomed(position, zoom); EndZoomed(position, zoom);

View File

@ -32,9 +32,7 @@ public partial class NodeEditorWindow {
public void FileContextMenu() { public void FileContextMenu() {
GenericMenu contextMenu = new GenericMenu(); GenericMenu contextMenu = new GenericMenu();
contextMenu.AddItem(new GUIContent("Create New"), false, New);
contextMenu.AddSeparator("");
contextMenu.AddItem(new GUIContent("Save"), false, Save); contextMenu.AddItem(new GUIContent("Save"), false, Save);
contextMenu.AddItem(new GUIContent("Save As"), false, SaveAs); contextMenu.AddItem(new GUIContent("Save As"), false, SaveAs);

View File

@ -48,10 +48,6 @@ public partial class NodeEditorWindow : EditorWindow {
} }
} }
public void New() {
_graph = new NodeGraph();
}
private void DraggableWindow(int windowID) { private void DraggableWindow(int windowID) {
GUI.DragWindow(); GUI.DragWindow();
} }

View File

@ -8,21 +8,23 @@ using System;
public abstract class Node { public abstract class Node {
/// <summary> Name of the node </summary> /// <summary> Name of the node </summary>
public string name = ""; [SerializeField] public string name = "";
[NonSerialized] public NodeGraph graph; [SerializeField] public NodeGraph graph;
[SerializeField] public Rect position = new Rect(0,0,200,200);
[SerializeField] private NodePort[] inputs = new NodePort[0]; [SerializeField] private NodePort[] inputs = new NodePort[0];
[SerializeField] private NodePort[] outputs = new NodePort[0]; [SerializeField] private NodePort[] outputs = new NodePort[0];
[SerializeField] public Rect rect = new Rect(0,0,200,200);
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; } }
/// <summary> Constructor </summary>
protected Node() { protected Node() {
CachePorts(); CachePorts(); //Cache the ports at creation time so we don't have to use reflection at runtime
Init(); Init();
} }
/// <summary> Initialize node. Called on creation. </summary>
protected virtual void Init() { protected virtual void Init() {
} }

View File

@ -2,9 +2,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System; using System;
/// <summary> Base class for all node graphs </summary> /// <summary> Base class for all node graphs </summary>
[Serializable, CreateAssetMenu(fileName = "NewNodeGraph", menuName = "Node Graph")] public abstract class NodeGraph : ScriptableObject {
public class NodeGraph : ScriptableObject {
/// <summary> All nodes in the graph. <para/> /// <summary> All nodes in the graph. <para/>
/// See: <see cref="AddNode{T}"/> </summary> /// See: <see cref="AddNode{T}"/> </summary>
[NonSerialized] public List<Node> nodes = new List<Node>(); [NonSerialized] public List<Node> nodes = new List<Node>();
@ -49,7 +49,6 @@ public class NodeGraph : ScriptableObject {
nodes.Clear(); nodes.Clear();
} }
private class NodeTyper { private class NodeTyper {
public string nodeType = "Node"; public string nodeType = "Node";
} }

View File

@ -4,7 +4,7 @@ using UnityEngine;
using System; using System;
[Serializable] [Serializable]
public class NodePort : ISerializationCallbackReceiver{ public class NodePort {
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,15 +23,13 @@ public class NodePort : ISerializationCallbackReceiver{
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; } }
[NonSerialized] private List<NodePort> connections = new List<NodePort>(); [SerializeField] private List<NodePort> connections = new List<NodePort>();
[SerializeField] public Type type; [SerializeField] public 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;
this.type = type; this.type = type;
@ -69,34 +67,6 @@ public class NodePort : ISerializationCallbackReceiver{
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] [Serializable]
private class PortID { private class PortID {
public int nodeID; public int nodeID;