From: DeaDDooMER Date: Wed, 22 Apr 2020 11:20:29 +0000 (+0400) Subject: menu: fix menus with disabled assertions X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=99e9995ba077aa90b20b47d528ccd3ff27949652;p=flatwaifu.git menu: fix menus with disabled assertions --- diff --git a/src/gl/render.c b/src/gl/render.c index c4da0dc..0dc1ac3 100644 --- a/src/gl/render.c +++ b/src/gl/render.c @@ -803,58 +803,59 @@ static void get_entry_size (const menu_t *m, int i, int *w, int *h) { int type = 0; menu_msg_t msg; msg.type = GM_GETENTRY; - assert(GM_send(m, i, &msg)); - type = msg.integer.i; - switch (type) { - case GM_BUTTON: - case GM_SCROLLER: - case GM_TEXTFIELD: - case GM_TEXTFIELD_BUTTON: - msg.type = GM_GETCAPTION; - if (GM_send(m, i, &msg)) { - x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); - } - break; - case GM_SMALL_BUTTON: - msg.type = GM_GETCAPTION; - if (GM_send(m, i, &msg)) { - x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); - } - break; - default: - assert(0); - } - switch (type) { - case GM_BUTTON: - msg.type = GM_GETSTR; - if (GM_send(m, i, &msg)) { - x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); - } - y = 16; - break; - case GM_SMALL_BUTTON: - msg.type = GM_GETSTR; - if (GM_send(m, i, &msg)) { - x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); - } - y = 12; - break; - case GM_SCROLLER: - x += (SCROLLER_MIDDLE + 2) * 8; - y = 16; - break; - case GM_TEXTFIELD: - case GM_TEXTFIELD_BUTTON: - msg.type = GM_GETSTR; - if (GM_send(m, i, &msg)) { - x += (msg.string.maxlen + 2) * 8; - } else { - x += (TEXTFIELD_MIDDLE + 2) * 8; - } - y = 16; - break; - default: - assert(0); + if (GM_send(m, i, &msg)) { + type = msg.integer.i; + switch (type) { + case GM_BUTTON: + case GM_SCROLLER: + case GM_TEXTFIELD: + case GM_TEXTFIELD_BUTTON: + msg.type = GM_GETCAPTION; + if (GM_send(m, i, &msg)) { + x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); + } + break; + case GM_SMALL_BUTTON: + msg.type = GM_GETCAPTION; + if (GM_send(m, i, &msg)) { + x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); + } + break; + default: + assert(0); + } + switch (type) { + case GM_BUTTON: + msg.type = GM_GETSTR; + if (GM_send(m, i, &msg)) { + x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); + } + y = 16; + break; + case GM_SMALL_BUTTON: + msg.type = GM_GETSTR; + if (GM_send(m, i, &msg)) { + x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); + } + y = 12; + break; + case GM_SCROLLER: + x += (SCROLLER_MIDDLE + 2) * 8; + y = 16; + break; + case GM_TEXTFIELD: + case GM_TEXTFIELD_BUTTON: + msg.type = GM_GETSTR; + if (GM_send(m, i, &msg)) { + x += (msg.string.maxlen + 2) * 8; + } else { + x += (TEXTFIELD_MIDDLE + 2) * 8; + } + y = 16; + break; + default: + assert(0); + } } *w = x; *h = y; diff --git a/src/menu.c b/src/menu.c index 4205285..639fed9 100644 --- a/src/menu.c +++ b/src/menu.c @@ -354,6 +354,7 @@ static const menu_t exit_menu = { static int main_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) { static int cur; enum { NEWGAME, OLDGAME, SAVEGAME, OPTIONS, EXIT, __NUM__ }; + assert(i >= 0 && i < __NUM__); static const simple_menu_t sm = { GM_BIG, "Menu", NULL, { @@ -497,7 +498,7 @@ static int state_for_anykey (int x) { int GM_act (void) { menu_msg_t msg; int n, cur, type; - const menu_t *m = GM_get (); + const menu_t *m = GM_get(); if (m == NULL) { if (lastkey == KEY_ESCAPE || (state_for_anykey(g_st) && lastkey != KEY_UNKNOWN)) { GM_push(&main_menu); @@ -505,93 +506,98 @@ int GM_act (void) { } } else { msg.type = GM_QUERY; - assert(GM_send_this(m, &msg)); - cur = msg.integer.i; - n = msg.integer.a; - msg.type = GM_GETENTRY; - assert(GM_send(m, cur, &msg)); - type = msg.integer.i; - switch (lastkey) { - case KEY_ESCAPE: - if (type == GM_TEXTFIELD && input) { - input = 0; - Y_disable_text_input(); - msg.type = GM_CANCEL; - GM_send(m, cur, &msg); - } else { - GM_pop(); - Z_sound(msnd4, 128); - } - break; - case KEY_UP: - case KEY_DOWN: - msg.type = lastkey == KEY_UP ? GM_UP : GM_DOWN; - if (GM_send(m, cur, &msg)) { - Z_sound(msnd1, 128); - } - break; - case KEY_LEFT: - case KEY_RIGHT: - if (type == GM_SCROLLER) { - msg.integer.type = GM_GETINT; - if (GM_send(m, cur, &msg)) { - msg.integer.type = GM_SETINT; - msg.integer.i += lastkey == KEY_LEFT ? -msg.integer.s : msg.integer.s; - msg.integer.i = min(max(msg.integer.i, msg.integer.a), msg.integer.b); - if (GM_send(m, cur, &msg)) { - Z_sound(lastkey == KEY_LEFT ? msnd5 : msnd6, 255); + if (GM_send_this(m, &msg)) { + cur = msg.integer.i; + n = msg.integer.a; + msg.type = GM_GETENTRY; + if (GM_send(m, cur, &msg)) { + type = msg.integer.i; + switch (lastkey) { + case KEY_ESCAPE: + if (type == GM_TEXTFIELD && input) { + input = 0; + Y_disable_text_input(); + msg.type = GM_CANCEL; + GM_send(m, cur, &msg); + } else { + GM_pop(); + Z_sound(msnd4, 128); } - } - } else if (type == GM_TEXTFIELD) { - //if (input) { - // icur += lastkey == KEY_LEFT ? -1 : +1; - // icur = min(max(icur, 0), strnlen(ibuf, imax)); - //} - } - break; - case KEY_BACKSPACE: - if (type == GM_TEXTFIELD) { - if (input && icur > 0) { - // FIXIT buffers in strncpy must not overlap - strncpy(&ibuf[icur - 1], &ibuf[icur], imax - icur); - ibuf[imax - 1] = 0; - icur -= 1; - } - } - break; - case KEY_RETURN: - if (type == GM_TEXTFIELD) { - if (input) { - input = 0; - Y_disable_text_input(); - msg.type = GM_END; - msg.string.s = ibuf; - msg.string.maxlen = imax; - GM_send(m, cur, &msg); - } else { - msg.type = GM_GETSTR; - if (GM_send(m, cur, &msg)) { - imax = min(msg.string.maxlen, GM_MAX_INPUT); - strncpy(ibuf, msg.string.s, imax); - icur = strnlen(ibuf, imax); + break; + case KEY_UP: + case KEY_DOWN: + if (input == 0) { + msg.type = lastkey == KEY_UP ? GM_UP : GM_DOWN; + if (GM_send(m, cur, &msg)) { + Z_sound(msnd1, 128); + } + } + break; + case KEY_LEFT: + case KEY_RIGHT: + if (type == GM_SCROLLER) { + msg.integer.type = GM_GETINT; + if (GM_send(m, cur, &msg)) { + msg.integer.type = GM_SETINT; + msg.integer.i += lastkey == KEY_LEFT ? -msg.integer.s : msg.integer.s; + msg.integer.i = min(max(msg.integer.i, msg.integer.a), msg.integer.b); + if (GM_send(m, cur, &msg)) { + Z_sound(lastkey == KEY_LEFT ? msnd5 : msnd6, 255); + } + } + } else if (type == GM_TEXTFIELD) { + //if (input) { + // icur += lastkey == KEY_LEFT ? -1 : +1; + // icur = min(max(icur, 0), strnlen(ibuf, imax)); + //} + } + break; + case KEY_BACKSPACE: + if (type == GM_TEXTFIELD) { + if (input && icur > 0) { + // FIXIT buffers in strncpy must not overlap + strncpy(&ibuf[icur - 1], &ibuf[icur], imax - icur); + ibuf[imax - 1] = 0; + icur -= 1; + } + } + break; + case KEY_RETURN: + if (type == GM_TEXTFIELD) { + if (input) { + input = 0; + Y_disable_text_input(); + msg.type = GM_END; + msg.string.s = ibuf; + msg.string.maxlen = imax; + GM_send(m, cur, &msg); + } else { + msg.type = GM_GETSTR; + if (GM_send(m, cur, &msg)) { + imax = min(msg.string.maxlen, GM_MAX_INPUT); + strncpy(ibuf, msg.string.s, imax); + icur = strnlen(ibuf, imax); + } else { + memset(ibuf, 0, GM_MAX_INPUT); + imax = GM_MAX_INPUT; + icur = 0; + } + input = 1; + Y_enable_text_input(); + msg.type = GM_BEGIN; + GM_send(m, cur, &msg); + } + Z_sound(msnd2, 128); } else { - memset(ibuf, 0, GM_MAX_INPUT); - imax = GM_MAX_INPUT; - icur = 0; + msg.type = GM_SELECT; + if (cur < 0) abort(); + if (GM_send(m, cur, &msg)) { + Z_sound(msnd2, 128); + } } - input = 1; - Y_enable_text_input(); - msg.type = GM_BEGIN; - GM_send(m, cur, &msg); - } - Z_sound(msnd2, 128); - } else { - msg.type = GM_SELECT; - if (GM_send(m, cur, &msg)) { - Z_sound(msnd2, 128); - } + break; } - break; + } } } lastkey = KEY_UNKNOWN; diff --git a/src/soft/render.c b/src/soft/render.c index 25c3872..705de17 100644 --- a/src/soft/render.c +++ b/src/soft/render.c @@ -356,58 +356,59 @@ static void get_entry_size (const menu_t *m, int i, int *w, int *h) { int type = 0; menu_msg_t msg; msg.type = GM_GETENTRY; - assert(GM_send(m, i, &msg)); - type = msg.integer.i; - switch (type) { - case GM_BUTTON: - case GM_SCROLLER: - case GM_TEXTFIELD: - case GM_TEXTFIELD_BUTTON: - msg.type = GM_GETCAPTION; - if (GM_send(m, i, &msg)) { - x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); - } - break; - case GM_SMALL_BUTTON: - msg.type = GM_GETCAPTION; - if (GM_send(m, i, &msg)) { - x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); - } - break; - default: - assert(0); - } - switch (type) { - case GM_BUTTON: - msg.type = GM_GETSTR; - if (GM_send(m, i, &msg)) { - x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); - } - y = 16; - break; - case GM_SMALL_BUTTON: - msg.type = GM_GETSTR; - if (GM_send(m, i, &msg)) { - x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); - } - y = 12; - break; - case GM_SCROLLER: - x += (SCROLLER_MIDDLE + 2) * 8; - y = 16; - break; - case GM_TEXTFIELD: - case GM_TEXTFIELD_BUTTON: - msg.type = GM_GETSTR; - if (GM_send(m, i, &msg)) { - x += (msg.string.maxlen + 2) * 8; - } else { - x += (TEXTFIELD_MIDDLE + 2) * 8; - } - y = 16; - break; - default: - assert(0); + if (GM_send(m, i, &msg)) { + type = msg.integer.i; + switch (type) { + case GM_BUTTON: + case GM_SCROLLER: + case GM_TEXTFIELD: + case GM_TEXTFIELD_BUTTON: + msg.type = GM_GETCAPTION; + if (GM_send(m, i, &msg)) { + x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); + } + break; + case GM_SMALL_BUTTON: + msg.type = GM_GETCAPTION; + if (GM_send(m, i, &msg)) { + x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); + } + break; + default: + assert(0); + } + switch (type) { + case GM_BUTTON: + msg.type = GM_GETSTR; + if (GM_send(m, i, &msg)) { + x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); + } + y = 16; + break; + case GM_SMALL_BUTTON: + msg.type = GM_GETSTR; + if (GM_send(m, i, &msg)) { + x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); + } + y = 12; + break; + case GM_SCROLLER: + x += (SCROLLER_MIDDLE + 2) * 8; + y = 16; + break; + case GM_TEXTFIELD: + case GM_TEXTFIELD_BUTTON: + msg.type = GM_GETSTR; + if (GM_send(m, i, &msg)) { + x += (msg.string.maxlen + 2) * 8; + } else { + x += (TEXTFIELD_MIDDLE + 2) * 8; + } + y = 16; + break; + default: + assert(0); + } } *w = x; *h = y;