mirror of
https://github.com/Siccity/xNode.git
synced 2026-02-04 14:24:54 +08:00
Editor core complete
Removed NodeConnection.cs: Node ports now operate by crossreferencing eachother Changed from separate static classes to partial class Removed UNEC Namespace Added live hover info
This commit is contained in:
parent
2a6341728b
commit
f9152bf891
@ -3,9 +3,9 @@ using System.Collections.Generic;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
|
|
||||||
|
/// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary>
|
||||||
public class NodeEditor : Editor {
|
public class NodeEditor : Editor {
|
||||||
|
|
||||||
|
|
||||||
public override void OnInspectorGUI() {
|
public override void OnInspectorGUI() {
|
||||||
base.OnInspectorGUI();
|
base.OnInspectorGUI();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,79 +3,111 @@ using System.Collections.Generic;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using System;
|
using System;
|
||||||
using UNEC;
|
|
||||||
|
|
||||||
public static class NodeEditorAction {
|
/// <summary> User input related UNEC functionality </summary>
|
||||||
|
public partial class NodeEditorWindow {
|
||||||
|
|
||||||
public static bool dragging;
|
public static bool isPanning { get; private set; }
|
||||||
public static Vector2 dragOffset;
|
public static Vector2 dragOffset;
|
||||||
|
|
||||||
public static void Controls(NodeEditorWindow window) {
|
private bool IsDraggingNode { get { return draggedNode != null; } }
|
||||||
|
private bool IsDraggingPort { get { return draggedOutput != null; } }
|
||||||
|
private bool IsHoveringPort { get { return hoveredPort != null; } }
|
||||||
|
private bool IsHoveringNode { get { return hoveredNode != null; } }
|
||||||
|
private bool HasSelectedNode { get { return selectedNode != null; } }
|
||||||
|
|
||||||
|
private Node hoveredNode;
|
||||||
|
private Node selectedNode;
|
||||||
|
private Node draggedNode;
|
||||||
|
private NodePort hoveredPort;
|
||||||
|
private NodePort draggedOutput;
|
||||||
|
private NodePort draggedOutputTarget;
|
||||||
|
|
||||||
|
private Rect nodeRects;
|
||||||
|
|
||||||
|
public void Controls() {
|
||||||
|
wantsMouseMove = true;
|
||||||
|
|
||||||
Event e = Event.current;
|
Event e = Event.current;
|
||||||
switch (e.type) {
|
switch (e.type) {
|
||||||
|
case EventType.MouseMove:
|
||||||
|
UpdateHovered();
|
||||||
|
break;
|
||||||
case EventType.ScrollWheel:
|
case EventType.ScrollWheel:
|
||||||
if (e.delta.y > 0) window.zoom += 0.1f * window.zoom;
|
if (e.delta.y > 0) zoom += 0.1f * zoom;
|
||||||
else window.zoom -= 0.1f * window.zoom;
|
else zoom -= 0.1f * zoom;
|
||||||
break;
|
break;
|
||||||
case EventType.MouseDrag:
|
case EventType.MouseDrag:
|
||||||
|
UpdateHovered();
|
||||||
if (e.button == 0) {
|
if (e.button == 0) {
|
||||||
if (window.HasSelectedNode) {
|
if (IsDraggingPort) {
|
||||||
//If we are currently dragging a connection, check if we are hovering any matching port to connect to
|
if (IsHoveringPort && hoveredPort.IsInput) {
|
||||||
if (window.IsDraggingConnection) {
|
if (!draggedOutput.IsConnectedTo(hoveredPort)) {
|
||||||
if (window.IsHoveringPort && window.IsHoveringNode && window.hoveredPort.IsInput) {
|
draggedOutputTarget = hoveredPort;
|
||||||
window.draggedConnection.outputNodeId = window.graph.GetNodeId(window.hoveredNode);
|
|
||||||
window.draggedConnection.inputPortId = window.hoveredNode.GetInputPortId(window.hoveredPort);
|
|
||||||
} else {
|
|
||||||
window.draggedConnection.outputNodeId = -1;
|
|
||||||
window.draggedConnection.inputPortId = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//If we just started dragging from a port, grab connection
|
|
||||||
else if (window.IsHoveringPort) {
|
|
||||||
if (window.hoveredPort.direction == NodePort.IO.Output) {
|
|
||||||
dragging = true;
|
|
||||||
int inputNodeId = window.graph.GetNodeId(window.selectedNode);
|
|
||||||
int outputPortId = window.selectedNode.GetOutputPortId(window.hoveredPort);
|
|
||||||
window.draggedConnection = 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 {
|
else {
|
||||||
window.selectedNode.position.position = window.WindowToGridPosition(e.mousePosition) + dragOffset;
|
draggedOutputTarget = null;
|
||||||
window.Repaint();
|
|
||||||
}
|
}
|
||||||
|
Repaint();
|
||||||
|
}
|
||||||
|
else if (IsDraggingNode) {
|
||||||
|
draggedNode.position.position = WindowToGridPosition(e.mousePosition) + dragOffset;
|
||||||
|
Repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (e.button == 1) {
|
else if (e.button == 1) {
|
||||||
window.panOffset += e.delta * window.zoom;
|
panOffset += e.delta * zoom;
|
||||||
dragging = true;
|
isPanning = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case EventType.KeyDown:
|
case EventType.KeyDown:
|
||||||
if (e.keyCode == KeyCode.F) Focus(window);
|
if (e.keyCode == KeyCode.F) Home();
|
||||||
break;
|
break;
|
||||||
case EventType.MouseDown:
|
case EventType.MouseDown:
|
||||||
dragging = false;
|
UpdateHovered();
|
||||||
window.Repaint();
|
Repaint();
|
||||||
window.SelectNode(window.hoveredNode);
|
SelectNode(hoveredNode);
|
||||||
if (window.hoveredNode != null) {
|
if (IsHoveringPort) {
|
||||||
dragOffset = window.hoveredNode.position.position - window.WindowToGridPosition(e.mousePosition);
|
if (hoveredPort.IsOutput) {
|
||||||
|
draggedOutput = hoveredPort;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (hoveredPort.IsConnected) {
|
||||||
|
NodePort output = hoveredPort.Connection;
|
||||||
|
hoveredPort.Disconnect(output);
|
||||||
|
draggedOutput = output;
|
||||||
|
draggedOutputTarget = hoveredPort;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (IsHoveringNode) {
|
||||||
|
draggedNode = hoveredNode;
|
||||||
|
dragOffset = hoveredNode.position.position - WindowToGridPosition(e.mousePosition);
|
||||||
}
|
}
|
||||||
window.Repaint();
|
|
||||||
break;
|
break;
|
||||||
case EventType.MouseUp:
|
case EventType.MouseUp:
|
||||||
window.draggedConnection.enabled = false;
|
if (e.button == 0) {
|
||||||
if (dragging) return;
|
//Port drag release
|
||||||
|
if (IsDraggingPort) {
|
||||||
if (e.button == 1) {
|
//If connection is valid, save it
|
||||||
NodeEditorGUI.RightClickContextMenu(window);
|
if (draggedOutputTarget != null) {
|
||||||
|
draggedOutput.Connect(draggedOutputTarget);
|
||||||
}
|
}
|
||||||
|
//Release dragged connection
|
||||||
|
draggedOutput = null;
|
||||||
|
draggedOutputTarget = null;
|
||||||
|
Repaint();
|
||||||
|
}
|
||||||
|
else if (IsDraggingNode) {
|
||||||
|
draggedNode = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (e.button == 1) {
|
||||||
|
if (!isPanning) RightClickContextMenu();
|
||||||
|
isPanning = false;
|
||||||
|
}
|
||||||
|
UpdateHovered();
|
||||||
break;
|
break;
|
||||||
case EventType.repaint:
|
case EventType.repaint:
|
||||||
|
|
||||||
@ -84,13 +116,63 @@ public static class NodeEditorAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Puts all nodes in focus. If no nodes are present, resets view to </summary>
|
/// <summary> Puts all nodes in focus. If no nodes are present, resets view to </summary>
|
||||||
public static void Focus(this NodeEditorWindow window) {
|
public void Home() {
|
||||||
window.zoom = 2;
|
zoom = 2;
|
||||||
window.panOffset = Vector2.zero;
|
panOffset = Vector2.zero;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void CreateNode(this NodeEditorWindow window, Type type, Vector2 position) {
|
public void CreateNode(Type type, Vector2 position) {
|
||||||
Node node = window.graph.AddNode(type);
|
Node node = graph.AddNode(type);
|
||||||
node.position.position = position;
|
node.position.position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Draw a connection as we are dragging it </summary>
|
||||||
|
public void DrawDraggedConnection() {
|
||||||
|
if (IsDraggingPort) {
|
||||||
|
Vector2 from = _portConnectionPoints[draggedOutput];
|
||||||
|
Vector2 to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget] : WindowToGridPosition(Event.current.mousePosition);
|
||||||
|
DrawConnection(from, to);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdateHovered() {
|
||||||
|
Vector2 mousePos = Event.current.mousePosition;
|
||||||
|
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));
|
||||||
|
if (windowRect.Contains(mousePos)) {
|
||||||
|
newHoverNode = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (newHoverNode != hoveredNode) {
|
||||||
|
hoveredNode = newHoverNode;
|
||||||
|
Repaint();
|
||||||
|
}
|
||||||
|
if (IsHoveringNode) {
|
||||||
|
NodePort newHoverPort = null;
|
||||||
|
for (int i = 0; i < hoveredNode.InputCount; i++) {
|
||||||
|
NodePort port = hoveredNode.GetInput(i);
|
||||||
|
if (!portRects.ContainsKey(port)) continue;
|
||||||
|
Rect r = portRects[port];
|
||||||
|
r.position = GridToWindowPosition(r.position + hoveredNode.position.position);
|
||||||
|
r.size /= zoom;
|
||||||
|
if (r.Contains(mousePos)) newHoverPort = port;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < hoveredNode.OutputCount; i++) {
|
||||||
|
NodePort port = hoveredNode.GetOutput(i);
|
||||||
|
if (!portRects.ContainsKey(port)) continue;
|
||||||
|
Rect r = portRects[port];
|
||||||
|
r.position = GridToWindowPosition(r.position + hoveredNode.position.position);
|
||||||
|
r.size /= zoom;
|
||||||
|
if (r.Contains(mousePos)) newHoverPort = port;
|
||||||
|
}
|
||||||
|
if (newHoverPort != hoveredPort) {
|
||||||
|
hoveredPort = newHoverPort;
|
||||||
|
Repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else hoveredPort = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,9 +4,8 @@ using UnityEngine;
|
|||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace UNEC {
|
|
||||||
/// <summary> Contains GUI methods </summary>
|
/// <summary> Contains GUI methods </summary>
|
||||||
public static class NodeEditorGUI {
|
public partial class NodeEditorWindow {
|
||||||
|
|
||||||
public static void DrawConnection(Vector2 from, Vector2 to, Color col) {
|
public static void DrawConnection(Vector2 from, Vector2 to, Color col) {
|
||||||
Handles.DrawBezier(from, to, from, to, col, new Texture2D(2, 2), 2);
|
Handles.DrawBezier(from, to, from, to, col, new Texture2D(2, 2), 2);
|
||||||
@ -39,8 +38,8 @@ namespace UNEC {
|
|||||||
rect.position = Vector2.zero;
|
rect.position = Vector2.zero;
|
||||||
|
|
||||||
Vector2 center = rect.size / 2f;
|
Vector2 center = rect.size / 2f;
|
||||||
Texture2D gridTex = NodeEditorResources.gridTexture;
|
Texture2D gridTex = gridTexture;
|
||||||
Texture2D crossTex = NodeEditorResources.crossTexture;
|
Texture2D crossTex = crossTexture;
|
||||||
|
|
||||||
// Offset from origin in tile units
|
// Offset from origin in tile units
|
||||||
float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width;
|
float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width;
|
||||||
@ -59,14 +58,20 @@ namespace UNEC {
|
|||||||
GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f,0.5f), tileAmount));
|
GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f,0.5f), tileAmount));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DrawToolbar(NodeEditorWindow window) {
|
public void DrawToolbar() {
|
||||||
EditorGUILayout.BeginHorizontal("Toolbar");
|
EditorGUILayout.BeginHorizontal("Toolbar");
|
||||||
|
|
||||||
if (DropdownButton("File", 50)) FileContextMenu();
|
if (DropdownButton("File", 50)) FileContextMenu();
|
||||||
if (DropdownButton("Edit", 50)) EditContextMenu(window);
|
if (DropdownButton("Edit", 50)) EditContextMenu();
|
||||||
if (DropdownButton("View", 50)) { }
|
if (DropdownButton("View", 50)) { }
|
||||||
if (DropdownButton("Settings", 70)) { }
|
if (DropdownButton("Settings", 70)) { }
|
||||||
if (DropdownButton("Tools", 50)) { }
|
if (DropdownButton("Tools", 50)) { }
|
||||||
|
if (IsHoveringNode) {
|
||||||
|
GUILayout.Space(20);
|
||||||
|
string hoverInfo = hoveredNode.GetType().ToString();
|
||||||
|
if (IsHoveringPort) hoverInfo += " > "+hoveredPort.name;
|
||||||
|
GUILayout.Label(hoverInfo);
|
||||||
|
}
|
||||||
|
|
||||||
// Make the toolbar extend all throughout the window extension.
|
// Make the toolbar extend all throughout the window extension.
|
||||||
GUILayout.FlexibleSpace();
|
GUILayout.FlexibleSpace();
|
||||||
@ -78,19 +83,19 @@ namespace UNEC {
|
|||||||
return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width));
|
return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RightClickContextMenu(NodeEditorWindow window) {
|
public void RightClickContextMenu() {
|
||||||
GenericMenu contextMenu = new GenericMenu();
|
GenericMenu contextMenu = new GenericMenu();
|
||||||
|
|
||||||
if (window.hoveredNode != null) {
|
if (hoveredNode != null) {
|
||||||
Node node = window.hoveredNode;
|
Node node = hoveredNode;
|
||||||
contextMenu.AddItem(new GUIContent("Remove"), false, () => window.graph.RemoveNode(node));
|
contextMenu.AddItem(new GUIContent("Remove"), false, () => graph.RemoveNode(node));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Vector2 pos = window.WindowToGridPosition(Event.current.mousePosition);
|
Vector2 pos = WindowToGridPosition(Event.current.mousePosition);
|
||||||
for (int i = 0; i < NodeEditorReflection.nodeTypes.Length; i++) {
|
for (int i = 0; i < nodeTypes.Length; i++) {
|
||||||
Type type = NodeEditorReflection.nodeTypes[i];
|
Type type = nodeTypes[i];
|
||||||
contextMenu.AddItem(new GUIContent(NodeEditorReflection.nodeTypes[i].ToString()), false, () => {
|
contextMenu.AddItem(new GUIContent(nodeTypes[i].ToString()), false, () => {
|
||||||
window.CreateNode(type, pos);
|
CreateNode(type, pos);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,11 +114,26 @@ namespace UNEC {
|
|||||||
contextMenu.DropDown(new Rect(5f, 17f, 0f, 0f));
|
contextMenu.DropDown(new Rect(5f, 17f, 0f, 0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void EditContextMenu(NodeEditorWindow window) {
|
public void EditContextMenu() {
|
||||||
GenericMenu contextMenu = new GenericMenu();
|
GenericMenu contextMenu = new GenericMenu();
|
||||||
contextMenu.AddItem(new GUIContent("Clear"), false, () => window.graph.Clear());
|
contextMenu.AddItem(new GUIContent("Clear"), false, () => graph.Clear());
|
||||||
|
|
||||||
contextMenu.DropDown(new Rect(5f, 17f, 0f, 0f));
|
contextMenu.DropDown(new Rect(5f, 17f, 0f, 0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void DrawConnection(Vector2 startPoint, Vector2 endPoint) {
|
||||||
|
startPoint = GridToWindowPosition(startPoint);
|
||||||
|
endPoint = GridToWindowPosition(endPoint);
|
||||||
|
|
||||||
|
Vector2 startTangent = startPoint;
|
||||||
|
if (startPoint.x < endPoint.x) startTangent.x = Mathf.LerpUnclamped(startPoint.x, endPoint.x, 0.7f);
|
||||||
|
else startTangent.x = Mathf.LerpUnclamped(startPoint.x, endPoint.x, -0.7f);
|
||||||
|
|
||||||
|
Vector2 endTangent = endPoint;
|
||||||
|
if (startPoint.x > endPoint.x) endTangent.x = Mathf.LerpUnclamped(endPoint.x, startPoint.x, -0.7f);
|
||||||
|
else endTangent.x = Mathf.LerpUnclamped(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,9 +2,8 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace UNEC {
|
|
||||||
/// <summary> Contains reflection-related info </summary>
|
/// <summary> Contains reflection-related info </summary>
|
||||||
public static class NodeEditorReflection {
|
public partial class NodeEditorWindow {
|
||||||
|
|
||||||
public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } }
|
public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } }
|
||||||
private static Type[] _nodeTypes;
|
private static Type[] _nodeTypes;
|
||||||
@ -19,4 +18,3 @@ namespace UNEC {
|
|||||||
).ToArray();
|
).ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@ -2,8 +2,7 @@
|
|||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace UNEC {
|
public partial class NodeEditorWindow {
|
||||||
public static class NodeEditorResources {
|
|
||||||
|
|
||||||
public static Texture2D gridTexture { get { return _gridTexture != null ? _gridTexture : _gridTexture = GenerateGridTexture(); } }
|
public static Texture2D gridTexture { get { return _gridTexture != null ? _gridTexture : _gridTexture = GenerateGridTexture(); } }
|
||||||
private static Texture2D _gridTexture;
|
private static Texture2D _gridTexture;
|
||||||
@ -16,7 +15,8 @@ namespace UNEC {
|
|||||||
private static Color arteryColor = new Color(0.34f, 0.34f, 0.34f);
|
private static Color arteryColor = new Color(0.34f, 0.34f, 0.34f);
|
||||||
private static Color crossColor = new Color(0.45f, 0.45f, 0.45f);
|
private static Color crossColor = new Color(0.45f, 0.45f, 0.45f);
|
||||||
|
|
||||||
public static Styles styles = new Styles();
|
public static Styles styles { get { return _styles != null ? _styles : _styles = new Styles(); } }
|
||||||
|
public static Styles _styles = null;
|
||||||
|
|
||||||
public class Styles {
|
public class Styles {
|
||||||
GUIStyle inputInt, inputString, inputFloat, inputObject, inputTexture, inputColor;
|
GUIStyle inputInt, inputString, inputFloat, inputObject, inputTexture, inputColor;
|
||||||
@ -100,4 +100,3 @@ namespace UNEC {
|
|||||||
return tex;
|
return tex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@ -2,91 +2,70 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UNEC;
|
|
||||||
|
|
||||||
public class NodeEditorWindow : EditorWindow {
|
[InitializeOnLoad]
|
||||||
|
public partial class NodeEditorWindow : EditorWindow {
|
||||||
|
|
||||||
private Dictionary<NodePort, Vector2> portConnectionPoints = new Dictionary<NodePort, Vector2>();
|
public Dictionary<NodePort, Vector2> portConnectionPoints { get { return _portConnectionPoints; } }
|
||||||
public bool IsDraggingConnection { get { return draggedConnection.enabled; } }
|
private Dictionary<NodePort, Vector2> _portConnectionPoints = new Dictionary<NodePort, Vector2>();
|
||||||
public bool IsHoveringPort { get { return hoveredPort != null; } }
|
private Dictionary<NodePort, Rect> portRects = new Dictionary<NodePort, Rect>();
|
||||||
public bool IsHoveringNode { get { return hoveredNode != null; } }
|
|
||||||
public bool HasSelectedNode { get { return selectedNode != null; } }
|
|
||||||
public NodeGraph graph { get { return _graph != null ? _graph : _graph = new NodeGraph(); } }
|
public NodeGraph graph { get { return _graph != null ? _graph : _graph = new NodeGraph(); } }
|
||||||
public NodeGraph _graph;
|
public NodeGraph _graph;
|
||||||
public Node hoveredNode;
|
|
||||||
/// <summary> Currently selected node </summary>
|
|
||||||
public Node selectedNode { get; private set; }
|
|
||||||
public NodePort hoveredPort;
|
|
||||||
public NodeConnection draggedConnection;
|
|
||||||
|
|
||||||
public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } }
|
public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } }
|
||||||
private Vector2 _panOffset;
|
private Vector2 _panOffset;
|
||||||
public float zoom { get { return _zoom; } set { _zoom = Mathf.Clamp(value, 1f, 5f); Repaint(); } }
|
public float zoom { get { return _zoom; } set { _zoom = Mathf.Clamp(value, 1f, 5f); Repaint(); } }
|
||||||
private float _zoom = 1;
|
private float _zoom = 1;
|
||||||
|
|
||||||
|
partial void OnEnable();
|
||||||
|
|
||||||
[MenuItem("Window/UNEC")]
|
[MenuItem("Window/UNEC")]
|
||||||
static void Init() {
|
static void Init() {
|
||||||
NodeEditorWindow w = CreateInstance<NodeEditorWindow>();
|
NodeEditorWindow w = CreateInstance<NodeEditorWindow>();
|
||||||
w.titleContent = new GUIContent("UNEC");
|
w.titleContent = new GUIContent("UNEC");
|
||||||
|
w.wantsMouseMove = true;
|
||||||
w.Show();
|
w.Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnGUI() {
|
private void OnGUI() {
|
||||||
|
Event e = Event.current;
|
||||||
Matrix4x4 m = GUI.matrix;
|
Matrix4x4 m = GUI.matrix;
|
||||||
NodeEditorAction.Controls(this);
|
Controls();
|
||||||
|
|
||||||
NodeEditorGUI.DrawGrid(position, zoom, panOffset);
|
DrawGrid(position, zoom, panOffset);
|
||||||
DrawNodes();
|
DrawNodes();
|
||||||
|
DrawConnections();
|
||||||
DrawDraggedConnection();
|
DrawDraggedConnection();
|
||||||
NodeEditorGUI.DrawToolbar(this);
|
if (e.type == EventType.Repaint || e.type == EventType.Layout) DrawToolbar();
|
||||||
|
|
||||||
GUI.matrix = m;
|
GUI.matrix = m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Draw a connection as we are dragging it </summary>
|
public void DrawConnections() {
|
||||||
private void DrawDraggedConnection() {
|
foreach(Node node in graph.nodes) {
|
||||||
if (IsDraggingConnection) {
|
for (int i = 0; i < node.OutputCount; i++) {
|
||||||
Node inputNode = graph.GetNode(draggedConnection.inputNodeId);
|
NodePort output = node.GetOutput(i);
|
||||||
Node outputNode = graph.GetNode(draggedConnection.outputNodeId);
|
Vector2 from = _portConnectionPoints[output];
|
||||||
if (inputNode != null) {
|
for (int k = 0; k < output.ConnectionCount; k++) {
|
||||||
NodePort outputPort = inputNode.GetOutput(draggedConnection.outputPortId);
|
NodePort input = output.GetConnection(k);
|
||||||
Vector2 startPoint = GridToWindowPosition( portConnectionPoints[outputPort]);
|
Vector2 to = _portConnectionPoints[input];
|
||||||
Vector2 endPoint = Event.current.mousePosition;
|
DrawConnection(from, to);
|
||||||
if (outputNode != null) {
|
|
||||||
NodePort inputPort = outputNode.GetInput(draggedConnection.inputPortId);
|
|
||||||
if (inputPort != null) endPoint = GridToWindowPosition(portConnectionPoints[inputPort]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
private void DrawNodes() {
|
||||||
portConnectionPoints.Clear();
|
portConnectionPoints.Clear();
|
||||||
Event e = Event.current;
|
Event e = Event.current;
|
||||||
|
|
||||||
BeginWindows();
|
BeginWindows();
|
||||||
NodeEditorGUI.BeginZoomed(position, zoom);
|
BeginZoomed(position, zoom);
|
||||||
if (e.type == EventType.repaint) {
|
if (e.type == EventType.Repaint) portRects.Clear();
|
||||||
hoveredPort = null;
|
foreach (Node node in graph.nodes) {
|
||||||
}
|
|
||||||
hoveredNode = null;
|
|
||||||
foreach (KeyValuePair<int, Node> kvp in graph.nodes) {
|
|
||||||
Node node = kvp.Value;
|
|
||||||
int id = kvp.Key;
|
|
||||||
|
|
||||||
//Get node position
|
//Get node position
|
||||||
Vector2 nodePos = GridToWindowPositionNoClipped(node.position.position);
|
Vector2 nodePos = GridToWindowPositionNoClipped(node.position.position);
|
||||||
|
|
||||||
Rect windowRect = new Rect(nodePos, new Vector2(200, 200));
|
Rect windowRect = new Rect(nodePos, node.position.size);
|
||||||
if (windowRect.Contains(e.mousePosition)) hoveredNode = node;
|
|
||||||
|
|
||||||
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(windowRect, node.ToString(), style);
|
GUILayout.BeginArea(windowRect, node.ToString(), style);
|
||||||
@ -96,11 +75,9 @@ public class NodeEditorWindow : EditorWindow {
|
|||||||
GUILayout.BeginVertical();
|
GUILayout.BeginVertical();
|
||||||
for (int i = 0; i < node.InputCount; i++) {
|
for (int i = 0; i < node.InputCount; i++) {
|
||||||
NodePort input = node.GetInput(i);
|
NodePort input = node.GetInput(i);
|
||||||
Rect r = GUILayoutUtility.GetRect(new GUIContent(input.name), NodeEditorResources.styles.GetInputStyle(input.type));
|
Rect r = GUILayoutUtility.GetRect(new GUIContent(input.name), styles.GetInputStyle(input.type));
|
||||||
GUI.Label(r, input.name, NodeEditorResources.styles.GetInputStyle(input.type));
|
GUI.Label(r, input.name, styles.GetInputStyle(input.type));
|
||||||
if (e.type == EventType.repaint) {
|
if (e.type == EventType.Repaint) portRects.Add(input, r);
|
||||||
if (r.Contains(e.mousePosition)) hoveredPort = input;
|
|
||||||
}
|
|
||||||
portConnectionPoints.Add(input, new Vector2(r.xMin, r.yMin + (r.height * 0.5f)) + node.position.position);
|
portConnectionPoints.Add(input, new Vector2(r.xMin, r.yMin + (r.height * 0.5f)) + node.position.position);
|
||||||
}
|
}
|
||||||
GUILayout.EndVertical();
|
GUILayout.EndVertical();
|
||||||
@ -109,11 +86,9 @@ public class NodeEditorWindow : EditorWindow {
|
|||||||
GUILayout.BeginVertical();
|
GUILayout.BeginVertical();
|
||||||
for (int i = 0; i < node.OutputCount; i++) {
|
for (int i = 0; i < node.OutputCount; i++) {
|
||||||
NodePort output = node.GetOutput(i);
|
NodePort output = node.GetOutput(i);
|
||||||
Rect r = GUILayoutUtility.GetRect(new GUIContent(output.name), NodeEditorResources.styles.GetOutputStyle(output.type));
|
Rect r = GUILayoutUtility.GetRect(new GUIContent(output.name), styles.GetOutputStyle(output.type));
|
||||||
GUI.Label(r, output.name, NodeEditorResources.styles.GetOutputStyle(output.type));
|
GUI.Label(r, output.name, styles.GetOutputStyle(output.type));
|
||||||
if (e.type == EventType.repaint) {
|
if (e.type == EventType.Repaint) portRects.Add(output, r);
|
||||||
if (r.Contains(e.mousePosition)) hoveredPort = output;
|
|
||||||
}
|
|
||||||
portConnectionPoints.Add(output, new Vector2(r.xMax, r.yMin + (r.height * 0.5f)) + node.position.position);
|
portConnectionPoints.Add(output, new Vector2(r.xMax, r.yMin + (r.height * 0.5f)) + node.position.position);
|
||||||
}
|
}
|
||||||
GUILayout.EndVertical();
|
GUILayout.EndVertical();
|
||||||
@ -132,7 +107,7 @@ public class NodeEditorWindow : EditorWindow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
NodeEditorGUI.EndZoomed(position, zoom);
|
EndZoomed(position, zoom);
|
||||||
EndWindows();
|
EndWindows();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System;
|
using System;
|
||||||
using UNEC;
|
|
||||||
|
|
||||||
/// <summary> Base class for all nodes </summary>
|
/// <summary> Base class for all nodes </summary>
|
||||||
public abstract class Node {
|
public abstract class Node {
|
||||||
@ -19,14 +18,14 @@ public abstract class Node {
|
|||||||
|
|
||||||
abstract protected void Init();
|
abstract protected void Init();
|
||||||
|
|
||||||
public int GetInputPortId(NodePort input) {
|
public int GetInputId(NodePort input) {
|
||||||
for (int i = 0; i < inputs.Length; i++) {
|
for (int i = 0; i < inputs.Length; i++) {
|
||||||
if (input == inputs[i]) return i;
|
if (input == inputs[i]) return i;
|
||||||
|
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
public int GetOutputPortId(NodePort output) {
|
public int GetOutputId(NodePort output) {
|
||||||
for (int i = 0; i < outputs.Length; i++) {
|
for (int i = 0; i < outputs.Length; i++) {
|
||||||
if (output == outputs[i]) return i;
|
if (output == outputs[i]) return i;
|
||||||
|
|
||||||
@ -43,9 +42,18 @@ public abstract class Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public NodePort CreateNodeInput(string name, Type type, bool enabled = true) {
|
public NodePort CreateNodeInput(string name, Type type, bool enabled = true) {
|
||||||
return new NodePort(name, type, this, enabled);
|
return new NodePort(name, type, this, enabled, NodePort.IO.Input);
|
||||||
}
|
}
|
||||||
public NodePort CreateNodeOutput(string name, Type type, bool enabled = true) {
|
public NodePort CreateNodeOutput(string name, Type type, bool enabled = true) {
|
||||||
return new NodePort(name, type, this, enabled);
|
return new NodePort(name, type, this, enabled, NodePort.IO.Output);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClearConnections() {
|
||||||
|
for (int i = 0; i < inputs.Length; i++) {
|
||||||
|
inputs[i].ClearConnections();
|
||||||
|
}
|
||||||
|
for (int i = 0; i < outputs.Length; i++) {
|
||||||
|
outputs[i].ClearConnections();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +0,0 @@
|
|||||||
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 bool enabled;
|
|
||||||
/// <summary> Data travels from Input Node's Output port to Output Node's Input port </summary>
|
|
||||||
public 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) {
|
|
||||||
this.inputNodeId = inputNodeId;
|
|
||||||
this.outputPortId = outputPortId;
|
|
||||||
this.outputNodeId = outputNodeId;
|
|
||||||
this.inputPortId = inputPortId;
|
|
||||||
enabled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 1f29a204e0cc2934e8f9cb2010723de9
|
|
||||||
timeCreated: 1505747662
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
||||||
@ -2,67 +2,38 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System;
|
using System;
|
||||||
using UNEC;
|
|
||||||
|
|
||||||
/// <summary> Base class for all node graphs </summary>
|
/// <summary> Base class for all node graphs </summary>
|
||||||
public class NodeGraph {
|
public class NodeGraph {
|
||||||
|
|
||||||
public Dictionary<int, Node> nodes = new Dictionary<int, Node>();
|
/// <summary> All nodes in the graph. <para/>
|
||||||
private List<NodeConnection> connections = new List<NodeConnection>();
|
/// See: <see cref="AddNode{T}"/> </summary>
|
||||||
|
public List<Node> nodes = new List<Node>();
|
||||||
|
|
||||||
public T AddNode<T>() where T : Node {
|
public T AddNode<T>() where T : Node {
|
||||||
T node = default(T);
|
T node = default(T);
|
||||||
nodes.Add(GetUniqueID(), node);
|
nodes.Add(node);
|
||||||
return node;
|
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 null;
|
if (node == null) {
|
||||||
nodes.Add(GetUniqueID(), node);
|
Debug.LogError("Node could node be instanced");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
nodes.Add(node);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Safely remove a node and all its connections </summary>
|
||||||
|
/// <param name="node"></param>
|
||||||
public void RemoveNode(Node node) {
|
public void RemoveNode(Node node) {
|
||||||
int id = GetNodeId(node);
|
node.ClearConnections();
|
||||||
if (id != -1) nodes.Remove(id);
|
nodes.Remove(node);
|
||||||
else Debug.LogWarning("Node " + node.ToString() + " is not part of NodeGraph");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveNode(int nodeId) {
|
|
||||||
nodes.Remove(nodeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetNodeId(Node node) {
|
|
||||||
foreach(var kvp in nodes) {
|
|
||||||
if (kvp.Value == node) return kvp.Key;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
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++;
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear() {
|
public void Clear() {
|
||||||
nodes.Clear();
|
nodes.Clear();
|
||||||
connections.Clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,46 +4,65 @@ using UnityEngine;
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
public class NodePort {
|
public class NodePort {
|
||||||
public enum IO { None, Input, Output}
|
public enum IO { Input, Output}
|
||||||
|
|
||||||
public IO direction {
|
public int ConnectionCount { get { return connections.Count; } }
|
||||||
get {
|
/// <summary> Return the first connection </summary>
|
||||||
for (int i = 0; i < node.InputCount; i++) {
|
public NodePort Connection { get { return connections.Count > 0 ? connections[0] : null; } }
|
||||||
if (node.GetInput(i) == this) return IO.Input;
|
/// <summary> Returns a copy of the connections list </summary>
|
||||||
}
|
public List<NodePort> Connections { get { return new List<NodePort>(connections); } }
|
||||||
for (int i = 0; i < node.OutputCount; i++) {
|
|
||||||
if (node.GetOutput(i) == this) return IO.Output;
|
|
||||||
}
|
|
||||||
return IO.None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public IO direction { get { return _direction; } }
|
||||||
|
/// <summary> Is this port connected to anytihng? </summary>
|
||||||
|
public bool IsConnected { get { return connections.Count != 0; } }
|
||||||
public bool IsInput { get { return direction == IO.Input; } }
|
public bool IsInput { get { return direction == IO.Input; } }
|
||||||
public bool IsOutput { get { return direction == IO.Output; } }
|
public bool IsOutput { get { return direction == IO.Output; } }
|
||||||
|
|
||||||
|
public Type type { get { return _type; } }
|
||||||
public Node node { get; private set; }
|
public Node node { get; private set; }
|
||||||
public string name { get { return _name; } set { _name = value; } }
|
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; } }
|
public bool enabled { get { return _enabled; } set { _enabled = value; } }
|
||||||
[SerializeField]
|
|
||||||
private bool _enabled;
|
|
||||||
|
|
||||||
public NodePort(string name, Type type, Node node, bool enabled) {
|
private Type _type;
|
||||||
|
private List<NodePort> connections = new List<NodePort>();
|
||||||
|
|
||||||
|
[SerializeField] private string _name;
|
||||||
|
[SerializeField] private bool _enabled;
|
||||||
|
[SerializeField] private IO _direction;
|
||||||
|
|
||||||
|
public NodePort(string name, Type type, Node node, bool enabled, IO direction) {
|
||||||
_name = name;
|
_name = name;
|
||||||
_enabled = enabled;
|
_enabled = enabled;
|
||||||
this.type = type;
|
_type = type;
|
||||||
_type = type.FullName;
|
|
||||||
this.node = node;
|
this.node = node;
|
||||||
|
_direction = direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NodePort GetConnection() {
|
public void Connect(NodePort port) {
|
||||||
return null;
|
if (port == this) { Debug.LogWarning("Attempting to connect port to self."); return; }
|
||||||
|
if (connections.Contains(port)) { Debug.LogWarning("Port already connected."); return; }
|
||||||
|
if (direction == port.direction) { Debug.LogWarning("Cannot connect two " + (direction == IO.Input ? "input" : "output") + " connections"); return; }
|
||||||
|
connections.Add(port);
|
||||||
|
port.connections.Add(this);
|
||||||
}
|
}
|
||||||
public NodePort[] GetConnections() {
|
|
||||||
return null;
|
public NodePort GetConnection(int i) {
|
||||||
|
return connections[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsConnectedTo(NodePort port) {
|
||||||
|
return connections.Contains(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Disconnect(NodePort port) {
|
||||||
|
connections.Remove(port);
|
||||||
|
port.connections.Remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClearConnections() {
|
||||||
|
for (int i = 0; i < connections.Count; i++) {
|
||||||
|
connections[i].connections.Remove(this);
|
||||||
|
}
|
||||||
|
connections.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user