Exemplo n.º 1
0
bool InstructionSelectorDialog::MatchSearchCriteria(gd::String search, const gd::InstructionMetadata & instrMetadata)
{
    if (search.empty()) return true;

    return instrMetadata.GetGroup().FindCaseInsensitive(search) != string::npos ||
        instrMetadata.GetFullName().FindCaseInsensitive(search) != string::npos;
}
Exemplo n.º 2
0
/**
 * Generate the sentence describing an action.
 */
string InstructionSentenceFormatter::Translate(const gd::Instruction & action, const gd::InstructionMetadata & infos)
{
    std::string trad = infos.GetSentence();
    if ( trad.empty() ) trad = "   "; //Prevent empty sentences that could trigger graphical glitches.

    //Format special
    /*if ( trad.substr(0, 3) == "Do " && infos.parameters.size() >  )
    {

    }*/

    //Replace _PARAMx_ by values
    for (unsigned int i =0;i<infos.parameters.size();++i)
    {
        while ( trad.find( "_PARAM"+ToString(i)+"_" ) != std::string::npos )
        {
            std::string parameter = action.GetParameter( i ).GetPlainString();

            trad.replace(   trad.find( "_PARAM"+ToString(i)+"_" ), //Chaine à remplacer
                            std::string("_PARAM"+ToString(i)+"_").length(), //Longueur de la chaine
                            parameter );
        }
    }

    std::replace( trad.begin(), trad.end(), '\n', ' ');

    return trad;
}
std::vector< std::pair<gd::String, gd::TextFormatting> > InstructionSentenceFormatter::GetAsFormattedText(
    const Instruction & instr, const gd::InstructionMetadata & metadata)
{
    std::vector< std::pair<gd::String, gd::TextFormatting> > formattedStr;

    gd::String sentence = metadata.GetSentence();
    std::replace( sentence.Raw().begin(), sentence.Raw().end(), '\n', ' ');
    bool parse = true;

    while ( parse )
    {
        //Search first parameter
        parse = false;
        size_t firstParamPosition = gd::String::npos;
        size_t firstParamIndex = gd::String::npos;
        for (std::size_t i =0;i<metadata.parameters.size();++i)
        {
            size_t paramPosition = sentence.find( "_PARAM"+gd::String::From(i)+"_" );
            if ( paramPosition < firstParamPosition )
            {
                firstParamPosition = paramPosition;
                firstParamIndex = i;
                parse = true;
            }
        }

        //When a parameter is found, complete formatted gd::String.
        if ( parse )
        {
            if ( firstParamPosition != 0 ) //Add constant text before the parameter if any
            {
                TextFormatting format;
                formattedStr.push_back(std::make_pair(sentence.substr(0, firstParamPosition), format));
            }

            //Add the parameter
            TextFormatting format = GetFormattingFromType(metadata.parameters[firstParamIndex].type);
            format.userData = firstParamIndex;

            gd::String text = instr.GetParameter( firstParamIndex ).GetPlainString();
            std::replace( text.Raw().begin(), text.Raw().end(), '\n', ' '); //Using the raw std::string inside gd::String (no problems because it's only ANSI characters)

            formattedStr.push_back(std::make_pair(text, format));
            gd::String placeholder = "_PARAM"+gd::String::From(firstParamIndex)+"_";
            sentence = sentence.substr(firstParamPosition+placeholder.length());
        }
        else if ( !sentence.empty() )//No more parameter found: Add the end of the sentence
        {
            TextFormatting format;
            formattedStr.push_back(std::make_pair(sentence, format));
        }
    }

    return formattedStr;
}
Exemplo n.º 4
0
/**
 * Create a formatted sentence from an action
 */
