1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

* next ubuntux's patch

* normalized line endings (CR+LF)
* translated comments in CMessage.cpp
This commit is contained in:
mateuszb 2009-04-16 11:14:13 +00:00
parent d80afb1902
commit cc631b1362
36 changed files with 3428 additions and 3400 deletions

View File

@ -110,66 +110,86 @@ SDL_Surface * CMessage::drawBox1(int w, int h, int playerColor) //draws box for
return ret; return ret;
} }
std::vector<std::string> * CMessage::breakText(std::string text, size_t line, bool userBreak, bool ifor) //TODO userBreak -- use me /* The map file contains long texts, with or without line breaks. This
* method takes such a text and breaks it into into several lines. */
std::vector<std::string> * CMessage::breakText(std::string text, size_t max_line_size,
bool userBreak, bool ifor)
{ {
std::vector<std::string> * ret = new std::vector<std::string>(); std::vector<std::string> * ret = new std::vector<std::string>();
while (text.length()>line)
{ boost::algorithm::trim(text);
int whereCut = -1, braces=0;
bool pom = true, opened=false; while (text.length()) {
for (size_t i=0; i<line+braces; i++)
{ unsigned int z = 0;
if (text[i]==10) //end of line sign unsigned int braces = 0;
{ bool opened = false;
whereCut=i+1;
pom=false; while((text[z] != 0) && (text[z] != 0x0a) && (z < max_line_size+braces)) {
break; /* We don't count braces in string length. */
} if (text[z] == '{') {
else if (ifor && ((text[i]=='{') || (text[i]=='}'))) // ignore braces opened=true;
{ braces++;
if (text[i]=='{') } else if (text[z]=='}') {
opened=true; opened=false;
else
opened=false;
braces++; braces++;
} }
z++;
} }
for (int i=line+braces; i>0&&pom; i--)
{ if ((text[z] != 0) && (text[z] != 0x0a)) {
if (text[i]==' ') /* We have a long line. Try to do a nice line break, if
{ * possible. We backtrack on the line until we find a
whereCut = i; * suitable character. */
break; int pos = z-1;
}
else if (opened && text[i]=='{') /* TODO: boost should have a nice method to do that. */
opened = false; while(pos > 0 &&
else if (text[i]=='}') text[pos] != ' ' &&
opened = true; text[pos] != ',' &&
text[pos] != '.' &&
text[pos] != ';' &&
text[pos] != '!' &&
text[pos] != '?')
pos --;
if (pos > 0)
z = pos+1;
} }
ret->push_back(text.substr(0,whereCut));
text.erase(0,whereCut); /* Note: empty lines will be skipped. Is that different than H3? */
boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(" ")); if (z) {
if (opened) ret->push_back(text.substr(0, z));
{
(*(ret->end()-1))+='}'; if (opened)
text.insert(0,"{"); /* Close the brace for the current line. */
(*(ret->end()-1))+='}';
text.erase(0, z);
}
if (text[0] == 0x0a) {
/* Braces do not carry over lines. The map author forgot
* to close it. */
opened = false;
/* Remove LF */
text.erase(0, 1);
}
boost::algorithm::trim(text);
if (opened) {
/* Add an opening brace for the next line. */
if (text.length())
text.insert(0, "{");
} }
} }
for (size_t i=0;i<text.length();i++)
{ /* Trim whitespaces of every line. */
if (text[i]==10) //end of line sign
{
ret->push_back(text.substr(0,i));
text.erase(0,i);
i=0;
}
}
if (text.length() > 0)
ret->push_back(text);
for (size_t i=0; i<ret->size(); i++) for (size_t i=0; i<ret->size(); i++)
{
boost::algorithm::trim((*ret)[i]); boost::algorithm::trim((*ret)[i]);
}
return ret; return ret;
} }
@ -178,7 +198,7 @@ std::pair<int,int> CMessage::getMaxSizes(std::vector<std::vector<SDL_Surface*> >
std::pair<int,int> ret; std::pair<int,int> ret;
ret.first = -1; ret.first = -1;
ret.second=0; ret.second=0;
for (size_t i=0; i<txtg->size();i++) //szukamy najszerszej linii i lacznej wysokosci for (size_t i=0; i<txtg->size();i++) //we are searching widest line and total height
{ {
int lw=0; int lw=0;
for (size_t j=0;j<(*txtg)[i].size();j++) for (size_t j=0;j<(*txtg)[i].size();j++)
@ -223,33 +243,40 @@ std::vector<std::vector<SDL_Surface*> > * CMessage::drawText(std::vector<std::st
txtg->resize(brtext->size()); txtg->resize(brtext->size());
for (size_t i=0; i<brtext->size();i++) //foreach line for (size_t i=0; i<brtext->size();i++) //foreach line
{ {
while((*brtext)[i].length()) //jesli zostalo cos while((*brtext)[i].length()) //if something left
{ {
size_t z=0; bool br=true; size_t z;
while( ((*brtext)[i][z]) != ('{') )
{ /* Handle normal text. */
if (z >= (((*brtext)[i].length())-1)) z = 0;
{ while((*brtext)[i][z]!= 0 && (*brtext)[i][z] != ('{'))
br=false;
break;
}
z++;
}
if (!br)
z++; z++;
if (z) if (z)
(*txtg)[i].push_back(TTF_RenderText_Blended(font,(*brtext)[i].substr(0,z).c_str(),zwykly)); (*txtg)[i].push_back(TTF_RenderText_Blended(font, (*brtext)[i].substr(0,z).c_str(), zwykly));
(*brtext)[i].erase(0,z); (*brtext)[i].erase(0,z);
z=0;
if ( ((*brtext)[i].length()==0) || ((*brtext)[i][z]!='{') ) if ((*brtext)[i][0] == '{')
{ /* Remove '{' */
(*brtext)[i].erase(0,1);
if ((*brtext)[i].length()==0)
/* End of line */
continue; continue;
}
while( ((*brtext)[i][++z]) != ('}') ) /* This text will be highlighted. */
{} z = 0;
//tyemp = (*brtext)[i].substr(1,z-1); //od 1 bo pomijamy otwierajaca klamre while((*brtext)[i][z]!= 0 && (*brtext)[i][z] != ('}'))
(*txtg)[i].push_back(TTF_RenderText_Blended(font,(*brtext)[i].substr(1,z-1).c_str(),tytulowy)); z++;
(*brtext)[i].erase(0,z+1); //z+1 bo dajemy zamykajaca klamre
if (z)
(*txtg)[i].push_back(TTF_RenderText_Blended(font, (*brtext)[i].substr(0,z).c_str(), tytulowy));
(*brtext)[i].erase(0,z);
if ((*brtext)[i][0] == '}')
/* Remove '}' */
(*brtext)[i].erase(0,1);
} //ends while((*brtext)[i].length()) } //ends while((*brtext)[i].length())
} //ends for(int i=0; i<brtext->size();i++) } //ends for(int i=0; i<brtext->size();i++)
return txtg; return txtg;
@ -367,13 +394,13 @@ SDL_Surface * CMessage::genMessage
{ {
//max x 320 okolo 30 znakow //max x 320 okolo 30 znakow
std::vector<std::string> * tekst; std::vector<std::string> * tekst;
if (text.length() < 30) //nie trzeba polamac if (text.length() < 30) //does not need breaking
{ {
tekst = new std::vector<std::string>(); tekst = new std::vector<std::string>();
tekst->push_back(text); tekst->push_back(text);
} }
else tekst = breakText(text); else tekst = breakText(text);
int ww, hh; //wymiary boksa int ww, hh; //dimensions of box
if (319>30+13*text.length()) if (319>30+13*text.length())
ww = 30+13*text.length(); ww = 30+13*text.length();
else ww = 319; else ww = 319;
@ -428,7 +455,7 @@ SDL_Surface * CMessage::genMessage
void CMessage::drawBorder(int playerColor, SDL_Surface * ret, int w, int h, int x, int y) void CMessage::drawBorder(int playerColor, SDL_Surface * ret, int w, int h, int x, int y)
{ {
//obwodka I-szego rzedu pozioma //obwodka I-szego rzedu pozioma //border of 1st series, horizontal
for (int i=0; i<w-piecesOfBox[playerColor][6]->w; i+=piecesOfBox[playerColor][6]->w) for (int i=0; i<w-piecesOfBox[playerColor][6]->w; i+=piecesOfBox[playerColor][6]->w)
{ {
SDL_BlitSurface SDL_BlitSurface
@ -436,7 +463,7 @@ void CMessage::drawBorder(int playerColor, SDL_Surface * ret, int w, int h, int
SDL_BlitSurface SDL_BlitSurface
(piecesOfBox[playerColor][7],NULL,ret,&genRect(piecesOfBox[playerColor][7]->h,piecesOfBox[playerColor][7]->w,x+i,y+h-piecesOfBox[playerColor][7]->h)); (piecesOfBox[playerColor][7],NULL,ret,&genRect(piecesOfBox[playerColor][7]->h,piecesOfBox[playerColor][7]->w,x+i,y+h-piecesOfBox[playerColor][7]->h));
} }
//obwodka I-szego rzedu pionowa //obwodka I-szego rzedu pionowa //border of 1st series, vertical
for (int i=0; i<h-piecesOfBox[playerColor][4]->h; i+=piecesOfBox[playerColor][4]->h) for (int i=0; i<h-piecesOfBox[playerColor][4]->h; i+=piecesOfBox[playerColor][4]->h)
{ {
SDL_BlitSurface SDL_BlitSurface
@ -505,6 +532,7 @@ ComponentsToBlit::ComponentsToBlit(std::vector<SComponent*> & SComps, int maxw,
int toadd = (SComps[i]->getImg()->w + 12 + (_or ? _or->w : 0)); int toadd = (SComps[i]->getImg()->w + 12 + (_or ? _or->w : 0));
if (curw + toadd > maxw) if (curw + toadd > maxw)
{ {
curr++;
amax(w,curw); amax(w,curw);
curw = SComps[i]->getImg()->w; curw = SComps[i]->getImg()->w;
comps.resize(curr+1); comps.resize(curr+1);

View File

@ -68,7 +68,7 @@ public:
static SDL_Surface * drawBox1(int w, int h, int playerColor=1); static SDL_Surface * drawBox1(int w, int h, int playerColor=1);
static void drawBorder(int playerColor, SDL_Surface * ret, int w, int h, int x=0, int y=0); static void drawBorder(int playerColor, SDL_Surface * ret, int w, int h, int x=0, int y=0);
static SDL_Surface * drawBoxTextBitmapSub(int player, std::string text, SDL_Surface* bitmap, std::string sub, int charperline=30, int imgToBmp=55); static SDL_Surface * drawBoxTextBitmapSub(int player, std::string text, SDL_Surface* bitmap, std::string sub, int charperline=30, int imgToBmp=55);
static std::vector<std::string> * breakText(std::string text, size_t line=30, bool userBreak=true, bool ifor=true); //line - chars per line static std::vector<std::string> * breakText(std::string text, size_t max_line_size=30, bool userBreak=true, bool ifor=true);
static void init(); static void init();
static void dispose(); static void dispose();
}; };