// Create and open the "Create New Node" dropdown menu. public void CreateNodeMenuGUI(Patch patch) { if (GUILayout.Button(_buttonText, EditorStyles.toolbarDropDown)) { var menu = new GenericMenu(); foreach (var nodeType in _nodeTypes) menu.AddItem( new GUIContent(nodeType.label), false, OnMenuItem, new MenuItemData(patch, nodeType.type) ); var oy = EditorStyles.toolbar.fixedHeight - 2; menu.DropDown(new Rect(1, oy, 1, 1)); } }
// Reset the internal state. void ResetState() { _patchManager.Reset(); if (_patch == null || !_patch.isValid) _patch = _patchManager.RetrieveLastSelected(); else _patch.Rescan(); _mainViewSize = Vector2.one * 300; // minimum view size Repaint(); }
void OnGUI() { // Do nothing while play mode. if (isPlayMode) { DrawPlaceholderGUI("Not available in play mode"); return; } // If there is something wrong with the patch manager, reset it. if (!_patchManager.isValid) _patchManager.Reset(); // Patch validity check. if (_patch != null) if (!_patch.isValid) _patch = null; // Seems like not good. Abandon it. else if (!_patch.CheckNodesValidity()) _patch.Rescan(); // Some nodes are not good. Rescan them. // Get a patch if no one is selected. if (_patch == null) _patch = _patchManager.RetrieveLastSelected(); // Draw a placeholder if no patch is available. // Disable GUI during the play mode, or when no patch is available. if ( _patch == null) { DrawPlaceholderGUI("No patch available"); return; } // Tool bar EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); // - Create node menu _nodeFactory.CreateNodeMenuGUI(_patch); GUILayout.Space(100); // - Patch selector var patchIndex = _patchManager.GetIndexOf(_patch); var newPatchIndex = EditorGUILayout.Popup( patchIndex, _patchManager.MakeNameList(), EditorStyles.toolbarDropDown ); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); // View area EditorGUILayout.BeginHorizontal(); // - Main view DrawMainViewGUI(); // - Side view (property editor) DrawSideBarGUI(); EditorGUILayout.EndHorizontal(); // Re-initialize the editor if the patch selection was changed. if (patchIndex != newPatchIndex) { _patch = _patchManager.RetrieveAt(newPatchIndex); _patchManager.Select(_patch); Repaint(); } // Cancel wiring with a mouse click or hitting the esc key. if (_wiring != null) { var e = Event.current; if (e.type == EventType.MouseUp || (e.type == EventType.KeyDown && e.keyCode == KeyCode.Escape)) { _wiring = null; e.Use(); } } }
void OnDisable() { if (_propertyEditor != null) { DestroyImmediate(_propertyEditor); _propertyEditor = null; } _patchManager = null; _nodeFactory = null; _patch = null; Undo.undoRedoPerformed -= OnUndo; EditorApplication.hierarchyWindowChanged -= OnHierarchyWindowChanged; }
void OnEnable() { _patchManager = new PatchManager(); _nodeFactory = new NodeFactory(); _mainViewSize = Vector2.one * 300; // minimum view size _patchManager.Reset(); _patch = _patchManager.RetrieveLastSelected(); Undo.undoRedoPerformed += OnUndo; EditorApplication.hierarchyWindowChanged += OnHierarchyWindowChanged; }
// Remove itself from the patch. public void RemoveFromPatch(Patch patch) { Undo.DestroyObjectImmediate(_instance.gameObject); }
// Determine the index of a given patch. public int GetIndexOf(Patch patch) { return(Array.FindIndex( _instances, i => patch.IsRepresentationOf(i) )); }
// Determine the index of a given patch. public int GetIndexOf(Patch patch) { return Array.FindIndex( _instances, i => patch.IsRepresentationOf(i) ); }
// Enumerate all links from the outlets and cache them. void CacheLinks(Patch patch) { _cachedLinks = new List<NodeLink>(); foreach (var outlet in _outlets) { // Scan all the events from the outlet. var boundEvent = outlet.boundEvent; var targetCount = boundEvent.GetPersistentEventCount(); for (var i = 0; i < targetCount; i++) { var target = boundEvent.GetPersistentTarget(i); // Ignore it if it's a null event or the target is not a node. if (target == null || !(target is Wiring.NodeBase)) continue; // Try to retrieve the linked inlet. var targetNode = patch.GetNodeOfInstance((Wiring.NodeBase)target); var methodName = boundEvent.GetPersistentMethodName(i); var inlet = targetNode.GetInletWithName(methodName); // Cache it if it's a valid link. if (targetNode != null && inlet != null) _cachedLinks.Add(new NodeLink(this, outlet, targetNode, inlet)); } } }
// Draw lines of the links from this node. public bool DrawLinkLines(Patch patch) { // Check if the position information is ready. if (_inlets.Count > 0 && _inlets[0].buttonRect.center == Vector2.zero) return false; if (_outlets.Count > 0 && _outlets[0].buttonRect.center == Vector2.zero) return false; // Make cache and draw all the lines. if (_cachedLinks == null) CacheLinks(patch); foreach (var link in _cachedLinks) link.DrawLine(); return true; }
// Remove all links to a given node. public void RemoveLinksTo(Node targetNode, Patch patch) { if (_cachedLinks == null) CacheLinks(patch); foreach (var link in _cachedLinks) if (link.toNode == targetNode) RemoveLink(link.fromOutlet, link.toNode, link.toInlet); }
// If this node has a link to a given inlet, return it. public NodeLink TryGetLinkTo(Node targetNode, Inlet inlet, Patch patch) { if (_cachedLinks == null) CacheLinks(patch); foreach (var link in _cachedLinks) if (link.toInlet == inlet) return link; return null; }
// Enumerate all the links from a given outlet. public NodeLink[] EnumerateLinksFrom(Outlet outlet, Patch patch) { if (_cachedLinks == null) CacheLinks(patch); var temp = new List<NodeLink>(); foreach (var link in _cachedLinks) if (link.fromOutlet == outlet) temp.Add(link); return temp.ToArray(); }
// Select a given patch for later use. public void Select(Patch patch) { foreach (var instance in _instances) instance._wiringSelected = patch.IsRepresentationOf(instance); }
public MenuItemData(Patch patch, Type type) { this.patch = patch; this.type = type; }
public MenuItemData(Patch patch, System.Type type) { this.patch = patch; this.type = type; }