2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* editor_quick_open.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-05 00:50:27 +01:00
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#include "editor_quick_open.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/keyboard.h"
|
2022-06-26 02:07:01 +02:00
|
|
|
#include "editor/editor_node.h"
|
2024-06-29 16:59:33 +02:00
|
|
|
#include "editor/editor_string_names.h"
|
2024-01-15 13:14:55 +01:00
|
|
|
#include "editor/themes/editor_scale.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-05-17 17:22:26 +02:00
|
|
|
Rect2i EditorQuickOpen::prev_rect = Rect2i();
|
|
|
|
bool EditorQuickOpen::was_showed = false;
|
2022-12-01 15:08:07 +01:00
|
|
|
|
|
|
|
void EditorQuickOpen::popup_dialog(const String &p_base, bool p_enable_multi, bool p_dont_clear) {
|
2020-06-27 16:29:44 +02:00
|
|
|
base_type = p_base;
|
2020-07-13 21:23:50 +02:00
|
|
|
allow_multi_select = p_enable_multi;
|
|
|
|
search_options->set_select_mode(allow_multi_select ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
|
2022-11-07 13:44:14 +01:00
|
|
|
|
2022-12-01 15:08:07 +01:00
|
|
|
if (was_showed) {
|
|
|
|
popup(prev_rect);
|
2022-11-07 13:44:14 +01:00
|
|
|
} else {
|
2022-12-01 15:08:07 +01:00
|
|
|
popup_centered_clamped(Size2(600, 440) * EDSCALE, 0.8f);
|
2022-11-07 13:44:14 +01:00
|
|
|
}
|
2020-07-13 21:23:50 +02:00
|
|
|
|
|
|
|
EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
|
|
|
|
_build_search_cache(efsd);
|
2020-06-27 16:29:44 +02:00
|
|
|
|
2022-12-01 15:08:07 +01:00
|
|
|
if (p_dont_clear) {
|
2014-02-10 02:10:30 +01:00
|
|
|
search_box->select_all();
|
2020-07-13 21:23:50 +02:00
|
|
|
_update_search();
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2020-07-13 21:23:50 +02:00
|
|
|
search_box->clear(); // This will emit text_changed.
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
search_box->grab_focus();
|
2016-01-08 12:27:34 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:23:50 +02: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 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-11-15 15:12:42 +01:00
|
|
|
Vector<String> base_types = base_type.split(",");
|
2020-07-13 21:23:50 +02:00
|
|
|
for (int i = 0; i < p_efsd->get_file_count(); i++) {
|
2022-06-26 02:07:01 +02:00
|
|
|
String file = p_efsd->get_file_path(i);
|
|
|
|
String engine_type = p_efsd->get_file_type(i);
|
2023-01-19 19:12:25 +01:00
|
|
|
String script_type = p_efsd->get_file_resource_script_class(i);
|
2022-06-26 02:07:01 +02:00
|
|
|
String actual_type = script_type.is_empty() ? engine_type : script_type;
|
2023-03-31 21:17:59 +02:00
|
|
|
|
2021-10-22 23:27:46 +02:00
|
|
|
// Iterate all possible base types.
|
|
|
|
for (String &parent_type : base_types) {
|
2022-06-26 02:07:01 +02:00
|
|
|
if (ClassDB::is_parent_class(engine_type, parent_type) || EditorNode::get_editor_data().script_class_is_parent(script_type, parent_type)) {
|
2021-10-22 23:27:46 +02:00
|
|
|
files.push_back(file.substr(6, file.length()));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-22 23:27:46 +02:00
|
|
|
// Store refs to used icons.
|
|
|
|
String ext = file.get_extension();
|
|
|
|
if (!icons.has(ext)) {
|
2023-03-31 21:17:59 +02:00
|
|
|
icons.insert(ext, EditorNode::get_singleton()->get_class_icon(actual_type, "Object"));
|
2021-10-22 23:27:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop testing base types as soon as we got a match.
|
|
|
|
break;
|
2020-07-13 21:23:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 12:27:34 +01:00
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
void EditorQuickOpen::_update_search() {
|
2024-02-22 09:22:16 +01:00
|
|
|
const PackedStringArray search_tokens = search_box->get_text().to_lower().replace("/", " ").split(" ", false);
|
|
|
|
const bool empty_search = search_tokens.is_empty();
|
2020-07-13 21:23:50 +02:00
|
|
|
|
|
|
|
// Filter possible candidates.
|
|
|
|
Vector<Entry> entries;
|
|
|
|
for (int i = 0; i < files.size(); i++) {
|
2024-02-22 09:22:16 +01:00
|
|
|
Entry r;
|
|
|
|
r.path = files[i];
|
|
|
|
if (empty_search) {
|
2020-07-13 21:23:50 +02:00
|
|
|
entries.push_back(r);
|
2024-02-22 09:22:16 +01:00
|
|
|
} else {
|
|
|
|
r.score = _score_search_result(search_tokens, r.path.to_lower());
|
|
|
|
if (r.score > 0) {
|
|
|
|
entries.push_back(r);
|
|
|
|
}
|
2020-07-13 21:23:50 +02:00
|
|
|
}
|
2016-01-08 12:27:34 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
// Display results
|
|
|
|
TreeItem *root = search_options->get_root();
|
|
|
|
root->clear_children();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
if (entries.size() > 0) {
|
|
|
|
if (!empty_search) {
|
|
|
|
SortArray<Entry, EntryComparator> sorter;
|
|
|
|
sorter.sort(entries.ptrw(), entries.size());
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2024-06-29 16:59:33 +02:00
|
|
|
const int class_icon_size = search_options->get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
|
2020-07-13 21:23:50 +02: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);
|
2024-06-29 16:59:33 +02:00
|
|
|
ti->set_icon_max_width(0, class_icon_size);
|
2020-07-13 21:23:50 +02:00
|
|
|
ti->set_icon(0, *icons.lookup_ptr(entries[i].path.get_extension()));
|
|
|
|
}
|
2016-01-17 14:25:15 +01:00
|
|
|
|
2021-03-07 21:07:30 +01:00
|
|
|
TreeItem *to_select = root->get_first_child();
|
2020-07-13 21:23:50 +02:00
|
|
|
to_select->select(0);
|
|
|
|
to_select->set_as_cursor(0);
|
|
|
|
search_options->scroll_to_item(to_select);
|
2016-01-17 14:25:15 +01:00
|
|
|
|
2020-12-14 19:37:30 +01:00
|
|
|
get_ok_button()->set_disabled(false);
|
2020-07-13 21:23:50 +02:00
|
|
|
} else {
|
|
|
|
search_options->deselect_all();
|
2016-01-17 14:25:15 +01:00
|
|
|
|
2020-12-14 19:37:30 +01:00
|
|
|
get_ok_button()->set_disabled(true);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-22 09:22:16 +01:00
|
|
|
float EditorQuickOpen::_score_search_result(const PackedStringArray &p_search_tokens, const String &p_path) {
|
|
|
|
float score = 0.0f;
|
|
|
|
int prev_min_match_idx = -1;
|
2020-07-13 21:23:50 +02:00
|
|
|
|
2024-02-22 09:22:16 +01:00
|
|
|
for (const String &s : p_search_tokens) {
|
|
|
|
int min_match_idx = p_path.find(s);
|
|
|
|
|
|
|
|
if (min_match_idx == -1) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
float token_score = s.length();
|
|
|
|
|
|
|
|
int max_match_idx = p_path.rfind(s);
|
|
|
|
|
|
|
|
// Prioritize the actual file name over folder.
|
|
|
|
if (max_match_idx > p_path.rfind("/")) {
|
|
|
|
token_score *= 2.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prioritize matches at the front of the path token.
|
2024-05-06 16:20:20 +02:00
|
|
|
if (min_match_idx == 0 || p_path.contains("/" + s)) {
|
2024-02-22 09:22:16 +01:00
|
|
|
token_score += 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
score += token_score;
|
|
|
|
|
|
|
|
// Prioritize tokens which appear in order.
|
|
|
|
if (prev_min_match_idx != -1 && max_match_idx > prev_min_match_idx) {
|
|
|
|
score += 1.0f;
|
|
|
|
}
|
2021-07-21 17:35:25 +02:00
|
|
|
|
2024-02-22 09:22:16 +01:00
|
|
|
prev_min_match_idx = min_match_idx;
|
2016-07-07 00:29:15 +02:00
|
|
|
}
|
2019-11-12 19:45:14 +01:00
|
|
|
|
2024-02-22 09:22:16 +01:00
|
|
|
return score;
|
2016-07-07 00:29:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
void EditorQuickOpen::_confirmed() {
|
|
|
|
if (!search_options->get_selected()) {
|
|
|
|
return;
|
2017-09-02 16:17:59 +02:00
|
|
|
}
|
2020-07-13 21:23:50 +02:00
|
|
|
_cleanup();
|
|
|
|
hide();
|
2022-06-25 21:41:10 +02:00
|
|
|
emit_signal(SNAME("quick_open"));
|
2017-09-02 16:17:59 +02:00
|
|
|
}
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
void EditorQuickOpen::cancel_pressed() {
|
|
|
|
_cleanup();
|
|
|
|
}
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
void EditorQuickOpen::_cleanup() {
|
|
|
|
files.clear();
|
|
|
|
icons.clear();
|
|
|
|
}
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
void EditorQuickOpen::_text_changed(const String &p_newtext) {
|
|
|
|
_update_search();
|
|
|
|
}
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
|
|
|
|
Ref<InputEventKey> k = p_ie;
|
|
|
|
if (k.is_valid()) {
|
|
|
|
switch (k->get_keycode()) {
|
2021-08-13 23:31:57 +02:00
|
|
|
case Key::UP:
|
|
|
|
case Key::DOWN:
|
|
|
|
case Key::PAGEUP:
|
|
|
|
case Key::PAGEDOWN: {
|
2021-08-22 17:37:22 +02:00
|
|
|
search_options->gui_input(k);
|
2020-07-13 21:23:50 +02:00
|
|
|
search_box->accept_event();
|
2015-08-24 01:15:56 +02:00
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
if (allow_multi_select) {
|
|
|
|
TreeItem *root = search_options->get_root();
|
2021-03-07 21:07:30 +01:00
|
|
|
if (!root->get_first_child()) {
|
2020-07-13 21:23:50 +02: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;
|
2021-06-20 19:12:33 +02:00
|
|
|
default:
|
|
|
|
break;
|
2015-08-24 01:15:56 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
String EditorQuickOpen::get_selected() const {
|
|
|
|
TreeItem *ti = search_options->get_selected();
|
|
|
|
if (!ti) {
|
|
|
|
return String();
|
2016-01-17 14:25:15 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:23:50 +02:00
|
|
|
return "res://" + ti->get_text(0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:23:50 +02: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 16:41:43 +02:00
|
|
|
}
|
2020-07-13 21:23:50 +02:00
|
|
|
|
|
|
|
return selected_files;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 15:12:42 +01:00
|
|
|
String EditorQuickOpen::get_base_type() const {
|
2020-07-13 21:23:50 +02:00
|
|
|
return base_type;
|
2020-03-06 18:00:16 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void EditorQuickOpen::_notification(int p_what) {
|
2019-03-17 01:47:57 +01:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2024-05-14 14:28:18 +02:00
|
|
|
connect(SceneStringName(confirmed), callable_mp(this, &EditorQuickOpen::_confirmed));
|
2019-03-17 01:47:57 +01:00
|
|
|
|
|
|
|
search_box->set_clear_button_enabled(true);
|
2020-04-08 10:38:50 +02:00
|
|
|
} break;
|
2022-02-16 15:17:55 +01:00
|
|
|
|
2022-12-01 15:08:07 +01:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (!is_visible()) {
|
|
|
|
prev_rect = Rect2i(get_position(), get_size());
|
|
|
|
was_showed = true;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2023-12-04 13:18:48 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
|
|
|
|
} break;
|
|
|
|
|
2019-03-17 01:47:57 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2024-05-14 14:28:18 +02:00
|
|
|
disconnect(SceneStringName(confirmed), callable_mp(this, &EditorQuickOpen::_confirmed));
|
2019-03-17 01:47:57 +01:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorQuickOpen::_bind_methods() {
|
2016-01-08 12:27:34 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("quick_open"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorQuickOpen::EditorQuickOpen() {
|
2017-03-05 16:44:50 +01:00
|
|
|
VBoxContainer *vbc = memnew(VBoxContainer);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(vbc);
|
2020-06-27 16:29:44 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
search_box = memnew(LineEdit);
|
2024-05-14 11:42:00 +02:00
|
|
|
search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorQuickOpen::_text_changed));
|
2024-05-13 16:56:03 +02:00
|
|
|
search_box->connect(SceneStringName(gui_input), callable_mp(this, &EditorQuickOpen::_sbox_input));
|
2020-06-27 16:29:44 +02:00
|
|
|
vbc->add_margin_child(TTR("Search:"), search_box);
|
2020-07-13 21:23:50 +02:00
|
|
|
register_text_enter(search_box);
|
2020-06-27 16:29:44 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
search_options = memnew(Tree);
|
2024-03-17 09:28:18 +01:00
|
|
|
search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
|
2020-06-27 16:29:44 +02:00
|
|
|
search_options->connect("item_activated", callable_mp(this, &EditorQuickOpen::_confirmed));
|
2020-07-13 21:23:50 +02:00
|
|
|
search_options->create_item();
|
2020-06-27 16:29:44 +02:00
|
|
|
search_options->set_hide_root(true);
|
|
|
|
search_options->set_hide_folding(true);
|
2022-02-08 10:14:58 +01:00
|
|
|
search_options->add_theme_constant_override("draw_guides", 1);
|
2017-03-05 16:44:50 +01:00
|
|
|
vbc->add_margin_child(TTR("Matches:"), search_options, true);
|
2020-06-27 16:29:44 +02:00
|
|
|
|
2022-07-08 02:31:19 +02:00
|
|
|
set_ok_button_text(TTR("Open"));
|
2014-02-10 02:10:30 +01:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
}
|