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,116 +4,136 @@ using UnityEngine;
|
|||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace UNEC {
|
/// <summary> Contains GUI methods </summary>
|
||||||
/// <summary> Contains GUI methods </summary>
|
public partial class NodeEditorWindow {
|
||||||
public static class NodeEditorGUI {
|
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void BeginZoomed(Rect rect, float zoom) {
|
||||||
|
GUI.EndClip();
|
||||||
|
|
||||||
|
GUIUtility.ScaleAroundPivot(Vector2.one / zoom, rect.size * 0.5f);
|
||||||
|
Vector4 padding = new Vector4(0, 22, 0, 0);
|
||||||
|
padding *= zoom;
|
||||||
|
GUI.BeginClip(new Rect(
|
||||||
|
-((rect.width * zoom) - rect.width) * 0.5f,
|
||||||
|
-(((rect.height * zoom) - rect.height) * 0.5f) + (22 * zoom),
|
||||||
|
rect.width * zoom,
|
||||||
|
rect.height * zoom));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void EndZoomed(Rect rect, float zoom) {
|
||||||
|
GUIUtility.ScaleAroundPivot(Vector2.one * zoom, rect.size * 0.5f);
|
||||||
|
Vector3 offset = new Vector3(
|
||||||
|
(((rect.width * zoom) - rect.width) * 0.5f),
|
||||||
|
(((rect.height * zoom) - rect.height) * 0.5f) + (-22 * zoom)+22,
|
||||||
|
0);
|
||||||
|
GUI.matrix = Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void DrawGrid(Rect rect, float zoom, Vector2 panOffset) {
|
||||||
|
|
||||||
|
rect.position = Vector2.zero;
|
||||||
|
|
||||||
|
Vector2 center = rect.size / 2f;
|
||||||
|
Texture2D gridTex = gridTexture;
|
||||||
|
Texture2D crossTex = crossTexture;
|
||||||
|
|
||||||
|
// Offset from origin in tile units
|
||||||
|
float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width;
|
||||||
|
float yOffset = ((center.y - rect.size.y) * zoom + panOffset.y) / gridTex.height;
|
||||||
|
|
||||||
|
Vector2 tileOffset = new Vector2(xOffset, yOffset);
|
||||||
|
|
||||||
|
// Amount of tiles
|
||||||
|
float tileAmountX = Mathf.Round(rect.size.x * zoom) / gridTex.width;
|
||||||
|
float tileAmountY = Mathf.Round(rect.size.y * zoom) / gridTex.height;
|
||||||
|
|
||||||
|
Vector2 tileAmount = new Vector2(tileAmountX, tileAmountY);
|
||||||
|
|
||||||
|
// Draw tiled background
|
||||||
|
GUI.DrawTextureWithTexCoords(rect, gridTex, new Rect(tileOffset, tileAmount));
|
||||||
|
GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f,0.5f), tileAmount));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawToolbar() {
|
||||||
|
EditorGUILayout.BeginHorizontal("Toolbar");
|
||||||
|
|
||||||
|
if (DropdownButton("File", 50)) FileContextMenu();
|
||||||
|
if (DropdownButton("Edit", 50)) EditContextMenu();
|
||||||
|
if (DropdownButton("View", 50)) { }
|
||||||
|
if (DropdownButton("Settings", 70)) { }
|
||||||
|
if (DropdownButton("Tools", 50)) { }
|
||||||
|
if (IsHoveringNode) {
|
||||||
|
GUILayout.Space(20);
|
||||||
|
string hoverInfo = hoveredNode.GetType().ToString();
|
||||||
|
if (IsHoveringPort) hoverInfo += " > "+hoveredPort.name;
|
||||||
|
GUILayout.Label(hoverInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void BeginZoomed(Rect rect, float zoom) {
|
// Make the toolbar extend all throughout the window extension.
|
||||||
GUI.EndClip();
|
GUILayout.FlexibleSpace();
|
||||||
|
|
||||||
GUIUtility.ScaleAroundPivot(Vector2.one / zoom, rect.size * 0.5f);
|
EditorGUILayout.EndHorizontal();
|
||||||
Vector4 padding = new Vector4(0, 22, 0, 0);
|
}
|
||||||
padding *= zoom;
|
|
||||||
GUI.BeginClip(new Rect(
|
public static bool DropdownButton(string name, float width) {
|
||||||
-((rect.width * zoom) - rect.width) * 0.5f,
|
return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width));
|
||||||
-(((rect.height * zoom) - rect.height) * 0.5f) + (22 * zoom),
|
}
|
||||||
rect.width * zoom,
|
|
||||||
rect.height * zoom));
|
public void RightClickContextMenu() {
|
||||||
|
GenericMenu contextMenu = new GenericMenu();
|
||||||
|
|
||||||
|
if (hoveredNode != null) {
|
||||||
|
Node node = hoveredNode;
|
||||||
|
contextMenu.AddItem(new GUIContent("Remove"), false, () => graph.RemoveNode(node));
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
public static void EndZoomed(Rect rect, float zoom) {
|
Vector2 pos = WindowToGridPosition(Event.current.mousePosition);
|
||||||
GUIUtility.ScaleAroundPivot(Vector2.one * zoom, rect.size * 0.5f);
|
for (int i = 0; i < nodeTypes.Length; i++) {
|
||||||
Vector3 offset = new Vector3(
|
Type type = nodeTypes[i];
|
||||||
(((rect.width * zoom) - rect.width) * 0.5f),
|
contextMenu.AddItem(new GUIContent(nodeTypes[i].ToString()), false, () => {
|
||||||
(((rect.height * zoom) - rect.height) * 0.5f) + (-22 * zoom)+22,
|
CreateNode(type, pos);
|
||||||
0);
|
});
|
||||||
GUI.matrix = Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void DrawGrid(Rect rect, float zoom, Vector2 panOffset) {
|
|
||||||
|
|
||||||
rect.position = Vector2.zero;
|
|
||||||
|
|
||||||
Vector2 center = rect.size / 2f;
|
|
||||||
Texture2D gridTex = NodeEditorResources.gridTexture;
|
|
||||||
Texture2D crossTex = NodeEditorResources.crossTexture;
|
|
||||||
|
|
||||||
// Offset from origin in tile units
|
|
||||||
float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width;
|
|
||||||
float yOffset = ((center.y - rect.size.y) * zoom + panOffset.y) / gridTex.height;
|
|
||||||
|
|
||||||
Vector2 tileOffset = new Vector2(xOffset, yOffset);
|
|
||||||
|
|
||||||
// Amount of tiles
|
|
||||||
float tileAmountX = Mathf.Round(rect.size.x * zoom) / gridTex.width;
|
|
||||||
float tileAmountY = Mathf.Round(rect.size.y * zoom) / gridTex.height;
|
|
||||||
|
|
||||||
Vector2 tileAmount = new Vector2(tileAmountX, tileAmountY);
|
|
||||||
|
|
||||||
// Draw tiled background
|
|
||||||
GUI.DrawTextureWithTexCoords(rect, gridTex, new Rect(tileOffset, tileAmount));
|
|
||||||
GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f,0.5f), tileAmount));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void DrawToolbar(NodeEditorWindow window) {
|
|
||||||
EditorGUILayout.BeginHorizontal("Toolbar");
|
|
||||||
|
|
||||||
if (DropdownButton("File", 50)) FileContextMenu();
|
|
||||||
if (DropdownButton("Edit", 50)) EditContextMenu(window);
|
|
||||||
if (DropdownButton("View", 50)) { }
|
|
||||||
if (DropdownButton("Settings", 70)) { }
|
|
||||||
if (DropdownButton("Tools", 50)) { }
|
|
||||||
|
|
||||||
// Make the toolbar extend all throughout the window extension.
|
|
||||||
GUILayout.FlexibleSpace();
|
|
||||||
|
|
||||||
EditorGUILayout.EndHorizontal();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool DropdownButton(string name, float width) {
|
|
||||||
return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void RightClickContextMenu(NodeEditorWindow window) {
|
|
||||||
GenericMenu contextMenu = new GenericMenu();
|
|
||||||
|
|
||||||
if (window.hoveredNode != null) {
|
|
||||||
Node node = window.hoveredNode;
|
|
||||||
contextMenu.AddItem(new GUIContent("Remove"), false, () => window.graph.RemoveNode(node));
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
Vector2 pos = window.WindowToGridPosition(Event.current.mousePosition);
|
|
||||||
for (int i = 0; i < NodeEditorReflection.nodeTypes.Length; i++) {
|
|
||||||
Type type = NodeEditorReflection.nodeTypes[i];
|
|
||||||
contextMenu.AddItem(new GUIContent(NodeEditorReflection.nodeTypes[i].ToString()), false, () => {
|
|
||||||
window.CreateNode(type, pos);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
|
|
||||||
}
|
}
|
||||||
|
contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
|
||||||
|
}
|
||||||
|
|
||||||
public static void FileContextMenu() {
|
public static void FileContextMenu() {
|
||||||
GenericMenu contextMenu = new GenericMenu();
|
GenericMenu contextMenu = new GenericMenu();
|
||||||
contextMenu.AddItem(new GUIContent("Create New"), false, null);
|
contextMenu.AddItem(new GUIContent("Create New"), false, null);
|
||||||
contextMenu.AddItem(new GUIContent("Load"), false, null);
|
contextMenu.AddItem(new GUIContent("Load"), false, null);
|
||||||
|
|
||||||
contextMenu.AddSeparator("");
|
contextMenu.AddSeparator("");
|
||||||
contextMenu.AddItem(new GUIContent("Save"), false, null);
|
contextMenu.AddItem(new GUIContent("Save"), false, null);
|
||||||
contextMenu.AddItem(new GUIContent("Save As"), false, null);
|
contextMenu.AddItem(new GUIContent("Save As"), false, null);
|
||||||
|
|
||||||
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,21 +2,19 @@
|
|||||||
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 partial class NodeEditorWindow {
|
||||||
public static class NodeEditorReflection {
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
public static Type[] GetNodeTypes() {
|
public static Type[] GetNodeTypes() {
|
||||||
//Get all classes deriving from Node via reflection
|
//Get all classes deriving from Node via reflection
|
||||||
Type derivedType = typeof(Node);
|
Type derivedType = typeof(Node);
|
||||||
Assembly assembly = Assembly.GetAssembly(derivedType);
|
Assembly assembly = Assembly.GetAssembly(derivedType);
|
||||||
return assembly.GetTypes().Where(t =>
|
return assembly.GetTypes().Where(t =>
|
||||||
t != derivedType &&
|
t != derivedType &&
|
||||||
derivedType.IsAssignableFrom(t)
|
derivedType.IsAssignableFrom(t)
|
||||||
).ToArray();
|
).ToArray();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,102 +2,101 @@
|
|||||||
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;
|
||||||
public static Texture2D crossTexture { get { return _crossTexture != null ? _crossTexture : _crossTexture = GenerateCrossTexture(); } }
|
public static Texture2D crossTexture { get { return _crossTexture != null ? _crossTexture : _crossTexture = GenerateCrossTexture(); } }
|
||||||
private static Texture2D _crossTexture;
|
private static Texture2D _crossTexture;
|
||||||
|
|
||||||
|
|
||||||
private static Color backgroundColor = new Color(0.18f, 0.18f, 0.18f);
|
private static Color backgroundColor = new Color(0.18f, 0.18f, 0.18f);
|
||||||
private static Color veinColor = new Color(0.25f, 0.25f, 0.25f);
|
private static Color veinColor = new Color(0.25f, 0.25f, 0.25f);
|
||||||
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;
|
||||||
GUIStyle outputInt, outputString, outputFloat, outputObject, outputTexture, outputColor;
|
GUIStyle outputInt, outputString, outputFloat, outputObject, outputTexture, outputColor;
|
||||||
|
|
||||||
public Styles() {
|
public Styles() {
|
||||||
inputObject = new GUIStyle((GUIStyle)"flow shader in 0");
|
inputObject = new GUIStyle((GUIStyle)"flow shader in 0");
|
||||||
inputString = new GUIStyle((GUIStyle)"flow shader in 1");
|
inputString = new GUIStyle((GUIStyle)"flow shader in 1");
|
||||||
inputInt = new GUIStyle((GUIStyle)"flow shader in 2");
|
inputInt = new GUIStyle((GUIStyle)"flow shader in 2");
|
||||||
inputFloat = new GUIStyle((GUIStyle)"flow shader in 3");
|
inputFloat = new GUIStyle((GUIStyle)"flow shader in 3");
|
||||||
inputColor = new GUIStyle((GUIStyle)"flow shader in 4");
|
inputColor = new GUIStyle((GUIStyle)"flow shader in 4");
|
||||||
inputTexture = new GUIStyle((GUIStyle)"flow shader in 5");
|
inputTexture = new GUIStyle((GUIStyle)"flow shader in 5");
|
||||||
outputObject = new GUIStyle((GUIStyle)"flow shader out 0");
|
outputObject = new GUIStyle((GUIStyle)"flow shader out 0");
|
||||||
outputString = new GUIStyle((GUIStyle)"flow shader out 1");
|
outputString = new GUIStyle((GUIStyle)"flow shader out 1");
|
||||||
outputInt = new GUIStyle((GUIStyle)"flow shader out 2");
|
outputInt = new GUIStyle((GUIStyle)"flow shader out 2");
|
||||||
outputFloat = new GUIStyle((GUIStyle)"flow shader out 3");
|
outputFloat = new GUIStyle((GUIStyle)"flow shader out 3");
|
||||||
outputColor = new GUIStyle((GUIStyle)"flow shader out 4");
|
outputColor = new GUIStyle((GUIStyle)"flow shader out 4");
|
||||||
outputTexture = new GUIStyle((GUIStyle)"flow shader out 5");
|
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 }) {
|
foreach (GUIStyle style in new GUIStyle[] { inputInt, inputString, inputFloat, inputObject, inputTexture, inputColor, outputInt, outputString, outputFloat, outputObject, outputTexture, outputColor }) {
|
||||||
style.normal.textColor = Color.black;
|
style.normal.textColor = Color.black;
|
||||||
style.fixedHeight = 18;
|
style.fixedHeight = 18;
|
||||||
style.alignment = TextAnchor.MiddleLeft;
|
style.alignment = TextAnchor.MiddleLeft;
|
||||||
style.onHover.textColor = Color.red;
|
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() {
|
public GUIStyle GetInputStyle(Type type) {
|
||||||
Texture2D tex = new Texture2D(64,64);
|
if (type == typeof(int)) return inputInt;
|
||||||
Color[] cols = new Color[64 * 64];
|
else if (type == typeof(string)) return inputString;
|
||||||
for (int y = 0; y < 64; y++) {
|
else if (type == typeof(Texture2D)) return inputTexture;
|
||||||
for (int x = 0; x < 64; x++) {
|
else if (type == typeof(float)) return inputFloat;
|
||||||
Color col = backgroundColor;
|
else if (type == typeof(Color)) return inputColor;
|
||||||
if (y % 16 == 0 || x % 16 == 0) col = veinColor;
|
else return inputObject;
|
||||||
if (y == 63 || x == 63) col = arteryColor;
|
|
||||||
cols[(y * 64) + x] = col;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tex.SetPixels(cols);
|
|
||||||
tex.wrapMode = TextureWrapMode.Repeat;
|
|
||||||
tex.filterMode = FilterMode.Bilinear;
|
|
||||||
tex.name = "Grid";
|
|
||||||
tex.Apply();
|
|
||||||
return tex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Texture2D GenerateCrossTexture() {
|
public GUIStyle GetOutputStyle(Type type) {
|
||||||
Texture2D tex = new Texture2D(64, 64);
|
if (type == typeof(int)) return outputInt;
|
||||||
Color[] cols = new Color[64 * 64];
|
else if (type == typeof(string)) return outputString;
|
||||||
for (int y = 0; y < 64; y++) {
|
else if (type == typeof(Texture2D)) return outputTexture;
|
||||||
for (int x = 0; x < 64; x++) {
|
else if (type == typeof(float)) return outputFloat;
|
||||||
Color col = crossColor;
|
else if (type == typeof(Color)) return outputColor;
|
||||||
if (y != 31 && x != 31) col.a = 0;
|
else return outputObject;
|
||||||
cols[(y * 64) + x] = col;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tex.SetPixels(cols);
|
|
||||||
tex.wrapMode = TextureWrapMode.Clamp;
|
|
||||||
tex.filterMode = FilterMode.Bilinear;
|
|
||||||
tex.name = "Grid";
|
|
||||||
tex.Apply();
|
|
||||||
return tex;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Texture2D GenerateGridTexture() {
|
||||||
|
Texture2D tex = new Texture2D(64,64);
|
||||||
|
Color[] cols = new Color[64 * 64];
|
||||||
|
for (int y = 0; y < 64; y++) {
|
||||||
|
for (int x = 0; x < 64; x++) {
|
||||||
|
Color col = backgroundColor;
|
||||||
|
if (y % 16 == 0 || x % 16 == 0) col = veinColor;
|
||||||
|
if (y == 63 || x == 63) col = arteryColor;
|
||||||
|
cols[(y * 64) + x] = col;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tex.SetPixels(cols);
|
||||||
|
tex.wrapMode = TextureWrapMode.Repeat;
|
||||||
|
tex.filterMode = FilterMode.Bilinear;
|
||||||
|
tex.name = "Grid";
|
||||||
|
tex.Apply();
|
||||||
|
return tex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Texture2D GenerateCrossTexture() {
|
||||||
|
Texture2D tex = new Texture2D(64, 64);
|
||||||
|
Color[] cols = new Color[64 * 64];
|
||||||
|
for (int y = 0; y < 64; y++) {
|
||||||
|
for (int x = 0; x < 64; x++) {
|
||||||
|
Color col = crossColor;
|
||||||
|
if (y != 31 && x != 31) col.a = 0;
|
||||||
|
cols[(y * 64) + x] = col;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tex.SetPixels(cols);
|
||||||
|
tex.wrapMode = TextureWrapMode.Clamp;
|
||||||
|
tex.filterMode = FilterMode.Bilinear;
|
||||||
|
tex.name = "Grid";
|
||||||
|
tex.Apply();
|
||||||
|
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