2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* quick_open.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "quick_open.h"
|
|
|
|
|
2017-01-16 08:04:19 +01:00
|
|
|
#include "os/keyboard.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-01-08 12:27:34 +01:00
|
|
|
void EditorQuickOpen::popup(const StringName &p_base, bool p_enable_multi, bool p_add_dirs, bool p_dontclear) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
add_directories = p_add_dirs;
|
2014-02-10 02:10:30 +01:00
|
|
|
popup_centered_ratio(0.6);
|
|
|
|
if (p_dontclear)
|
|
|
|
search_box->select_all();
|
|
|
|
else
|
|
|
|
search_box->clear();
|
2016-01-08 12:27:34 +01:00
|
|
|
if (p_enable_multi)
|
|
|
|
search_options->set_select_mode(Tree::SELECT_MULTI);
|
|
|
|
else
|
|
|
|
search_options->set_select_mode(Tree::SELECT_SINGLE);
|
2014-02-10 02:10:30 +01:00
|
|
|
search_box->grab_focus();
|
2017-03-05 16:44:50 +01:00
|
|
|
base_type = p_base;
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_search();
|
2016-01-08 12:27:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String EditorQuickOpen::get_selected() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-01-08 12:27:34 +01:00
|
|
|
TreeItem *ti = search_options->get_selected();
|
|
|
|
if (!ti)
|
|
|
|
return String();
|
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-03-05 16:44:50 +01:00
|
|
|
void EditorQuickOpen::_sbox_input(const InputEvent &p_ie) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_ie.type == InputEvent::KEY) {
|
2016-01-17 14:25:15 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_ie.key.scancode) {
|
2016-01-17 14:25:15 +01:00
|
|
|
case KEY_UP:
|
|
|
|
case KEY_DOWN:
|
|
|
|
case KEY_PAGEUP:
|
|
|
|
case KEY_PAGEDOWN: {
|
|
|
|
|
2017-01-08 20:28:12 +01:00
|
|
|
search_options->call("_gui_input", p_ie);
|
2016-01-17 14:25:15 +01:00
|
|
|
search_box->accept_event();
|
|
|
|
|
|
|
|
TreeItem *root = search_options->get_root();
|
|
|
|
if (!root->get_children())
|
|
|
|
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);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 00:29:15 +02:00
|
|
|
float EditorQuickOpen::_path_cmp(String search, String path) const {
|
|
|
|
|
|
|
|
if (search == path) {
|
|
|
|
return 1.2f;
|
|
|
|
}
|
|
|
|
if (path.findn(search) != -1) {
|
|
|
|
return 1.1f;
|
|
|
|
}
|
|
|
|
return path.to_lower().similarity(search.to_lower());
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector<Pair<String, Ref<Texture> > > &list) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-08-24 01:15:56 +02:00
|
|
|
if (!add_directories) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < efsd->get_subdir_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-30 20:47:13 +02:00
|
|
|
_parse_fs(efsd->get_subdir(i), list);
|
2015-08-24 01:15:56 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 20:47:13 +02:00
|
|
|
String search_text = search_box->get_text();
|
2015-08-24 01:15:56 +02:00
|
|
|
|
|
|
|
if (add_directories) {
|
|
|
|
String path = efsd->get_path();
|
|
|
|
if (!path.ends_with("/"))
|
2017-03-05 16:44:50 +01:00
|
|
|
path += "/";
|
|
|
|
if (path != "res://") {
|
|
|
|
path = path.substr(6, path.length());
|
2016-06-30 20:47:13 +02:00
|
|
|
if (search_text.is_subsequence_ofi(path)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Pair<String, Ref<Texture> > pair;
|
2016-06-30 20:47:13 +02:00
|
|
|
pair.first = path;
|
|
|
|
pair.second = get_icon("folder", "FileDialog");
|
|
|
|
|
2016-07-07 00:29:15 +02:00
|
|
|
if (search_text != String() && list.size() > 0) {
|
|
|
|
|
|
|
|
float this_sim = _path_cmp(search_text, path);
|
|
|
|
float other_sim = _path_cmp(list[0].first, path);
|
2016-06-30 20:47:13 +02:00
|
|
|
int pos = 1;
|
|
|
|
|
2016-07-07 00:29:15 +02:00
|
|
|
while (pos < list.size() && this_sim <= other_sim) {
|
|
|
|
other_sim = _path_cmp(list[pos++].first, path);
|
2016-06-30 20:47:13 +02:00
|
|
|
}
|
|
|
|
|
2016-07-07 00:29:15 +02:00
|
|
|
pos = this_sim >= other_sim ? pos - 1 : pos;
|
2016-06-30 20:47:13 +02:00
|
|
|
list.insert(pos, pair);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
list.push_back(pair);
|
|
|
|
}
|
2015-08-24 01:15:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < efsd->get_file_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String file = efsd->get_file_path(i);
|
2017-03-05 16:44:50 +01:00
|
|
|
file = file.substr(6, file.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (ClassDB::is_parent_class(efsd->get_file_type(i), base_type) && (search_text.is_subsequence_ofi(file))) {
|
|
|
|
Pair<String, Ref<Texture> > pair;
|
2016-06-30 20:47:13 +02:00
|
|
|
pair.first = file;
|
|
|
|
pair.second = get_icon((has_icon(efsd->get_file_type(i), ei) ? efsd->get_file_type(i) : ot), ei);
|
|
|
|
|
2016-07-07 00:29:15 +02:00
|
|
|
if (search_text != String() && list.size() > 0) {
|
2016-06-30 20:47:13 +02:00
|
|
|
|
2016-07-07 00:29:15 +02:00
|
|
|
float this_sim = _path_cmp(search_text, file);
|
|
|
|
float other_sim = _path_cmp(list[0].first, file);
|
2016-06-30 20:47:13 +02:00
|
|
|
int pos = 1;
|
|
|
|
|
2016-07-07 00:29:15 +02:00
|
|
|
while (pos < list.size() && this_sim <= other_sim) {
|
|
|
|
other_sim = _path_cmp(list[pos++].first, file);
|
2016-06-30 20:47:13 +02:00
|
|
|
}
|
|
|
|
|
2016-07-07 00:29:15 +02:00
|
|
|
pos = this_sim >= other_sim ? pos - 1 : pos;
|
2016-06-30 20:47:13 +02:00
|
|
|
list.insert(pos, pair);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
list.push_back(pair);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 01:15:56 +02:00
|
|
|
|
|
|
|
if (add_directories) {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < efsd->get_subdir_count(); i++) {
|
2015-08-24 01:15:56 +02:00
|
|
|
|
2016-06-30 20:47:13 +02:00
|
|
|
_parse_fs(efsd->get_subdir(i), list);
|
2015-08-24 01:15:56 +02:00
|
|
|
}
|
|
|
|
}
|
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();
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<Pair<String, Ref<Texture> > > list;
|
2016-06-30 20:47:13 +02:00
|
|
|
|
|
|
|
_parse_fs(efsd, 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);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-01-17 14:25:15 +01:00
|
|
|
if (root->get_children()) {
|
|
|
|
TreeItem *ti = root->get_children();
|
|
|
|
|
|
|
|
ti->select(0);
|
|
|
|
ti->set_as_cursor(0);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
get_ok()->set_disabled(root->get_children() == NULL);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorQuickOpen::_confirmed() {
|
|
|
|
|
|
|
|
TreeItem *ti = search_options->get_selected();
|
|
|
|
if (!ti)
|
|
|
|
return;
|
2016-01-08 12:27:34 +01:00
|
|
|
emit_signal("quick_open");
|
2014-02-10 02:10:30 +01:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorQuickOpen::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
connect("confirmed", this, "_confirmed");
|
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() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_text_changed"), &EditorQuickOpen::_text_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_confirmed"), &EditorQuickOpen::_confirmed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_sbox_input"), &EditorQuickOpen::_sbox_input);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
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);
|
2017-01-10 05:49:55 +01:00
|
|
|
//set_child_rect(vbc);
|
2017-03-05 16:44:50 +01:00
|
|
|
search_box = memnew(LineEdit);
|
|
|
|
vbc->add_margin_child(TTR("Search:"), search_box);
|
|
|
|
search_box->connect("text_changed", this, "_text_changed");
|
|
|
|
search_box->connect("gui_input", this, "_sbox_input");
|
|
|
|
search_options = memnew(Tree);
|
|
|
|
vbc->add_margin_child(TTR("Matches:"), search_options, true);
|
2016-05-04 03:25:37 +02:00
|
|
|
get_ok()->set_text(TTR("Open"));
|
2014-02-10 02:10:30 +01:00
|
|
|
get_ok()->set_disabled(true);
|
|
|
|
register_text_enter(search_box);
|
|
|
|
set_hide_on_ok(false);
|
2017-03-05 16:44:50 +01:00
|
|
|
search_options->connect("item_activated", this, "_confirmed");
|
2014-02-10 02:10:30 +01:00
|
|
|
search_options->set_hide_root(true);
|
2017-03-05 16:44:50 +01:00
|
|
|
ei = "EditorIcons";
|
|
|
|
ot = "Object";
|
|
|
|
add_directories = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|