Example #1
0
        private void CreateMethods()
        {
            // Generate Interface methods
            if (!string.IsNullOrEmpty(m_objectBuilder.Interface))
            {
                var methodNodes = m_navigator.OfType <MyVisualSyntaxInterfaceMethodNode>();
                foreach (var methodNode in methodNodes)
                {
                    var methodSyntax = methodNode.GetMethodDeclaration();
                    ProcessNodes(new[] { methodNode }, ref methodSyntax);
                    m_methodDeclarations.Add(methodSyntax);
                }
            }

            var events = m_navigator.OfType <MyVisualSyntaxEventNode>();

            events.AddRange(m_navigator.OfType <MyVisualSyntaxKeyEventNode>());
            // Generate Event methods
            // Take all events of same name and make a method out of theire bodies.
            while (events.Count > 0)
            {
                var firstEvent         = events[0];
                var eventsWithSameName = events.Where(@event => @event.ObjectBuilder.Name == firstEvent.ObjectBuilder.Name);
                var methodDeclaration  = MySyntaxFactory.PublicMethodDeclaration(
                    firstEvent.EventName,
                    SyntaxKind.VoidKeyword,
                    firstEvent.ObjectBuilder.OutputNames,
                    firstEvent.ObjectBuilder.OuputTypes);

                ProcessNodes(eventsWithSameName, ref methodDeclaration);
                // Bind with VisualScriptingProxy in constructor.
                m_constructor = m_constructor.AddBodyStatements(
                    MySyntaxFactory.DelegateAssignment(
                        firstEvent.ObjectBuilder.Name,
                        methodDeclaration.Identifier.ToString())
                    );
                // unBind from visualScriptingProxy in dispose method
                m_disposeMethod = m_disposeMethod.AddBodyStatements(
                    MySyntaxFactory.DelegateRemoval(
                        firstEvent.ObjectBuilder.Name,
                        methodDeclaration.Identifier.ToString())
                    );

                m_methodDeclarations.Add(methodDeclaration);

                events.RemoveAll(@event => eventsWithSameName.Contains(@event));
            }

            // There can be only one method from single input node.
            // Input nodes are of type Event.
            var inputs  = m_navigator.OfType <MyVisualSyntaxInputNode>();
            var outputs = m_navigator.OfType <MyVisualSyntaxOutputNode>();

            if (inputs.Count > 0)
            {
                Debug.Assert(inputs.Count == 1);

                var input = inputs[0];
                MethodDeclarationSyntax methodDeclaration = null;
                if (outputs.Count > 0)
                {
                    List <string> outputParamNames = new List <string>(outputs[0].ObjectBuilder.Inputs.Count);
                    List <string> outputParamTypes = new List <string>(outputs[0].ObjectBuilder.Inputs.Count);

                    foreach (var outputData in outputs[0].ObjectBuilder.Inputs)
                    {
                        outputParamNames.Add(outputData.Name);
                        outputParamTypes.Add(outputData.Type);
                    }

                    methodDeclaration = MySyntaxFactory.PublicMethodDeclaration("RunScript", SyntaxKind.BoolKeyword, input.ObjectBuilder.OutputNames, input.ObjectBuilder.OuputTypes, outputParamNames, outputParamTypes);
                }
                else
                {
                    methodDeclaration = MySyntaxFactory.PublicMethodDeclaration("RunScript", SyntaxKind.BoolKeyword, input.ObjectBuilder.OutputNames, input.ObjectBuilder.OuputTypes);
                }

                ProcessNodes(new[] { input }, ref methodDeclaration, new[] { SyntaxFactory.ReturnStatement(SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression)) });
                m_methodDeclarations.Add(methodDeclaration);
            }
        }