/** * 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; }
/** * 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; }
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; }