diff options
Diffstat (limited to 'src/frontend/mame/ui/selector.cpp')
-rw-r--r-- | src/frontend/mame/ui/selector.cpp | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/src/frontend/mame/ui/selector.cpp b/src/frontend/mame/ui/selector.cpp index 6a583346400..f8791a37887 100644 --- a/src/frontend/mame/ui/selector.cpp +++ b/src/frontend/mame/ui/selector.cpp @@ -27,10 +27,12 @@ namespace ui { menu_selector::menu_selector( mame_ui_manager &mui, render_container &container, + std::string &&title, std::vector<std::string> &&sel, int initial, std::function<void (int)> &&handler) : menu(mui, container) + , m_title(std::move(title)) , m_search() , m_str_items(std::move(sel)) , m_handler(std::move(handler)) @@ -50,30 +52,36 @@ menu_selector::~menu_selector() void menu_selector::handle(event const *ev) { // process the menu - if (ev && ev->itemref) + if (ev) { - if (ev->iptkey == IPT_UI_SELECT) + switch (ev->iptkey) { - int selection(-1); - for (size_t idx = 0; (m_str_items.size() > idx) && (0 > selection); ++idx) - if ((void*)&m_str_items[idx] == ev->itemref) - selection = int(unsigned(idx)); + case IPT_UI_SELECT: + if (ev->itemref) + { + int selection(-1); + for (size_t idx = 0; (m_str_items.size() > idx) && (0 > selection); ++idx) + if ((void*)&m_str_items[idx] == ev->itemref) + selection = int(unsigned(idx)); - m_handler(selection); + m_handler(selection); + stack_pop(); + } + break; - stack_pop(); - } - else if (ev->iptkey == IPT_SPECIAL) - { + case IPT_SPECIAL: if (input_character(m_search, ev->unichar, uchar_is_printable)) reset(reset_options::SELECT_FIRST); - } + break; - // escape pressed with non-empty text clears the text - else if (ev->iptkey == IPT_UI_CANCEL && !m_search.empty()) - { - m_search.clear(); - reset(reset_options::SELECT_FIRST); + case IPT_UI_CANCEL: + if (!m_search.empty()) + { + // escape pressed with non-empty search text clears the search text + m_search.clear(); + reset(reset_options::SELECT_FIRST); + } + break; } } } @@ -84,12 +92,17 @@ void menu_selector::handle(event const *ev) void menu_selector::populate(float &customtop, float &custombottom) { + set_heading(util::string_format(_("menu-selector", "%1$s - Search: %2$s_"), m_title, m_search)); + if (!m_search.empty()) { find_matches(m_search.c_str()); - for (int curitem = 0; m_searchlist[curitem]; ++curitem) + int curitem; + for (curitem = 0; m_searchlist[curitem]; ++curitem) item_append(*m_searchlist[curitem], 0, (void *)m_searchlist[curitem]); + if (!curitem) + item_append(_("menu-selector", "[no matches]"), FLAG_DISABLE, nullptr); } else { @@ -100,10 +113,13 @@ void menu_selector::populate(float &customtop, float &custombottom) item_append(m_str_items[index], 0, (void *)&m_str_items[index]); } + + if (m_str_items.empty()) + item_append(_("menu-selector", "[no choices]"), FLAG_DISABLE, nullptr); // the caller was probably being dumb } item_append(menu_item_type::SEPARATOR); - customtop = custombottom = ui().get_line_height() + 3.0f * ui().box_tb_border(); + custombottom = ui().get_line_height() + 3.0f * ui().box_tb_border(); m_initial = -1; } @@ -113,15 +129,8 @@ void menu_selector::populate(float &customtop, float &custombottom) void menu_selector::custom_render(void *selectedref, float top, float bottom, float origx1, float origy1, float origx2, float origy2) { - std::string tempbuf[1] = { std::string(_("Selection List - Search: ")).append(m_search).append("_") }; - draw_text_box( - std::begin(tempbuf), std::end(tempbuf), - origx1, origx2, origy1 - top, origy1 - ui().box_tb_border(), - text_layout::text_justify::CENTER, text_layout::word_wrapping::TRUNCATE, false, - ui().colors().text_color(), UI_GREEN_COLOR, 1.0f); - // get the text for 'UI Select' - tempbuf[0] = string_format(_("Double-click or press %1$s to select"), ui().get_general_input_setting(IPT_UI_SELECT)); + std::string const tempbuf[] = { util::string_format(_("menu-selector", "Double-click or press %1$s to select"), ui().get_general_input_setting(IPT_UI_SELECT)) }; draw_text_box( std::begin(tempbuf), std::end(tempbuf), origx1, origx2, origy2 + ui().box_tb_border(), origy2 + bottom, @@ -137,7 +146,7 @@ void menu_selector::find_matches(const char *str) { // allocate memory to track the penalty value m_ucs_items.reserve(m_str_items.size()); - std::vector<double> penalty(VISIBLE_GAMES_IN_SEARCH, 1.0); + std::vector<double> penalty(VISIBLE_SEARCH_ITEMS, 2.0); // impossibly high penalty for unpopulated slots std::u32string const search(ustr_from_utf8(normalize_unicode(str, unicode_normalization_form::D, true))); int index = 0; @@ -149,14 +158,14 @@ void menu_selector::find_matches(const char *str) double const curpenalty(util::edit_distance(search, m_ucs_items[index])); // insert into the sorted table of matches - for (int matchnum = VISIBLE_GAMES_IN_SEARCH - 1; matchnum >= 0; --matchnum) + for (int matchnum = VISIBLE_SEARCH_ITEMS - 1; matchnum >= 0; --matchnum) { // stop if we're worse than the current entry if (curpenalty >= penalty[matchnum]) break; // as long as this isn't the last entry, bump this one down - if (matchnum < VISIBLE_GAMES_IN_SEARCH - 1) + if (matchnum < VISIBLE_SEARCH_ITEMS - 1) { penalty[matchnum + 1] = penalty[matchnum]; m_searchlist[matchnum + 1] = m_searchlist[matchnum]; @@ -166,7 +175,7 @@ void menu_selector::find_matches(const char *str) penalty[matchnum] = curpenalty; } } - (index < VISIBLE_GAMES_IN_SEARCH) ? m_searchlist[index] = nullptr : m_searchlist[VISIBLE_GAMES_IN_SEARCH] = nullptr; + (index < VISIBLE_SEARCH_ITEMS) ? m_searchlist[index] = nullptr : m_searchlist[VISIBLE_SEARCH_ITEMS] = nullptr; } } // namespace ui |