summaryrefslogtreecommitdiff
path: root/editor/quick_open.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'editor/quick_open.cpp')
-rw-r--r--editor/quick_open.cpp330
1 files changed, 146 insertions, 184 deletions
diff --git a/editor/quick_open.cpp b/editor/quick_open.cpp
index bcef29dfa6..7f720d65d0 100644
--- a/editor/quick_open.cpp
+++ b/editor/quick_open.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -32,225 +32,186 @@
#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;
+ allow_multi_select = p_enable_multi;
+ search_options->set_select_mode(allow_multi_select ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
+ popup_centered_clamped(Size2i(600, 440), 0.8f);
+
+ EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
+ _build_search_cache(efsd);
+
if (p_dontclear) {
search_box->select_all();
+ _update_search();
} 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->clear(); // This will emit text_changed.
}
search_box->grab_focus();
- base_type = p_base;
- _update_search();
}
-String EditorQuickOpen::get_selected() const {
- TreeItem *ti = search_options->get_selected();
- if (!ti) {
- return String();
+void EditorQuickOpen::_build_search_cache(EditorFileSystemDirectory *p_efsd) {
+ for (int i = 0; i < p_efsd->get_subdir_count(); i++) {
+ _build_search_cache(p_efsd->get_subdir(i));
}
- return "res://" + ti->get_text(0);
-}
-
-Vector<String> EditorQuickOpen::get_selected_files() const {
- Vector<String> files;
-
- TreeItem *item = search_options->get_next_selected(search_options->get_root());
- while (item) {
- files.push_back("res://" + item->get_text(0));
+ for (int i = 0; i < p_efsd->get_file_count(); i++) {
+ String file_type = p_efsd->get_file_type(i);
+ if (ClassDB::is_parent_class(file_type, base_type)) {
+ String file = p_efsd->get_file_path(i);
+ files.push_back(file.substr(6, file.length()));
- item = search_options->get_next_selected(item);
+ // Store refs to used icons.
+ String ext = file.get_extension();
+ if (!icons.has(ext)) {
+ icons.insert(ext, get_theme_icon((has_theme_icon(file_type, "EditorIcons") ? file_type : "Object"), "EditorIcons"));
+ }
+ }
}
-
- return files;
}
-void EditorQuickOpen::_text_changed(const String &p_newtext) {
- _update_search();
-}
+void EditorQuickOpen::_update_search() {
+ const String search_text = search_box->get_text();
+ const bool empty_search = search_text == "";
+
+ // Filter possible candidates.
+ Vector<Entry> entries;
+ for (int i = 0; i < files.size(); i++) {
+ if (empty_search || search_text.is_subsequence_ofi(files[i])) {
+ Entry r;
+ r.path = files[i];
+ r.score = empty_search ? 0 : _score_path(search_text, files[i].to_lower());
+ entries.push_back(r);
+ }
+ }
-void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
- Ref<InputEventKey> k = p_ie;
- if (k.is_valid()) {
- switch (k->get_keycode()) {
- case KEY_UP:
- case KEY_DOWN:
- case KEY_PAGEUP:
- case KEY_PAGEDOWN: {
- search_options->call("_gui_input", k);
- search_box->accept_event();
+ // Display results
+ TreeItem *root = search_options->get_root();
+ root->clear_children();
- TreeItem *root = search_options->get_root();
- if (!root->get_children()) {
- break;
- }
+ if (entries.size() > 0) {
+ if (!empty_search) {
+ SortArray<Entry, EntryComparator> sorter;
+ sorter.sort(entries.ptrw(), entries.size());
+ }
- TreeItem *current = search_options->get_selected();
+ const int entry_limit = MIN(entries.size(), 300);
+ for (int i = 0; i < entry_limit; i++) {
+ TreeItem *ti = search_options->create_item(root);
+ ti->set_text(0, entries[i].path);
+ ti->set_icon(0, *icons.lookup_ptr(entries[i].path.get_extension()));
+ }
- TreeItem *item = search_options->get_next_selected(root);
- while (item) {
- item->deselect(0);
- item = search_options->get_next_selected(item);
- }
+ TreeItem *to_select = root->get_children();
+ to_select->select(0);
+ to_select->set_as_cursor(0);
+ search_options->scroll_to_item(to_select);
- current->select(0);
+ get_ok_button()->set_disabled(false);
+ } else {
+ search_options->deselect_all();
- } break;
- }
+ get_ok_button()->set_disabled(true);
}
}
-float EditorQuickOpen::_path_cmp(String search, String path) const {
- // Exact match.
- if (search == path) {
- return 1.2f;
+float EditorQuickOpen::_score_path(const String &p_search, const String &p_path) {
+ float score = 0.9f + .1f * (p_search.length() / (float)p_path.length());
+
+ // Positive bias for matches close to the beginning of the file name.
+ String file = p_path.get_file();
+ int pos = file.findn(p_search);
+ if (pos != -1) {
+ return score * (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.
+ pos = p_path.rfindn(p_search);
if (pos != -1) {
- return 1.1f + 0.09 / (path.length() - pos + 1);
+ return score * (0.8f - 0.1f * (float(p_path.length() - pos) / p_path.length()));
}
- // Similarity.
- return path.to_lower().similarity(search.to_lower());
+ // Remaining results belong to the same class of results.
+ return score * 0.69f;
}
-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);
- }
+void EditorQuickOpen::_confirmed() {
+ if (!search_options->get_selected()) {
+ return;
}
+ _cleanup();
+ emit_signal("quick_open");
+ hide();
+}
- 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);
- }
-
- pos = this_sim >= other_sim ? pos - 1 : pos;
- list.insert(pos, pair);
-
- } else {
- 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);
- }
- }
+void EditorQuickOpen::cancel_pressed() {
+ _cleanup();
+}
- if (add_directories) {
- for (int i = 0; i < efsd->get_subdir_count(); i++) {
- _parse_fs(efsd->get_subdir(i), list);
- }
- }
+void EditorQuickOpen::_cleanup() {
+ files.clear();
+ icons.clear();
}
-Vector<Pair<String, Ref<Texture2D>>> EditorQuickOpen::_sort_fs(Vector<Pair<String, Ref<Texture2D>>> &list) {
- String search_text = search_box->get_text();
- Vector<Pair<String, Ref<Texture2D>>> sorted_list;
+void EditorQuickOpen::_text_changed(const String &p_newtext) {
+ _update_search();
+}
- if (search_text == String() || list.size() == 0) {
- return list;
- }
+void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
+ Ref<InputEventKey> k = p_ie;
+ if (k.is_valid()) {
+ switch (k->get_keycode()) {
+ case KEY_UP:
+ case KEY_DOWN:
+ case KEY_PAGEUP:
+ case KEY_PAGEDOWN: {
+ search_options->call("_gui_input", k);
+ search_box->accept_event();
- 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);
- }
+ if (allow_multi_select) {
+ TreeItem *root = search_options->get_root();
+ if (!root->get_children()) {
+ break;
+ }
- while (list.size() > 0) {
- float best_score = 0.0f;
- int best_idx = 0;
+ TreeItem *current = search_options->get_selected();
+ TreeItem *item = search_options->get_next_selected(root);
+ while (item) {
+ item->deselect(0);
+ item = search_options->get_next_selected(item);
+ }
- for (int i = 0; i < list.size(); i++) {
- float current_score = scores[i];
- if (current_score > best_score) {
- best_score = current_score;
- best_idx = i;
- }
+ current->select(0);
+ current->set_as_cursor(0);
+ }
+ } break;
}
-
- sorted_list.push_back(list[best_idx]);
- list.remove(best_idx);
- scores.remove(best_idx);
}
-
- return sorted_list;
}
-void EditorQuickOpen::_update_search() {
- search_options->clear();
- TreeItem *root = search_options->create_item();
- EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
- Vector<Pair<String, Ref<Texture2D>>> list;
-
- _parse_fs(efsd, list);
- list = _sort_fs(list);
-
- for (int i = 0; i < list.size(); i++) {
- TreeItem *ti = search_options->create_item(root);
- ti->set_text(0, list[i].first);
- ti->set_icon(0, list[i].second);
- }
-
- if (root->get_children()) {
- TreeItem *ti = root->get_children();
-
- ti->select(0);
- ti->set_as_cursor(0);
+String EditorQuickOpen::get_selected() const {
+ TreeItem *ti = search_options->get_selected();
+ if (!ti) {
+ return String();
}
- get_ok()->set_disabled(root->get_children() == nullptr);
+ return "res://" + ti->get_text(0);
}
-void EditorQuickOpen::_confirmed() {
- TreeItem *ti = search_options->get_selected();
- if (!ti) {
- return;
+Vector<String> EditorQuickOpen::get_selected_files() const {
+ Vector<String> selected_files;
+
+ TreeItem *item = search_options->get_next_selected(search_options->get_root());
+ while (item) {
+ selected_files.push_back("res://" + item->get_text(0));
+ item = search_options->get_next_selected(item);
}
- emit_signal("quick_open");
- hide();
+
+ return selected_files;
}
-void EditorQuickOpen::_theme_changed() {
- search_box->set_right_icon(search_options->get_theme_icon("Search", "EditorIcons"));
+StringName EditorQuickOpen::get_base_type() const {
+ return base_type;
}
void EditorQuickOpen::_notification(int p_what) {
@@ -266,8 +227,8 @@ void EditorQuickOpen::_notification(int p_what) {
}
}
-StringName EditorQuickOpen::get_base_type() const {
- return base_type;
+void EditorQuickOpen::_theme_changed() {
+ search_box->set_right_icon(search_options->get_theme_icon("Search", "EditorIcons"));
}
void EditorQuickOpen::_bind_methods() {
@@ -275,25 +236,26 @@ void EditorQuickOpen::_bind_methods() {
}
EditorQuickOpen::EditorQuickOpen() {
+ allow_multi_select = false;
+
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));
- search_options = memnew(Tree);
- vbc->add_margin_child(TTR("Matches:"), search_options, true);
- get_ok()->set_text(TTR("Open"));
- get_ok()->set_disabled(true);
+ vbc->add_margin_child(TTR("Search:"), search_box);
register_text_enter(search_box);
- set_hide_on_ok(false);
+
+ search_options = memnew(Tree);
search_options->connect("item_activated", callable_mp(this, &EditorQuickOpen::_confirmed));
+ search_options->create_item();
search_options->set_hide_root(true);
search_options->set_hide_folding(true);
search_options->add_theme_constant_override("draw_guides", 1);
- ei = "EditorIcons";
- ot = "Object";
- add_directories = false;
+ vbc->add_margin_child(TTR("Matches:"), search_options, true);
+
+ get_ok_button()->set_text(TTR("Open"));
+ set_hide_on_ok(false);
}