public void DuplicateMe()
        {
            bool previewOpen = m_showPreview;

            string allOptions = m_allFunctionSwitches.Count.ToString();

            for (int i = 0; i < m_allFunctionSwitches.Count; i++)
            {
                allOptions += "," + m_allFunctionSwitches[i].UniqueId + "," + m_allFunctionSwitches[i].GetCurrentSelectedInput();
            }

            ReadOptionsHelper = allOptions.Split(',');

            ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;

            ContainerGraph.ParentWindow.CustomGraph = null;
            if (ContainerGraph.ParentWindow.CurrentGraph.CurrentStandardSurface != null)
            {
                ContainerGraph.ParentWindow.CurrentGraph.CurrentStandardSurface.InvalidateMaterialPropertyCount();
            }
            ContainerGraph.ParentWindow.CustomGraph = cachedGraph;

            ParentNode newNode = ContainerGraph.CreateNode(m_function, false, Vec2Position);

            newNode.ShowPreview = previewOpen;
            (newNode as FunctionNode).ReadOptionsHelper = ReadOptionsHelper;
            newNode.RefreshExternalReferences();
            if ((newNode as FunctionNode).m_reordenator && m_reordenator)
            {
                (newNode as FunctionNode).m_reordenator.OrderIndex = m_reordenator.OrderIndex;
            }

            for (int i = 0; i < m_outputPorts.Count; i++)
            {
                if (m_outputPorts[i].IsConnected)
                {
                    OutputPort newOutputPort = newNode.GetOutputPortByUniqueId(m_outputPorts[i].PortId);
                    if (newNode.OutputPorts != null && newOutputPort != null)
                    {
                        for (int j = m_outputPorts[i].ExternalReferences.Count - 1; j >= 0; j--)
                        {
                            ContainerGraph.CreateConnection(m_outputPorts[i].ExternalReferences[j].NodeId, m_outputPorts[i].ExternalReferences[j].PortId, newOutputPort.NodeId, newOutputPort.PortId);
                        }
                    }
                }
                //else
                //{
                //if( newNode.OutputPorts != null && newNode.OutputPorts[ i ] != null )
                //{
                //    ContainerGraph.DeleteConnection( false, newNode.UniqueId, newNode.OutputPorts[ i ].PortId, false, false, false );
                //}
                //}
            }

            for (int i = 0; i < m_inputPorts.Count; i++)
            {
                if (m_inputPorts[i].IsConnected)
                {
                    InputPort newInputPort = newNode.GetInputPortByUniqueId(m_inputPorts[i].PortId);
                    if (newNode.InputPorts != null && newInputPort != null)
                    {
                        ContainerGraph.CreateConnection(newInputPort.NodeId, newInputPort.PortId, m_inputPorts[i].ExternalReferences[0].NodeId, m_inputPorts[i].ExternalReferences[0].PortId);
                    }
                }
            }

            ContainerGraph.OnDuplicateEvent -= DuplicateMe;

            if (Selected)
            {
                ContainerGraph.DeselectNode(this);
                ContainerGraph.SelectNode(newNode, true, false);
            }

            ContainerGraph.DestroyNode(this, false);
        }
