specially per @savask's request, put matcher into a function

returns 0 if no match, returns position of pipe character if there is
This commit is contained in:
mniip 2013-08-22 19:50:20 +04:00
parent 4a308cbf66
commit e2cb5838c6
6 changed files with 34 additions and 76 deletions

View File

@ -621,5 +621,26 @@ void membwand(void * destv, void * srcv, size_t destsize, size_t srcsize)
dest[i] = dest[i] & src[i%srcsize];
}
}
int splitsign(const char* str)
{
int match=0,r;
if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9')
{
const char* p=str+4;
while (*p>='0' && *p<='9')
p++;
if (*p=='|')
{
r=p-str;
while (*p)
p++;
if (p[-1]=='}')
return r;
}
}
return 0;
}
vector2d v2d_zero = {0,0};
matrix2d m2d_identity = {1,0,0,1};

View File

@ -85,6 +85,9 @@ void RGB_to_HSV(int r,int g,int b,int *h,int *s,int *v);
void OpenURI(std::string uri);
void membwand(void * dest, void * src, size_t destsize, size_t srcsize);
int splitsign(const char* str);
// a b
// c d

View File

@ -924,26 +924,10 @@ void Renderer::DrawSigns()
if (signs[i].text.length())
{
std::string text = signs[i].getText(sim);
const char* str = signs[i].text.c_str();
signs[i].pos(text, x, y, w, h);
clearrect(x, y, w+1, h);
drawrect(x, y, w+1, h, 192, 192, 192, 255);
match=0;
// check if it's a link sign
if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9')
{
const char* p=str+4;
while (*p>='0' && *p<='9')
p++;
if (*p=='|')
{
while (*p)
p++;
if (p[-1]=='}')
match=1;
}
}
if (match)
if (splitsign(signs[i].text.c_str()))
drawtext(x+3, y+3, text, 0, 191, 255, 255);
else
drawtext(x+3, y+3, text, 255, 255, 255, 255);

View File

@ -571,30 +571,13 @@ bool GameController::MouseUp(int x, int y, unsigned button)
(*iter).pos((*iter).getText(sim), signx, signy, signw, signh);
if (x>=signx && x<=signx+signw && y>=signy && y<=signy+signh)
{
int match=0;
const char* str=(*iter).text.c_str();
const char* e;
if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9')
{
const char* p=str+4;
while (*p>='0' && *p<='9')
p++;
e=p;
if (*p=='|')
{
while (*p)
p++;
if (p[-1]=='}')
{
match=1;
}
}
}
if (match)
int pos=splitsign(str);
if (pos)
{
char buff[256];
strcpy(buff, str+3);
buff[e-str-3]=0;
buff[pos]=0;
int tempSaveID = format::StringToNumber<int>(std::string(buff));
if (tempSaveID)
{

View File

@ -177,27 +177,13 @@ void SignWindow::DoDraw()
for(std::vector<sign>::iterator iter = sim->signs.begin(), end = sim->signs.end(); iter != end; ++iter)
{
sign & currentSign = *iter;
int x, y, w, h, dx, dy, match=0;
int x, y, w, h, dx, dy;
Graphics * g = ui::Engine::Ref().g;
std::string text = currentSign.getText(sim);
const char* str = currentSign.text.c_str();
currentSign.pos(text, x, y, w, h);
g->clearrect(x, y, w+1, h);
g->drawrect(x, y, w+1, h, 192, 192, 192, 255);
if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9')
{
const char* p=str+4;
while (*p>='0' && *p<='9')
p++;
if (*p=='|')
{
while (*p)
p++;
if (p[-1]=='}')
match=1;
}
}
if (match)
if (splitsign(currentSign.text.c_str()))
g->drawtext(x+3, y+3, text, 0, 191, 255, 255);
else
g->drawtext(x+3, y+3, text, 255, 255, 255, 255);

View File

@ -33,30 +33,11 @@ std::string sign::getText(Simulation *sim)
}
else
{
int match=0;
const char* r;
const char* e;
if (signText[0]=='{' && (signText[1]=='c' || signText[1]=='t') && signText[2]==':' && signText[3]>='0' && signText[3]<='9')
int pos=splitsign(signText);
if (pos)
{
const char* p=signText+4;
while (*p>='0' && *p<='9')
p++;
if (*p=='|')
{
r=p+1;
while (*p)
p++;
if (p[-1]=='}')
{
match=1;
e=p;
}
}
}
if (match)
{
strcpy(buff, r);
buff[e-r-1]=0;
strcpy(buff, signText+pos+1);
buff[strlen(signText)-pos-2]=0;
}
else
strcpy(buff, signText);