From 90f8c4f4835004e683ab5fe06937fa1f1e5ce637 Mon Sep 17 00:00:00 2001 From: jacksonmj Date: Sat, 2 Apr 2011 15:49:06 +0100 Subject: [PATCH] error_ui: wrap text and change message box height to fit Stops banned user message overflowing out of message box --- includes/graphics.h | 1 + src/graphics.c | 39 +++++++++++++++++++++++++++++++++++++++ src/interface.c | 20 +++++++++++++------- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/includes/graphics.h b/includes/graphics.h index b7bfdfa66..eed3ed641 100644 --- a/includes/graphics.h +++ b/includes/graphics.h @@ -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); diff --git a/src/graphics.c b/src/graphics.c index 928b6a7c6..b4cd51c13 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -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=-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__) diff --git a/src/interface.c b/src/interface.c index a6c327dea..138a8cf1b 100644 --- a/src/interface.c +++ b/src/interface.c @@ -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=y0+44 && my<=y0+60) + if (b && !bq && mx>=x0 && mx=y0+textheight+32 && my<=y0+textheight+48) break; if (sdl_key==SDLK_RETURN)