/// <summary> /// Gets Handle struct of clicked Connector of any Node /// </summary> /// <returns>ConHandle at current mouseposition. If there's no Connector at current position, the returned ConHandle is not active</returns> /// <param name="pos">Position of mouse cursor</param> public ConHandle ConAtPosition(Vector2 pos) { ConHandle handle = new ConHandle { active = false }; foreach (Node n in coreSystem.nodes) { foreach (Connector c in n.connectors) { if (c.visible && c.GetRect().Contains(pos)) { handle.active = true; handle.node = n; handle.connector = c; handle.position = c.GetRect().center; handle.left = c.input; break; } } } return(handle); }
// Handles mouse operations void InputEvents(Event e) { // Events mousePos = e.mousePosition; // Mouse Events Node clickedNode = null; if (e.type == EventType.MouseDown) { if (coreSystem != null && !EditorApplication.isPlaying) { clickedNode = NodeAtPosition(mousePos); if (clickedNode == null) { clickedConnector = ConAtPosition(mousePos); } } if (clickedNode != null && !EditorApplication.isPlaying) { // Click on a Node if (e.button == 0) { // Left Click -> Window Drag. Handled by Unity } else if (e.button == 1) { if (clickedNode is StartAINode) { } else { // Right click -> Node delete customMenu.Call(ContextCallback, "Delete Node", "Duplicate Node"); e.Use(); } } } else if (clickedConnector.active && !EditorApplication.isPlaying) { // Click on Connector // Store really clicked connector. Because clickedConnector might change below reallyClickedConnector = clickedConnector.connector; // Break old Connection(s) if there are some if (clickedConnector.connector.connections.Count > 0) { Undo.RegisterCompleteObjectUndo(clickedConnector.connector, "Disconnect Nodes"); Connector otherConnector = clickedConnector.connector.Break(); if (otherConnector != null) { // The Curve Handle should now be detatched from Connector, // And still be connected with the Connector on the other Node clickedConnector = ConAtPosition(otherConnector.GetRect().center); } } } else { // Click on empty Canvas (no Node, not sideWindow) if (e.button == 2 || e.button == 0) { Rect rect = new Rect(sideWindow.opened ? position.width - 220 : position.width - 20, 0f, 220, position.height); if (!rect.Contains(mousePos)) { // Left/Middle Click -> Start scrolling scrollWindow = true; e.delta = new Vector2(0, 0); } } else if (e.button == 1) { if (SelectedObject != null && coreSystem == null) { CreateEasyAISystem(); EditorGUIUtility.ExitGUI(); } // new menu system // Now create the menu, add items and show it NEED TO CHANGE TO CUSTOM GENERIC MENU GenericMenu menu = new GenericMenu(); // no gameobject selected in scene if (coreSystem == null) { menu.AddItem(new GUIContent("Create GameObject"), false, ContextCallback, "Create GameObject"); } else { coreSystem.zoomFactor = 1f; foreach (var item in nodes.Keys) { menu.AddItem(new GUIContent(item.ToString()), false, ContextCallback, item.ToString()); } } menu.ShowAsContext(); e.Use(); // Right click -> Editor Context Click //customMenu.Call(ContextCallback, nodes.Keys.ToArray()); //e.Use (); } } } else if (e.type == EventType.MouseUp) { // Left/Middle click up if (e.button == 2 || e.button == 0) { // Connect 2 Nodes, if possible if (clickedConnector.active) { ConHandle secondHandle = ConAtPosition(mousePos); if (secondHandle.active) { if (clickedConnector.node != secondHandle.node) { // If Mouse Click and Release on the same Connector, connect them but don't record UNDO if (secondHandle.connector != reallyClickedConnector) { Undo.RegisterCompleteObjectUndo(clickedConnector.connector, "Connect Nodes"); Undo.RegisterCompleteObjectUndo(secondHandle.connector, "Connect Nodes"); } if (clickedConnector.connector.color == secondHandle.connector.color) { // Connect both Connectors to each other. clickedConnector.connector.ConnectTo(secondHandle.connector); } // // RECALCULE NODES //clickedConnector.node.CalculateNode(); //secondHandle.node.CalculateNode(); } } // // RECALCULE NODES //if (clickedConnector.node != null) clickedConnector.node.CalculateNode(); //if (secondHandle.node != null) secondHandle.node.CalculateNode(); // recalcule all nodes ONLY FOR TEST foreach (var item in coreSystem.nodes) { item.CalculateNode(); } } // Stop scrolling clickedConnector.active = false; scrollWindow = false; } } else if (e.type == EventType.ScrollWheel) { if (e.delta.y > 1 && coreSystem.zoomFactor < 1f) { coreSystem.zoomFactor = coreSystem.zoomFactor + 0.1f; } else if (e.delta.y < 0 && coreSystem.zoomFactor > 0.4f) { coreSystem.zoomFactor = coreSystem.zoomFactor - 0.1f; } } // Scroll Mainwindow if (scrollWindow) { // Change Window and Nodes by Mouse delta (difference to last rendered position) scrollOffset += e.delta / 2; if (coreSystem != null) { foreach (Node n in coreSystem.nodes) { n.rect.position += e.delta / 2; } } //Repaint(); } }