public static bool DeleteSelected(IDirectedGraphObjectConfig graphObject, int index, bool isNode) { if (index < 0 || graphObject == null || graphObject.GraphProperty == null) { return(false); } SerializedProperty links = graphObject.GraphProperty.FindPropertyRelative("Links"); SerializedProperty graphProperty = graphObject.GraphProperty; if (isNode) { SerializedProperty nodes = graphProperty.FindPropertyRelative("Nodes"); if (nodes == null || nodes.arraySize <= 1) { return(false); } DeleteIndex(graphProperty, "Positions", index); if (!DeleteIndex(graphProperty, "Nodes", index)) { return(false); } Vector2Int ln; for (int i = links.arraySize - 1; i >= 0; i--) { ln = links.GetArrayElementAtIndex(i).vector2IntValue; if (ln.x == index || ln.y == index) { DeleteIndex(graphProperty, "Links", i); DeleteIndex(graphProperty, "LinkData", i); } else { if (ln.x > index) { ln.x--; } if (ln.y > index) { ln.y--; } links.GetArrayElementAtIndex(i).vector2IntValue = ln; } } } else { DeleteIndex(graphProperty, "LinkData", index); if (!DeleteIndex(graphProperty, "Links", index)) { return(false); } } graphObject.Select(-1, false); GraphEditorWindow window = GetWindow <GraphEditorWindow>(((ScriptableObject)graphObject).name + " | Graph Editor", false); window._isNode = false; window._selection = -1; graphObject.GraphObject.ApplyModifiedProperties(); return(true); }
public void OnGUI() { if (_graphObject != null) { int index; _graphObject.GraphObject.Update(); _scroll = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height), _scroll, _scrollRect); switch (Event.current.type) { case EventType.KeyDown: if ((Event.current.modifiers & (EventModifiers.Command | EventModifiers.Control)) > 0 && (Event.current.keyCode == KeyCode.Backspace || Event.current.keyCode == KeyCode.Delete)) { DeleteSelected(); } else if (Event.current.modifiers == EventModifiers.None && _selection >= 0) { EditorWindow win = Array.Find(Resources.FindObjectsOfTypeAll <EditorWindow>(), w => w.titleContent.text == "Inspector"); if (win != null) { win.Focus(); } } break; case EventType.MouseDown: _mousePos = Event.current.mousePosition; _selection = IntersectNode(_mousePos); _isNode = _selection >= 0; if (!_isNode) { _selection = IntersectLink(_mousePos); } break; case EventType.MouseDrag: if (_selection >= 0) { switch (Event.current.button) { // Drag nodes with lmb case 0: if (_isNode) { _tempPos = _positions.GetArrayElementAtIndex(_selection).vector2Value; _tempPos += Event.current.mousePosition - _mousePos; _positions.GetArrayElementAtIndex(_selection).vector2Value = _tempPos; } else if (!_relinking) { _relinking = true; _tempPos = GetLinkEnds(_selection)[0]; } break; // Reposition links/Create nodes with rmb case 1: if (!_relinking) { _relinking = true; _tempPos = _isNode ? _positions.GetArrayElementAtIndex(_selection).vector2Value : GetLinkEnds(_selection)[0]; } break; } } // else -- TODO: Marquis select multiple objects. Also support multiple selections. _mousePos = Event.current.mousePosition; break; case EventType.MouseUp: _mousePos = Event.current.mousePosition; if (_relinking) { index = IntersectNode(_mousePos); // Create a new node if right-dragging if (index < 0 && Event.current.button == 1) { index = CreateNewNode(_mousePos); } if (_isNode) // Linking to a new node { Link(_selection, index); if (index >= 0) { _selection = index; } } else // Redirecting an existing link { // Redirecting a link to its own source deletes the link if (GetLink(_selection).x == index) { DeleteSelected(); } // Redirect the Link else { RedirectLink(_selection, index); } } _relinking = false; } break; } if (_graphObject != null) { _graphObject.Select(_selection, _isNode); } _graphObject.GraphObject.ApplyModifiedProperties(); DrawLinks(); if (_relinking) { DrawLink(_tempPos, _mousePos, !_isNode); } DrawNodes(); GUI.EndScrollView(); Repaint(); } }