From a3a8b762c32c1ec43be851e44818fcab89968e7f Mon Sep 17 00:00:00 2001 From: Themaister Date: Mon, 30 Sep 2013 18:42:44 +0200 Subject: [PATCH] Allow multi-line messages in RGUI. --- frontend/menu/rgui.c | 2 +- frontend/menu/rguidisp_bitmap.c | 46 ++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/frontend/menu/rgui.c b/frontend/menu/rgui.c index e2c8e594b9..2bda1cdef7 100644 --- a/frontend/menu/rgui.c +++ b/frontend/menu/rgui.c @@ -552,7 +552,7 @@ static int rgui_custom_bind_iterate(rgui_handle_t *rgui, rgui_action_t action) render_text(rgui); char msg[256]; - snprintf(msg, sizeof(msg), "[%s] press joypad (RETURN to skip)", input_config_bind_map[rgui->binds.begin - RGUI_SETTINGS_BIND_BEGIN].desc); + snprintf(msg, sizeof(msg), "[%s]\npress joypad\n(RETURN to skip)", input_config_bind_map[rgui->binds.begin - RGUI_SETTINGS_BIND_BEGIN].desc); render_messagebox(rgui, msg); struct rgui_bind_state binds = rgui->binds; diff --git a/frontend/menu/rguidisp_bitmap.c b/frontend/menu/rguidisp_bitmap.c index 2dfbc2c887..1a26173a96 100644 --- a/frontend/menu/rguidisp_bitmap.c +++ b/frontend/menu/rguidisp_bitmap.c @@ -145,17 +145,36 @@ static void render_messagebox(rgui_handle_t *rgui, const char *message) if (!message || !*message) return; - char *msg = strdup(message); - if (strlen(msg) > TERM_WIDTH) + struct string_list *list = string_split(message, "\n"); + if (!list) + return; + if (list->elems == 0) { - msg[TERM_WIDTH - 2] = '.'; - msg[TERM_WIDTH - 1] = '.'; - msg[TERM_WIDTH - 0] = '.'; - msg[TERM_WIDTH + 1] = '\0'; + string_list_free(list); + return; } - unsigned width = strlen(msg) * FONT_WIDTH_STRIDE - 1 + 6 + 10; - unsigned height = FONT_HEIGHT + 6 + 10; + unsigned width = 0; + unsigned glyphs_width = 0; + for (size_t i = 0; i < list->size; i++) + { + char *msg = list->elems[i].data; + unsigned msglen = strlen(msg); + if (msglen > TERM_WIDTH) + { + msg[TERM_WIDTH - 2] = '.'; + msg[TERM_WIDTH - 1] = '.'; + msg[TERM_WIDTH - 0] = '.'; + msg[TERM_WIDTH + 1] = '\0'; + msglen = TERM_WIDTH; + } + + unsigned line_width = msglen * FONT_WIDTH_STRIDE - 1 + 6 + 10; + width = max(width, line_width); + glyphs_width = max(glyphs_width, msglen); + } + + unsigned height = FONT_HEIGHT_STRIDE * list->size + 6 + 10; int x = (RGUI_WIDTH - width) / 2; int y = (RGUI_HEIGHT - height) / 2; @@ -174,8 +193,15 @@ static void render_messagebox(rgui_handle_t *rgui, const char *message) fill_rect(rgui->frame_buf, rgui->frame_buf_pitch, x, y + 5, 5, height - 5, green_filler); - blit_line(rgui, x + 8, y + 8, msg, false); - free(msg); + for (size_t i = 0; i < list->size; i++) + { + const char *msg = list->elems[i].data; + int offset_x = FONT_WIDTH_STRIDE * (glyphs_width - strlen(msg)) / 2; + int offset_y = FONT_HEIGHT_STRIDE * i; + blit_line(rgui, x + 8 + offset_x, y + 8 + offset_y, msg, false); + } + + string_list_free(list); } static void render_text(rgui_handle_t *rgui)