error_ui: wrap text and change message box height to fit

Stops banned user message overflowing out of message box
This commit is contained in:
jacksonmj 2011-04-02 15:49:06 +01:00 committed by Simon
parent 45b1b2bc96
commit 90f8c4f483
3 changed files with 53 additions and 7 deletions

View File

@ -99,6 +99,7 @@ void textnpos(char *s, int n, int w, int *cx, int *cy);
int textwidthx(char *s, int w);
int textposxy(char *s, int width, int w, int h);
int textwrapheight(char *s, int width);
#if defined(WIN32) && !defined(__GNUC__)
_inline void blendpixel(pixel *vid, int x, int y, int r, int g, int b, int a);

View File

@ -1104,6 +1104,45 @@ int textposxy(char *s, int width, int w, int h)
}
return n;
}
int textwrapheight(char *s, int width)
{
int x=0, height=FONT_H+2, cw;
int wordlen;
int charspace;
while (*s)
{
wordlen = strcspn(s," .,!?\n");
charspace = textwidthx(s, width-x);
if (charspace<wordlen && wordlen && width-x<width/3)
{
x = 0;
height += FONT_H+2;
}
for (; *s && --wordlen>=-1; s++)
{
if (*s == '\n')
{
x = 0;
height += FONT_H+2;
}
else if (*s == '\b')
{
s++;
}
else
{
cw = font_data[font_ptrs[(int)(*(unsigned char *)s)]];
if (x+cw>=width)
{
x = 0;
height += FONT_H+2;
}
x += cw;
}
}
}
return height;
}
//the most used function for drawing a pixel, because it has OpenGL support, which is not fully implemented.
#if defined(WIN32) && !defined(__GNUC__)

View File

@ -613,7 +613,7 @@ void draw_svf_ui(pixel *vid_buf)// all the buttons at the bottom
void error_ui(pixel *vid_buf, int err, char *txt)
{
int x0=(XRES-240)/2,y0=(YRES-MENUSIZE)/2,b=1,bq,mx,my;
int x0=(XRES-240)/2,y0=YRES/2,b=1,bq,mx,my,textheight;
char *msg;
msg = malloc(strlen(txt)+16);
@ -621,6 +621,12 @@ void error_ui(pixel *vid_buf, int err, char *txt)
sprintf(msg, "%03d %s", err, txt);
else
sprintf(msg, "%s", txt);
textheight = textwrapheight(msg, 240);
y0 -= (52+textheight)/2;
if (y0<2)
y0 = 2;
if (y0+50+textheight>YRES)
textheight = YRES-50-y0;
while (!sdl_poll())
{
@ -636,18 +642,18 @@ void error_ui(pixel *vid_buf, int err, char *txt)
mx /= sdl_scale;
my /= sdl_scale;
clearrect(vid_buf, x0-2, y0-2, 244, 64);
drawrect(vid_buf, x0, y0, 240, 60, 192, 192, 192, 255);
clearrect(vid_buf, x0-2, y0-2, 244, 52+textheight);
drawrect(vid_buf, x0, y0, 240, 48+textheight, 192, 192, 192, 255);
if (err)
drawtext(vid_buf, x0+8, y0+8, "HTTP error:", 255, 64, 32, 255);
else
drawtext(vid_buf, x0+8, y0+8, "Error:", 255, 64, 32, 255);
drawtext(vid_buf, x0+8, y0+26, msg, 255, 255, 255, 255);
drawtext(vid_buf, x0+5, y0+49, "Dismiss", 255, 255, 255, 255);
drawrect(vid_buf, x0, y0+44, 240, 16, 192, 192, 192, 255);
drawtextwrap(vid_buf, x0+8, y0+26, 224, msg, 255, 255, 255, 255);
drawtext(vid_buf, x0+5, y0+textheight+37, "Dismiss", 255, 255, 255, 255);
drawrect(vid_buf, x0, y0+textheight+32, 240, 16, 192, 192, 192, 255);
sdl_blit(0, 0, (XRES+BARSIZE), YRES+MENUSIZE, vid_buf, (XRES+BARSIZE));
if (b && !bq && mx>=x0 && mx<x0+240 && my>=y0+44 && my<=y0+60)
if (b && !bq && mx>=x0 && mx<x0+240 && my>=y0+textheight+32 && my<=y0+textheight+48)
break;
if (sdl_key==SDLK_RETURN)