mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 01:06:01 +08:00
Connections, Ports
This commit is contained in:
parent
10a56513df
commit
36ecfde3fe
@ -1,16 +1,15 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BaseNode : Node {
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
protected override void Init() {
|
||||
inputs = new NodePort[2];
|
||||
inputs[0] = CreateNodeInput("IntInput", typeof(int));
|
||||
inputs[1] = CreateNodeInput("StringInput", typeof(string));
|
||||
outputs = new NodePort[1];
|
||||
outputs[0] = CreateNodeOutput("StringOutput", typeof(string));
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,6 @@ public static class NodeEditorAction {
|
||||
|
||||
public static void Controls(NodeEditorWindow window) {
|
||||
Event e = Event.current;
|
||||
|
||||
switch (e.type) {
|
||||
|
||||
case EventType.ScrollWheel:
|
||||
@ -22,10 +21,28 @@ public static class NodeEditorAction {
|
||||
case EventType.MouseDrag:
|
||||
if (e.button == 0) {
|
||||
if (window.activeNode != null) {
|
||||
if (window.hoveredPort != null || window.tempConnection != null) {
|
||||
if (window.tempConnection == null) {
|
||||
if (window.hoveredPort.direction == NodePort.IO.Output) {
|
||||
dragging = true;
|
||||
int inputNodeId = window.graph.GetNodeId(window.activeNode);
|
||||
int outputPortId = window.activeNode.GetOutputPortId(window.hoveredPort);
|
||||
window.tempConnection = new NodeConnection(inputNodeId, outputPortId, -1,-1);
|
||||
}
|
||||
else {
|
||||
Debug.Log("input");
|
||||
/*int outputNodeId = window.graph.GetNodeId(window.activeNode);
|
||||
int outputPortId = window.activeNode.GetInputPortId(window.hoveredPort);
|
||||
window.tempConnection = new NodeConnection(-1,-1,outputNodeId,outputPortId);*/
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
window.activeNode.position.position = window.WindowToGridPosition(e.mousePosition) + dragOffset;
|
||||
window.Repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (e.button == 1) {
|
||||
window.panOffset += e.delta * window.zoom;
|
||||
dragging = true;
|
||||
@ -36,6 +53,7 @@ public static class NodeEditorAction {
|
||||
break;
|
||||
case EventType.MouseDown:
|
||||
dragging = false;
|
||||
window.Repaint();
|
||||
window.SelectNode(window.hoveredNode);
|
||||
if (window.hoveredNode != null) {
|
||||
dragOffset = window.hoveredNode.position.position - window.WindowToGridPosition(e.mousePosition);
|
||||
@ -43,11 +61,15 @@ public static class NodeEditorAction {
|
||||
window.Repaint();
|
||||
break;
|
||||
case EventType.MouseUp:
|
||||
window.tempConnection = null;
|
||||
if (dragging) return;
|
||||
|
||||
if (e.button == 1) {
|
||||
NodeEditorGUI.RightClickContextMenu(window);
|
||||
}
|
||||
break;
|
||||
case EventType.repaint:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,10 @@ namespace UNEC {
|
||||
/// <summary> Contains GUI methods </summary>
|
||||
public static class NodeEditorGUI {
|
||||
|
||||
public static void DrawConnection(Vector2 from, Vector2 to, Color col) {
|
||||
Handles.DrawBezier(from, to, from, to, col, new Texture2D(2, 2), 2);
|
||||
}
|
||||
|
||||
public static void BeginZoomed(Rect rect, float zoom) {
|
||||
GUI.EndClip();
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System;
|
||||
|
||||
namespace UNEC {
|
||||
public static class NodeEditorResources {
|
||||
@ -15,6 +16,53 @@ namespace UNEC {
|
||||
private static Color arteryColor = new Color(0.34f, 0.34f, 0.34f);
|
||||
private static Color crossColor = new Color(0.45f, 0.45f, 0.45f);
|
||||
|
||||
public static Styles styles = new Styles();
|
||||
|
||||
public class Styles {
|
||||
GUIStyle inputInt, inputString, inputFloat, inputObject, inputTexture, inputColor;
|
||||
GUIStyle outputInt, outputString, outputFloat, outputObject, outputTexture, outputColor;
|
||||
|
||||
public Styles() {
|
||||
inputObject = new GUIStyle((GUIStyle)"flow shader in 0");
|
||||
inputString = new GUIStyle((GUIStyle)"flow shader in 1");
|
||||
inputInt = new GUIStyle((GUIStyle)"flow shader in 2");
|
||||
inputFloat = new GUIStyle((GUIStyle)"flow shader in 3");
|
||||
inputColor = new GUIStyle((GUIStyle)"flow shader in 4");
|
||||
inputTexture = new GUIStyle((GUIStyle)"flow shader in 5");
|
||||
outputObject = new GUIStyle((GUIStyle)"flow shader out 0");
|
||||
outputString = new GUIStyle((GUIStyle)"flow shader out 1");
|
||||
outputInt = new GUIStyle((GUIStyle)"flow shader out 2");
|
||||
outputFloat = new GUIStyle((GUIStyle)"flow shader out 3");
|
||||
outputColor = new GUIStyle((GUIStyle)"flow shader out 4");
|
||||
outputTexture = new GUIStyle((GUIStyle)"flow shader out 5");
|
||||
|
||||
foreach (GUIStyle style in new GUIStyle[] { inputInt, inputString, inputFloat, inputObject, inputTexture, inputColor, outputInt, outputString, outputFloat, outputObject, outputTexture, outputColor }) {
|
||||
style.normal.textColor = Color.black;
|
||||
style.fixedHeight = 18;
|
||||
style.alignment = TextAnchor.MiddleLeft;
|
||||
style.onHover.textColor = Color.red;
|
||||
}
|
||||
}
|
||||
|
||||
public GUIStyle GetInputStyle(Type type) {
|
||||
if (type == typeof(int)) return inputInt;
|
||||
else if (type == typeof(string)) return inputString;
|
||||
else if (type == typeof(Texture2D)) return inputTexture;
|
||||
else if (type == typeof(float)) return inputFloat;
|
||||
else if (type == typeof(Color)) return inputColor;
|
||||
else return inputObject;
|
||||
}
|
||||
|
||||
public GUIStyle GetOutputStyle(Type type) {
|
||||
if (type == typeof(int)) return outputInt;
|
||||
else if (type == typeof(string)) return outputString;
|
||||
else if (type == typeof(Texture2D)) return outputTexture;
|
||||
else if (type == typeof(float)) return outputFloat;
|
||||
else if (type == typeof(Color)) return outputColor;
|
||||
else return outputObject;
|
||||
}
|
||||
}
|
||||
|
||||
public static Texture2D GenerateGridTexture() {
|
||||
Texture2D tex = new Texture2D(64,64);
|
||||
Color[] cols = new Color[64 * 64];
|
||||
|
||||
@ -6,10 +6,14 @@ using UNEC;
|
||||
|
||||
public class NodeEditorWindow : EditorWindow {
|
||||
|
||||
private Dictionary<NodePort, Vector2> portConnectionPoints = new Dictionary<NodePort, Vector2>();
|
||||
|
||||
public NodeGraph graph { get { return _graph != null ? _graph : _graph = new NodeGraph(); } }
|
||||
public NodeGraph _graph;
|
||||
public Node hoveredNode;
|
||||
public Node activeNode { get; private set; }
|
||||
public NodePort hoveredPort;
|
||||
public NodeConnection? tempConnection;
|
||||
|
||||
public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } }
|
||||
private Vector2 _panOffset;
|
||||
@ -28,36 +32,92 @@ public class NodeEditorWindow : EditorWindow {
|
||||
Matrix4x4 m = GUI.matrix;
|
||||
NodeEditorAction.Controls(this);
|
||||
|
||||
|
||||
NodeEditorGUI.DrawGrid(position, zoom, panOffset);
|
||||
DrawNodes();
|
||||
DrawTempConnection();
|
||||
NodeEditorGUI.DrawToolbar(this);
|
||||
|
||||
GUI.matrix = m;
|
||||
}
|
||||
|
||||
/// <summary> Draw a connection as we are dragging it </summary>
|
||||
private void DrawTempConnection() {
|
||||
if (tempConnection.HasValue) {
|
||||
Node inputNode = graph.GetNode(tempConnection.Value.inputNodeId);
|
||||
if (inputNode != null) {
|
||||
NodePort outputPort = inputNode.GetOutput(tempConnection.Value.outputPortId);
|
||||
Vector2 startPoint = GridToWindowPosition( portConnectionPoints[outputPort]);
|
||||
Vector2 endPoint = Event.current.mousePosition;
|
||||
Vector2 startTangent = startPoint;
|
||||
startTangent.x = Mathf.Lerp(startPoint.x,endPoint.x,0.7f);
|
||||
Vector2 endTangent = endPoint;
|
||||
endTangent.x = Mathf.Lerp(endPoint.x, startPoint.x, 0.7f);
|
||||
Handles.DrawBezier(startPoint, endPoint, startTangent, endTangent, Color.gray, null, 4);
|
||||
Handles.DrawBezier(startPoint, endPoint, startTangent, endTangent, Color.white, null, 2);
|
||||
Repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
private void DrawNodes() {
|
||||
portConnectionPoints.Clear();
|
||||
Event e = Event.current;
|
||||
|
||||
BeginWindows();
|
||||
NodeEditorGUI.BeginZoomed(position, zoom);
|
||||
Event e = Event.current;
|
||||
if (e.type == EventType.repaint) {
|
||||
hoveredPort = null;
|
||||
}
|
||||
hoveredNode = null;
|
||||
foreach (KeyValuePair<int, Node> kvp in graph.nodes) {
|
||||
Node node = kvp.Value;
|
||||
int id = kvp.Key;
|
||||
|
||||
//Get node position
|
||||
Vector2 windowPos = GridToWindowPositionNoClipped(node.position.position);
|
||||
Vector2 nodePos = GridToWindowPositionNoClipped(node.position.position);
|
||||
|
||||
Rect windowRect = new Rect(windowPos, new Vector2(200, 200));
|
||||
Rect windowRect = new Rect(nodePos, new Vector2(200, 200));
|
||||
if (windowRect.Contains(e.mousePosition)) hoveredNode = node;
|
||||
|
||||
GUIStyle style = (node == activeNode) ? (GUIStyle)"flow node 0 on" : (GUIStyle)"flow node 0";
|
||||
GUI.Box(windowRect, node.ToString(), style);
|
||||
GUILayout.BeginArea(windowRect, node.ToString(), style);
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
//Inputs
|
||||
GUILayout.BeginVertical();
|
||||
for (int i = 0; i < node.InputCount; i++) {
|
||||
NodePort input = node.GetInput(i);
|
||||
Rect r = GUILayoutUtility.GetRect(new GUIContent(input.name), NodeEditorResources.styles.GetInputStyle(input.type));
|
||||
GUI.Label(r, input.name, NodeEditorResources.styles.GetInputStyle(input.type));
|
||||
if (e.type == EventType.repaint) {
|
||||
if (r.Contains(e.mousePosition)) hoveredPort = input;
|
||||
}
|
||||
portConnectionPoints.Add(input, new Vector2(r.xMin, r.yMin + (r.height * 0.5f)) + node.position.position);
|
||||
}
|
||||
GUILayout.EndVertical();
|
||||
|
||||
if (windowRect.position != windowPos) {
|
||||
windowPos = windowRect.position;
|
||||
node.position.position = WindowToGridPosition(windowPos);
|
||||
//Outputs
|
||||
GUILayout.BeginVertical();
|
||||
for (int i = 0; i < node.OutputCount; i++) {
|
||||
NodePort output = node.GetOutput(i);
|
||||
Rect r = GUILayoutUtility.GetRect(new GUIContent(output.name), NodeEditorResources.styles.GetOutputStyle(output.type));
|
||||
GUI.Label(r, output.name, NodeEditorResources.styles.GetOutputStyle(output.type));
|
||||
if (e.type == EventType.repaint) {
|
||||
if (r.Contains(e.mousePosition)) hoveredPort = output;
|
||||
}
|
||||
portConnectionPoints.Add(output, new Vector2(r.xMax, r.yMin + (r.height * 0.5f)) + node.position.position);
|
||||
}
|
||||
GUILayout.EndVertical();
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.Label("More stuff");
|
||||
EditorGUILayout.Toggle("aDF",false);
|
||||
|
||||
GUILayout.EndArea();
|
||||
|
||||
if (windowRect.position != nodePos) {
|
||||
nodePos = windowRect.position;
|
||||
node.position.position = WindowToGridPosition(nodePos);
|
||||
//Vector2 newPos = windowRect =
|
||||
}
|
||||
|
||||
@ -78,7 +138,8 @@ public class NodeEditorWindow : EditorWindow {
|
||||
}
|
||||
|
||||
public Vector2 GridToWindowPosition(Vector2 gridPosition) {
|
||||
return (position.size * 0.5f) + (panOffset / zoom) + gridPosition;
|
||||
//Vector2 center = position.size * 0.5f;
|
||||
return (position.size * 0.5f) + (panOffset / zoom) + (gridPosition/zoom);
|
||||
}
|
||||
|
||||
public Vector2 GridToWindowPositionNoClipped(Vector2 gridPosition) {
|
||||
@ -91,4 +152,5 @@ public class NodeEditorWindow : EditorWindow {
|
||||
public void SelectNode(Node node) {
|
||||
activeNode = node;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,8 +1,51 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using UNEC;
|
||||
|
||||
/// <summary> Base class for all nodes </summary>
|
||||
public abstract class Node {
|
||||
public Rect position = new Rect(0,0,200,200);
|
||||
protected NodePort[] inputs = new NodePort[0];
|
||||
protected NodePort[] outputs = new NodePort[0];
|
||||
|
||||
public int InputCount { get { return inputs.Length; } }
|
||||
public int OutputCount { get { return outputs.Length; } }
|
||||
|
||||
protected Node() {
|
||||
Init();
|
||||
}
|
||||
|
||||
abstract protected void Init();
|
||||
|
||||
public int GetInputPortId(NodePort input) {
|
||||
for (int i = 0; i < inputs.Length; i++) {
|
||||
if (input == inputs[i]) return i;
|
||||
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public int GetOutputPortId(NodePort output) {
|
||||
for (int i = 0; i < outputs.Length; i++) {
|
||||
if (output == outputs[i]) return i;
|
||||
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public NodePort GetInput(int portId) {
|
||||
return inputs[portId];
|
||||
}
|
||||
|
||||
public NodePort GetOutput(int portId) {
|
||||
return outputs[portId];
|
||||
}
|
||||
|
||||
public NodePort CreateNodeInput(string name, Type type, bool enabled = true) {
|
||||
return new NodePort(name, type, this, enabled);
|
||||
}
|
||||
public NodePort CreateNodeOutput(string name, Type type, bool enabled = true) {
|
||||
return new NodePort(name, type, this, enabled);
|
||||
}
|
||||
}
|
||||
|
||||
22
Scripts/NodeConnection.cs
Normal file
22
Scripts/NodeConnection.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UNEC {
|
||||
/// <summary> Data travels from Input Node's Output port to Output Node's Input port </summary>
|
||||
public struct NodeConnection {
|
||||
public int inputNodeId { get { return _inputNodeId; } }
|
||||
public int inputPortId { get { return _inputPortId; } }
|
||||
public int outputNodeId { get { return _outputNodeId; } }
|
||||
public int outputPortId { get { return _outputPortId; } }
|
||||
[SerializeField] private int _inputNodeId, _inputPortId, _outputNodeId, _outputPortId;
|
||||
|
||||
/// <summary> Data travels from Input Node's Output port to Output Node's Input port </summary>
|
||||
public NodeConnection(int inputNodeId, int outputPortId, int outputNodeId, int inputPortId) {
|
||||
_inputNodeId = inputNodeId;
|
||||
_outputPortId = outputPortId;
|
||||
_outputNodeId = outputNodeId;
|
||||
_inputPortId = inputPortId;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Scripts/NodeConnection.cs.meta
Normal file
12
Scripts/NodeConnection.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1f29a204e0cc2934e8f9cb2010723de9
|
||||
timeCreated: 1505747662
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -2,11 +2,13 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using UNEC;
|
||||
|
||||
/// <summary> Base class for all node graphs </summary>
|
||||
public class NodeGraph {
|
||||
|
||||
public Dictionary<int, Node> nodes = new Dictionary<int, Node>();
|
||||
private List<NodeConnection> connections = new List<NodeConnection>();
|
||||
|
||||
public T AddNode<T>() where T : Node {
|
||||
T node = default(T);
|
||||
@ -22,27 +24,37 @@ public class NodeGraph {
|
||||
}
|
||||
|
||||
public void RemoveNode(Node node) {
|
||||
int id = GetNodeID(node);
|
||||
int id = GetNodeId(node);
|
||||
if (id != -1) nodes.Remove(id);
|
||||
else Debug.LogWarning("Node " + node.ToString() + " is not part of NodeGraph");
|
||||
}
|
||||
|
||||
public void RemoveNode(int id) {
|
||||
nodes.Remove(id);
|
||||
public void RemoveNode(int nodeId) {
|
||||
nodes.Remove(nodeId);
|
||||
}
|
||||
|
||||
public int GetNodeID(Node node) {
|
||||
public int GetNodeId(Node node) {
|
||||
foreach(var kvp in nodes) {
|
||||
if (kvp.Value == node) return kvp.Key;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Node GetNode(int id) {
|
||||
if (nodes.ContainsKey(id)) return nodes[id];
|
||||
public Node GetNode(int nodeId) {
|
||||
if (nodes.ContainsKey(nodeId)) return nodes[nodeId];
|
||||
return null;
|
||||
}
|
||||
|
||||
public void AddConnection(NodePort input, NodePort output) {
|
||||
int inputNodeId = GetNodeId(input.node);
|
||||
int outputPortId = input.node.GetInputPortId(output);
|
||||
|
||||
int outputNodeId = GetNodeId(output.node);
|
||||
int inputPortId = output.node.GetInputPortId(input);
|
||||
|
||||
NodeConnection connection = new NodeConnection(inputNodeId, inputPortId, outputNodeId, outputPortId);
|
||||
}
|
||||
|
||||
private int GetUniqueID() {
|
||||
int id = 0;
|
||||
while (nodes.ContainsKey(id)) id++;
|
||||
@ -51,5 +63,6 @@ public class NodeGraph {
|
||||
|
||||
public void Clear() {
|
||||
nodes.Clear();
|
||||
connections.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
45
Scripts/NodePort.cs
Normal file
45
Scripts/NodePort.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public class NodePort {
|
||||
public enum IO { None, Input, Output}
|
||||
|
||||
public IO direction {
|
||||
get {
|
||||
for (int i = 0; i < node.InputCount; i++) {
|
||||
if (node.GetInput(i) == this) return IO.Input;
|
||||
}
|
||||
for (int i = 0; i < node.OutputCount; i++) {
|
||||
if (node.GetOutput(i) == this) return IO.Output;
|
||||
}
|
||||
return IO.None;
|
||||
}
|
||||
}
|
||||
public Node node { get; private set; }
|
||||
public string name { get { return _name; } set { _name = value; } }
|
||||
[SerializeField]
|
||||
private string _name;
|
||||
public Type type { get; private set; }
|
||||
[SerializeField]
|
||||
private string _type;
|
||||
public bool enabled { get { return _enabled; } set { _enabled = value; } }
|
||||
[SerializeField]
|
||||
private bool _enabled;
|
||||
|
||||
public NodePort(string name, Type type, Node node, bool enabled) {
|
||||
_name = name;
|
||||
_enabled = enabled;
|
||||
this.type = type;
|
||||
_type = type.FullName;
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
public NodePort GetConnection() {
|
||||
return null;
|
||||
}
|
||||
public NodePort[] GetConnections() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
12
Scripts/NodePort.cs.meta
Normal file
12
Scripts/NodePort.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7dd2f76ac25c6f44c9426dff3e7491a3
|
||||
timeCreated: 1505734054
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
x
Reference in New Issue
Block a user