Esempio n. 1
0
                virtual std::string Generate(gd::BaseEvent & event_, gd::EventsCodeGenerator & codeGenerator,
                                             gd::EventsCodeGenerationContext & /* The function has nothing to do with the current context */)
                {
                    FunctionEvent & event = dynamic_cast<FunctionEvent&>(event_);

                    //Declaring the pointer to the function parameters
                    codeGenerator.AddGlobalDeclaration(event.globalDeclaration);

                    //Declaring function prototype.
                    codeGenerator.AddGlobalDeclaration("void "+FunctionEvent::MangleFunctionName(event)+"(RuntimeContext *, std::map <std::string, std::vector<RuntimeObject*> *>);\n");

                    //Generating function code:
                    std::string functionCode;
                    functionCode += "\nvoid "+FunctionEvent::MangleFunctionName(event)+"(RuntimeContext * runtimeContext, std::map <std::string, std::vector<RuntimeObject*> *> objectsListsMap)\n{\n";

                    gd::EventsCodeGenerationContext callerContext;
                    {
                        std::vector<std::string> realObjects = codeGenerator.ExpandObjectsName(event.GetObjectsPassedAsArgument(), callerContext);
                        for (unsigned int i = 0;i<realObjects.size();++i)
                        {
                            callerContext.EmptyObjectsListNeeded(realObjects[i]);
                            functionCode += "std::vector<RuntimeObject*> "+ManObjListName(realObjects[i]) + ";\n";
                            functionCode += "if ( objectsListsMap[\""+realObjects[i]+"\"] != NULL ) "+ManObjListName(realObjects[i])+" = *objectsListsMap[\""+realObjects[i]+"\"];\n";
                        }
                    }
                    functionCode += "{";

                    gd::EventsCodeGenerationContext context;
                    context.InheritsFrom(callerContext);

                    //Generating function body code
                    std::string conditionsCode = codeGenerator.GenerateConditionsListCode(event.GetConditions(), context);
                    std::string actionsCode = codeGenerator.GenerateActionsListCode(event.GetActions(), context);
                    std::string subeventsCode = codeGenerator.GenerateEventsListCode(event.GetSubEvents(), context);

                    functionCode += codeGenerator.GenerateObjectsDeclarationCode(context);
                    std::string ifPredicat = "true";
                    for (unsigned int i = 0;i<event.GetConditions().size();++i)
                        ifPredicat += " && condition"+ToString(i)+"IsTrue";

                    functionCode += conditionsCode;
                    functionCode += "if (" +ifPredicat+ ")\n";
                    functionCode += "{\n";
                    functionCode += actionsCode;
                    if ( event.HasSubEvents() ) //Sub events
                    {
                        functionCode += "\n{\n";
                        functionCode += subeventsCode;
                        functionCode += "}\n";
                    }
                    functionCode += "}\n";

                    functionCode += "}\n"; //Context end
                    functionCode += "}\n"; //Function end
                    codeGenerator.AddCustomCodeOutsideMain(functionCode);

                    return "";
                }