Пример #2
0
		public void DuplicateMe()
		{
			bool previewOpen = m_showPreview:

			string allOptions = m_allFunctionSwitches.Count.ToString():
			for( int i = 0: i < m_allFunctionSwitches.Count: i++ )
			{
				allOptions += "," + m_allFunctionSwitches[ i ].UniqueId + "," + m_allFunctionSwitches[ i ].GetCurrentSelectedInput():
			}

			ReadOptionsHelper = allOptions.Split( ',' ):

			ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph:
			ContainerGraph.ParentWindow.CustomGraph = null:
			MasterNode masterNode = ContainerGraph.ParentWindow.CurrentGraph.CurrentMasterNode:
			if( masterNode != null )
				masterNode.InvalidateMaterialPropertyCount():

			ContainerGraph.ParentWindow.CustomGraph = cachedGraph:

			ParentNode newNode = ContainerGraph.CreateNode( m_function, false, Vec2Position ):
			newNode.ShowPreview = previewOpen:
			( newNode as FunctionNode ).ReadOptionsHelper = ReadOptionsHelper:
			newNode.RefreshExternalReferences():
			if( ( newNode as FunctionNode ).m_reordenator && m_reordenator )
				( newNode as FunctionNode ).m_reordenator.OrderIndex = m_reordenator.OrderIndex:

			for( int i = 0: i < m_outputPorts.Count: i++ )
			{
				if( m_outputPorts[ i ].IsConnected )
				{
					OutputPort newOutputPort = newNode.GetOutputPortByUniqueId( m_outputPorts[ i ].PortId ):
					if( newNode.OutputPorts != null && newOutputPort != null )
					{
						for( int j = m_outputPorts[ i ].ExternalReferences.Count - 1: j >= 0: j-- )
						{
							ContainerGraph.CreateConnection( m_outputPorts[ i ].ExternalReferences[ j ].NodeId, m_outputPorts[ i ].ExternalReferences[ j ].PortId, newOutputPort.NodeId, newOutputPort.PortId ):
						}
					}
				}
				//else
				//{
				//if( newNode.OutputPorts != null && newNode.OutputPorts[ i ] != null )
				//{
				//    ContainerGraph.DeleteConnection( false, newNode.UniqueId, newNode.OutputPorts[ i ].PortId, false, false, false ):
				//}
				//}
			}

			for( int i = 0: i < m_inputPorts.Count: i++ )
			{
				if( m_inputPorts[ i ].IsConnected )
				{
					InputPort newInputPort = newNode.GetInputPortByUniqueId( m_inputPorts[ i ].PortId ):
					if( newNode.InputPorts != null && newInputPort != null )
					{
						ContainerGraph.CreateConnection( newInputPort.NodeId, newInputPort.PortId, m_inputPorts[ i ].ExternalReferences[ 0 ].NodeId, m_inputPorts[ i ].ExternalReferences[ 0 ].PortId ):
					}
				}
			}

			ContainerGraph.OnDuplicateEvent -= DuplicateMe:

			if( Selected )
			{
				ContainerGraph.DeselectNode( this ):
				ContainerGraph.SelectNode( newNode, true, false ):
			}

			ContainerGraph.DestroyNode( this, false ):
		}
