Exemple #1
0
                virtual std::string GenerateCode(gd::Instruction & instruction, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context)
                {
                    codeGenerator.AddGlobalDeclaration(FunctionEvent::globalDeclaration);
                    std::string functionName = instruction.GetParameter(0).GetPlainString();
                    const gd::Project & project = codeGenerator.GetProject();
                    const gd::Layout & scene = codeGenerator.GetLayout();

                    const FunctionEvent * functionEvent = FunctionEvent::SearchForFunctionInEvents(scene.GetEvents(), functionName);
                    if ( !functionEvent )
                    {
                        std::cout << "Function \""+functionName+"\" not found!" << std::endl;
                        return "//Function \""+functionName+"\" not found.\n";
                    }

                    std::string code;

                    //Generate code for objects passed as arguments
                    std::string objectsAsArgumentCode;
                    {
                        objectsAsArgumentCode += "runtimeContext->ClearObjectListsMap()";
                        std::vector<std::string> realObjects = codeGenerator.ExpandObjectsName(functionEvent->GetObjectsPassedAsArgument(), context);
                        for (unsigned int i = 0;i<realObjects.size();++i)
                        {
                            context.EmptyObjectsListNeeded(realObjects[i]);
                            objectsAsArgumentCode += ".AddObjectListToMap(\""+codeGenerator.ConvertToString(realObjects[i])+"\", "+ManObjListName(realObjects[i])+")";
                        }
                        objectsAsArgumentCode += ".ReturnObjectListsMap()";
                    }

                    //Generate code for evaluating parameters
                    code += "std::vector<std::string> functionParameters;\n";
                    for (unsigned int i = 1;i<8;++i)
                    {
                        std::string parameterCode;
                        gd::CallbacksForGeneratingExpressionCode callbacks(parameterCode, codeGenerator, context);
                        gd::ExpressionParser parser(instruction.GetParameter(i).GetPlainString());
                        parser.ParseStringExpression(CppPlatform::Get(), project, scene, callbacks);
                        if (parameterCode.empty()) parameterCode = "\"\"";

                        code += "functionParameters.push_back("+parameterCode+");\n";
                    }
                    code += "std::vector<std::string> * oldFunctionParameters = currentFunctionParameters;\n";
                    code += "currentFunctionParameters = &functionParameters;\n";

                    code += FunctionEvent::MangleFunctionName(*functionEvent)+"(runtimeContext, "+objectsAsArgumentCode+");\n";
                    code += "currentFunctionParameters = oldFunctionParameters;\n";

                    return code;
                };
gd::String EventsCodeGenerator::GenerateParameterCodes(const gd::String & parameter, const gd::ParameterMetadata & metadata,
                                                        gd::EventsCodeGenerationContext & context,
                                                        const gd::String & previousParameter,
                                                        std::vector < std::pair<gd::String, gd::String> > * supplementaryParametersTypes)
{
    gd::String argOutput;

    //Code only parameter type
    if ( metadata.type == "currentScene" )
    {
        argOutput += "*runtimeContext->scene";
    }
    //Code only parameter type
    else if ( metadata.type == "objectList" )
    {
        std::vector<gd::String> realObjects = ExpandObjectsName(parameter, context);

        argOutput += "runtimeContext->ClearObjectListsMap()";
        for (std::size_t i = 0;i<realObjects.size();++i)
        {
            context.ObjectsListNeeded(realObjects[i]);
            argOutput += ".AddObjectListToMap(\""+ConvertToString(realObjects[i])+"\", "+ManObjListName(realObjects[i])+")";
        }
        argOutput += ".ReturnObjectListsMap()";
    }
    //Code only parameter type
    else if ( metadata.type == "objectListWithoutPicking" )
    {
        std::vector<gd::String> realObjects = ExpandObjectsName(parameter, context);

        argOutput += "runtimeContext->ClearObjectListsMap()";
        for (std::size_t i = 0;i<realObjects.size();++i)
        {
            context.EmptyObjectsListNeeded(realObjects[i]);
            argOutput += ".AddObjectListToMap(\""+ConvertToString(realObjects[i])+"\", "+ManObjListName(realObjects[i])+")";
        }
        argOutput += ".ReturnObjectListsMap()";
    }
    //Code only parameter type
    else if ( metadata.type == "objectPtr")
    {
        std::vector<gd::String> realObjects = ExpandObjectsName(parameter, context);

        if ( find(realObjects.begin(), realObjects.end(), context.GetCurrentObject()) != realObjects.end() && !context.GetCurrentObject().empty())
        {
            //If object currently used by instruction is available, use it directly.
            argOutput += ManObjListName(context.GetCurrentObject())+"[i]";
        }
        else
        {
            for (std::size_t i = 0;i<realObjects.size();++i)
            {
                context.ObjectsListNeeded(realObjects[i]);
                argOutput += "(!"+ManObjListName(realObjects[i])+".empty() ? "+ManObjListName(realObjects[i])+"[0] : ";
            }
            argOutput += "NULL";
            for (std::size_t i = 0;i<realObjects.size();++i)
                argOutput += ")";
        }
    }
    else if (metadata.type == "scenevar")
    {
        VariableCodeGenerationCallbacks callbacks(argOutput, *this, context, VariableCodeGenerationCallbacks::LAYOUT_VARIABLE);

        gd::VariableParser parser(parameter);
        if ( !parser.Parse(callbacks) )
        {
            cout << "Error :" << parser.firstErrorStr << " in: "<< parameter << endl;
            argOutput = "runtimeContext->GetSceneVariables().GetBadVariable()";
        }
    }
    else if (metadata.type == "globalvar")
    {
        VariableCodeGenerationCallbacks callbacks(argOutput, *this, context, VariableCodeGenerationCallbacks::PROJECT_VARIABLE);

        gd::VariableParser parser(parameter);
        if ( !parser.Parse(callbacks) )
        {
            cout << "Error :" << parser.firstErrorStr << " in: "<< parameter << endl;
            argOutput = "runtimeContext->GetGameVariables().GetBadVariable()";
        }
    }
    else if (metadata.type == "objectvar")
    {
        //Object is either the object of the previous parameter or, if it is empty,
        //the object being picked by the instruction.
        gd::String object = previousParameter;
        if ( object.empty() ) object = context.GetCurrentObject();

        VariableCodeGenerationCallbacks callbacks(argOutput, *this, context, object);

        gd::VariableParser parser(parameter);
        if ( !parser.Parse(callbacks) )
        {
            cout << "Error :" << parser.firstErrorStr << " in: "<< parameter << endl;
            argOutput = "runtimeContext->GetGameVariables().GetBadVariable()";
        }
    }
    else
    {
        argOutput += gd::EventsCodeGenerator::GenerateParameterCodes(parameter, metadata, context, previousParameter, supplementaryParametersTypes);
    }

    return argOutput;
}