diff options
Diffstat (limited to 'editor/quick_open.cpp')
| -rw-r--r-- | editor/quick_open.cpp | 137 |
1 files changed, 49 insertions, 88 deletions
diff --git a/editor/quick_open.cpp b/editor/quick_open.cpp index bcef29dfa6..4af6fb2053 100644 --- a/editor/quick_open.cpp +++ b/editor/quick_open.cpp @@ -32,21 +32,18 @@ #include "core/os/keyboard.h" -void EditorQuickOpen::popup_dialog(const StringName &p_base, bool p_enable_multi, bool p_add_dirs, bool p_dontclear) { - add_directories = p_add_dirs; - popup_centered_ratio(0.6); +void EditorQuickOpen::popup_dialog(const StringName &p_base, bool p_enable_multi, bool p_dontclear) { + base_type = p_base; + search_options->set_select_mode(p_enable_multi ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE); + popup_centered_ratio(0.4); + if (p_dontclear) { search_box->select_all(); } else { search_box->clear(); } - if (p_enable_multi) { - search_options->set_select_mode(Tree::SELECT_MULTI); - } else { - search_options->set_select_mode(Tree::SELECT_SINGLE); - } + search_box->grab_focus(); - base_type = p_base; _update_search(); } @@ -65,7 +62,6 @@ Vector<String> EditorQuickOpen::get_selected_files() const { TreeItem *item = search_options->get_next_selected(search_options->get_root()); while (item) { files.push_back("res://" + item->get_text(0)); - item = search_options->get_next_selected(item); } @@ -93,7 +89,6 @@ void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) { } TreeItem *current = search_options->get_selected(); - TreeItem *item = search_options->get_next_selected(root); while (item) { item->deselect(0); @@ -101,88 +96,55 @@ void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) { } current->select(0); - + current->set_as_cursor(0); } break; } } } -float EditorQuickOpen::_path_cmp(String search, String path) const { - // Exact match. - if (search == path) { - return 1.2f; +float EditorQuickOpen::_score_path(String search, String path) const { + // Positive bias for matches close to the _beginning of the file name_. + String file = path.get_file(); + int pos = file.findn(search); + if (pos != -1) { + return 1.0f - 0.1f * (float(pos) / file.length()); } - // Substring match, with positive bias for matches close to the end of the path. - int pos = path.rfindn(search); + // Positive bias for matches close to the _end of the path_. + String base = path.get_base_dir(); + pos = base.rfindn(search); if (pos != -1) { - return 1.1f + 0.09 / (path.length() - pos + 1); + return 0.9f - 0.1f * (float(base.length() - pos) / base.length()); } - // Similarity. - return path.to_lower().similarity(search.to_lower()); + // Results that contain all characters but not the string. + return path.similarity(search) * 0.8f; } void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture2D>>> &list) { - if (!add_directories) { - for (int i = 0; i < efsd->get_subdir_count(); i++) { - _parse_fs(efsd->get_subdir(i), list); - } + for (int i = 0; i < efsd->get_subdir_count(); i++) { + _parse_fs(efsd->get_subdir(i), list); } - String search_text = search_box->get_text(); - - if (add_directories) { - String path = efsd->get_path(); - if (!path.ends_with("/")) { - path += "/"; - } - if (path != "res://") { - path = path.substr(6, path.length()); - if (search_text.is_subsequence_ofi(path)) { - Pair<String, Ref<Texture2D>> pair; - pair.first = path; - pair.second = search_options->get_theme_icon("folder", "FileDialog"); - - if (search_text != String() && list.size() > 0) { - float this_sim = _path_cmp(search_text, path); - float other_sim = _path_cmp(list[0].first, path); - int pos = 1; - - while (pos < list.size() && this_sim <= other_sim) { - other_sim = _path_cmp(list[pos++].first, path); - } + for (int i = 0; i < efsd->get_file_count(); i++) { + StringName file_type = efsd->get_file_type(i); - pos = this_sim >= other_sim ? pos - 1 : pos; - list.insert(pos, pair); + if (ClassDB::is_parent_class(file_type, base_type)) { + String file = efsd->get_file_path(i); + file = file.substr(6, file.length()); - } else { - list.push_back(pair); - } + if (search_box->get_text().is_subsequence_ofi(file)) { + Pair<String, Ref<Texture2D>> pair; + pair.first = file; + pair.second = search_options->get_theme_icon(search_options->has_theme_icon(file_type, ei) ? file_type : ot, ei); + list.push_back(pair); } } } - for (int i = 0; i < efsd->get_file_count(); i++) { - String file = efsd->get_file_path(i); - file = file.substr(6, file.length()); - - if (ClassDB::is_parent_class(efsd->get_file_type(i), base_type) && (search_text.is_subsequence_ofi(file))) { - Pair<String, Ref<Texture2D>> pair; - pair.first = file; - pair.second = search_options->get_theme_icon((search_options->has_theme_icon(efsd->get_file_type(i), ei) ? efsd->get_file_type(i) : ot), ei); - list.push_back(pair); - } - } - - if (add_directories) { - for (int i = 0; i < efsd->get_subdir_count(); i++) { - _parse_fs(efsd->get_subdir(i), list); - } - } } Vector<Pair<String, Ref<Texture2D>>> EditorQuickOpen::_sort_fs(Vector<Pair<String, Ref<Texture2D>>> &list) { - String search_text = search_box->get_text(); + String search_text = search_box->get_text().to_lower(); Vector<Pair<String, Ref<Texture2D>>> sorted_list; if (search_text == String() || list.size() == 0) { @@ -192,7 +154,7 @@ Vector<Pair<String, Ref<Texture2D>>> EditorQuickOpen::_sort_fs(Vector<Pair<Strin Vector<float> scores; scores.resize(list.size()); for (int i = 0; i < list.size(); i++) { - scores.write[i] = _path_cmp(search_text, list[i].first); + scores.write[i] = _score_path(search_text, list[i].first.to_lower()); } while (list.size() > 0) { @@ -230,19 +192,17 @@ void EditorQuickOpen::_update_search() { ti->set_icon(0, list[i].second); } - if (root->get_children()) { - TreeItem *ti = root->get_children(); - - ti->select(0); - ti->set_as_cursor(0); + TreeItem *result = root->get_children(); + if (result) { + result->select(0); + result->set_as_cursor(0); } - get_ok()->set_disabled(root->get_children() == nullptr); + get_ok()->set_disabled(!result); } void EditorQuickOpen::_confirmed() { - TreeItem *ti = search_options->get_selected(); - if (!ti) { + if (!search_options->get_selected()) { return; } emit_signal("quick_open"); @@ -250,7 +210,7 @@ void EditorQuickOpen::_confirmed() { } void EditorQuickOpen::_theme_changed() { - search_box->set_right_icon(search_options->get_theme_icon("Search", "EditorIcons")); + search_box->set_right_icon(search_options->get_theme_icon("Search", ei)); } void EditorQuickOpen::_notification(int p_what) { @@ -277,23 +237,24 @@ void EditorQuickOpen::_bind_methods() { EditorQuickOpen::EditorQuickOpen() { VBoxContainer *vbc = memnew(VBoxContainer); vbc->connect("theme_changed", callable_mp(this, &EditorQuickOpen::_theme_changed)); - add_child(vbc); + search_box = memnew(LineEdit); - vbc->add_margin_child(TTR("Search:"), search_box); search_box->connect("text_changed", callable_mp(this, &EditorQuickOpen::_text_changed)); search_box->connect("gui_input", callable_mp(this, &EditorQuickOpen::_sbox_input)); + vbc->add_margin_child(TTR("Search:"), search_box); + search_options = memnew(Tree); - vbc->add_margin_child(TTR("Matches:"), search_options, true); - get_ok()->set_text(TTR("Open")); - get_ok()->set_disabled(true); - register_text_enter(search_box); - set_hide_on_ok(false); search_options->connect("item_activated", callable_mp(this, &EditorQuickOpen::_confirmed)); search_options->set_hide_root(true); search_options->set_hide_folding(true); search_options->add_theme_constant_override("draw_guides", 1); + vbc->add_margin_child(TTR("Matches:"), search_options, true); + + get_ok()->set_text(TTR("Open")); + register_text_enter(search_box); + set_hide_on_ok(false); + ei = "EditorIcons"; ot = "Object"; - add_directories = false; } |
