/// <summary> /// draw view GUI, buttons and all /// </summary> public override void DrawView(Rect editorRect, Rect PercentRect, Event e, NodeGraph graph) { base.DrawView(editorRect, PercentRect, e, graph); ProcessEvents(e); if (graph != null) { title = graph.name; } else { title = GetTitle(); } GUI.DrawTextureWithTexCoords(new Rect(body.position + pan, body.size), backgroundTexture, new Rect(0, 0, body.width / backgroundTexture.width, body.height / backgroundTexture.height)); GUI.Box(body, title, skin.GetStyle("WorkViewBackground")); if (graph != null) { float btnH = 25, btnMY = 10; graph.DrawGraph(e, body); if (GUI.Button(new Rect(0, editorRect.y + btnMY, 80, btnH), "Compile")) { graph.Compile(); } GUI.Toggle(new Rect(100, editorRect.y + btnMY, 200, btnH), graph.compiled, "Has Compiled: "); if (GUI.Button(new Rect(0, editorRect.y + btnH + 2 * btnMY, 80, btnH), "Save")) { NodeUtilities.UpdateGraph(graph); } if (GUI.Button(new Rect(0, editorRect.y + 2 * (btnH + 2 * btnMY), 80, btnH), "Resize")) { foreach (NodeBase n in graph.nodes) { n.Resize(true); } } } if (SelectedPin != null) { // is a window currently showing? if so, don't constantly draw to mouse pos if (NCPopup != null) { DrawConnectionToMouse(NCPopup.mouseLoc); } else if (window.nodeCreateView != null) { DrawConnectionToMouse(window.nodeCreateView.mouseLoc); } else { DrawConnectionToMouse(e.mousePosition); } } }
private void Promote() { Variable var = new Variable(varName, pinToAttach.varType, null, scope); // put variable into appropiate dictionary if (scope == Variable.VarScope.Local) { graph.CreateVariable(var); } else { } // create node Vector2 off = new Vector2(10, 0); off *= pinToAttach.isInput ? -1 : 1; Vector2 loc = pinToAttach.Center + off; VarNode node = NodeUtilities.CreateNode(graph, var, !pinToAttach.isInput, loc); // establish connection between selected pin and nodes pin if (pinToAttach != null) { graph.ConnectPins(pinToAttach.isInput ? (InputPin)pinToAttach : node.VIP[0], pinToAttach.isInput ? node.VOP[0] : (OutputPin)pinToAttach); } //// establish connection between selected pin and first input/output of matching type //// e.g. float to float //if (pinToAttach != null) { // if (!pinToAttach.isInput) { // foreach (InputPin ip in node.inPins) // if (ip.varType == pinToAttach.varType) { // graph.ConnectPins(ip, (OutputPin)pinToAttach); // break; // } // } else { // foreach (OutputPin op in node.outPins) // if (op.varType == pinToAttach.varType) { // graph.ConnectPins((InputPin)pinToAttach, op); // break; // } // } //} }
private void NewScript(GameObject obj) { string graphName; string key = Path.Contains("/") ? "/" : "\\"; int kI = Path.Contains(key) ? Path.LastIndexOf(key) + 1 : 0, aI = Path.IndexOf(".asset"); if (aI > 0) { graphName = Path.Substring(kI, aI - kI); Path = Path.Replace(graphName, "").Replace(".asset", ""); NodeUtilities.CreateNodeGraph(graphName, Path.Substring(Path.IndexOf("Assets/"))); if (obj != null) { // attach script to object?? } } }
public override void DrawView(Rect editorRect, Rect PercentRect, Event e, NodeGraph graph) { base.DrawView(editorRect, PercentRect, e, graph); ProcessEvents(e); if (graph != null) { title = graph.name; } else { title = GetTitle(); } try { GUI.Box(body, "", skin.GetStyle("HeaderViewBackground")); GUILayout.BeginArea(body); { GUILayout.BeginHorizontal(); if (GUILayout.Button("New Script", skin.GetStyle("HeaderButton"), GUILayout.Width(80), GUILayout.Height(body.height))) { GraphCreatePopup.Init(); } if (graph != null) { if (GUILayout.Button("Delete Script", skin.GetStyle("HeaderButton"), GUILayout.Width(80), GUILayout.Height(body.height))) { NodeUtilities.DeleteScript(graph); } } GUILayout.EndHorizontal(); } GUILayout.EndArea(); } catch { } }
/// <remarks>TODO: this function takes up most of Unity's OnGui call, leaving no time for anything else. /// Please optimize! </remarks> public override void ProcessEvents(Event e) { base.ProcessEvents(e); Vector2 mousePos = e.mousePosition; NodeBase node = (graph != null) ? graph.InsideNode(mousePos) : null; NodePin pin = null, txtPin = null; if (node != null) { pin = node.InsidePin(mousePos); txtPin = node.InsidePinText(mousePos); } // pan the view //***Not highest priority for optimization if (body.Contains(e.mousePosition)) { //pan += e.delta; if (e.type == EventType.MouseDrag && e.button == 2) { foreach (var n in graph.nodes) { n.Pan(e.delta); } } } // zoom the view //if (e.type == EventType.ScrollWheel) { // Debug.Log(e.delta); // float tmp = StaticMethods.Clamp(window.zoomScale + e.delta.y, 1.0f / 25.0f, 2.0f, true); // Debug.Log(tmp); // window.zoomScale = tmp; //} // toolTip if (window.nodeCreateView == null) { if (window.toolTipView == null) { if (node != null) { if (pin == null && txtPin == null) { window.toolTipView = new NodeToolTipView(node, node.description); } else { if (SelectedPin != null && pin != null) { // check if selected input pin can be cast to the highlighted output pin and vice versa // if check is valid, show a tooltip if (!SelectedPin.GetType().Equals(pin.GetType()) && pin.varType != SelectedPin.varType) { if (ControlNode.castables.ContainsKey(SelectedPin.varType.ToString())) { if (ControlNode.castables[SelectedPin.varType.ToString()].Contains(pin.varType.ToString())) { window.toolTipView = new NodeToolTipView(pin, ("Cast " + Enum.GetName(typeof(VarType), SelectedPin.varType) + " to " + Enum.GetName(typeof(VarType), pin.varType))); } } } } else { NodePin p = pin == null ? txtPin : pin; if (!String.IsNullOrEmpty(p.Description)) { window.toolTipView = new NodeToolTipView(p, p.Description); } } } } else if (SelectedPin != null) { window.toolTipView = new NodeToolTipView(null, "Create New Node"); } } else { if (pin != null) { if (!String.IsNullOrEmpty(pin.Description) && window.toolTipView.Parent != pin) { window.toolTipView = new NodeToolTipView(pin, pin.Description); } } } } // hit escape // deselect the current pin if (e.keyCode == KeyCode.Escape) { if (SelectedPin != null) { SelectedPin = null; } } // hold shift // activate shift key relate functions if (e.keyCode == KeyCode.LeftShift || e.keyCode == KeyCode.RightShift) { shift = (e.type == EventType.KeyDown); } // press delete // delete the current node if (graph != null) { if (e.keyCode == KeyCode.Delete && graph.SelectedNode != null) { graph.DeleteNode(graph.SelectedNode); graph.SelectedNode = null; Selection.activeObject = null; } } if (e.type == EventType.MouseDrag) { window.toolTipView = null; } // right click a thing // show a context menu if (window.nodeCreateView == null && e.type == EventType.ContextClick) { GenericMenu menu = new GenericMenu(); if (graph != null) { if (node != null) { if (pin != null) { //Debug.Log("Right Clicked a PIN"); // pin connection menu //break all connections if (pin.IsConnected) { menu.AddItem(new GUIContent("Break Connection"), false, NodeBase.RemoveConnection, pin); { //break individual Conections //if (pin.isInput) { // foreach (OutputPin n in pin.node.outPins) { // menu.AddItem(new GUIContent("Break Connection to " + pin.ConName()), false, node.RemovePin, "pin obj?"); // } //} else { // foreach (InputPin n in pin.node.inPins) { // menu.AddItem(new GUIContent("Break Connection to " + pin.ConName()), false, node.RemovePin, "pin obj?"); // //} } //add separator menu.AddSeparator(""); } // Promote input to variable Node if (!pin.IsConnected && pin.isInput && pin.varType != VarType.Exec) { menu.AddItem(new GUIContent("Promote to Variable"), false, PromoteVariable, pin); } } // cut, copy, duplicate, delete if (!node.GetType().Equals(typeof(StartNode))) { menu.AddItem(new GUIContent("Cut Node"), false, CutNode, node); menu.AddItem(new GUIContent("Copy Node"), false, CopyNode, node); menu.AddItem(new GUIContent("Duplicate Node"), false, DupeNode, node); menu.AddItem(new GUIContent("Delete Node"), false, graph.DeleteNode, node); } else { menu.AddDisabledItem(new GUIContent("Cut Node")); menu.AddDisabledItem(new GUIContent("Copy Node")); menu.AddDisabledItem(new GUIContent("Duplicate Node")); menu.AddDisabledItem(new GUIContent("Delete Node")); } menu.ShowAsContext(); Event.current.Use(); } else if (window.nodeCreateView == null) { // add node creation window //Debug.Log("Right Clicked nowhere"); window.nodeCreateView = new NodeCreateView(mousePos, SelectedPin); window.toolTipView = null; //SelectedPin = null; } else { //Debug.Log("NCV: " + new Rect(window.nodeCreateView.mouseLoc, window.nodeCreateView.size)); } } } // left click a pin //***high priority for optimization if (window.nodeCreateView == null && e.button == 0 && e.type == EventType.MouseDown) { if (node != null) { if (pin != null) { if (SelectedPin != null) { // must connect inputs to outputs if (!SelectedPin.GetType().Equals(pin.GetType())) { if (pin.varType == SelectedPin.varType) { if (pin.parentNode != SelectedPin.parentNode) { //if (pin.isConnected && pin.isInput && pin.varType==VarType.Exec) // NodeBase.RemoveConnection(pin); if (SelectedPin.isInput) { graph.ConnectPins((InputPin)SelectedPin, (OutputPin)pin); } else { graph.ConnectPins((InputPin)pin, (OutputPin)SelectedPin); } } } else { // if vartype of selectedPin can be cast to clicked pin, if (ControlNode.castables.ContainsKey(SelectedPin.varType.ToString())) { if (ControlNode.castables[SelectedPin.varType.ToString()].Contains(pin.varType.ToString())) { //Debug.Log("sPin can be cast to Pin"); // create a cast node ControlNode CN; if (SelectedPin.isInput) { CN = (ControlNode)NodeUtilities.CreateNode(graph, pin.varType, SelectedPin.varType, e.mousePosition); //CN.outPins[0].ConnectedInputID = SelectedPin.node.inPins.IndexOf((InputPin)SelectedPin); //((OutputPin)pin).ConnectedInputID = 0; graph.ConnectPins((InputPin)SelectedPin, CN.VOP[0]); graph.ConnectPins(CN.VIP[0], (OutputPin)pin); } else { CN = (ControlNode)NodeUtilities.CreateNode(graph, SelectedPin.varType, pin.varType, e.mousePosition); //CN.outPins[0].ConnectedInputID = pin.node.inPins.IndexOf((InputPin)pin); //((OutputPin)SelectedPin).ConnectedInputID = 0; graph.ConnectPins((InputPin)pin, CN.VOP[0]); graph.ConnectPins(CN.VIP[0], (OutputPin)SelectedPin); } } } } SelectedPin = null; } } else { SelectedPin = pin; } } } else { if (SelectedPin != null) { SelectedPin = null; window.toolTipView = null; } } } }
public void CreateNode(NodeGraph GraphObj, NodeType nT, VarType pT, object subType, int nodeCount = 2) { NodeBase node = null; switch (nT) { case NodeType.Math: node = NodeUtilities.CreateNode(GraphObj, NodeType.Math, subType, pT, mouseLoc, nodeCount); break; case NodeType.Event: node = NodeUtilities.CreateNode(GraphObj, nT, subType, mouseLoc); break; case NodeType.Fetch: break; case NodeType.Control: //Debug.Log("I control the world"); if ((ControlNode.ControlType)subType != ControlNode.ControlType.Cast) { node = NodeUtilities.CreateNode(GraphObj, nT, subType, mouseLoc); } break; case NodeType.Function: node = NodeUtilities.CreateNode(GraphObj, nT, subType, pT, mouseLoc, nodeCount); break; } //Debug.Log(node); // establish connection between selected pin and first input/output of matching type // e.g. float to float if (pinToAttach != null) { if (!pinToAttach.isInput) { foreach (InputPin ip in node.InPins) { if (ip.varType == pinToAttach.varType) { graph.ConnectPins(ip, (OutputPin)pinToAttach); break; } } } else { foreach (OutputPin op in node.OutPins) { if (op.varType == pinToAttach.varType) { graph.ConnectPins((InputPin)pinToAttach, op); break; } } } } Destroy(false); }
/// <summary> /// Create the node using NodeUtilities /// </summary> public void CreateNode() { NodeBase node = null; switch (nodeType) { case NodeType.Math: VarType pT = (VarType)Enum.Parse(typeof(VarType), posInp[(int)selectedIndex.x]); //Debug.Log(GraphObj + "," + subType + ", " + pT); node = NodeUtilities.CreateNode(GraphObj, NodeType.Math, subType, pT, mouseLoc, nodeCount); break; case NodeType.Fetch: break; case NodeType.Control: if ((ControlNode.ControlType)subType != ControlNode.ControlType.Cast) { node = NodeUtilities.CreateNode(GraphObj, nodeType, subType, mouseLoc); } else { node = NodeUtilities.CreateNode(GraphObj, (VarType)Enum.Parse(typeof(VarType), posInp[(int)selectedIndex.x]), (VarType)Enum.Parse(typeof(VarType), ControlNode.castables[posInp[(int)selectedIndex.x]][(int)selectedIndex.y]), mouseLoc); } break; } // establish connection between selected pin and first input/output of matching type // e.g. float to float if (pinToAttach != null) { if (!pinToAttach.isInput) { foreach (InputPin ip in node.InPins) { if (ip.varType == pinToAttach.varType) { GraphObj.ConnectPins(ip, (OutputPin)pinToAttach); break; } } } else { foreach (OutputPin op in node.OutPins) { if (op.varType == pinToAttach.varType) { GraphObj.ConnectPins((InputPin)pinToAttach, op); break; } } } } Instance.Close(); }