From 7738284b0e6a3b4e5ee066fb5b93f0c7ef77db25 Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Sat, 9 Jul 2016 01:06:33 +1000 Subject: [PATCH] another baby step untangling the menu code --- src/frontend/mame/ui/barcode.cpp | 4 +- src/frontend/mame/ui/filecreate.cpp | 4 +- src/frontend/mame/ui/filesel.cpp | 2 +- src/frontend/mame/ui/menu.cpp | 61 +++++++++++++---------------- src/frontend/mame/ui/menu.h | 32 ++++++++------- src/frontend/mame/ui/selgame.cpp | 10 ++--- src/frontend/mame/ui/swlist.cpp | 6 +-- 7 files changed, 56 insertions(+), 63 deletions(-) diff --git a/src/frontend/mame/ui/barcode.cpp b/src/frontend/mame/ui/barcode.cpp index bd31e625a5a..6fb322b0ed7 100644 --- a/src/frontend/mame/ui/barcode.cpp +++ b/src/frontend/mame/ui/barcode.cpp @@ -60,7 +60,7 @@ void menu_barcode_reader::populate() item_append(current_display_name(), "", current_display_flags(), ITEMREF_SELECT_READER); // append the "New Barcode" item - if (get_selection() == ITEMREF_NEW_BARCODE) + if (get_selection_ref() == ITEMREF_NEW_BARCODE) { buffer.append(m_barcode_buffer); new_barcode = buffer.c_str(); @@ -129,7 +129,7 @@ void menu_barcode_reader::handle() break; case IPT_SPECIAL: - if (get_selection() == ITEMREF_NEW_BARCODE) + if (get_selection_ref() == ITEMREF_NEW_BARCODE) { auto const buflen = std::strlen(m_barcode_buffer); diff --git a/src/frontend/mame/ui/filecreate.cpp b/src/frontend/mame/ui/filecreate.cpp index 7ea9fc53173..80513c27318 100644 --- a/src/frontend/mame/ui/filecreate.cpp +++ b/src/frontend/mame/ui/filecreate.cpp @@ -189,7 +189,7 @@ void menu_file_create::populate() const std::string *new_image_name; // append the "New Image Name" item - if (get_selection() == ITEMREF_NEW_IMAGE_NAME) + if (get_selection_ref() == ITEMREF_NEW_IMAGE_NAME) { buffer = m_filename + "_"; new_image_name = &buffer; @@ -246,7 +246,7 @@ void menu_file_create::handle() break; case IPT_SPECIAL: - if (get_selection() == ITEMREF_NEW_IMAGE_NAME) + if (get_selection_ref() == ITEMREF_NEW_IMAGE_NAME) { input_character(m_filename, event->unichar, &osd_is_valid_filename_char); reset(reset_options::REMEMBER_POSITION); diff --git a/src/frontend/mame/ui/filesel.cpp b/src/frontend/mame/ui/filesel.cpp index fdefe84715d..3343d82367c 100644 --- a/src/frontend/mame/ui/filesel.cpp +++ b/src/frontend/mame/ui/filesel.cpp @@ -460,7 +460,7 @@ void menu_file_selector::handle() if (update_selected) { - const file_selector_entry *cur_selected = (const file_selector_entry *)get_selection(); + file_selector_entry const *const cur_selected(reinterpret_cast(get_selection_ref())); // check for entries which matches our m_filename_buffer: for (auto &entry : m_entrylist) diff --git a/src/frontend/mame/ui/menu.cpp b/src/frontend/mame/ui/menu.cpp index 80895713ba2..623511ae50c 100644 --- a/src/frontend/mame/ui/menu.cpp +++ b/src/frontend/mame/ui/menu.cpp @@ -211,6 +211,8 @@ menu::menu(mame_ui_manager &mui, render_container *_container) , m_event() , m_pool(nullptr) , m_focus(focused_menu::main) + , m_resetpos(0) + , m_resetref(nullptr) { container = _container; @@ -244,12 +246,12 @@ menu::~menu() void menu::reset(reset_options options) { // based on the reset option, set the reset info - resetpos = 0; - resetref = nullptr; + m_resetpos = 0; + m_resetref = nullptr; if (options == reset_options::REMEMBER_POSITION) - resetpos = selected; + m_resetpos = selected; else if (options == reset_options::REMEMBER_REF) - resetref = item[selected].ref; + m_resetref = get_selection_ref(); // reset all the pools and the item.size() back to 0 for (pool *ppool = m_pool; ppool != nullptr; ppool = ppool->next) @@ -368,9 +370,9 @@ void menu::item_append(std::string &&text, std::string &&subtext, UINT32 flags, item.emplace_back(std::move(pitem)); // update the selection if we need to - if (resetpos == index || (resetref != nullptr && resetref == ref)) + if (m_resetpos == index || (m_resetref != nullptr && m_resetref == ref)) selected = index; - if (resetpos == item.size() - 1) + if (m_resetpos == (item.size() - 1)) selected = item.size() - 1; } @@ -420,7 +422,7 @@ const menu::event *menu::process(UINT32 flags, float x0, float y0) } // update the selected item in the event - if (m_event.iptkey != IPT_INVALID && selected >= 0 && selected < item.size()) + if ((m_event.iptkey != IPT_INVALID) && selection_valid()) { m_event.itemref = item[selected].ref; m_event.type = item[selected].type; @@ -462,17 +464,6 @@ void *menu::m_pool_alloc(size_t size) } -//------------------------------------------------- -// get_selection - retrieves the index -// of the currently selected menu item -//------------------------------------------------- - -void *menu::get_selection() -{ - return (selected >= 0 && selected < item.size()) ? item[selected].ref : nullptr; -} - - //------------------------------------------------- // set_selection - changes the index // of the currently selected menu item @@ -482,11 +473,13 @@ void menu::set_selection(void *selected_itemref) { selected = -1; for (int itemnum = 0; itemnum < item.size(); itemnum++) + { if (item[itemnum].ref == selected_itemref) { selected = itemnum; break; } + } } @@ -570,7 +563,7 @@ void menu::draw(UINT32 flags, float origx0, float origy0) if (!customonly) ui().draw_outlined_box(container, x1, y1, x2, y2, UI_BACKGROUND_COLOR); - if (top_line < 0 || selected == 0) + if (top_line < 0 || is_first_selected()) top_line = 0; if (selected >= (top_line + visible_lines)) top_line = selected - (visible_lines / 2); @@ -778,7 +771,7 @@ void menu::draw(UINT32 flags, float origx0, float origy0) } // if there is something special to add, do it by calling the virtual method - custom_render((selected >= 0 && selected < item.size()) ? item[selected].ref : nullptr, customtop, custombottom, x1, y1, x2, y2); + custom_render(get_selection_ref(), customtop, custombottom, x1, y1, x2, y2); } void menu::custom_render(void *selectedref, float top, float bottom, float x, float y, float x2, float y2) @@ -907,7 +900,7 @@ void menu::handle_events(UINT32 flags) { selected = hover; m_event.iptkey = IPT_UI_SELECT; - if (selected == item.size() - 1) + if (is_last_selected()) { m_event.iptkey = IPT_UI_CANCEL; menu::stack_pop(machine()); @@ -927,7 +920,7 @@ void menu::handle_events(UINT32 flags) top_line -= local_menu_event.num_lines; return; } - (selected == 0) ? selected = top_line = item.size() - 1 : selected -= local_menu_event.num_lines; + is_first_selected() ? selected = top_line = item.size() - 1 : selected -= local_menu_event.num_lines; validate_selection(-1); top_line -= (selected <= top_line && top_line != 0); if (selected <= top_line && visitems != visible_lines) @@ -940,7 +933,7 @@ void menu::handle_events(UINT32 flags) top_line += local_menu_event.num_lines; return; } - (selected == item.size() - 1) ? selected = top_line = 0 : selected += local_menu_event.num_lines; + is_last_selected() ? selected = top_line = 0 : selected += local_menu_event.num_lines; validate_selection(1); top_line += (selected >= top_line + visitems + (top_line != 0)); if (selected >= (top_line + visitems + (top_line != 0))) @@ -981,7 +974,7 @@ void menu::handle_keys(UINT32 flags) // if we hit select, return TRUE or pop the stack, depending on the item if (exclusive_input_pressed(IPT_UI_SELECT, 0)) { - if (selected == item.size() - 1) + if (is_last_selected()) { m_event.iptkey = IPT_UI_CANCEL; menu::stack_pop(machine()); @@ -1025,7 +1018,7 @@ void menu::handle_keys(UINT32 flags) top_line--; return; } - (selected == 0) ? selected = top_line = item.size() - 1 : --selected; + is_first_selected() ? selected = top_line = item.size() - 1 : --selected; validate_selection(-1); top_line -= (selected <= top_line && top_line != 0); if (selected <= top_line && visitems != visible_lines) @@ -1040,7 +1033,7 @@ void menu::handle_keys(UINT32 flags) top_line++; return; } - (selected == item.size() - 1) ? selected = top_line = 0 : ++selected; + is_last_selected() ? selected = top_line = 0 : ++selected; validate_selection(1); top_line += (selected >= top_line + visitems + (top_line != 0)); if (selected >= (top_line + visitems + (top_line != 0))) @@ -1484,7 +1477,7 @@ void menu::draw_select_game(UINT32 flags) if (visible_items < visible_lines) visible_lines = visible_items; - if (top_line < 0 || selected == 0) + if (top_line < 0 || is_first_selected()) top_line = 0; if (selected < visible_items && top_line + visible_lines >= visible_items) top_line = visible_items - visible_lines; @@ -1633,13 +1626,13 @@ void menu::draw_select_game(UINT32 flags) x1 = x2; x2 += right_panel_size; - draw_right_panel((selected >= 0 && selected < item.size()) ? item[selected].ref : nullptr, x1, y1, x2, y2); + draw_right_panel(get_selection_ref(), x1, y1, x2, y2); x1 = primary_left - UI_BOX_LR_BORDER; x2 = primary_left + primary_width + UI_BOX_LR_BORDER; // if there is something special to add, do it by calling the virtual method - custom_render((selected >= 0 && selected < item.size()) ? item[selected].ref : nullptr, customtop, custombottom, x1, y1, x2, y2); + custom_render(get_selection_ref(), customtop, custombottom, x1, y1, x2, y2); // return the number of visible lines, minus 1 for top arrow and 1 for bottom arrow visitems = visible_lines - (top_line != 0) - (top_line + visible_lines != visible_items); @@ -1711,7 +1704,7 @@ void menu::handle_main_keys(UINT32 flags) // if we hit select, return TRUE or pop the stack, depending on the item if (exclusive_input_pressed(IPT_UI_SELECT, 0)) { - if (selected == item.size() - 1 && m_focus == focused_menu::main) + if (is_last_selected() && m_focus == focused_menu::main) { m_event.iptkey = IPT_UI_CANCEL; menu::stack_pop(machine()); @@ -1771,7 +1764,7 @@ void menu::handle_main_keys(UINT32 flags) return; } - if (selected == visible_items + 1 || selected == 0 || ui_error) + if (selected == visible_items + 1 || is_first_selected() || ui_error) return; selected--; @@ -1798,7 +1791,7 @@ void menu::handle_main_keys(UINT32 flags) return; } - if (selected == item.size() - 1 || selected == visible_items - 1 || ui_error) + if (is_last_selected() || selected == visible_items - 1 || ui_error) return; selected++; @@ -2744,7 +2737,7 @@ void menu::draw_palette_menu() } // if there is something special to add, do it by calling the virtual method - custom_render((selected >= 0 && selected < item.size()) ? item[selected].ref : nullptr, customtop, custombottom, x1, y1, x2, y2); + custom_render(get_selection_ref(), customtop, custombottom, x1, y1, x2, y2); // return the number of visible lines, minus 1 for top arrow and 1 for bottom arrow visitems = visible_lines - (top_line != 0) - (top_line + visible_lines != item.size()); @@ -2890,7 +2883,7 @@ void menu::draw_dats_menu() } // if there is something special to add, do it by calling the virtual method - custom_render((selected >= 0 && selected < item.size()) ? item[selected].ref : nullptr, customtop, custombottom, x1, y1, x2, y2); + custom_render(get_selection_ref(), customtop, custombottom, x1, y1, x2, y2); // return the number of visible lines, minus 1 for top arrow and 1 for bottom arrow visitems = visible_lines - (top_line != 0) - (top_line + visible_lines != visible_items); diff --git a/src/frontend/mame/ui/menu.h b/src/frontend/mame/ui/menu.h index 38e2367102e..7317150ba9e 100644 --- a/src/frontend/mame/ui/menu.h +++ b/src/frontend/mame/ui/menu.h @@ -53,9 +53,6 @@ public: running_machine &machine() const { return m_ui.machine(); } render_container *container; // render_container we render to - int resetpos; // reset position - void *resetref; // reset reference - int selected; // which item is selected int hover; // which item is being hovered over int visitems; // number of visible items float customtop; // amount of extra height to add at the top @@ -74,12 +71,6 @@ public: // allocate temporary memory from the menu's memory pool void *m_pool_alloc(size_t size); - // retrieves the index of the currently selected menu item - void *get_selection(); - - // changes the index of the currently selected menu item - void set_selection(void *selected_itemref); - // request the specific handling of the game selection main menu bool is_special_main_menu() const; @@ -253,6 +244,7 @@ protected: int l_hover; int totallines; int skip_main_items; + int selected; // which item is selected menu(mame_ui_manager &mui, render_container *container); @@ -268,6 +260,15 @@ protected: focused_menu get_focus() const { return m_focus; } void set_focus(focused_menu focus) { m_focus = focus; } + // retrieves the ref of the currently selected menu item or nullptr + void *get_selection_ref() const { return selection_valid() ? item[selected].ref : nullptr; } + bool selection_valid() const { return (0 <= selected) && (item.size() > selected); } + bool is_first_selected() const { return 0 == selected; } + bool is_last_selected() const { return (item.size() - 1) == selected; } + + // changes the index of the currently selected menu item + void set_selection(void *selected_itemref); + // draw right box float draw_right_box_title(float x1, float y1, float x2, float y2); @@ -348,13 +349,16 @@ private: void extra_text_draw_box(float origx1, float origx2, float origy, float yspan, const char *text, int direction); bool m_special_main_menu; - mame_ui_manager &m_ui; // UI we are attached to - std::unique_ptr m_parent; // pointer to parent menu - bool m_pressed; // mouse button held down + mame_ui_manager &m_ui; // UI we are attached to + std::unique_ptr m_parent; // pointer to parent menu + bool m_pressed; // mouse button held down osd_ticks_t m_repeat; - event m_event; // the UI event that occurred - pool *m_pool; // list of memory pools + event m_event; // the UI event that occurred + pool *m_pool; // list of memory pools focused_menu m_focus; + int m_resetpos; // reset position + void *m_resetref; // reset reference + static std::vector m_old_icons; static std::unique_ptr menu_stack; diff --git a/src/frontend/mame/ui/selgame.cpp b/src/frontend/mame/ui/selgame.cpp index 979902732a6..992cab5c758 100644 --- a/src/frontend/mame/ui/selgame.cpp +++ b/src/frontend/mame/ui/selgame.cpp @@ -145,13 +145,8 @@ menu_select_game::menu_select_game(mame_ui_manager &mui, render_container *conta menu_select_game::~menu_select_game() { std::string error_string, last_driver; - const game_driver *driver = nullptr; - ui_software_info *swinfo = nullptr; - ui_options &mopt = ui().options(); - if (isfavorite()) - swinfo = (selected >= 0 && selected < item.size()) ? (ui_software_info *)item[selected].ref : nullptr; - else - driver = (selected >= 0 && selected < item.size()) ? (const game_driver *)item[selected].ref : nullptr; + game_driver const *const driver(isfavorite() ? nullptr : reinterpret_cast(get_selection_ref())); + ui_software_info *const swinfo(isfavorite() ? reinterpret_cast(get_selection_ref()) : nullptr); if ((FPTR)driver > skip_main_items) last_driver = driver->name; @@ -165,6 +160,7 @@ menu_select_game::~menu_select_game() else if (main_filters::actual == FILTER_YEAR) filter.append(",").append(c_year::ui[c_year::actual]); + ui_options &mopt = ui().options(); mopt.set_value(OPTION_LAST_USED_FILTER, filter.c_str(), OPTION_PRIORITY_CMDLINE, error_string); mopt.set_value(OPTION_LAST_USED_MACHINE, last_driver.c_str(), OPTION_PRIORITY_CMDLINE, error_string); mopt.set_value(OPTION_HIDE_PANELS, ui_globals::panels_status, OPTION_PRIORITY_CMDLINE, error_string); diff --git a/src/frontend/mame/ui/swlist.cpp b/src/frontend/mame/ui/swlist.cpp index e26fc754687..e0703323eea 100644 --- a/src/frontend/mame/ui/swlist.cpp +++ b/src/frontend/mame/ui/swlist.cpp @@ -276,9 +276,9 @@ void menu_software_list::handle() if (update_selected) { // identify the selected entry - const entry_info *cur_selected = ((FPTR)event->itemref != 1) - ? (const entry_info *)get_selection() - : nullptr; + entry_info const *const cur_selected = (FPTR(event->itemref) != 1) + ? reinterpret_cast(get_selection_ref()) + : nullptr; // loop through all entries for (auto &entry : m_entrylist)