Esempio n. 2
0
                virtual std::string Generate(gd::BaseEvent & event_, gd::EventsCodeGenerator & codeGenerator,
                                             gd::EventsCodeGenerationContext & context)
                {
                    TimedEvent & event = dynamic_cast<TimedEvent&>(event_);
                    const gd::Layout & scene = codeGenerator.GetLayout();

                    codeGenerator.AddIncludeFile("TimedEvent/TimedEventTools.h");

                    //Notify parent timed event that they have a child
                    for (unsigned int i = 0;i<TimedEvent::codeGenerationCurrentParents.size();++i)
                        TimedEvent::codeGenerationCurrentParents[i]->codeGenerationChildren.push_back(&event);

                    //And register this event as potential parent
                    TimedEvent::codeGenerationCurrentParents.push_back(&event);
                    event.codeGenerationChildren.clear();

                    //Prepare code for computing timeout
                    std::string timeOutCode;
                    gd::CallbacksForGeneratingExpressionCode callbacks(timeOutCode, codeGenerator, context);
                    gd::ExpressionParser parser(event.GetTimeoutExpression());
                    if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), scene, callbacks) || timeOutCode.empty()) timeOutCode = "0";

                    //Prepare name
                    std::string codeName = !event.GetName().empty() ? "GDNamedTimedEvent_"+codeGenerator.ConvertToString(event.GetName()) : "GDTimedEvent_"+ToString(&event);

                    std::string outputCode;

                    outputCode += "if ( static_cast<double>(GDpriv::TimedEvents::UpdateAndGetTimeOf(*runtimeContext->scene, \""+codeName+"\"))/1000000.0 > "+timeOutCode+")";
                    outputCode += "{";

                    outputCode += codeGenerator.GenerateConditionsListCode(event.GetConditions(), context);

                    std::string ifPredicat;
                    for (unsigned int i = 0;i<event.GetConditions().size();++i)
                    {
                        if (i!=0) ifPredicat += " && ";
                        ifPredicat += "condition"+ToString(i)+"IsTrue";
                    }

                    if ( !ifPredicat.empty() ) outputCode += "if (" +ifPredicat+ ")\n";
                    outputCode += "{\n";
                    outputCode += codeGenerator.GenerateActionsListCode(event.GetActions(), context);
                    if ( event.HasSubEvents() ) //Sub events
                    {
                        outputCode += "\n{\n";
                        outputCode += codeGenerator.GenerateEventsListCode(event.GetSubEvents(), context);
                        outputCode += "}\n";
                    }

                    outputCode += "}\n";

                    outputCode += "}";

                    //This event cannot be a parent of other TimedEvent anymore
                    if (!TimedEvent::codeGenerationCurrentParents.empty())
                        TimedEvent::codeGenerationCurrentParents.pop_back();
                    else
                        std::cout << "Error! CodeGenerationCurrentParents cannot be empty!";

                    return outputCode;
                }
std::string Array3DEvent::CodeGenerator::Generate(gd::BaseEvent & event_, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext &context)
{
    arr::threeDim::Array3DEvent &event = dynamic_cast<arr::threeDim::Array3DEvent&>(event_);

            // Adding needed includes
            codeGenerator.AddIncludeFile("stack");
            codeGenerator.AddIncludeFile("Array/ArrayValue.h");
            codeGenerator.AddIncludeFile("Array/Array3D.h");
            codeGenerator.AddIncludeFile("Array/ArrayTools.h");

            //Adding the main code of the event
            std::string code("// 3D-Array Iterate Event\n");

            std::string arrayNameExpr;
            gd::CallbacksForGeneratingExpressionCode callbacks(arrayNameExpr, codeGenerator, context);
            gd::ExpressionParser parser(event.GetArrayName());
            parser.ParseStringExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks);
            if (arrayNameExpr.empty()) arrayNameExpr = "\"\""; //If generation failed, we make sure output code is not empty.

            code += "arr::Array3D &currentArray = arr::ArrayManager::GetInstance()->GetArray3D(runtimeContext->scene->game, " + arrayNameExpr + ");\n";
            code += "for(unsigned int x = 0; x < currentArray.GetSize(1); x++)\n";
            code += "{\n";
            {
                code += "for(unsigned int y = 0; y < currentArray.GetSize(2); y++)\n";
                code += "{\n";
                {
                    code += "for(unsigned int z = 0; z < currentArray.GetSize(3); z++)\n";
                    code += "{\n";
                    {

                        code += "arr::ArrayManager::GetInstance()->GetArray3DEventInfo(runtimeContext->scene->game).PushNewEventInfo(x, y, z, currentArray.GetValue(x, y, z));";

                        // Generating condition/action/sub-event //
                        code += codeGenerator.GenerateConditionsListCode(event.GetConditions(), context);

                        std::string ifPredicat;
                        for (unsigned int i = 0;i<event.GetConditions().size();++i)
                        {
                            if (i!=0)
                                ifPredicat += " && ";
                            ifPredicat += "condition"+ToString(i)+"IsTrue";
                        }

                        if ( !ifPredicat.empty() ) code += "if (" +ifPredicat+ ")\n";

                        code += "{\n";
                        code += codeGenerator.GenerateActionsListCode(event.GetActions(), context);

                        if ( event.HasSubEvents() )
                        {
                            code += "\n{\n";
                            code += codeGenerator.GenerateEventsListCode(event.GetSubEvents(), context);
                            code += "}\n";
                        }

                        code += "}\n";
                        ///////////////////////////////////////////

                        code += "arr::ArrayManager::GetInstance()->GetArray3DEventInfo(runtimeContext->scene->game).PopEventInfo();";
                    }
                    code += "}\n";
                }
                code += "}\n";
            }
            code += "}\n";


            return code;
}