public virtual void OnRemoveConnection(NodeInput input) { }
/// <summary> /// Call this method to create an input on your node /// </summary> public void CreateInput(string inputName, string inputType) { NodeInput.Create(this, inputName, inputType); }
/// <summary> /// Processes input events /// </summary> public static void InputEvents() { Event e = Event.current; mousePos = e.mousePosition; bool leftClick = e.button == 0, rightClick = e.button == 1, mouseDown = e.type == EventType.MouseDown, mousUp = e.type == EventType.MouseUp; if (ignoreInput(mousePos)) { return; } #region Change Node selection and focus // Choose focused and selected Node, accounting for focus changes curEditorState.focusedNode = null; if (mouseDown || mousUp) { curEditorState.focusedNode = NodeEditor.NodeAtPosition(mousePos); if (curEditorState.focusedNode != curEditorState.selectedNode) { unfocusControls = true; } if (mouseDown && leftClick) { curEditorState.selectedNode = curEditorState.focusedNode; RepaintClients(); } } // Perform above mentioned focus changes in Repaint, which is the only suitable time to do this if (unfocusControls && Event.current.type == EventType.Repaint) { GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; unfocusControls = false; } #if UNITY_EDITOR if (curEditorState.focusedNode != null) { UnityEditor.Selection.activeObject = curEditorState.focusedNode; } #endif #endregion switch (e.type) { case EventType.MouseDown: curEditorState.dragNode = false; curEditorState.panWindow = false; if (curEditorState.focusedNode != null) { // Clicked a Node if (rightClick) { // Node Context Click GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Delete Node"), false, ContextCallback, new NodeEditorMenuCallback("deleteNode", curNodeCanvas, curEditorState)); menu.AddItem(new GUIContent("Duplicate Node"), false, ContextCallback, new NodeEditorMenuCallback("duplicateNode", curNodeCanvas, curEditorState)); if (curEditorState.focusedNode.AcceptsTranstitions) { menu.AddSeparator("Seperator"); menu.AddItem(new GUIContent("Make Transition"), false, ContextCallback, new NodeEditorMenuCallback("startTransition", curNodeCanvas, curEditorState)); } menu.ShowAsContext(); e.Use(); } else if (leftClick) { // Detect click on a connection knob if (!CanvasGUIToScreenRect(curEditorState.focusedNode.rect).Contains(mousePos)) { // Clicked NodeEdge, check Node Inputs and Outputs NodeOutput nodeOutput = curEditorState.focusedNode.GetOutputAtPos(e.mousePosition); if (nodeOutput != null) { // Output clicked -> New Connection drawn from this curEditorState.connectOutput = nodeOutput; e.Use(); return; } NodeInput nodeInput = curEditorState.focusedNode.GetInputAtPos(e.mousePosition); if (nodeInput != null && nodeInput.connection != null) { // Input clicked -> Loose and edit Connection // TODO: Draw input from NodeInput curEditorState.connectOutput = nodeInput.connection; nodeInput.RemoveConnection(); e.Use(); } } } } else { // Clicked on canvas // NOTE: Panning is not done here but in LateEvents, so buttons on the canvas won't be blocked when clicking if (rightClick) { // Editor Context Click GenericMenu menu = new GenericMenu(); if (curEditorState.connectOutput != null) { // A connection is drawn, so provide a context menu with apropriate nodes to auto-connect foreach (Node node in NodeTypes.nodes.Keys) { // Iterate through all nodes and check for compability foreach (NodeInput input in node.Inputs) { if (input.type == curEditorState.connectOutput.type) { menu.AddItem(new GUIContent("Add " + NodeTypes.nodes[node].adress), false, ContextCallback, new NodeEditorMenuCallback(node.GetID, curNodeCanvas, curEditorState)); break; } } } } else if (curEditorState.makeTransition != null && curEditorState.makeTransition.AcceptsTranstitions) { // A transition is drawn, so provide a context menu with nodes to auto-connect foreach (Node node in NodeTypes.nodes.Keys) { // Iterate through all nodes and check for compability if (node.AcceptsTranstitions) { menu.AddItem(new GUIContent("Add " + NodeTypes.nodes[node].adress), false, ContextCallback, new NodeEditorMenuCallback(node.GetID, curNodeCanvas, curEditorState)); } } } else { // Ordinary context click, add all nodes to add foreach (Node node in NodeTypes.nodes.Keys) { menu.AddItem(new GUIContent("Add " + NodeTypes.nodes [node].adress), false, ContextCallback, new NodeEditorMenuCallback(node.GetID, curNodeCanvas, curEditorState)); } } menu.ShowAsContext(); e.Use(); } } break; case EventType.MouseUp: if (curEditorState.focusedNode != null && curEditorState.connectOutput != null) { // Apply Drawn connections on node if theres a clicked input if (!curEditorState.focusedNode.Outputs.Contains(curEditorState.connectOutput)) { // An input was clicked, it'll will now be connected NodeInput clickedInput = curEditorState.focusedNode.GetInputAtPos(e.mousePosition); if (clickedInput.CanApplyConnection(curEditorState.connectOutput)) { // It can connect (type is equals, it does not cause recursion, ...) clickedInput.ApplyConnection(curEditorState.connectOutput); } } e.Use(); } curEditorState.makeTransition = null; curEditorState.connectOutput = null; curEditorState.dragNode = false; curEditorState.panWindow = false; break; case EventType.ScrollWheel: // Apply Zoom curEditorState.zoom = (float)Math.Round(Math.Min(2.0f, Math.Max(0.6f, curEditorState.zoom + e.delta.y / 15)), 2); RepaintClients(); break; case EventType.KeyDown: // TODO: Node Editor: Shortcuts if (e.keyCode == KeyCode.N) // Start Navigating (curve to origin / active Node) { curEditorState.navigate = true; } if (e.keyCode == KeyCode.LeftControl && curEditorState.selectedNode != null) { // Snap selected Node's position to multiples of 10 Vector2 pos = curEditorState.selectedNode.rect.position; pos = (pos - curEditorState.panOffset) / 10; pos = new Vector2(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y)); curEditorState.selectedNode.rect.position = pos * 10 + curEditorState.panOffset; } RepaintClients(); break; case EventType.KeyUp: if (e.keyCode == KeyCode.N) // Stop Navigating { curEditorState.navigate = false; } RepaintClients(); break; case EventType.MouseDrag: if (curEditorState.panWindow) { // Scroll everything with the current mouse delta curEditorState.panOffset += e.delta * curEditorState.zoom; foreach (Node node in curNodeCanvas.nodes) { node.rect.position += e.delta * curEditorState.zoom; } e.delta = Vector2.zero; RepaintClients(); } if (curEditorState.dragNode && curEditorState.selectedNode != null && GUIUtility.hotControl == 0) { // Drag the active node with the current mouse delta curEditorState.selectedNode.rect.position += e.delta * curEditorState.zoom; NodeEditorCallbacks.IssueOnMoveNode(curEditorState.selectedNode); e.delta = Vector2.zero; RepaintClients(); } else { curEditorState.dragNode = false; } break; } }
// Connection public virtual void OnAddConnection(NodeInput input) { }
/// <summary> /// Creates and input on this Node of the given type at the specified NodeSide and position. /// </summary> public void CreateInput(string inputName, string inputType, NodeSide nodeSide, float sidePosition) { NodeInput.Create(this, inputName, inputType, nodeSide, sidePosition); }
/// <summary> /// Callback when the NodeInput was assigned a new connection /// </summary> protected internal virtual void OnAddInputConnection(NodeInput input) { }
/// <summary> /// Will validate this canvas for any broken nodes or references and cleans them. /// </summary> public void Validate() { if (nodes == null) { Debug.LogWarning("NodeCanvas '" + name + "' nodes were erased and set to null! Automatically fixed!"); nodes = new List <Node> (); } for (int nodeCnt = 0; nodeCnt < nodes.Count; nodeCnt++) { Node node = nodes[nodeCnt]; if (node == null) { Debug.LogWarning("NodeCanvas '" + name + "' contained broken (null) nodes! Automatically fixed!"); nodes.RemoveAt(nodeCnt); nodeCnt--; continue; } for (int knobCnt = 0; knobCnt < node.nodeKnobs.Count; knobCnt++) { NodeKnob nodeKnob = node.nodeKnobs[knobCnt]; if (nodeKnob == null) { Debug.LogWarning("NodeCanvas '" + name + "' Node '" + node.name + "' contained broken (null) NodeKnobs! Automatically fixed!"); node.nodeKnobs.RemoveAt(knobCnt); knobCnt--; continue; } if (nodeKnob is NodeInput) { NodeInput input = nodeKnob as NodeInput; if (input.connection != null && input.connection.body == null) { // References broken node; Clear connection input.connection = null; } // for (int conCnt = 0; conCnt < (nodeKnob as NodeInput).connection.Count; conCnt++) } else if (nodeKnob is NodeOutput) { NodeOutput output = nodeKnob as NodeOutput; for (int conCnt = 0; conCnt < output.connections.Count; conCnt++) { NodeInput con = output.connections[conCnt]; if (con == null || con.body == null) { // Broken connection; Clear connection output.connections.RemoveAt(conCnt); conCnt--; } } } } } if (editorStates == null) { Debug.LogWarning("NodeCanvas '" + name + "' editorStates were erased! Automatically fixed!"); editorStates = new NodeEditorState[0]; } editorStates = editorStates.Where((NodeEditorState state) => state != null).ToArray(); foreach (NodeEditorState state in editorStates) { if (!nodes.Contains(state.selectedNode)) { state.selectedNode = null; } } }
/// <summary> /// Creates and input on this Node of the given type at the specified NodeSide and position. /// </summary> public NodeInput CreateInput(string inputName, string inputType, NodeSide nodeSide, float sidePosition) { return(NodeInput.Create(this, inputName, inputType, nodeSide, sidePosition)); }
/// <summary> /// Creates and input on this Node of the given type at the specified NodeSide. /// </summary> public NodeInput CreateInput(string inputName, string inputType, NodeSide nodeSide) { return(NodeInput.Create(this, inputName, inputType, nodeSide)); }