public GraphContextMenu(ParentGraph currentGraph) { m_items = new List <ContextMenuItem>(); m_castTypes = new Dictionary <Type, Type>(); m_shortcutTypes = new Dictionary <KeyCode, ShortcuKeyData>(); m_lastKeyPressed = KeyCode.None; // Fetch all available nodes by their attributes IEnumerable <Type> availableTypes = AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(type => type.GetTypes()); foreach (Type type in availableTypes) { foreach (NodeAttributes attribute in Attribute.GetCustomAttributes(type).OfType <NodeAttributes>()) { if (attribute.Available) { if (attribute.CastType != null && attribute.CastType.Length > 0 && type != null) { for (int i = 0; i < attribute.CastType.Length; i++) { m_castTypes.Add(attribute.CastType[i], type); } } if (attribute.ShortcutKey != KeyCode.None && type != null) { m_shortcutTypes.Add(attribute.ShortcutKey, new ShortcuKeyData(type)); } m_items.Add(new ContextMenuItem(type, attribute.Name, attribute.Category, attribute.Description, attribute.ShortcutKey, (Type newType, Vector2 position) => { ParentNode newNode = ( ParentNode )ScriptableObject.CreateInstance(newType); if (newNode != null) { UIUtils.RegisterCreatedObjectUndo(newNode); newNode.Vec2Position = position; currentGraph.AddNode(newNode, true); if (UIUtils.InputPortReference.IsValid) { OutputPort port = newNode.GetFirstOutputPortOfType(UIUtils.InputPortReference.DataType, true); if (port != null) { port.ConnectTo(UIUtils.InputPortReference.NodeId, UIUtils.InputPortReference.PortId, UIUtils.InputPortReference.DataType, UIUtils.InputPortReference.TypeLocked); UIUtils.GetNode(UIUtils.InputPortReference.NodeId).GetInputPortById(UIUtils.InputPortReference.PortId).ConnectTo(port.NodeId, port.PortId, port.DataType, UIUtils.InputPortReference.TypeLocked); } } if (UIUtils.OutputPortReference.IsValid) { InputPort port = newNode.GetFirstInputPortOfType(UIUtils.OutputPortReference.DataType, true); if (port != null) { port.ConnectTo(UIUtils.OutputPortReference.NodeId, UIUtils.OutputPortReference.PortId, UIUtils.OutputPortReference.DataType, port.TypeLocked); UIUtils.GetNode(UIUtils.OutputPortReference.NodeId).GetOutputPortById(UIUtils.OutputPortReference.PortId).ConnectTo(port.NodeId, port.PortId, port.DataType, port.TypeLocked); } } UIUtils.InvalidateReferences(); } return(newNode); })); } } } //Sort out the final list by name m_items.Sort((ContextMenuItem item0, ContextMenuItem item1) => { return(item0.Name.CompareTo(item1.Name)); }); // Add them to the context menu m_menu = new GenericMenu(); foreach (ContextMenuItem item in m_items) { //The / on the GUIContent creates categories on the context menu m_menu.AddItem(new GUIContent(item.Category + "/" + item.Name, item.Description), false, OnItemSelected, item); } }
public void RefreshNodes(ParentGraph currentGraph) { if (m_items != null) { m_items.Clear(); } m_items = new List <ContextMenuItem>(); if (m_itemsDict != null) { m_itemsDict.Clear(); } m_itemsDict = new Dictionary <Type, NodeAttributes>(); if (m_deprecatedItemsDict != null) { m_deprecatedItemsDict.Clear(); } m_deprecatedItemsDict = new Dictionary <Type, NodeAttributes>(); if (m_castTypes != null) { m_castTypes.Clear(); } m_castTypes = new Dictionary <Type, Type>(); if (m_shortcutTypes != null) { m_shortcutTypes.Clear(); } m_shortcutTypes = new Dictionary <KeyCode, ShortcuKeyData>(); m_lastKeyPressed = KeyCode.None; // Fetch all available nodes by their attributes IEnumerable <Type> availableTypes = AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(type => type.GetTypes()); foreach (Type type in availableTypes) { foreach (NodeAttributes attribute in Attribute.GetCustomAttributes(type).OfType <NodeAttributes>()) { if (attribute.Available && !attribute.Deprecated) { if (!UIUtils.HasColorCategory(attribute.Category)) { if (!String.IsNullOrEmpty(attribute.CustomCategoryColor)) { try { Color color = new Color(); ColorUtility.TryParseHtmlString(attribute.CustomCategoryColor, out color); UIUtils.AddColorCategory(attribute.Category, color); } catch (Exception e) { Debug.LogException(e); UIUtils.AddColorCategory(attribute.Category, Constants.DefaultCategoryColor); } } else { UIUtils.AddColorCategory(attribute.Category, Constants.DefaultCategoryColor); } } if (attribute.CastType != null && attribute.CastType.Length > 0 && type != null) { for (int i = 0; i < attribute.CastType.Length; i++) { m_castTypes.Add(attribute.CastType[i], type); } } if (attribute.ShortcutKey != KeyCode.None && type != null) { m_shortcutTypes.Add(attribute.ShortcutKey, new ShortcuKeyData(type)); } m_itemsDict.Add(type, attribute); m_items.Add(new ContextMenuItem(attribute, type, attribute.Name, attribute.Category, attribute.Description, attribute.ShortcutKey, (Type newType, Vector2 position) => { ParentNode newNode = ( ParentNode )ScriptableObject.CreateInstance(newType); if (newNode != null) { newNode.Vec2Position = position; currentGraph.AddNode(newNode, true); if (UIUtils.InputPortReference.IsValid) { OutputPort port = newNode.GetFirstOutputPortOfType(UIUtils.InputPortReference.DataType, true); if (port != null) { port.ConnectTo(UIUtils.InputPortReference.NodeId, UIUtils.InputPortReference.PortId, UIUtils.InputPortReference.DataType, UIUtils.InputPortReference.TypeLocked); UIUtils.GetNode(UIUtils.InputPortReference.NodeId).GetInputPortByUniqueId(UIUtils.InputPortReference.PortId).ConnectTo(port.NodeId, port.PortId, port.DataType, UIUtils.InputPortReference.TypeLocked); } } if (UIUtils.OutputPortReference.IsValid) { InputPort port = newNode.GetFirstInputPortOfType(UIUtils.OutputPortReference.DataType, true); if (port != null) { port.ConnectTo(UIUtils.OutputPortReference.NodeId, UIUtils.OutputPortReference.PortId, UIUtils.OutputPortReference.DataType, port.TypeLocked); UIUtils.GetNode(UIUtils.OutputPortReference.NodeId).GetOutputPortByUniqueId(UIUtils.OutputPortReference.PortId).ConnectTo(port.NodeId, port.PortId, port.DataType, port.TypeLocked); } } UIUtils.InvalidateReferences(); } return(newNode); })); } else { m_deprecatedItemsDict.Add(type, attribute); } } } //Sort out the final list by name m_items.Sort((ContextMenuItem item0, ContextMenuItem item1) => { return(item0.Name.CompareTo(item1.Name)); }); // Add them to the context menu m_menu = new GenericMenu(); foreach (ContextMenuItem item in m_items) { //The / on the GUIContent creates categories on the context menu m_menu.AddItem(new GUIContent(item.Category + "/" + item.Name, item.Description), false, OnItemSelected, item); } }