2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* quick_open.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01: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-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "quick_open.h"
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/keyboard.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-06-27 16:29:44 +02:00
|
|
|
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);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_dontclear) {
|
2014-02-10 02:10:30 +01:00
|
|
|
search_box->select_all();
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
search_box->clear();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-06-27 16:29:44 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
search_box->grab_focus();
|
|
|
|
_update_search();
|
2016-01-08 12:27:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String EditorQuickOpen::get_selected() const {
|
|
|
|
TreeItem *ti = search_options->get_selected();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!ti) {
|
2016-01-08 12:27:34 +01:00
|
|
|
return String();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-01-08 12:27:34 +01:00
|
|
|
return "res://" + ti->get_text(0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 12:27:34 +01:00
|
|
|
Vector<String> EditorQuickOpen::get_selected_files() const {
|
|
|
|
Vector<String> files;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
TreeItem *item = search_options->get_next_selected(search_options->get_root());
|
2016-01-08 12:27:34 +01:00
|
|
|
while (item) {
|
2017-03-05 16:44:50 +01:00
|
|
|
files.push_back("res://" + item->get_text(0));
|
2016-01-08 12:27:34 +01:00
|
|
|
item = search_options->get_next_selected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void EditorQuickOpen::_text_changed(const String &p_newtext) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_search();
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
void EditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
|
|
|
|
Ref<InputEventKey> k = p_ie;
|
|
|
|
if (k.is_valid()) {
|
2018-04-05 19:59:35 +02:00
|
|
|
switch (k->get_keycode()) {
|
2016-01-17 14:25:15 +01:00
|
|
|
case KEY_UP:
|
|
|
|
case KEY_DOWN:
|
|
|
|
case KEY_PAGEUP:
|
|
|
|
case KEY_PAGEDOWN: {
|
2017-05-20 17:38:03 +02:00
|
|
|
search_options->call("_gui_input", k);
|
2016-01-17 14:25:15 +01:00
|
|
|
search_box->accept_event();
|
|
|
|
|
|
|
|
TreeItem *root = search_options->get_root();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!root->get_children()) {
|
2016-01-17 14:25:15 +01:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-01-17 14:25:15 +01:00
|
|
|
|
|
|
|
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);
|
2020-06-27 22:46:42 +02:00
|
|
|
current->set_as_cursor(0);
|
2016-01-17 14:25:15 +01:00
|
|
|
} break;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 22:46:42 +02:00
|
|
|
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());
|
2016-07-07 00:29:15 +02:00
|
|
|
}
|
2019-11-12 19:45:14 +01:00
|
|
|
|
2020-06-27 22:46:42 +02:00
|
|
|
// Positive bias for matches close to the _end of the path_.
|
|
|
|
String base = path.get_base_dir();
|
|
|
|
pos = base.rfindn(search);
|
2019-11-12 19:45:14 +01:00
|
|
|
if (pos != -1) {
|
2020-06-27 22:46:42 +02:00
|
|
|
return 0.9f - 0.1f * (float(base.length() - pos) / base.length());
|
2016-07-07 00:29:15 +02:00
|
|
|
}
|
2019-11-12 19:45:14 +01:00
|
|
|
|
2020-06-27 22:46:42 +02:00
|
|
|
// Results that contain all characters but not the string.
|
|
|
|
return path.similarity(search) * 0.8f;
|
2016-07-07 00:29:15 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 07:33:00 +01:00
|
|
|
void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture2D>>> &list) {
|
2020-06-27 16:29:44 +02:00
|
|
|
for (int i = 0; i < efsd->get_subdir_count(); i++) {
|
|
|
|
_parse_fs(efsd->get_subdir(i), list);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < efsd->get_file_count(); i++) {
|
2020-06-27 16:29:44 +02:00
|
|
|
StringName file_type = efsd->get_file_type(i);
|
2020-06-27 22:46:42 +02:00
|
|
|
|
|
|
|
if (ClassDB::is_parent_class(file_type, base_type)) {
|
|
|
|
String file = efsd->get_file_path(i);
|
|
|
|
file = file.substr(6, file.length());
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2017-09-02 16:17:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2020-03-17 07:33:00 +01:00
|
|
|
Vector<Pair<String, Ref<Texture2D>>> EditorQuickOpen::_sort_fs(Vector<Pair<String, Ref<Texture2D>>> &list) {
|
2020-06-27 22:46:42 +02:00
|
|
|
String search_text = search_box->get_text().to_lower();
|
2020-03-17 07:33:00 +01:00
|
|
|
Vector<Pair<String, Ref<Texture2D>>> sorted_list;
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (search_text == String() || list.size() == 0) {
|
2017-09-16 15:14:22 +02:00
|
|
|
return list;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2017-09-02 16:17:59 +02:00
|
|
|
Vector<float> scores;
|
|
|
|
scores.resize(list.size());
|
2020-05-14 16:41:43 +02:00
|
|
|
for (int i = 0; i < list.size(); i++) {
|
2020-06-27 22:46:42 +02:00
|
|
|
scores.write[i] = _score_path(search_text, list[i].first.to_lower());
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2017-09-02 16:17:59 +02:00
|
|
|
while (list.size() > 0) {
|
|
|
|
float best_score = 0.0f;
|
|
|
|
int best_idx = 0;
|
2015-08-24 01:15:56 +02:00
|
|
|
|
2017-09-02 16:17:59 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-08-24 01:15:56 +02:00
|
|
|
}
|
2017-09-02 16:17:59 +02:00
|
|
|
|
|
|
|
sorted_list.push_back(list[best_idx]);
|
|
|
|
list.remove(best_idx);
|
|
|
|
scores.remove(best_idx);
|
2015-08-24 01:15:56 +02:00
|
|
|
}
|
2017-09-02 16:17:59 +02:00
|
|
|
|
|
|
|
return sorted_list;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorQuickOpen::_update_search() {
|
|
|
|
search_options->clear();
|
|
|
|
TreeItem *root = search_options->create_item();
|
2016-06-30 20:47:13 +02:00
|
|
|
EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
|
2020-03-17 07:33:00 +01:00
|
|
|
Vector<Pair<String, Ref<Texture2D>>> list;
|
2016-06-30 20:47:13 +02:00
|
|
|
|
|
|
|
_parse_fs(efsd, list);
|
2017-09-02 16:17:59 +02:00
|
|
|
list = _sort_fs(list);
|
2016-06-30 20:47:13 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-06-27 22:46:42 +02:00
|
|
|
TreeItem *result = root->get_children();
|
|
|
|
if (result) {
|
|
|
|
result->select(0);
|
|
|
|
result->set_as_cursor(0);
|
2016-01-17 14:25:15 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 22:46:42 +02:00
|
|
|
get_ok()->set_disabled(!result);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorQuickOpen::_confirmed() {
|
2020-06-27 22:46:42 +02:00
|
|
|
if (!search_options->get_selected()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-01-08 12:27:34 +01:00
|
|
|
emit_signal("quick_open");
|
2014-02-10 02:10:30 +01:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:00:16 +01:00
|
|
|
void EditorQuickOpen::_theme_changed() {
|
2020-06-27 16:29:44 +02:00
|
|
|
search_box->set_right_icon(search_options->get_theme_icon("Search", ei));
|
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: {
|
2020-02-21 18:28:45 +01:00
|
|
|
connect("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;
|
2019-03-17 01:47:57 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2020-02-21 18:28:45 +01:00
|
|
|
disconnect("confirmed", callable_mp(this, &EditorQuickOpen::_confirmed));
|
2019-03-17 01:47:57 +01:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 01:15:56 +02:00
|
|
|
StringName EditorQuickOpen::get_base_type() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return base_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-03-06 18:00:16 +01:00
|
|
|
vbc->connect("theme_changed", callable_mp(this, &EditorQuickOpen::_theme_changed));
|
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);
|
2020-02-21 18:28:45 +01: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 16:29:44 +02:00
|
|
|
vbc->add_margin_child(TTR("Search:"), search_box);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
search_options = memnew(Tree);
|
2020-06-27 16:29:44 +02:00
|
|
|
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);
|
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
|
|
|
|
2016-05-04 03:25:37 +02:00
|
|
|
get_ok()->set_text(TTR("Open"));
|
2014-02-10 02:10:30 +01:00
|
|
|
register_text_enter(search_box);
|
|
|
|
set_hide_on_ok(false);
|
2020-06-27 16:29:44 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ei = "EditorIcons";
|
|
|
|
ot = "Object";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|