From 57d7d67812ad55fa3d72bc5a635feb8118e89de3 Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Wed, 23 Nov 2016 13:31:33 +1100 Subject: [PATCH] a bit of prep for making selected index private (nw) --- src/frontend/mame/ui/custui.cpp | 8 ++++---- src/frontend/mame/ui/dirmenu.cpp | 6 +++--- src/frontend/mame/ui/menu.cpp | 22 +++++++++++----------- src/frontend/mame/ui/menu.h | 13 +++++++++---- src/frontend/mame/ui/selgame.cpp | 2 +- src/frontend/mame/ui/selmenu.cpp | 18 +++++++++--------- 6 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/frontend/mame/ui/custui.cpp b/src/frontend/mame/ui/custui.cpp index fea51106f07..17a5c22539b 100644 --- a/src/frontend/mame/ui/custui.cpp +++ b/src/frontend/mame/ui/custui.cpp @@ -485,7 +485,7 @@ void menu_colors_ui::handle() if (menu_event != nullptr && menu_event->itemref != nullptr && menu_event->iptkey == IPT_UI_SELECT) { if ((uintptr_t)menu_event->itemref != MUI_RESTORE) - menu::stack_push(ui(), container(), &m_color_table[(uintptr_t)menu_event->itemref].color, item[selected].text); + menu::stack_push(ui(), container(), &m_color_table[(uintptr_t)menu_event->itemref].color, selected_item().text); else { changed = true; @@ -1024,7 +1024,7 @@ void menu_palette_sel::handle() { if (menu_event->iptkey == IPT_UI_SELECT) { - m_original = rgb_t((uint32_t)strtoul(item[selected].subtext.c_str(), nullptr, 16)); + m_original = rgb_t(uint32_t(strtoul(selected_item().subtext.c_str(), nullptr, 16))); reset_parent(reset_options::SELECT_FIRST); stack_pop(); } @@ -1118,7 +1118,7 @@ void menu_palette_sel::draw(uint32_t flags) ui().draw_outlined_box(container(), x1, y1, x2, y2, UI_BACKGROUND_COLOR); // determine the first visible line based on the current selection - int top_line = selected - visible_lines / 2; + int top_line = selected_index() - visible_lines / 2; if (top_line < 0) top_line = 0; if (top_line + visible_lines >= item.size()) @@ -1151,7 +1151,7 @@ void menu_palette_sel::draw(uint32_t flags) rgb_t fgcolor = UI_TEXT_COLOR; rgb_t bgcolor = UI_TEXT_BG_COLOR; - if (itemnum == selected) + if (is_selected(itemnum)) { // if we're selected, draw with a different background fgcolor = UI_SELECTED_COLOR; diff --git a/src/frontend/mame/ui/dirmenu.cpp b/src/frontend/mame/ui/dirmenu.cpp index a8c5ffc20d2..1ef39925bfd 100644 --- a/src/frontend/mame/ui/dirmenu.cpp +++ b/src/frontend/mame/ui/dirmenu.cpp @@ -91,7 +91,7 @@ void menu_directory::handle() const event *menu_event = process(0); if (menu_event != nullptr && menu_event->itemref != nullptr && menu_event->iptkey == IPT_UI_SELECT) - menu::stack_push(ui(), container(), selected); + menu::stack_push(ui(), container(), selected_index()); } //------------------------------------------------- @@ -391,7 +391,7 @@ void menu_add_change_folder::handle() // check for entries which matches our search buffer if (update_selected) { - const int cur_selected = selected; + const int cur_selected = selected_index(); int entry, bestmatch = 0; // from current item to the end @@ -583,7 +583,7 @@ void menu_remove_folder::handle() if (menu_event != nullptr && menu_event->itemref != nullptr && menu_event->iptkey == IPT_UI_SELECT) { std::string tmppath, error_string; - m_folders.erase(m_folders.begin() + selected); + m_folders.erase(m_folders.begin() + selected_index()); for (int x = 0; x < m_folders.size(); ++x) { tmppath.append(m_folders[x]); diff --git a/src/frontend/mame/ui/menu.cpp b/src/frontend/mame/ui/menu.cpp index eafc5e2d40f..50703ac99e9 100644 --- a/src/frontend/mame/ui/menu.cpp +++ b/src/frontend/mame/ui/menu.cpp @@ -449,7 +449,7 @@ const menu::event *menu::process(uint32_t flags, float x0, float y0) // update the selected item in the event if ((m_event.iptkey != IPT_INVALID) && selection_valid()) { - m_event.itemref = item[selected].ref; + m_event.itemref = get_selection_ref(); m_event.type = item[selected].type; return &m_event; } @@ -591,7 +591,7 @@ void menu::draw(uint32_t flags) top_line = 0; if (selected >= (top_line + m_visible_lines)) top_line = selected - (m_visible_lines / 2); - if ((top_line > (item.size() - m_visible_lines)) || (selected == (item.size() - 1))) + if ((top_line > (item.size() - m_visible_lines)) || is_last_selected()) top_line = item.size() - m_visible_lines; // if scrolling, show arrows @@ -635,7 +635,7 @@ void menu::draw(uint32_t flags) hover = itemnum; // if we're selected, draw with a different background - if (itemnum == selected) + if (is_selected(itemnum)) { fgcolor = fgcolor2 = fgcolor3 = UI_SELECTED_COLOR; bgcolor = UI_SELECTED_BG_COLOR; @@ -713,7 +713,7 @@ void menu::draw(uint32_t flags) if (ui().get_string_width(subitem_text) > effective_width - item_width) { subitem_text = "..."; - if (itemnum == selected) + if (is_selected(itemnum)) selected_subitem_too_big = true; } @@ -732,7 +732,7 @@ void menu::draw(uint32_t flags) ui::text_layout::RIGHT, ui::text_layout::TRUNCATE, mame_ui_manager::NORMAL, subitem_invert ? fgcolor3 : fgcolor2, bgcolor, &subitem_width, nullptr); // apply arrows - if (itemnum == selected && (pitem.flags & FLAG_LEFT_ARROW)) + if (is_selected(itemnum) && (pitem.flags & FLAG_LEFT_ARROW)) { draw_arrow( effective_left + effective_width - subitem_width - gutter_width, @@ -742,7 +742,7 @@ void menu::draw(uint32_t flags) fgcolor, ROT90 ^ ORIENTATION_FLIP_X); } - if (itemnum == selected && (pitem.flags & FLAG_RIGHT_ARROW)) + if (is_selected(itemnum) && (pitem.flags & FLAG_RIGHT_ARROW)) { draw_arrow( effective_left + effective_width + gutter_width - lr_arrow_width, @@ -759,7 +759,7 @@ void menu::draw(uint32_t flags) // if the selected subitem is too big, display it in a separate offset box if (selected_subitem_too_big) { - menu_item const &pitem = item[selected]; + menu_item const &pitem = selected_item(); bool const subitem_invert(pitem.flags & FLAG_INVERT); auto const linenum = selected - top_line; float const line_y = visible_top + (float)linenum * line_height; @@ -934,7 +934,7 @@ void menu::handle_events(uint32_t flags, event &ev) top_line += m_visible_lines - 2; return; } - selected += m_visible_lines - 2 + (selected == 0); + selected += m_visible_lines - 2 + is_first_selected(); if (selected > item.size() - 1) selected = item.size() - 1; top_line += m_visible_lines - 2; @@ -1046,8 +1046,8 @@ void menu::handle_keys(uint32_t flags, int &iptkey) validate_selection(1); // swallow left/right keys if they are not appropriate - bool ignoreleft = ((item[selected].flags & FLAG_LEFT_ARROW) == 0); - bool ignoreright = ((item[selected].flags & FLAG_RIGHT_ARROW) == 0); + bool ignoreleft = ((selected_item().flags & FLAG_LEFT_ARROW) == 0); + bool ignoreright = ((selected_item().flags & FLAG_RIGHT_ARROW) == 0); if ((item[0].flags & FLAG_UI_DATS)) ignoreleft = ignoreright = false; @@ -1101,7 +1101,7 @@ void menu::handle_keys(uint32_t flags, int &iptkey) // page down advances by m_visible_items if (exclusive_input_pressed(iptkey, IPT_UI_PAGE_DOWN, 6)) { - selected += m_visible_lines - 2 + (selected == 0); + selected += m_visible_lines - 2 + is_first_selected(); top_line += m_visible_lines - 2; if (selected > item.size() - 1) diff --git a/src/frontend/mame/ui/menu.h b/src/frontend/mame/ui/menu.h index 1aa4d8f2697..a4e4d8a7c26 100644 --- a/src/frontend/mame/ui/menu.h +++ b/src/frontend/mame/ui/menu.h @@ -50,9 +50,6 @@ public: virtual ~menu(); - int hover; // which item is being hovered over - std::vector item; // array of items - // append a new item to the end of the menu void item_append(const std::string &text, const std::string &subtext, uint32_t flags, void *ref, menu_item_type type = menu_item_type::UNKNOWN); void item_append(std::string &&text, std::string &&subtext, uint32_t flags, void *ref, menu_item_type type = menu_item_type::UNKNOWN); @@ -131,6 +128,9 @@ protected: render_bounds mouse; // mouse position if iptkey == IPT_CUSTOM }; + int hover; // which item is being hovered over + std::vector item; // array of items + int top_line; // main box top line int l_sw_hover; int l_hover; @@ -170,7 +170,12 @@ protected: // retrieves the ref of the currently selected menu item or nullptr void *get_selection_ref() const { return selection_valid() ? item[selected].ref : nullptr; } + + menu_item &selected_item() { return item[selected]; } + menu_item const &selected_item() const { return item[selected]; } + int selected_index() const { return selected; } bool selection_valid() const { return (0 <= selected) && (item.size() > selected); } + bool is_selected(int index) const { return selection_valid() && (selected == index); } bool is_first_selected() const { return 0 == selected; } bool is_last_selected() const { return (item.size() - 1) == selected; } @@ -235,7 +240,7 @@ protected: // get arrows status template - uint32_t get_arrow_flags(T min, T max, T actual) + static uint32_t get_arrow_flags(T min, T max, T actual) { return ((actual > min) ? FLAG_LEFT_ARROW : 0) | ((actual < max) ? FLAG_RIGHT_ARROW : 0); } diff --git a/src/frontend/mame/ui/selgame.cpp b/src/frontend/mame/ui/selgame.cpp index ad6bc75b383..440985aaccf 100644 --- a/src/frontend/mame/ui/selgame.cpp +++ b/src/frontend/mame/ui/selgame.cpp @@ -184,7 +184,7 @@ void menu_select_game::handle() // if i have to reselect a software, force software list submenu if (reselect_last::get()) { - const game_driver *driver = (const game_driver *)item[selected].ref; + const game_driver *const driver = reinterpret_cast(get_selection_ref()); menu::stack_push(ui(), container(), driver); return; } diff --git a/src/frontend/mame/ui/selmenu.cpp b/src/frontend/mame/ui/selmenu.cpp index 97d52902391..84c0300d628 100644 --- a/src/frontend/mame/ui/selmenu.cpp +++ b/src/frontend/mame/ui/selmenu.cpp @@ -413,7 +413,7 @@ void menu_select_launch::inkey_navigation() case focused_menu::main: if (selected <= visible_items) { - m_prev_selected = item[selected].ref; + m_prev_selected = get_selection_ref(); selected = visible_items + 1; } else @@ -794,8 +794,8 @@ void menu_select_launch::handle_keys(uint32_t flags, int &iptkey) validate_selection(1); // swallow left/right keys if they are not appropriate - bool ignoreleft = ((item[selected].flags & FLAG_LEFT_ARROW) == 0); - bool ignoreright = ((item[selected].flags & FLAG_RIGHT_ARROW) == 0); + bool ignoreleft = ((selected_item().flags & FLAG_LEFT_ARROW) == 0); + bool ignoreright = ((selected_item().flags & FLAG_RIGHT_ARROW) == 0); bool leftclose = (ui_globals::panels_status == HIDE_BOTH || ui_globals::panels_status == HIDE_LEFT_PANEL); bool rightclose = (ui_globals::panels_status == HIDE_BOTH || ui_globals::panels_status == HIDE_RIGHT_PANEL); @@ -1017,7 +1017,7 @@ void menu_select_launch::handle_events(uint32_t flags, event &ev) if (hover >= 0 && hover < item.size()) { if (hover >= visible_items - 1 && selected < visible_items) - m_prev_selected = item[selected].ref; + m_prev_selected = get_selection_ref(); selected = hover; m_focus = focused_menu::main; } @@ -1111,7 +1111,7 @@ void menu_select_launch::handle_events(uint32_t flags, event &ev) ev.iptkey = IPT_UI_SELECT; } - if (selected == item.size() - 1) + if (is_last_selected()) { ev.iptkey = IPT_UI_CANCEL; stack_pop(); @@ -1125,7 +1125,7 @@ void menu_select_launch::handle_events(uint32_t flags, event &ev) { if (local_menu_event.zdelta > 0) { - if (selected >= visible_items || selected == 0 || m_ui_error) + if (selected >= visible_items || is_first_selected() || m_ui_error) break; selected -= local_menu_event.num_lines; if (selected < top_line + (top_line != 0)) @@ -1163,7 +1163,7 @@ void menu_select_launch::handle_events(uint32_t flags, event &ev) if (hover >= 0 && hover < item.size() - skip_main_items - 1) { selected = hover; - m_prev_selected = item[selected].ref; + m_prev_selected = get_selection_ref(); m_focus = focused_menu::main; ev.iptkey = IPT_CUSTOM; ev.mouse.x0 = local_menu_event.mouse_x; @@ -1275,7 +1275,7 @@ void menu_select_launch::draw(uint32_t flags) hover = itemnum; // if we're selected, draw with a different background - if (itemnum == selected && m_focus == focused_menu::main) + if (is_selected(itemnum) && m_focus == focused_menu::main) { fgcolor = rgb_t(0xff, 0xff, 0x00); bgcolor = rgb_t(0xff, 0xff, 0xff); @@ -1366,7 +1366,7 @@ void menu_select_launch::draw(uint32_t flags) hover = count; // if we're selected, draw with a different background - if (count == selected && m_focus == focused_menu::main) + if (is_selected(count) && m_focus == focused_menu::main) { fgcolor = rgb_t(0xff, 0xff, 0x00); bgcolor = rgb_t(0xff, 0xff, 0xff);