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; };
virtual std::string GenerateCode(const std::vector<gd::Expression> & parameters, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context) { codeGenerator.AddGlobalDeclaration(FunctionEvent::globalDeclaration); codeGenerator.AddIncludeFile("Function/FunctionTools.h"); const gd::Project & game = codeGenerator.GetProject(); const gd::Layout & scene = codeGenerator.GetLayout(); //Generate code for evaluating index std::string expression; gd::CallbacksForGeneratingExpressionCode callbacks(expression, codeGenerator, context); gd::ExpressionParser parser(parameters[0].GetPlainString()); if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), game, scene, callbacks) || expression.empty()) expression = "0"; std::string code; code += "GDpriv::FunctionTools::GetSafelyStringFromVector(currentFunctionParameters, "+expression+")"; return code; };
gd::String ProfileEvent::GenerateEventCode(gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & parentContext) { const gd::Layout & scene = codeGenerator.GetLayout(); codeGenerator.AddIncludeFile("GDCpp/BuiltinExtensions/ProfileTools.h"); ProfileLink profileLink; profileLink.originalEvent = originalEvent; std::cout << scene.GetProfiler() << std::endl; if ( scene.GetProfiler() ) //Should always be not NULL { scene.GetProfiler()->profileEventsInformation.push_back(profileLink); index = scene.GetProfiler()->profileEventsInformation.size()-1; } gd::String code; if ( previousProfileEvent ) code += "EndProfileTimer(*runtimeContext->scene, "+gd::String::From(previousProfileEvent->index)+");\n"; code += "StartProfileTimer(*runtimeContext->scene, "+gd::String::From(index)+");\n"; return code; }
virtual std::string GenerateCode(gd::Instruction & instruction, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context) { std::string outputCode; std::vector<std::string> realObjects = codeGenerator.ExpandObjectsName(instruction.GetParameter(0).GetPlainString(), context); for (unsigned int i = 0;i<realObjects.size();++i) { context.SetCurrentObject(realObjects[i]); context.ObjectsListNeeded(realObjects[i]); std::string newX, newY; std::string expression1Code; { gd::CallbacksForGeneratingExpressionCode callbacks(expression1Code, codeGenerator, context); gd::ExpressionParser parser(instruction.GetParameters()[2].GetPlainString()); if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || expression1Code.empty()) expression1Code = "0"; } std::string expression2Code; { gd::CallbacksForGeneratingExpressionCode callbacks(expression2Code, codeGenerator, context); gd::ExpressionParser parser(instruction.GetParameters()[4].GetPlainString()); if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || expression2Code.empty()) expression2Code = "0"; } std::string op1 = instruction.GetParameter(1).GetPlainString(); if ( op1 == "=" || op1.empty() ) newX = expression1Code; else if ( op1 == "/" || op1 == "*" || op1 == "-" || op1 == "+" ) newX = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].getX() "+op1 + expression1Code; else return ""; std::string op2 = instruction.GetParameter(3).GetPlainString(); if ( op2 == "=" || op2.empty() ) newY = expression2Code; else if ( op2 == "/" || op2 == "*" || op2 == "-" || op2 == "+" ) newY = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].getY() "+op2 + expression2Code; else return ""; std::string call = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].setPosition("+newX+","+newY+")"; outputCode += "for(var i = 0, len = "+codeGenerator.GetObjectListName(realObjects[i], context)+".length ;i < len;++i) {\n"; outputCode += " "+call+";\n"; outputCode += "}\n"; context.SetNoCurrentObject(); } return outputCode; };
virtual std::string GenerateCode(gd::Instruction & instruction, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context) { std::string value1Code; { gd::CallbacksForGeneratingExpressionCode callbacks(value1Code, codeGenerator, context); gd::ExpressionParser parser(instruction.GetParameters()[0].GetPlainString()); if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || value1Code.empty()) value1Code = "0"; } std::string value2Code; { gd::CallbacksForGeneratingExpressionCode callbacks(value2Code, codeGenerator, context); gd::ExpressionParser parser(instruction.GetParameters()[2].GetPlainString()); if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || value2Code.empty()) value2Code = "0"; } std::string resultingBoolean = codeGenerator.GenerateBooleanFullName("conditionTrue", context)+".val"; if ( instruction.GetParameters()[1].GetPlainString() == "=" || instruction.GetParameters()[1].GetPlainString().empty() ) return resultingBoolean + " = ("+value1Code+" == "+value2Code+");\n"; else if ( instruction.GetParameters()[1].GetPlainString() == ">") return resultingBoolean + " = ("+value1Code+" > "+value2Code+");\n"; else if ( instruction.GetParameters()[1].GetPlainString() == "<") return resultingBoolean + " = ("+value1Code+" < "+value2Code+");\n"; else if ( instruction.GetParameters()[1].GetPlainString() == "<=") return resultingBoolean + " = ("+value1Code+" <= "+value2Code+");\n"; else if ( instruction.GetParameters()[1].GetPlainString() == ">=") return resultingBoolean + " = ("+value1Code+" >= "+value2Code+");\n"; else if ( instruction.GetParameters()[1].GetPlainString() == "!=") return resultingBoolean + " = ("+value1Code+" != "+value2Code+");\n"; return ""; };
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; }
virtual std::string GenerateCode(gd::Instruction & instruction, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context) { std::string expressionCode; { gd::CallbacksForGeneratingExpressionCode callbacks(expressionCode, codeGenerator, context); gd::ExpressionParser parser(instruction.GetParameters()[2].GetPlainString()); if (!parser.ParseStringExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || expressionCode.empty()) expressionCode = "\"\""; } std::string varGetter; { VariableCodeGenerationCallbacks callbacks(varGetter, codeGenerator, context, VariableCodeGenerationCallbacks::LAYOUT_VARIABLE); gd::VariableParser parser(instruction.GetParameters()[0].GetPlainString()); if ( !parser.Parse(callbacks) ) varGetter = "runtimeScene.getVariables().get(\"\")"; } std::string op = instruction.GetParameters()[1].GetPlainString(); if ( op == "=" ) return varGetter+".setString("+expressionCode+");\n"; else if ( op == "+" ) return varGetter+".concatenate("+expressionCode+");\n"; return ""; };
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 ¤tArray = 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; }