Simplify menu_iterate.c code

This commit is contained in:
twinaphex 2015-09-25 01:59:20 +02:00
parent bd629776bc
commit 9c031e17ca
2 changed files with 46 additions and 45 deletions

View File

@ -86,16 +86,12 @@ typedef struct
char scratch_buf[PATH_MAX_LENGTH]; char scratch_buf[PATH_MAX_LENGTH];
char scratch2_buf[PATH_MAX_LENGTH]; char scratch2_buf[PATH_MAX_LENGTH];
uint64_t state;
struct struct
{ {
bool fb_is_dirty;
bool do_messagebox;
bool do_pop_stack;
bool do_post_iterate;
bool do_render;
size_t *pop_selected; size_t *pop_selected;
char msg[PATH_MAX_LENGTH]; char msg[PATH_MAX_LENGTH];
} state; } menu_state;
/* Menu display */ /* Menu display */
menu_display_t display; menu_display_t display;

View File

@ -26,6 +26,15 @@
#include "../input/input_common.h" #include "../input/input_common.h"
#include "../input/input_autodetect.h" #include "../input/input_autodetect.h"
enum menu_state_changes
{
MENU_STATE_RENDER_FRAMEBUFFER = 0,
MENU_STATE_RENDER_MESSAGEBOX,
MENU_STATE_BLIT,
MENU_STATE_POP_STACK,
MENU_STATE_POST_ITERATE
};
static int action_iterate_help(char *s, size_t len, const char *label) static int action_iterate_help(char *s, size_t len, const char *label)
{ {
unsigned i; unsigned i;
@ -431,13 +440,10 @@ int menu_iterate(bool render_this_frame, unsigned action)
if (!menu || !menu_list) if (!menu || !menu_list)
return 0; return 0;
menu->state.fb_is_dirty = false; menu->state = 0;
menu->state.do_messagebox = false;
menu->state.do_render = false; menu->menu_state.pop_selected = NULL;
menu->state.do_pop_stack = false; menu->menu_state.msg[0] = '\0';
menu->state.do_post_iterate = false;
menu->state.pop_selected = NULL;
menu->state.msg[0] = '\0';
hash = menu_hash_calculate(label); hash = menu_hash_calculate(label);
@ -446,44 +452,44 @@ int menu_iterate(bool render_this_frame, unsigned action)
if (action != MENU_ACTION_NOOP || menu_entries_needs_refresh() || menu_display_update_pending()) if (action != MENU_ACTION_NOOP || menu_entries_needs_refresh() || menu_display_update_pending())
{ {
if (render_this_frame) if (render_this_frame)
menu->state.fb_is_dirty = true; BIT64_SET(menu->state, MENU_STATE_RENDER_FRAMEBUFFER);
} }
switch (iterate_type) switch (iterate_type)
{ {
case ITERATE_TYPE_HELP: case ITERATE_TYPE_HELP:
ret = action_iterate_help(menu->state.msg, sizeof(menu->state.msg), label); ret = action_iterate_help(menu->menu_state.msg, sizeof(menu->menu_state.msg), label);
if (render_this_frame) if (render_this_frame)
menu->state.do_render = true; BIT64_SET(menu->state, MENU_STATE_BLIT);
menu->state.pop_selected = NULL; menu->menu_state.pop_selected = NULL;
menu->state.do_messagebox = true; BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
menu->state.do_pop_stack = true; BIT64_SET(menu->state, MENU_STATE_POP_STACK);
menu->state.do_post_iterate = true; BIT64_SET(menu->state, MENU_STATE_POST_ITERATE);
if (ret == 1) if (ret == 1)
action = MENU_ACTION_OK; action = MENU_ACTION_OK;
break; break;
case ITERATE_TYPE_BIND: case ITERATE_TYPE_BIND:
if (menu_input_bind_iterate(menu->state.msg, sizeof(menu->state.msg))) if (menu_input_bind_iterate(menu->menu_state.msg, sizeof(menu->menu_state.msg)))
menu_list_pop_stack(menu_list, &nav->selection_ptr); menu_list_pop_stack(menu_list, &nav->selection_ptr);
else else
menu->state.do_messagebox = true; BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
if (render_this_frame) if (render_this_frame)
menu->state.do_render = true; BIT64_SET(menu->state, MENU_STATE_BLIT);
break; break;
case ITERATE_TYPE_VIEWPORT: case ITERATE_TYPE_VIEWPORT:
ret = action_iterate_menu_viewport(menu->state.msg, sizeof(menu->state.msg), label, action, hash); ret = action_iterate_menu_viewport(menu->menu_state.msg, sizeof(menu->menu_state.msg), label, action, hash);
if (render_this_frame) if (render_this_frame)
menu->state.do_render = true; BIT64_SET(menu->state, MENU_STATE_BLIT);
menu->state.do_messagebox = true; BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
break; break;
case ITERATE_TYPE_INFO: case ITERATE_TYPE_INFO:
ret = action_iterate_info(menu->state.msg, sizeof(menu->state.msg), label); ret = action_iterate_info(menu->menu_state.msg, sizeof(menu->menu_state.msg), label);
menu->state.pop_selected = &nav->selection_ptr; menu->menu_state.pop_selected = &nav->selection_ptr;
if (render_this_frame) if (render_this_frame)
menu->state.do_render = true; BIT64_SET(menu->state, MENU_STATE_BLIT);
menu->state.do_messagebox = true; BIT64_SET(menu->state, MENU_STATE_RENDER_MESSAGEBOX);
menu->state.do_pop_stack = true; BIT64_SET(menu->state, MENU_STATE_POP_STACK);
menu->state.do_post_iterate = true; BIT64_SET(menu->state, MENU_STATE_POST_ITERATE);
break; break;
case ITERATE_TYPE_DEFAULT: case ITERATE_TYPE_DEFAULT:
selected = menu_navigation_get_selection(nav); selected = menu_navigation_get_selection(nav);
@ -496,9 +502,9 @@ int menu_iterate(bool render_this_frame, unsigned action)
if (ret) if (ret)
goto end; goto end;
menu->state.do_post_iterate = true; BIT64_SET(menu->state, MENU_STATE_POST_ITERATE);
if (render_this_frame) if (render_this_frame)
menu->state.do_render = true; BIT64_SET(menu->state, MENU_STATE_BLIT);
/* Have to defer it so we let settings refresh. */ /* Have to defer it so we let settings refresh. */
if (menu->push_help_screen) if (menu->push_help_screen)
@ -515,10 +521,10 @@ int menu_iterate(bool render_this_frame, unsigned action)
break; break;
} }
if (menu->state.do_pop_stack && action == MENU_ACTION_OK) if (BIT64_GET(menu->state, MENU_STATE_POP_STACK) && action == MENU_ACTION_OK)
menu_list_pop_stack(menu_list, menu->state.pop_selected); menu_list_pop_stack(menu_list, menu->menu_state.pop_selected);
if (menu->state.do_post_iterate) if (BIT64_GET(menu->state, MENU_STATE_POST_ITERATE))
menu_input_post_iterate(&ret, action); menu_input_post_iterate(&ret, action);
end: end:
@ -535,26 +541,25 @@ int menu_iterate_render(void)
if (!menu) if (!menu)
return -1; return -1;
if (menu->state.fb_is_dirty != menu->state.do_messagebox) if (BIT64_GET(menu->state, MENU_STATE_RENDER_FRAMEBUFFER) != BIT64_GET(menu->state, MENU_STATE_RENDER_MESSAGEBOX))
menu->state.fb_is_dirty = true; BIT64_SET(menu->state, MENU_STATE_RENDER_FRAMEBUFFER);
if (menu->state.fb_is_dirty) if (BIT64_GET(menu->state, MENU_STATE_RENDER_FRAMEBUFFER))
menu_display_fb_set_dirty(); menu_display_fb_set_dirty();
if (BIT64_GET(menu->state, MENU_STATE_RENDER_MESSAGEBOX) && menu->menu_state.msg[0] != '\0')
if (menu->state.do_messagebox && menu->state.msg[0] != '\0')
{ {
if (driver->render_messagebox) if (driver->render_messagebox)
driver->render_messagebox(menu->state.msg); driver->render_messagebox(menu->menu_state.msg);
if (ui_companion_is_on_foreground()) if (ui_companion_is_on_foreground())
{ {
const ui_companion_driver_t *ui = ui_companion_get_ptr(); const ui_companion_driver_t *ui = ui_companion_get_ptr();
if (ui->render_messagebox) if (ui->render_messagebox)
ui->render_messagebox(menu->state.msg); ui->render_messagebox(menu->menu_state.msg);
} }
} }
if (menu->state.do_render) if (BIT64_GET(menu->state, MENU_STATE_BLIT))
{ {
menu_animation_update_time(); menu_animation_update_time();
if (driver->render) if (driver->render)