std::vector< std::pair<std::string, gd::TextFormatting> > InstructionSentenceFormatter::GetAsFormattedText(const Instruction & action, const gd::InstructionMetadata & infos)
{
    std::vector< std::pair<std::string, gd::TextFormatting> > formattedStr;

    std::string sentence = infos.GetSentence();
    std::replace( sentence.begin(), sentence.end(), '\n', ' ');
    bool parse = true;

    while ( parse )
    {
        //Search first parameter
        parse = false;
        size_t firstParamPosition = std::string::npos;
        size_t firstParamIndex = std::string::npos;
        for (unsigned int i =0;i<infos.parameters.size();++i)
        {
            size_t paramPosition = sentence.find( "_PARAM"+ToString(i)+"_" );
            if ( paramPosition < firstParamPosition )
            {
                firstParamPosition = paramPosition;
                firstParamIndex = i;
                parse = true;
            }
        }

        //When a parameter is found, complete formatted std::string.
        if ( parse )
        {
            if ( firstParamPosition != 0 ) //Add constant text before the parameter if any
            {
                TextFormatting format;
                formattedStr.push_back(std::make_pair(sentence.substr(0, firstParamPosition), format));
            }

            //Add the parameter
            TextFormatting format = GetFormattingFromType(infos.parameters[firstParamIndex].type);
            format.userData = firstParamIndex;

            std::string text = action.GetParameter( firstParamIndex ).GetPlainString();
            std::replace( text.begin(), text.end(), '\n', ' ');

            formattedStr.push_back(std::make_pair(text, format));

            sentence = sentence.substr(firstParamPosition+ToString("_PARAM"+ToString(firstParamIndex)+"_").length());
        }
        else if ( !sentence.empty() )//No more parameter found : Add the end of the sentence
        {
            TextFormatting format;
            formattedStr.push_back(std::make_pair(sentence, format));
        }
    }

    return formattedStr;
}
gd::String InstructionSentenceFormatter::Translate(const gd::Instruction & instr, const gd::InstructionMetadata & metadata)
{
    gd::String out = metadata.GetSentence();
    if ( out.empty() ) out = "   "; //Prevent empty sentences that could trigger graphical glitches.

    //Replace _PARAMx_ placeholders by their values
    for (std::size_t i =0;i<metadata.parameters.size();++i)
    {
        gd::String placeholder = "_PARAM"+gd::String::From(i)+"_";
        gd::String parameter = instr.GetParameter(i).GetPlainString();
        out = out.FindAndReplace(placeholder, parameter);
    }

    out = out.FindAndReplace("\n", " ");
    return out;
}
Exemplo n.º 6
0
string InstructionSentenceFormatter::Translate(const gd::Instruction & instr, const gd::InstructionMetadata & metadata)
{
    std::string out = metadata.GetSentence();
    if ( out.empty() ) out = "   "; //Prevent empty sentences that could trigger graphical glitches.

    //Replace _PARAMx_ placeholders by their values
    for (unsigned int i =0;i<metadata.parameters.size();++i)
    {
        std::string placeholder = "_PARAM"+ToString(i)+"_";
        while ( out.find( placeholder ) != std::string::npos )
        {
            std::string parameter = instr.GetParameter(i).GetPlainString();
            out.replace(out.find(placeholder), placeholder.length(), parameter);
        }
    }

    std::replace( out.begin(), out.end(), '\n', ' ');

    return out;
}
Exemplo n.º 7
0
gd::String EventsCodeGenerator::GenerateBehaviorAction(
    const gd::String& objectName,
    const gd::String& behaviorName,
    const gd::BehaviorMetadata& autoInfo,
    const std::vector<gd::String>& arguments,
    const gd::InstructionMetadata& instrInfos,
    gd::EventsCodeGenerationContext& context) {
  gd::String actionCode;

  // Prepare call
  // Add a static_cast if necessary
  gd::String objectPart =
      (!instrInfos.parameters[1].supplementaryInformation.empty())
          ? "static_cast<" + autoInfo.className + "*>(" +
                ManObjListName(objectName) + "[i]->GetBehaviorRawPointer(" +
                GenerateGetBehaviorNameCode(behaviorName) + "))->"
          : ManObjListName(objectName) + "[i]->GetBehaviorRawPointer(" +
                GenerateGetBehaviorNameCode(behaviorName) + ")->";

  // Create call
  gd::String call;
  if ((instrInfos.codeExtraInformation.type == "number" ||
       instrInfos.codeExtraInformation.type == "string")) {
    if (instrInfos.codeExtraInformation.accessType ==
        gd::InstructionMetadata::ExtraInformation::MutatorAndOrAccessor)
      call = GenerateOperatorCall(
          instrInfos,
          arguments,
          objectPart + instrInfos.codeExtraInformation.functionCallName,
          objectPart +
              instrInfos.codeExtraInformation.optionalAssociatedInstruction,
          2);
    else if (instrInfos.codeExtraInformation.accessType ==
             gd::InstructionMetadata::ExtraInformation::Mutators)
      call = GenerateMutatorCall(
          instrInfos,
          arguments,
          objectPart + instrInfos.codeExtraInformation.functionCallName,
          2);
    else
      call = GenerateCompoundOperatorCall(
          instrInfos,
          arguments,
          objectPart + instrInfos.codeExtraInformation.functionCallName,
          2);
  } else {
    call = objectPart + instrInfos.codeExtraInformation.functionCallName + "(" +
           GenerateArgumentsList(arguments, 2) + ")";
  }

  // Verify that object has behavior.
  vector<gd::String> behaviors = gd::GetBehaviorsOfObject(
      GetGlobalObjectsAndGroups(), GetObjectsAndGroups(), objectName);
  if (find(behaviors.begin(), behaviors.end(), behaviorName) ==
      behaviors.end()) {
    cout << "Error: bad behavior \"" << behaviorName
         << "\" requested for object \'" << objectName
         << "\" (action: " << instrInfos.GetFullName() << ")." << endl;
  } else {
    actionCode += "for(std::size_t i = 0;i < " + ManObjListName(objectName) +
                  ".size();++i)\n";
    actionCode += "{\n";
    actionCode += "    " + call + ";\n";
    actionCode += "}\n";
  }

  return actionCode;
}
Exemplo n.º 8
0
gd::String EventsCodeGenerator::GenerateBehaviorCondition(
    const gd::String& objectName,
    const gd::String& behaviorName,
    const gd::BehaviorMetadata& autoInfo,
    const std::vector<gd::String>& arguments,
    const gd::InstructionMetadata& instrInfos,
    const gd::String& returnBoolean,
    bool conditionInverted,
    gd::EventsCodeGenerationContext& context) {
  gd::String conditionCode;

  // Prepare call
  // Add a static_cast if necessary
  gd::String objectFunctionCallNamePart =
      (!instrInfos.parameters[1].supplementaryInformation.empty())
          ? "static_cast<" + autoInfo.className + "*>(" +
                ManObjListName(objectName) + "[i]->GetBehaviorRawPointer(" +
                GenerateGetBehaviorNameCode(behaviorName) + "))->" +
                instrInfos.codeExtraInformation.functionCallName
          : ManObjListName(objectName) + "[i]->GetBehaviorRawPointer(" +
                GenerateGetBehaviorNameCode(behaviorName) + ")->" +
                instrInfos.codeExtraInformation.functionCallName;

  // Create call
  gd::String predicat;
  if ((instrInfos.codeExtraInformation.type == "number" ||
       instrInfos.codeExtraInformation.type == "string")) {
    predicat = GenerateRelationalOperatorCall(
        instrInfos, arguments, objectFunctionCallNamePart, 2);
  } else {
    predicat = objectFunctionCallNamePart + "(" +
               GenerateArgumentsList(arguments, 2) + ")";
  }
  if (conditionInverted) predicat = GenerateNegatedPredicat(predicat);

  // Verify that object has behavior.
  vector<gd::String> behaviors = gd::GetBehaviorsOfObject(
      GetGlobalObjectsAndGroups(), GetObjectsAndGroups(), objectName);
  if (find(behaviors.begin(), behaviors.end(), behaviorName) ==
      behaviors.end()) {
    cout << "Error: bad behavior \"" << behaviorName
         << "\" requested for object \'" << objectName
         << "\" (condition: " << instrInfos.GetFullName() << ")." << endl;
  } else {
    conditionCode += "for(std::size_t i = 0;i < " + ManObjListName(objectName) +
                     ".size();)\n";
    conditionCode += "{\n";
    conditionCode += "    if ( " + predicat + " )\n";
    conditionCode += "    {\n";
    conditionCode += "        " + returnBoolean + " = true;\n";
    conditionCode += "        ++i;\n";
    conditionCode += "    }\n";
    conditionCode += "    else\n";
    conditionCode += "    {\n";
    conditionCode += "        " + ManObjListName(objectName) + ".erase(" +
                     ManObjListName(objectName) + ".begin()+i);\n";
    conditionCode += "    }\n";
    conditionCode += "}";
  }

  return conditionCode;
}