private static void DrawSocket(NodeSocket socket) { Rect socketRect = TerraUtility.GetSocketRect(socket); if (socket.SocketType == SocketType.Output) { foreach (SocketConnection connection in socket.Connections) { DrawConnection(socketRect.center, TerraUtility.GetSocketRect(connection.Socket).center, TerraUtility.TypeToColor(socket.FieldType)); } } Handles.DrawSolidRectangleWithOutline(socketRect, Color.white, new Color(0, 0, 0, 0.6f)); Handles.DrawSolidRectangleWithOutline(new Rect(socket.SocketType == SocketType.Input ? socketRect.position : (socketRect.position + new Vector2(socketRect.size.x, 0) - new Vector2(socketRect.size.x * 0.3f, 0)), new Vector2(socketRect.size.x * 0.4f, socketRect.size.y)), TerraUtility.TypeToColor(socket.FieldType), new Color(0, 0, 0, 0.6f)); if (socket.UseField) { DrawSocketField(socket, socketRect); } else { GUI.enabled = false; GUIStyle textStyle = new GUIStyle(EditorStyles.centeredGreyMiniLabel); textStyle.alignment = socket.SocketType == SocketType.Input ? TextAnchor.MiddleLeft : TextAnchor.MiddleRight; GUI.TextField(new Rect(socketRect.position + (socket.SocketType == SocketType.Input ? new Vector2(15, -4) : new Vector2(-105, -4)), new Vector2(100, 16)), socket.FieldName, textStyle); GUI.enabled = true; } }
public static void DrawComment(GraphComment comment) { Handles.DrawSolidRectangleWithOutline(comment.Rect, comment.Color, new Color(1, 1, 1, 0.5f)); comment.Comment = GUI.TextField(new Rect(comment.Rect.position, new Vector2(comment.Rect.width, 16)), comment.Comment); comment.Color = EditorGUI.ColorField(new Rect(new Vector2(comment.Rect.position.x, comment.Rect.position.y + 16), new Vector2(comment.Rect.width, 6)), GUIContent.none, comment.Color, false, false, false); GUI.Box(TerraUtility.GetCommentResizeRect(comment), GUIContent.none); }
public static void ProcessSocketEvent(Event e, NodeSocket socket) { foreach (SocketConnection connection in socket.Connections) { ProcessConnectionEvent(e, socket, connection); } switch (e.type) { case EventType.MouseDown: if (e.button == 0 && TerraUtility.GetSocketRect(socket).Contains(e.mousePosition)) { TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.CommentSelection.Clear(); TerraGraphEditor.HeldSocket = socket; GUI.FocusControl(null); e.Use(); } break; case EventType.MouseUp: if (e.button == 0 && TerraUtility.GetSocketRect(socket).Contains(e.mousePosition) && TerraGraphEditor.HeldSocket != null) { socket.Connect(TerraGraphEditor.HeldSocket); TerraGraphEditor.HeldSocket.Connect(socket); TerraGraphEditor.HeldSocket = null; e.Use(); } break; } }
public static void ProcessNodeEvent(Event e, TerraNode node) { foreach (NodeSocket socket in node.Sockets) { ProcessSocketEvent(e, socket); } switch (e.type) { case EventType.MouseDown: if (e.button == 0) { if (node.Rect.Contains(e.mousePosition)) { if (!TerraGraphEditor.NodeSelection.Contains(node)) { if (!e.shift) { TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.CommentSelection.Clear(); } TerraGraphEditor.NodeSelection.Add(node); } else { if (e.shift) { TerraGraphEditor.NodeSelection.Remove(node); } } GUI.FocusControl(null); e.Use(); } } if (e.button == 1) { if (node.Rect.Contains(e.mousePosition)) { TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.CommentSelection.Clear(); GUI.FocusControl(null); GenericMenu contextMenu = TerraUtility.CreateNodeContextMenu((t) => TerraGraphEditor.Window.CreateNode(t, e.mousePosition)); contextMenu.AddSeparator(""); contextMenu.AddItem(new GUIContent("Remove Node"), false, () => TerraGraphEditor.Window.RemoveNode(node)); contextMenu.ShowAsContext(); e.Use(); } } break; } TerraUtility.BuildNodeCursorRects(node); }
public void CreateNode(Type type, Vector2 position) { TerraNode node = Graph.AddNode(type); TerraUtility.BuildNodeSockets(node); node.SetPosition(position - node.Rect.size * 0.5f); AssetDatabase.AddObjectToAsset(node, Graph); EditorUtility.SetDirty(Graph); EditorUtility.SetDirty(node); }
public static void ProcessConnectionEvent(Event e, NodeSocket socket, SocketConnection connection) { if (socket.SocketType == SocketType.Output) { Vector2 center = (TerraUtility.GetSocketRect(socket).center + TerraUtility.GetSocketRect(connection.Socket).center) * 0.5f; if (Handles.Button(center, Quaternion.identity, 4, 8, Handles.RectangleHandleCap)) { connection.Socket.Disconnect(socket); } } }
private void DrawActiveConnection() { if (HeldSocket == null) { return; } Vector2 output = HeldSocket.SocketType == SocketType.Output ? TerraUtility.GetSocketRect(HeldSocket).center : Event.current.mousePosition; Vector2 input = HeldSocket.SocketType == SocketType.Input ? TerraUtility.GetSocketRect(HeldSocket).center : Event.current.mousePosition; TerraGUI.DrawConnection(output, input, TerraUtility.TypeToColor(HeldSocket.FieldType)); }
public static void DrawNode(TerraNode node) { //if(TerraUtility.TryGetTooltip(node.GetType(), out string tooltip)) GUI.Box(node.Rect, new GUIContent("", TerraUtility.GetNodeTooltip(node.GetType()))); Handles.DrawSolidRectangleWithOutline(node.Rect, Color.white, new Color(0, 0, 0, 0.6f)); GUI.enabled = false; GUI.TextField(new Rect(node.Rect.position, new Vector2(node.Rect.width, 20)), ""); GUI.enabled = true; GUI.Label(new Rect(node.Rect.position, new Vector2(node.Rect.width, 20)), node.GetType().Name, EditorStyles.centeredGreyMiniLabel); foreach (NodeSocket socket in node.Sockets) { DrawSocket(socket); } }
public static void ProcessGraphEditorEvents(Event e, TerraGraphEditor window) { switch (e.type) { case EventType.KeyDown: if (e.keyCode == KeyCode.Delete) { foreach (TerraNode node in TerraGraphEditor.NodeSelection) { window.RemoveNode(node); } foreach (GraphComment comment in TerraGraphEditor.CommentSelection) { TerraGraphEditor.Graph.RemoveComment(comment); } TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.CommentSelection.Clear(); TerraGraphEditor.Selecting = false; e.Use(); } if (e.keyCode == KeyCode.F) { if (TerraGraphEditor.NodeSelection.Count != 0 || TerraGraphEditor.CommentSelection.Count != 0) { Vector2 currentCenter = TerraGraphEditor.Window.position.size * 0.5f; Vector2 targetCenter = TerraGraphEditor.NodeSelection[0].Rect.center; Vector2 offset = currentCenter - targetCenter; foreach (TerraNode node in TerraGraphEditor.Graph.Nodes) { node.SetPosition(node.Rect.position + offset); } foreach (GraphComment comment in TerraGraphEditor.Graph.Comments) { comment.SetPosition(comment.Rect.position + offset); } TerraGUI.ApplyGridOffset(offset); e.Use(); } } break; case EventType.MouseDown: if (e.button == 0) { TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.CommentSelection.Clear(); TerraGraphEditor.SelectionStart = e.mousePosition; TerraGraphEditor.Selecting = true; GUI.FocusControl(null); e.Use(); } if (e.button == 1) { TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.CommentSelection.Clear(); GUI.FocusControl(null); GenericMenu contextMenu = TerraUtility.CreateNodeContextMenu((t) => window.CreateNode(t, e.mousePosition)); contextMenu.AddSeparator(""); contextMenu.AddItem(new GUIContent("Add Comment"), false, () => TerraGraphEditor.Window.CreateComment(e.mousePosition)); contextMenu.ShowAsContext(); e.Use(); } break; case EventType.MouseUp: if (e.button == 0) { TerraGraphEditor.HeldSocket = null; TerraGraphEditor.ResizingComment = null; TerraGraphEditor.Selecting = false; e.Use(); } break; case EventType.MouseDrag: if (e.button == 0) { if (!new Rect(new Vector2(0, 0), window.position.size).Contains(e.mousePosition)) { TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.CommentSelection.Clear(); TerraGraphEditor.Selecting = false; TerraGraphEditor.ResizingComment = null; TerraGraphEditor.HeldSocket = null; GUI.FocusControl(null); e.Use(); } if (TerraGraphEditor.ResizingComment != null) { TerraGraphEditor.ResizingComment.SetSize(TerraGraphEditor.ResizingComment.Rect.size + e.delta); e.Use(); } else if (!TerraGraphEditor.Selecting) { foreach (TerraNode node in TerraGraphEditor.NodeSelection) { node.SetPosition(node.Rect.position + e.delta); } foreach (GraphComment comment in TerraGraphEditor.CommentSelection) { comment.SetPosition(comment.Rect.position + e.delta); } e.Use(); } } if (e.button == 2) { TerraGUI.ApplyGridOffset(e.delta); foreach (TerraNode node in TerraGraphEditor.Graph.Nodes) { node.SetPosition(node.Rect.position + e.delta); } foreach (GraphComment comment in TerraGraphEditor.Graph.Comments) { comment.SetPosition(comment.Rect.position + e.delta); } e.Use(); } break; } }
public static void ProcessCommentEvent(Event e, GraphComment comment) { switch (e.type) { case EventType.MouseDown: if (e.button == 0) { if (TerraUtility.GetCommentResizeRect(comment).Contains(e.mousePosition)) { TerraGraphEditor.CommentSelection.Clear(); TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.ResizingComment = comment; GUI.FocusControl(null); e.Use(); } else if (comment.Rect.Contains(e.mousePosition)) { if (!TerraGraphEditor.CommentSelection.Contains(comment)) { if (!e.shift) { TerraGraphEditor.CommentSelection.Clear(); TerraGraphEditor.NodeSelection.Clear(); } TerraGraphEditor.CommentSelection.Add(comment); } else { if (e.shift) { TerraGraphEditor.CommentSelection.Remove(comment); } } GUI.FocusControl(null); e.Use(); } } if (e.button == 1) { if (comment.Rect.Contains(e.mousePosition)) { TerraGraphEditor.NodeSelection.Clear(); TerraGraphEditor.CommentSelection.Clear(); GenericMenu contextMenu = TerraUtility.CreateNodeContextMenu((t) => TerraGraphEditor.Window.CreateNode(t, e.mousePosition)); contextMenu.AddSeparator(""); contextMenu.AddItem(new GUIContent("Remove Comment"), false, () => TerraGraphEditor.Graph.RemoveComment(comment)); contextMenu.ShowAsContext(); e.Use(); } } break; } TerraUtility.BuildCommentCursorRects(comment); }