2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* quick_open.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "quick_open.h"
|
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/keyboard.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-06-27 14:29:44 +00:00
|
|
|
void EditorQuickOpen::popup_dialog(const StringName &p_base, bool p_enable_multi, bool p_dontclear) {
|
|
|
|
base_type = p_base;
|
2020-07-13 19:23:50 +00:00
|
|
|
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);
|
2020-06-27 14:29:44 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_dontclear) {
|
2014-02-10 01:10:30 +00:00
|
|
|
search_box->select_all();
|
2020-07-13 19:23:50 +00:00
|
|
|
_update_search();
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2020-07-13 19:23:50 +00:00
|
|
|
search_box->clear(); // This will emit text_changed.
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
search_box->grab_focus();
|
2016-01-08 11:27:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
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));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
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()));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
// Store refs to used icons.
|
|
|
|
String ext = file.get_extension();
|
|
|
|
if (!icons.has(ext)) {
|
2021-07-17 21:22:52 +00:00
|
|
|
icons.insert(ext, get_theme_icon((has_theme_icon(file_type, SNAME("EditorIcons")) ? file_type : String("Object")), SNAME("EditorIcons")));
|
2020-07-13 19:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 11:27:34 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-01-08 11:27:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
// Display results
|
|
|
|
TreeItem *root = search_options->get_root();
|
|
|
|
root->clear_children();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
if (entries.size() > 0) {
|
|
|
|
if (!empty_search) {
|
|
|
|
SortArray<Entry, EntryComparator> sorter;
|
|
|
|
sorter.sort(entries.ptrw(), entries.size());
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
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()));
|
|
|
|
}
|
2016-01-17 13:25:15 +00:00
|
|
|
|
2021-03-07 20:07:30 +00:00
|
|
|
TreeItem *to_select = root->get_first_child();
|
2020-07-13 19:23:50 +00:00
|
|
|
to_select->select(0);
|
|
|
|
to_select->set_as_cursor(0);
|
|
|
|
search_options->scroll_to_item(to_select);
|
2016-01-17 13:25:15 +00:00
|
|
|
|
2020-12-14 18:37:30 +00:00
|
|
|
get_ok_button()->set_disabled(false);
|
2020-07-13 19:23:50 +00:00
|
|
|
} else {
|
|
|
|
search_options->deselect_all();
|
2016-01-17 13:25:15 +00:00
|
|
|
|
2020-12-14 18:37:30 +00:00
|
|
|
get_ok_button()->set_disabled(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
float EditorQuickOpen::_score_path(const String &p_search, const String &p_path) {
|
|
|
|
float score = 0.9f + .1f * (p_search.length() / (float)p_path.length());
|
|
|
|
|
2021-07-21 15:35:25 +00:00
|
|
|
// Exact match.
|
|
|
|
if (p_search == p_path) {
|
|
|
|
return 1.2f;
|
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
// Positive bias for matches close to the beginning of the file name.
|
|
|
|
String file = p_path.get_file();
|
|
|
|
int pos = file.findn(p_search);
|
2020-06-27 20:46:42 +00:00
|
|
|
if (pos != -1) {
|
2020-07-13 19:23:50 +00:00
|
|
|
return score * (1.0f - 0.1f * (float(pos) / file.length()));
|
2016-07-06 22:29:15 +00:00
|
|
|
}
|
2019-11-12 18:45:14 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
// Positive bias for matches close to the end of the path.
|
|
|
|
pos = p_path.rfindn(p_search);
|
2019-11-12 18:45:14 +00:00
|
|
|
if (pos != -1) {
|
2021-07-21 15:35:25 +00:00
|
|
|
return 1.1f + 0.09 / (p_path.length() - pos + 1);
|
2016-07-06 22:29:15 +00:00
|
|
|
}
|
2019-11-12 18:45:14 +00:00
|
|
|
|
2021-07-21 15:35:25 +00:00
|
|
|
// Similarity
|
|
|
|
return p_path.to_lower().similarity(p_search.to_lower());
|
2016-07-06 22:29:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
void EditorQuickOpen::_confirmed() {
|
|
|
|
if (!search_options->get_selected()) {
|
|
|
|
return;
|
2017-09-02 14:17:59 +00:00
|
|
|
}
|
2020-07-13 19:23:50 +00:00
|
|
|
_cleanup();
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("quick_open"));
|
2020-07-13 19:23:50 +00:00
|
|
|
hide();
|
2017-09-02 14:17:59 +00:00
|
|
|
}
|
2016-06-30 18:47:13 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
void EditorQuickOpen::cancel_pressed() {
|
|
|
|
_cleanup();
|
|
|
|
}
|
2016-06-30 18:47:13 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
void EditorQuickOpen::_cleanup() {
|
|
|
|
files.clear();
|
|
|
|
icons.clear();
|
|
|
|
}
|
2016-06-30 18:47:13 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
void EditorQuickOpen::_text_changed(const String &p_newtext) {
|
|
|
|
_update_search();
|
|
|
|
}
|
2016-06-30 18:47:13 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
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();
|
2015-08-23 23:15:56 +00:00
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
if (allow_multi_select) {
|
|
|
|
TreeItem *root = search_options->get_root();
|
2021-03-07 20:07:30 +00:00
|
|
|
if (!root->get_first_child()) {
|
2020-07-13 19:23:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
current->select(0);
|
|
|
|
current->set_as_cursor(0);
|
|
|
|
}
|
|
|
|
} break;
|
2015-08-23 23:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
String EditorQuickOpen::get_selected() const {
|
|
|
|
TreeItem *ti = search_options->get_selected();
|
|
|
|
if (!ti) {
|
|
|
|
return String();
|
2016-01-17 13:25:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
return "res://" + ti->get_text(0);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
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);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-07-13 19:23:50 +00:00
|
|
|
|
|
|
|
return selected_files;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
StringName EditorQuickOpen::get_base_type() const {
|
|
|
|
return base_type;
|
2020-03-06 17:00:16 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void EditorQuickOpen::_notification(int p_what) {
|
2019-03-17 00:47:57 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2020-02-21 17:28:45 +00:00
|
|
|
connect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
|
2019-03-17 00:47:57 +00:00
|
|
|
|
|
|
|
search_box->set_clear_button_enabled(true);
|
2020-04-08 08:38:50 +00:00
|
|
|
} break;
|
2019-03-17 00:47:57 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2020-02-21 17:28:45 +00:00
|
|
|
disconnect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
|
2019-03-17 00:47:57 +00:00
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 19:23:50 +00:00
|
|
|
void EditorQuickOpen::_theme_changed() {
|
2021-07-17 21:22:52 +00:00
|
|
|
search_box->set_right_icon(search_options->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorQuickOpen::_bind_methods() {
|
2016-01-08 11:27:34 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("quick_open"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorQuickOpen::EditorQuickOpen() {
|
2020-07-13 19:23:50 +00:00
|
|
|
allow_multi_select = false;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VBoxContainer *vbc = memnew(VBoxContainer);
|
2020-03-06 17:00:16 +00:00
|
|
|
vbc->connect("theme_changed", callable_mp(this, &EditorQuickOpen::_theme_changed));
|
2014-02-10 01:10:30 +00:00
|
|
|
add_child(vbc);
|
2020-06-27 14:29:44 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
search_box = memnew(LineEdit);
|
2020-02-21 17:28:45 +00:00
|
|
|
search_box->connect("text_changed", callable_mp(this, &EditorQuickOpen::_text_changed));
|
|
|
|
search_box->connect("gui_input", callable_mp(this, &EditorQuickOpen::_sbox_input));
|
2020-06-27 14:29:44 +00:00
|
|
|
vbc->add_margin_child(TTR("Search:"), search_box);
|
2020-07-13 19:23:50 +00:00
|
|
|
register_text_enter(search_box);
|
2020-06-27 14:29:44 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
search_options = memnew(Tree);
|
2020-06-27 14:29:44 +00:00
|
|
|
search_options->connect("item_activated", callable_mp(this, &EditorQuickOpen::_confirmed));
|
2020-07-13 19:23:50 +00:00
|
|
|
search_options->create_item();
|
2020-06-27 14:29:44 +00:00
|
|
|
search_options->set_hide_root(true);
|
|
|
|
search_options->set_hide_folding(true);
|
|
|
|
search_options->add_theme_constant_override("draw_guides", 1);
|
2017-03-05 15:44:50 +00:00
|
|
|
vbc->add_margin_child(TTR("Matches:"), search_options, true);
|
2020-06-27 14:29:44 +00:00
|
|
|
|
2020-12-14 18:37:30 +00:00
|
|
|
get_ok_button()->set_text(TTR("Open"));
|
2014-02-10 01:10:30 +00:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
}
|