Пример #3
0
        void DrawInputs()
        {
            int count = m_inputPorts.Count - m_firstAvailablePort;

            for (int i = 0; i < count; i++)
            {
                int portIdx = i + m_firstAvailablePort;
                m_foldoutValuesFlags[i] = EditorGUILayoutFoldout(m_foldoutValuesFlags[i], m_foldoutValuesLabels[i] + " - " + m_inputPorts[portIdx].Name);

                if (m_foldoutValuesFlags[i])
                {
                    EditorGUI.indentLevel += 1;

                    //Qualifier
                    // bool guiEnabled = GUI.enabled;
                    // GUI.enabled = m_functionMode;

                    VariableQualifiers newQualifier = (VariableQualifiers)EditorGUILayoutPopup(InputQualifierStr, (int)m_variableQualifiers[i], QualifiersStr);
                    if (newQualifier != m_variableQualifiers[i])
                    {
                        VariableQualifiers oldQualifier = m_variableQualifiers[i];
                        m_variableQualifiers[i] = newQualifier;
                        if (newQualifier == VariableQualifiers.In)
                        {
                            RemoveOutputPort(CreateOutputId(m_inputPorts[portIdx].PortId), false);
                        }
                        else if (oldQualifier == VariableQualifiers.In)
                        {
                            AddOutputPort(m_inputPorts[portIdx].DataType, m_inputPorts[portIdx].Name, CreateOutputId(m_inputPorts[portIdx].PortId));
                        }
                        RecalculateInOutOutputPorts();
                    }

                    //GUI.enabled = guiEnabled;

                    // Type
                    int typeIdx = WireToIdx[m_inputPorts[portIdx].DataType];
                    EditorGUI.BeginChangeCheck();
                    {
                        typeIdx = EditorGUILayoutPopup(InputTypeStr, typeIdx, AvailableWireTypesStr);
                    }

                    if (EditorGUI.EndChangeCheck())
                    {
                        m_inputPorts[portIdx].ChangeType(AvailableWireTypes[typeIdx], false);
                        if (m_variableQualifiers[i] != VariableQualifiers.In)
                        {
                            OutputPort currOutPort = GetOutputPortByUniqueId(CreateOutputId(m_inputPorts[portIdx].PortId));
                            currOutPort.ChangeType(AvailableWireTypes[typeIdx], false);
                        }
                    }

                    //Name
                    EditorGUI.BeginChangeCheck();
                    {
                        m_inputPorts[portIdx].Name = EditorGUILayoutTextField(InputNameStr, m_inputPorts[portIdx].Name);
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        m_nameModified             = true;
                        m_lastTimeNameModified     = EditorApplication.timeSinceStartup;
                        m_inputPorts[portIdx].Name = UIUtils.RemoveInvalidCharacters(m_inputPorts[portIdx].Name);
                        if (string.IsNullOrEmpty(m_inputPorts[portIdx].Name))
                        {
                            m_inputPorts[portIdx].Name = DefaultInputName + i;
                        }
                    }

                    // Port Data
                    if (!m_inputPorts[portIdx].IsConnected)
                    {
                        m_inputPorts[portIdx].ShowInternalData(this, true, InputValueStr);
                    }

                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Label(" ");
                        // Add new port
                        if (GUILayoutButton(string.Empty, UIUtils.PlusStyle, GUILayout.Width(ButtonLayoutWidth)))
                        {
                            AddPortAt(portIdx);
                            EditorGUI.FocusTextInControl(null);
                        }

                        //Remove port
                        if (GUILayoutButton(string.Empty, UIUtils.MinusStyle, GUILayout.Width(ButtonLayoutWidth)))
                        {
                            m_markedToDelete = portIdx;
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUI.indentLevel -= 1;
                }
            }

            if (m_markedToDelete > -1)
            {
                RemovePortAt(m_markedToDelete);
                m_markedToDelete = -1;
                EditorGUI.FocusTextInControl(null);
            }
        }
Пример #4
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (string.IsNullOrEmpty(m_code))
            {
                UIUtils.ShowMessage("Custom Expression need to have code associated", MessageSeverity.Warning);
                return("0");
            }

            bool codeContainsReturn = m_code.Contains(ReturnHelper);

            if (!codeContainsReturn && outputId != 0)
            {
                UIUtils.ShowMessage("Attempting to get value from inexisting inout/out variable", MessageSeverity.Warning);
                return("0");
            }
            OutputPort outputPort = GetOutputPortByUniqueId(outputId);

            if (outputPort.IsLocalValue)
            {
                return(outputPort.LocalValue);
            }

            string expressionName = UIUtils.RemoveInvalidCharacters(m_customExpressionName) + OutputId;
            int    count          = m_inputPorts.Count;

            if (count > 0)
            {
                if (m_callMode)
                {
                    string mainData = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);
                    RegisterLocalVariable(0, string.Format(Constants.CodeWrapper, mainData), ref dataCollector, "local" + expressionName + OutputId);
                }

                if (codeContainsReturn)
                {
                    string function = WrapCodeInFunction(dataCollector.IsTemplate, expressionName, false);
                    dataCollector.AddFunction(expressionName, function);

                    string functionCall = expressionName + "( ";
                    for (int i = m_firstAvailablePort; i < count; i++)
                    {
                        string inputPortLocalVar = m_inputPorts[i].Name + OutputId;
                        string result            = m_inputPorts[i].GeneratePortInstructions(ref dataCollector);
                        dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_inputPorts[i].DataType, inputPortLocalVar, result);
                        int idx = i - m_firstAvailablePort;
                        if (m_variableQualifiers[idx] != VariableQualifiers.In)
                        {
                            OutputPort currOutputPort = GetOutputPortByUniqueId(CreateOutputId(m_inputPorts[i].PortId));
                            currOutputPort.SetLocalValue(inputPortLocalVar, dataCollector.PortCategory);
                        }
                        functionCall += inputPortLocalVar;
                        if (i < (count - 1))
                        {
                            functionCall += " , ";
                        }
                    }
                    functionCall += " )";

                    if (m_callMode)
                    {
                        dataCollector.AddLocalVariable(0, functionCall + ";", true);
                    }
                    else
                    {
                        RegisterLocalVariable(0, functionCall, ref dataCollector, "local" + expressionName + OutputId);
                    }
                }
                else
                {
                    string localCode = m_code;
                    if (m_callMode)
                    {
                        for (int i = m_firstAvailablePort; i < count; i++)
                        {
                            string inputPortLocalVar = m_inputPorts[i].Name + OutputId;
                            localCode = localCode.Replace(m_inputPorts[i].Name, inputPortLocalVar);

                            if (m_inputPorts[i].IsConnected)
                            {
                                string result = m_inputPorts[i].GenerateShaderForOutput(ref dataCollector, m_inputPorts[i].DataType, true, true);
                                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_inputPorts[i].DataType, inputPortLocalVar, result);
                            }
                            else
                            {
                                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_inputPorts[i].DataType, inputPortLocalVar, m_inputPorts[i].WrappedInternalData);
                            }
                        }
                        string[] codeLines = localCode.Split('\n');
                        for (int codeIdx = 0; codeIdx < codeLines.Length; codeIdx++)
                        {
                            dataCollector.AddLocalVariable(0, codeLines[codeIdx], true);
                        }
                    }
                    else
                    {
                        string function = WrapCodeInFunction(dataCollector.IsTemplate, expressionName, true);
                        dataCollector.AddFunction(expressionName, function);

                        string functionCall = expressionName + "( ";
                        for (int i = m_firstAvailablePort; i < count; i++)
                        {
                            string inputPortLocalVar = m_inputPorts[i].Name + OutputId;
                            string result            = m_inputPorts[i].GeneratePortInstructions(ref dataCollector);
                            dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_inputPorts[i].DataType, inputPortLocalVar, result);
                            int idx = i - m_firstAvailablePort;
                            if (m_variableQualifiers[idx] != VariableQualifiers.In)
                            {
                                OutputPort currOutputPort = GetOutputPortByUniqueId(CreateOutputId(m_inputPorts[i].PortId));
                                currOutputPort.SetLocalValue(inputPortLocalVar, dataCollector.PortCategory);
                            }
                            functionCall += inputPortLocalVar;
                            if (i < (count - 1))
                            {
                                functionCall += " , ";
                            }
                        }
                        functionCall += " )";
                        RegisterLocalVariable(0, functionCall, ref dataCollector, "local" + expressionName + OutputId);
                    }
                }

                return(outputPort.LocalValue);
            }
            else
            {
                if (m_code.Contains(ReturnHelper))
                {
                    string function = WrapCodeInFunction(dataCollector.IsTemplate, expressionName, false);
                    dataCollector.AddFunction(expressionName, function);
                    string functionCall = expressionName + "()";
                    RegisterLocalVariable(0, functionCall, ref dataCollector, "local" + expressionName + OutputId);
                }
                else
                {
                    RegisterLocalVariable(0, string.Format(Constants.CodeWrapper, m_code), ref dataCollector, "local" + expressionName + OutputId);
                }

                return(m_outputPorts[0].LocalValue);
            }
        }
Пример #5
0
        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, ShortcutKeyData>();

            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 ShortcutKeyData(type, attribute.Name));
                        }

                        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);
            }
        }