Set size of message box, based on amount of text.

This commit is contained in:
Steve 2015-12-06 15:03:44 +00:00
parent 744d634f80
commit 28a4cda6fd
4 changed files with 52 additions and 2 deletions

View File

@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "messageBox.h" #include "messageBox.h"
static void calculateMessageBoxHeight(MessageBox *msg);
static MessageBox head; static MessageBox head;
static MessageBox *tail; static MessageBox *tail;
@ -79,6 +81,15 @@ void doMessageBox(void)
} }
} }
static void calculateMessageBoxHeight(MessageBox *msg)
{
limitTextWidth(575);
msg->height = getWrappedTextHeight(msg->body, 18);
limitTextWidth(0);
}
int showingMessageBoxes(void) int showingMessageBoxes(void)
{ {
return head.next != NULL; return head.next != NULL;
@ -91,9 +102,14 @@ void drawMessageBox(void)
if (msg && msg->time > 0) if (msg && msg->time > 0)
{ {
if (!msg->height)
{
calculateMessageBoxHeight(msg);
}
r.y = 50; r.y = 50;
r.w = 650; r.w = 650;
r.h = 110; r.h = msg->height + 40;
r.x = (SCREEN_WIDTH - r.w) / 2; r.x = (SCREEN_WIDTH - r.w) / 2;
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
@ -105,7 +121,7 @@ void drawMessageBox(void)
drawText(r.x + 10, r.y + 5, 18, TA_LEFT, colors.cyan, msg->title); drawText(r.x + 10, r.y + 5, 18, TA_LEFT, colors.cyan, msg->title);
limitTextWidth(550); limitTextWidth(575);
drawText(r.x + 10, r.y + 30, 18, TA_LEFT, colors.white, msg->body); drawText(r.x + 10, r.y + 30, 18, TA_LEFT, colors.white, msg->body);

View File

@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h" #include "../common.h"
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern int getWrappedTextHeight(char *text, int size);
extern void limitTextWidth(int width); extern void limitTextWidth(int width);
extern void playSound(int sound); extern void playSound(int sound);

View File

@ -142,6 +142,38 @@ static void drawTextSplit(int x, int y, int size, int align, SDL_Color c, char *
drawTextNormal(x, y, size, align, c, drawTextBuffer); drawTextNormal(x, y, size, align, c, drawTextBuffer);
} }
int getWrappedTextHeight(char *text, int size)
{
char textBuffer[MAX_DESCRIPTION_LENGTH];
char *token;
int w, h, currentWidth;
int y;
STRNCPY(textBuffer, text, MAX_DESCRIPTION_LENGTH);
token = strtok(textBuffer, " ");
y = 0;
currentWidth = 0;
while (token)
{
textSize(token, size, &w, &h);
if (currentWidth + w > maxWidth)
{
currentWidth = 0;
y += h;
}
currentWidth += w;
token = strtok(NULL, " ");
}
return y + h;
}
static void textSize(char *text, int size, int *w, int *h) static void textSize(char *text, int size, int *w, int *h)
{ {
if (!font[size]) if (!font[size])

View File

@ -296,6 +296,7 @@ struct MessageBox {
char title[MAX_NAME_LENGTH]; char title[MAX_NAME_LENGTH];
char body[MAX_DESCRIPTION_LENGTH]; char body[MAX_DESCRIPTION_LENGTH];
int time; int time;
int height;
MessageBox *next; MessageBox *next;
}; };