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

View File

@ -124,10 +124,10 @@ public partial class NodeEditorWindow {
//Needs cleanup. Null checks are ugly
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++) {
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));
}
}
@ -151,7 +151,7 @@ public partial class NodeEditorWindow {
foreach (Node node in graph.nodes) {
//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";
GUILayout.BeginArea(new Rect(nodePos,new Vector2(240,4000)));
@ -176,7 +176,7 @@ public partial class NodeEditorWindow {
GUILayout.FlexibleSpace();
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();
}
EndZoomed(position, zoom);

View File

@ -32,9 +32,7 @@ public partial class NodeEditorWindow {
public void FileContextMenu() {
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 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) {
GUI.DragWindow();
}

View File

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

View File

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

View File

@ -4,7 +4,7 @@ using UnityEngine;
using System;
[Serializable]
public class NodePort : ISerializationCallbackReceiver{
public class NodePort {
public enum IO { Input, Output}
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 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] 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;
this.type = type;
@ -69,34 +67,6 @@ public class NodePort : ISerializationCallbackReceiver{
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;