/// <summary> /// Resets important values for the AnyGraph window. /// </summary> private void Reset() { _zoomArea = new Rect(); _zoom = 1.0f; _zoomCoordsOrigin = Vector2.zero; _nodeDragDistance = new Vector2(); _initMousePos = new Vector2(); _scrollPos = new Vector2(); _graphExtents = new Rect(); _lastGraphExtents = new Rect(); _dragStartPoint = new Vector2(); _dragType = SelectionDragType.None; _toolbarRect = new Rect(); _optionWindowScrollPos = new Vector2(); _initialDragNodePosition = new Dictionary<Node, Rect>(); _allNodePos = new List<Rect>(); _cachedNodes = new List<IAnyGraphNode>(); _selected = null; _linkingNode = false; _nodeToLink = null; _allNodes = new List<Node>(); _selection = new List<Node>(); _oldSelection = new List<Node>(); _needRearrange = false; _rearrange = null; _searchString = ""; // Call on the garbage collector to free up resources. System.GC.Collect (); }
private void OnGUI() { // Draw the grid and background. Rect grapphRect = new Rect(0, 0, position.width - _toolbarRect.width, position.height); GUI.Box (grapphRect, "", UnityEditor.Graphs.Styles.graphBackground); DrawGrid (grapphRect); // Draw the search bar. Rect searchBarRect = new Rect(0, 0, position.width - _toolbarRect.width, 16); DrawSearchBar (searchBarRect); // Draw the Toolbar. DrawToolbar(); _zoomArea = new Rect(0, searchBarRect.height, position.width - _toolbarRect.width, position.height - searchBarRect.height); // If it isn't a repaint event, we can modify the node and links. if(Event.current.type != EventType.Repaint){ // Update the currently selected object. IAnyGraphable newSelected = null; if(Selection.activeObject is IAnyGraphable){ newSelected = Selection.activeObject as IAnyGraphable; } else if(Selection.activeGameObject != null){ MonoBehaviour[] behaviours = Selection.activeGameObject.GetComponents<MonoBehaviour>(); for(int i = 0; i < behaviours.Length; i++){ if(behaviours[i] is IAnyGraphable){ newSelected = behaviours[i] as IAnyGraphable; } } } // If the selected object has changed, we need to regraph. if(newSelected != _selected && newSelected != null){ Reset (); _selected = newSelected; GenerateCompleteNodeMap (_selected.Nodes); } if(_selected != null){ CheckNodes (); CheckNodeLinks (); // Move to the next rearrange iteration if it isn't null. if(_rearrange != null && _passedRepaint){ _passedRepaint = false; if(!_rearrange.MoveNext ()){ _rearrange = null; } } // Start a new rearrange if the instance was null and the graph needs rearranging. else if(_needRearrange){ _passedRepaint = false; RearrangeTree(SelectedSettings.nodePlacementOffset.x, SelectedSettings.nodePlacementOffset.y); } } } else{ _passedRepaint = true; } HandleEvents (); // if there is nothing to draw. if(_selected == null){ ShowNotification (new GUIContent("No graphable object\nselected.")); return; } else{ RemoveNotification (); } // Set the main view rect by removing the search bar and toolbar from the window size. Rect scrollViewRect = EditorZoomArea.Begin (_zoom, new Rect(0, 0, position.width - _toolbarRect.width, position.height - searchBarRect.height), searchBarRect.height); scrollViewRect.y -= 21 + searchBarRect.height; if(scrollViewRect.width > _graphExtents.width){ _scrollPos.x += (scrollViewRect.width - _graphExtents.width) / 2; } if(scrollViewRect.height > _graphExtents.height){ _scrollPos.y += (scrollViewRect.height - _graphExtents.height) / 2; } // Start graph scroll view. _scrollPos = GUI.BeginScrollView (scrollViewRect, _scrollPos, _graphExtents, GUIStyle.none, GUIStyle.none); // Get the current active path from the selected IAnyGraphable. // Recursively set active nodes. string[] activePath = _selected.ActiveNodePath; if(activePath != null && activePath.Length > 0){ for(int i = 0; i < _allNodes.Count; i++){ if(_allNodes[i].isRoot && _allNodes[i].representedNode.Name == activePath[0]){ _allNodes[i].SetActiveRecursively (activePath, 0); break; } } } DrawLinks (); DrawNodes (); DragSelection(new Rect(-5000f, -5000f, 10000f, 10000f)); UpdateScrollPosition (); DragGraph(); GUI.EndScrollView(); EditorZoomArea.End (); }