2016-07-04 23:22:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_autoload_settings.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-07-04 23:22:28 +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). */
|
2016-07-04 23:22:28 +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
|
|
|
|
2016-07-04 23:22:28 +00:00
|
|
|
#include "editor_autoload_settings.h"
|
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/global_constants.h"
|
|
|
|
#include "core/project_settings.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "editor_node.h"
|
2019-12-24 07:17:23 +00:00
|
|
|
#include "editor_scale.h"
|
|
|
|
#include "project_settings_editor.h"
|
2018-05-01 14:06:23 +00:00
|
|
|
#include "scene/main/viewport.h"
|
|
|
|
#include "scene/resources/packed_scene.h"
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
#define PREVIEW_LIST_MAX_SIZE 10
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_notification(int p_what) {
|
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
|
|
|
List<String> afn;
|
|
|
|
ResourceLoader::get_recognized_extensions_for_type("Script", &afn);
|
|
|
|
ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn);
|
|
|
|
|
|
|
|
EditorFileDialog *file_dialog = autoload_add_path->get_file_dialog();
|
|
|
|
|
|
|
|
for (List<String>::Element *E = afn.front(); E; E = E->next()) {
|
|
|
|
file_dialog->add_filter("*." + E->get());
|
|
|
|
}
|
2018-05-22 16:26:13 +00:00
|
|
|
|
|
|
|
for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
|
|
|
|
AutoLoadInfo &info = E->get();
|
|
|
|
if (info.node && info.in_editor) {
|
|
|
|
get_tree()->get_root()->call_deferred("add_child", info.node);
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, String *r_error) {
|
2016-07-04 23:22:28 +00:00
|
|
|
if (!p_name.is_valid_identifier()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2021-09-13 20:54:20 +00:00
|
|
|
*r_error = TTR("Invalid name.") + " ";
|
|
|
|
if (p_name.size() > 0 && p_name.left(1).is_numeric()) {
|
|
|
|
*r_error += TTR("Cannot begin with a digit.");
|
|
|
|
} else {
|
|
|
|
*r_error += TTR("Valid characters:") + " a-z, A-Z, 0-9 or _";
|
|
|
|
}
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-03 02:03:46 +00:00
|
|
|
if (ClassDB::class_exists(p_name)) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2021-09-13 20:54:20 +00:00
|
|
|
*r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing engine class name.");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (Variant::get_type_name(Variant::Type(i)) == p_name) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2021-09-13 20:54:20 +00:00
|
|
|
*r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing built-in type name.");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
|
|
|
|
if (GlobalConstants::get_global_constant_name(i) == p_name) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2021-09-13 20:54:20 +00:00
|
|
|
*r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing global constant name.");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 04:22:04 +00:00
|
|
|
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
|
|
|
|
List<String> keywords;
|
|
|
|
ScriptServer::get_language(i)->get_reserved_words(&keywords);
|
|
|
|
for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
|
|
|
|
if (E->get() == p_name) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2021-09-13 20:54:20 +00:00
|
|
|
*r_error = TTR("Invalid name.") + " " + TTR("Keyword cannot be used as an autoload name.");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-12-15 04:22:04 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 23:22:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_autoload_add() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (autoload_add(autoload_add_name->get_text(), autoload_add_path->get_line_edit()->get_text())) {
|
2018-12-15 04:22:04 +00:00
|
|
|
autoload_add_path->get_line_edit()->set_text("");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
autoload_add_name->set_text("");
|
2019-11-27 23:07:01 +00:00
|
|
|
add_autoload->set_disabled(true);
|
2016-07-04 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_autoload_selected() {
|
|
|
|
TreeItem *ti = tree->get_selected();
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!ti) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
selected_autoload = "autoload/" + ti->get_text(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_autoload_edited() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (updating_autoload) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
TreeItem *ti = tree->get_edited();
|
|
|
|
int column = tree->get_edited_column();
|
|
|
|
|
|
|
|
UndoRedo *undo_redo = EditorNode::get_undo_redo();
|
|
|
|
|
|
|
|
if (column == 0) {
|
|
|
|
String name = ti->get_text(0);
|
|
|
|
String old_name = selected_autoload.get_slice("/", 1);
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (name == old_name) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
String error;
|
|
|
|
if (!_autoload_name_is_valid(name, &error)) {
|
|
|
|
ti->set_text(0, old_name);
|
|
|
|
EditorNode::get_singleton()->show_warning(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-05 18:34:34 +00:00
|
|
|
if (ProjectSettings::get_singleton()->has_setting("autoload/" + name)) {
|
2016-07-04 23:22:28 +00:00
|
|
|
ti->set_text(0, old_name);
|
|
|
|
EditorNode::get_singleton()->show_warning(vformat(TTR("Autoload '%s' already exists!"), name));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updating_autoload = true;
|
|
|
|
|
|
|
|
name = "autoload/" + name;
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
int order = ProjectSettings::get_singleton()->get_order(selected_autoload);
|
|
|
|
String path = ProjectSettings::get_singleton()->get(selected_autoload);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
undo_redo->create_action(TTR("Rename Autoload"));
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), name, path);
|
|
|
|
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, order);
|
|
|
|
undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", selected_autoload);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), selected_autoload, path);
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", selected_autoload, order);
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", name);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2018-05-01 14:06:23 +00:00
|
|
|
undo_redo->add_do_method(this, "call_deferred", "update_autoload");
|
|
|
|
undo_redo->add_undo_method(this, "call_deferred", "update_autoload");
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "emit_signal", autoload_changed);
|
|
|
|
undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
|
|
|
|
selected_autoload = name;
|
|
|
|
} else if (column == 2) {
|
|
|
|
updating_autoload = true;
|
|
|
|
|
|
|
|
bool checked = ti->is_checked(2);
|
|
|
|
String base = "autoload/" + ti->get_text(0);
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
int order = ProjectSettings::get_singleton()->get_order(base);
|
|
|
|
String path = ProjectSettings::get_singleton()->get(base);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (path.begins_with("*")) {
|
2016-07-04 23:22:28 +00:00
|
|
|
path = path.substr(1, path.length());
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2018-10-14 05:05:53 +00:00
|
|
|
// Singleton autoloads are represented with a leading "*" in their path.
|
2021-05-05 10:44:11 +00:00
|
|
|
if (checked) {
|
2016-07-04 23:22:28 +00:00
|
|
|
path = "*" + path;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
undo_redo->create_action(TTR("Toggle AutoLoad Globals"));
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), base, path);
|
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), base, ProjectSettings::get_singleton()->get(base));
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", base, order);
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", base, order);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2018-05-01 14:06:23 +00:00
|
|
|
undo_redo->add_do_method(this, "call_deferred", "update_autoload");
|
|
|
|
undo_redo->add_undo_method(this, "call_deferred", "update_autoload");
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "emit_signal", autoload_changed);
|
|
|
|
undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
|
|
|
updating_autoload = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button) {
|
2017-08-24 20:58:51 +00:00
|
|
|
TreeItem *ti = Object::cast_to<TreeItem>(p_item);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
String name = "autoload/" + ti->get_text(0);
|
|
|
|
|
|
|
|
UndoRedo *undo_redo = EditorNode::get_undo_redo();
|
|
|
|
|
|
|
|
switch (p_button) {
|
2018-01-02 06:24:02 +00:00
|
|
|
case BUTTON_OPEN: {
|
|
|
|
_autoload_open(ti->get_text(1));
|
|
|
|
} break;
|
2017-03-05 15:44:50 +00:00
|
|
|
case BUTTON_MOVE_UP:
|
2016-07-04 23:22:28 +00:00
|
|
|
case BUTTON_MOVE_DOWN: {
|
2021-05-04 14:00:45 +00:00
|
|
|
TreeItem *swap = nullptr;
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
if (p_button == BUTTON_MOVE_UP) {
|
|
|
|
swap = ti->get_prev();
|
|
|
|
} else {
|
|
|
|
swap = ti->get_next();
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!swap) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
String swap_name = "autoload/" + swap->get_text(0);
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
int order = ProjectSettings::get_singleton()->get_order(name);
|
|
|
|
int swap_order = ProjectSettings::get_singleton()->get_order(swap_name);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
undo_redo->create_action(TTR("Move Autoload"));
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, swap_order);
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", swap_name, order);
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", swap_name, swap_order);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "update_autoload");
|
|
|
|
undo_redo->add_undo_method(this, "update_autoload");
|
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "emit_signal", autoload_changed);
|
|
|
|
undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
} break;
|
|
|
|
case BUTTON_DELETE: {
|
2017-07-19 20:00:46 +00:00
|
|
|
int order = ProjectSettings::get_singleton()->get_order(name);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
undo_redo->create_action(TTR("Remove Autoload"));
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, ProjectSettings::get_singleton()->get(name));
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_persisting", name, true);
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", order);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "update_autoload");
|
|
|
|
undo_redo->add_undo_method(this, "update_autoload");
|
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "emit_signal", autoload_changed);
|
|
|
|
undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 06:24:02 +00:00
|
|
|
void EditorAutoloadSettings::_autoload_activated() {
|
|
|
|
TreeItem *ti = tree->get_selected();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!ti) {
|
2018-01-02 06:24:02 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-01-02 06:24:02 +00:00
|
|
|
_autoload_open(ti->get_text(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_autoload_open(const String &fpath) {
|
|
|
|
if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
|
|
|
|
EditorNode::get_singleton()->open_request(fpath);
|
|
|
|
} else {
|
|
|
|
EditorNode::get_singleton()->load_resource(fpath);
|
|
|
|
}
|
|
|
|
ProjectSettingsEditor::get_singleton()->hide();
|
|
|
|
}
|
2018-12-15 04:22:04 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) {
|
2019-11-27 23:07:01 +00:00
|
|
|
// Convert the file name to PascalCase, which is the convention for classes in GDScript.
|
|
|
|
const String class_name = p_path.get_file().get_basename().capitalize().replace(" ", "");
|
|
|
|
|
|
|
|
// If the name collides with a built-in class, prefix the name to make it possible to add without having to edit the name.
|
|
|
|
// The prefix is subjective, but it provides better UX than leaving the Add button disabled :)
|
|
|
|
const String prefix = ClassDB::class_exists(class_name) ? "Global" : "";
|
|
|
|
|
|
|
|
autoload_add_name->set_text(prefix + class_name);
|
|
|
|
add_autoload->set_disabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_autoload_text_entered(const String p_name) {
|
2021-05-04 14:00:45 +00:00
|
|
|
if (autoload_add_path->get_line_edit()->get_text() != "" && _autoload_name_is_valid(p_name, nullptr)) {
|
2019-11-27 23:07:01 +00:00
|
|
|
_autoload_add();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_autoload_path_text_changed(const String p_path) {
|
|
|
|
add_autoload->set_disabled(
|
2021-05-04 14:00:45 +00:00
|
|
|
p_path == "" || !_autoload_name_is_valid(autoload_add_name->get_text(), nullptr));
|
2019-11-27 23:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::_autoload_text_changed(const String p_name) {
|
2021-09-13 20:54:20 +00:00
|
|
|
String error_string;
|
|
|
|
bool is_name_valid = _autoload_name_is_valid(p_name, &error_string);
|
|
|
|
add_autoload->set_disabled(autoload_add_path->get_line_edit()->get_text() == "" || !is_name_valid);
|
|
|
|
error_message->set_text(error_string);
|
|
|
|
error_message->set_visible(autoload_add_name->get_text() != "" && !is_name_valid);
|
2016-07-04 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
|
|
|
|
RES res = ResourceLoader::load(p_path);
|
2021-05-04 14:00:45 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, "Can't autoload: " + p_path + ".");
|
|
|
|
Node *n = nullptr;
|
2018-05-22 16:26:13 +00:00
|
|
|
if (res->is_class("PackedScene")) {
|
|
|
|
Ref<PackedScene> ps = res;
|
|
|
|
n = ps->instance();
|
|
|
|
} else if (res->is_class("Script")) {
|
|
|
|
Ref<Script> s = res;
|
|
|
|
StringName ibt = s->get_instance_base_type();
|
|
|
|
bool valid_type = ClassDB::is_parent_class(ibt, "Node");
|
2021-05-04 14:00:45 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!valid_type, nullptr, "Script does not inherit a Node: " + p_path + ".");
|
2018-05-22 16:26:13 +00:00
|
|
|
|
|
|
|
Object *obj = ClassDB::instance(ibt);
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(obj == nullptr, nullptr, "Cannot instance script for autoload, expected 'Node' inheritance, got: " + String(ibt) + ".");
|
2018-05-22 16:26:13 +00:00
|
|
|
|
|
|
|
n = Object::cast_to<Node>(obj);
|
|
|
|
n->set_script(s.get_ref_ptr());
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!n, nullptr, "Path in autoload not a node or script: " + p_path + ".");
|
2018-05-22 16:26:13 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-07-04 23:22:28 +00:00
|
|
|
void EditorAutoloadSettings::update_autoload() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (updating_autoload) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
updating_autoload = true;
|
|
|
|
|
2018-05-01 14:06:23 +00:00
|
|
|
Map<String, AutoLoadInfo> to_remove;
|
2018-05-22 16:26:13 +00:00
|
|
|
List<AutoLoadInfo *> to_add;
|
2018-05-01 14:06:23 +00:00
|
|
|
|
|
|
|
for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
|
2018-05-22 16:26:13 +00:00
|
|
|
AutoLoadInfo &info = E->get();
|
|
|
|
to_remove.insert(info.name, info);
|
2018-05-01 14:06:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 23:22:28 +00:00
|
|
|
autoload_cache.clear();
|
|
|
|
|
|
|
|
tree->clear();
|
|
|
|
TreeItem *root = tree->create_item();
|
|
|
|
|
|
|
|
List<PropertyInfo> props;
|
2017-07-19 20:00:46 +00:00
|
|
|
ProjectSettings::get_singleton()->get_property_list(&props);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
|
|
|
|
const PropertyInfo &pi = E->get();
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!pi.name.begins_with("autoload/")) {
|
2016-07-04 23:22:28 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
String name = pi.name.get_slice("/", 1);
|
2017-07-19 20:00:46 +00:00
|
|
|
String path = ProjectSettings::get_singleton()->get(pi.name);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (name.empty()) {
|
2016-07-04 23:22:28 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
AutoLoadInfo info;
|
2018-05-01 14:06:23 +00:00
|
|
|
info.is_singleton = path.begins_with("*");
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2018-05-01 14:06:23 +00:00
|
|
|
if (info.is_singleton) {
|
|
|
|
path = path.substr(1, path.length());
|
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2018-05-01 14:06:23 +00:00
|
|
|
info.name = name;
|
|
|
|
info.path = path;
|
|
|
|
info.order = ProjectSettings::get_singleton()->get_order(pi.name);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
bool need_to_add = true;
|
|
|
|
if (to_remove.has(name)) {
|
|
|
|
AutoLoadInfo &old_info = to_remove[name];
|
2018-05-01 14:06:23 +00:00
|
|
|
if (old_info.path == info.path) {
|
2018-05-22 16:26:13 +00:00
|
|
|
// Still the same resource, check status
|
|
|
|
info.node = old_info.node;
|
|
|
|
if (info.node) {
|
|
|
|
Ref<Script> scr = info.node->get_script();
|
|
|
|
info.in_editor = scr.is_valid() && scr->is_tool();
|
|
|
|
if (info.is_singleton == old_info.is_singleton && info.in_editor == old_info.in_editor) {
|
|
|
|
to_remove.erase(name);
|
|
|
|
need_to_add = false;
|
2018-05-01 14:06:23 +00:00
|
|
|
} else {
|
2021-05-04 14:00:45 +00:00
|
|
|
info.node = nullptr;
|
2018-05-01 14:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 14:06:23 +00:00
|
|
|
autoload_cache.push_back(info);
|
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
if (need_to_add) {
|
|
|
|
to_add.push_back(&(autoload_cache.back()->get()));
|
|
|
|
}
|
|
|
|
|
2016-07-04 23:22:28 +00:00
|
|
|
TreeItem *item = tree->create_item(root);
|
|
|
|
item->set_text(0, name);
|
|
|
|
item->set_editable(0, true);
|
|
|
|
|
|
|
|
item->set_text(1, path);
|
2018-01-02 06:24:02 +00:00
|
|
|
item->set_selectable(1, true);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
|
|
|
|
item->set_editable(2, true);
|
|
|
|
item->set_text(2, TTR("Enable"));
|
2018-05-01 14:06:23 +00:00
|
|
|
item->set_checked(2, info.is_singleton);
|
2019-11-27 23:07:01 +00:00
|
|
|
item->add_button(3, get_icon("Load", "EditorIcons"), BUTTON_OPEN);
|
2017-03-05 15:44:50 +00:00
|
|
|
item->add_button(3, get_icon("MoveUp", "EditorIcons"), BUTTON_MOVE_UP);
|
|
|
|
item->add_button(3, get_icon("MoveDown", "EditorIcons"), BUTTON_MOVE_DOWN);
|
2017-09-27 13:08:28 +00:00
|
|
|
item->add_button(3, get_icon("Remove", "EditorIcons"), BUTTON_DELETE);
|
2016-07-04 23:22:28 +00:00
|
|
|
item->set_selectable(3, false);
|
|
|
|
}
|
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
// Remove deleted/changed autoloads
|
2018-05-01 14:06:23 +00:00
|
|
|
for (Map<String, AutoLoadInfo>::Element *E = to_remove.front(); E; E = E->next()) {
|
|
|
|
AutoLoadInfo &info = E->get();
|
2018-05-22 16:26:13 +00:00
|
|
|
if (info.is_singleton) {
|
|
|
|
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
|
|
|
|
ScriptServer::get_language(i)->remove_named_global_constant(info.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info.in_editor) {
|
|
|
|
ERR_CONTINUE(!info.node);
|
2019-08-22 15:30:55 +00:00
|
|
|
get_tree()->get_root()->call_deferred("remove_child", info.node);
|
2018-05-22 16:26:13 +00:00
|
|
|
}
|
2018-05-01 14:06:23 +00:00
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
if (info.node) {
|
2019-08-22 15:30:55 +00:00
|
|
|
info.node->queue_delete();
|
2021-05-04 14:00:45 +00:00
|
|
|
info.node = nullptr;
|
2018-05-01 14:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
// Load new/changed autoloads
|
2018-05-01 14:06:23 +00:00
|
|
|
List<Node *> nodes_to_add;
|
2018-05-22 16:26:13 +00:00
|
|
|
for (List<AutoLoadInfo *>::Element *E = to_add.front(); E; E = E->next()) {
|
|
|
|
AutoLoadInfo *info = E->get();
|
2018-05-01 14:06:23 +00:00
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
info->node = _create_autoload(info->path);
|
2018-05-01 14:06:23 +00:00
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
ERR_CONTINUE(!info->node);
|
|
|
|
info->node->set_name(info->name);
|
2018-05-01 14:06:23 +00:00
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
Ref<Script> scr = info->node->get_script();
|
|
|
|
info->in_editor = scr.is_valid() && scr->is_tool();
|
2018-05-01 14:06:23 +00:00
|
|
|
|
2018-05-22 16:26:13 +00:00
|
|
|
if (info->in_editor) {
|
|
|
|
//defer so references are all valid on _ready()
|
|
|
|
nodes_to_add.push_back(info->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->is_singleton) {
|
2018-05-01 14:06:23 +00:00
|
|
|
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
|
2018-05-22 16:26:13 +00:00
|
|
|
ScriptServer::get_language(i)->add_named_global_constant(info->name, info->node);
|
2018-05-01 14:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-22 16:26:13 +00:00
|
|
|
|
|
|
|
if (!info->in_editor && !info->is_singleton) {
|
|
|
|
// No reason to keep this node
|
|
|
|
memdelete(info->node);
|
2021-05-04 14:00:45 +00:00
|
|
|
info->node = nullptr;
|
2018-05-22 16:26:13 +00:00
|
|
|
}
|
2018-05-01 14:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (List<Node *>::Element *E = nodes_to_add.front(); E; E = E->next()) {
|
|
|
|
get_tree()->get_root()->add_child(E->get());
|
|
|
|
}
|
|
|
|
|
2016-07-04 23:22:28 +00:00
|
|
|
updating_autoload = false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control *p_control) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (autoload_cache.size() <= 1) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-01-11 03:52:51 +00:00
|
|
|
PoolStringArray autoloads;
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
TreeItem *next = tree->get_next_selected(nullptr);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
while (next) {
|
|
|
|
autoloads.push_back(next->get_text(0));
|
|
|
|
next = tree->get_next_selected(next);
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (autoloads.size() == 0 || autoloads.size() == autoload_cache.size()) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return Variant();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VBoxContainer *preview = memnew(VBoxContainer);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
int max_size = MIN(PREVIEW_LIST_MAX_SIZE, autoloads.size());
|
|
|
|
|
|
|
|
for (int i = 0; i < max_size; i++) {
|
2017-03-05 15:44:50 +00:00
|
|
|
Label *label = memnew(Label(autoloads[i]));
|
|
|
|
label->set_self_modulate(Color(1, 1, 1, Math::lerp(1, 0, float(i) / PREVIEW_LIST_MAX_SIZE)));
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
preview->add_child(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
|
|
|
|
tree->set_drag_preview(preview);
|
|
|
|
|
|
|
|
Dictionary drop_data;
|
|
|
|
drop_data["type"] = "autoload";
|
|
|
|
drop_data["autoloads"] = autoloads;
|
|
|
|
|
|
|
|
return drop_data;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool EditorAutoloadSettings::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (updating_autoload) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
Dictionary drop_data = p_data;
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!drop_data.has("type")) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
if (drop_data.has("type")) {
|
2017-09-10 13:37:49 +00:00
|
|
|
TreeItem *ti = tree->get_item_at_position(p_point);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!ti) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-09-10 13:37:49 +00:00
|
|
|
int section = tree->get_drop_section_at_position(p_point);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2019-06-26 13:08:25 +00:00
|
|
|
return section >= -1;
|
2016-07-04 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) {
|
2017-09-10 13:37:49 +00:00
|
|
|
TreeItem *ti = tree->get_item_at_position(p_point);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!ti) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-09-10 13:37:49 +00:00
|
|
|
int section = tree->get_drop_section_at_position(p_point);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (section < -1) {
|
2016-07-04 23:22:28 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
String name;
|
|
|
|
bool move_to_back = false;
|
|
|
|
|
|
|
|
if (section < 0) {
|
|
|
|
name = ti->get_text(0);
|
|
|
|
} else if (ti->get_next()) {
|
|
|
|
name = ti->get_next()->get_text(0);
|
|
|
|
} else {
|
|
|
|
name = ti->get_text(0);
|
|
|
|
move_to_back = true;
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
int order = ProjectSettings::get_singleton()->get_order("autoload/" + name);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
AutoLoadInfo aux;
|
2021-05-04 14:00:45 +00:00
|
|
|
List<AutoLoadInfo>::Element *E = nullptr;
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
if (!move_to_back) {
|
|
|
|
aux.order = order;
|
|
|
|
E = autoload_cache.find(aux);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary drop_data = p_data;
|
2017-01-11 03:52:51 +00:00
|
|
|
PoolStringArray autoloads = drop_data["autoloads"];
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
Vector<int> orders;
|
|
|
|
orders.resize(autoload_cache.size());
|
|
|
|
|
|
|
|
for (int i = 0; i < autoloads.size(); i++) {
|
2017-07-19 20:00:46 +00:00
|
|
|
aux.order = ProjectSettings::get_singleton()->get_order("autoload/" + autoloads[i]);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
List<AutoLoadInfo>::Element *I = autoload_cache.find(aux);
|
|
|
|
|
|
|
|
if (move_to_back) {
|
|
|
|
autoload_cache.move_to_back(I);
|
|
|
|
} else if (E != I) {
|
|
|
|
autoload_cache.move_before(I, E);
|
|
|
|
} else if (E->next()) {
|
|
|
|
E = E->next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
2019-02-12 20:10:08 +00:00
|
|
|
for (List<AutoLoadInfo>::Element *F = autoload_cache.front(); F; F = F->next()) {
|
|
|
|
orders.write[i++] = F->get().order;
|
2016-07-04 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orders.sort();
|
|
|
|
|
|
|
|
UndoRedo *undo_redo = EditorNode::get_undo_redo();
|
|
|
|
|
|
|
|
undo_redo->create_action(TTR("Rearrange Autoloads"));
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
2019-02-12 20:10:08 +00:00
|
|
|
for (List<AutoLoadInfo>::Element *F = autoload_cache.front(); F; F = F->next()) {
|
|
|
|
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", "autoload/" + F->get().name, orders[i++]);
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", "autoload/" + F->get().name, F->get().order);
|
2016-07-04 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orders.clear();
|
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "update_autoload");
|
|
|
|
undo_redo->add_undo_method(this, "update_autoload");
|
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "emit_signal", autoload_changed);
|
|
|
|
undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
2018-12-15 04:22:04 +00:00
|
|
|
bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_path) {
|
2018-02-14 04:31:38 +00:00
|
|
|
String name = p_name;
|
|
|
|
|
|
|
|
String error;
|
|
|
|
if (!_autoload_name_is_valid(name, &error)) {
|
2020-06-01 06:32:28 +00:00
|
|
|
EditorNode::get_singleton()->show_warning(TTR("Can't add autoload:") + "\n" + error);
|
2018-12-15 04:22:04 +00:00
|
|
|
return false;
|
2018-02-14 04:31:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 13:08:25 +00:00
|
|
|
const String &path = p_path;
|
2018-02-14 04:31:38 +00:00
|
|
|
if (!FileAccess::exists(path)) {
|
2021-07-31 17:18:24 +00:00
|
|
|
EditorNode::get_singleton()->show_warning(TTR("Can't add autoload:") + "\n" + vformat(TTR("%s is an invalid path. File does not exist."), path));
|
2018-12-15 04:22:04 +00:00
|
|
|
return false;
|
2018-02-14 04:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!path.begins_with("res://")) {
|
2021-07-31 17:18:24 +00:00
|
|
|
EditorNode::get_singleton()->show_warning(TTR("Can't add autoload:") + "\n" + vformat(TTR("%s is an invalid path. Not in resource path (res://)."), path));
|
2018-12-15 04:22:04 +00:00
|
|
|
return false;
|
2018-02-14 04:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name = "autoload/" + name;
|
|
|
|
|
2019-06-26 13:08:25 +00:00
|
|
|
UndoRedo *undo_redo = EditorNode::get_undo_redo();
|
2018-02-14 04:31:38 +00:00
|
|
|
|
|
|
|
undo_redo->create_action(TTR("Add AutoLoad"));
|
2018-10-14 05:05:53 +00:00
|
|
|
// Singleton autoloads are represented with a leading "*" in their path.
|
2018-02-14 04:31:38 +00:00
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), name, "*" + path);
|
|
|
|
|
|
|
|
if (ProjectSettings::get_singleton()->has_setting(name)) {
|
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, ProjectSettings::get_singleton()->get(name));
|
|
|
|
} else {
|
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, Variant());
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "update_autoload");
|
|
|
|
undo_redo->add_undo_method(this, "update_autoload");
|
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "emit_signal", autoload_changed);
|
|
|
|
undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
2018-12-15 04:22:04 +00:00
|
|
|
|
|
|
|
return true;
|
2018-02-14 04:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorAutoloadSettings::autoload_remove(const String &p_name) {
|
|
|
|
String name = "autoload/" + p_name;
|
|
|
|
|
2019-06-26 13:08:25 +00:00
|
|
|
UndoRedo *undo_redo = EditorNode::get_undo_redo();
|
2018-02-14 04:31:38 +00:00
|
|
|
|
|
|
|
int order = ProjectSettings::get_singleton()->get_order(name);
|
|
|
|
|
|
|
|
undo_redo->create_action(TTR("Remove Autoload"));
|
|
|
|
|
|
|
|
undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());
|
|
|
|
|
|
|
|
undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, ProjectSettings::get_singleton()->get(name));
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_persisting", name, true);
|
|
|
|
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", order);
|
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "update_autoload");
|
|
|
|
undo_redo->add_undo_method(this, "update_autoload");
|
|
|
|
|
|
|
|
undo_redo->add_do_method(this, "emit_signal", autoload_changed);
|
|
|
|
undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
|
|
|
|
|
|
|
|
undo_redo->commit_action();
|
|
|
|
}
|
|
|
|
|
2016-07-04 23:22:28 +00:00
|
|
|
void EditorAutoloadSettings::_bind_methods() {
|
2017-01-03 02:03:46 +00:00
|
|
|
ClassDB::bind_method("_autoload_add", &EditorAutoloadSettings::_autoload_add);
|
|
|
|
ClassDB::bind_method("_autoload_selected", &EditorAutoloadSettings::_autoload_selected);
|
|
|
|
ClassDB::bind_method("_autoload_edited", &EditorAutoloadSettings::_autoload_edited);
|
|
|
|
ClassDB::bind_method("_autoload_button_pressed", &EditorAutoloadSettings::_autoload_button_pressed);
|
2018-01-02 06:24:02 +00:00
|
|
|
ClassDB::bind_method("_autoload_activated", &EditorAutoloadSettings::_autoload_activated);
|
2019-11-27 23:07:01 +00:00
|
|
|
ClassDB::bind_method("_autoload_path_text_changed", &EditorAutoloadSettings::_autoload_path_text_changed);
|
2018-12-15 04:22:04 +00:00
|
|
|
ClassDB::bind_method("_autoload_text_entered", &EditorAutoloadSettings::_autoload_text_entered);
|
2019-11-27 23:07:01 +00:00
|
|
|
ClassDB::bind_method("_autoload_text_changed", &EditorAutoloadSettings::_autoload_text_changed);
|
2018-01-02 06:24:02 +00:00
|
|
|
ClassDB::bind_method("_autoload_open", &EditorAutoloadSettings::_autoload_open);
|
2018-12-15 04:22:04 +00:00
|
|
|
ClassDB::bind_method("_autoload_file_callback", &EditorAutoloadSettings::_autoload_file_callback);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-01-03 02:03:46 +00:00
|
|
|
ClassDB::bind_method("get_drag_data_fw", &EditorAutoloadSettings::get_drag_data_fw);
|
|
|
|
ClassDB::bind_method("can_drop_data_fw", &EditorAutoloadSettings::can_drop_data_fw);
|
|
|
|
ClassDB::bind_method("drop_data_fw", &EditorAutoloadSettings::drop_data_fw);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-01-03 02:03:46 +00:00
|
|
|
ClassDB::bind_method("update_autoload", &EditorAutoloadSettings::update_autoload);
|
2018-02-14 04:31:38 +00:00
|
|
|
ClassDB::bind_method("autoload_add", &EditorAutoloadSettings::autoload_add);
|
|
|
|
ClassDB::bind_method("autoload_remove", &EditorAutoloadSettings::autoload_remove);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("autoload_changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorAutoloadSettings::EditorAutoloadSettings() {
|
2018-05-01 14:06:23 +00:00
|
|
|
// Make first cache
|
|
|
|
List<PropertyInfo> props;
|
|
|
|
ProjectSettings::get_singleton()->get_property_list(&props);
|
|
|
|
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
|
|
|
|
const PropertyInfo &pi = E->get();
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!pi.name.begins_with("autoload/")) {
|
2018-05-01 14:06:23 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-05-01 14:06:23 +00:00
|
|
|
|
|
|
|
String name = pi.name.get_slice("/", 1);
|
|
|
|
String path = ProjectSettings::get_singleton()->get(pi.name);
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (name.empty()) {
|
2018-05-01 14:06:23 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-05-01 14:06:23 +00:00
|
|
|
|
|
|
|
AutoLoadInfo info;
|
|
|
|
info.is_singleton = path.begins_with("*");
|
|
|
|
|
|
|
|
if (info.is_singleton) {
|
|
|
|
path = path.substr(1, path.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
info.name = name;
|
|
|
|
info.path = path;
|
|
|
|
info.order = ProjectSettings::get_singleton()->get_order(pi.name);
|
2018-06-18 18:52:46 +00:00
|
|
|
|
|
|
|
if (info.is_singleton) {
|
|
|
|
// Make sure name references work before parsing scripts
|
|
|
|
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
|
|
|
|
ScriptServer::get_language(i)->add_named_global_constant(info.name, Variant());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
autoload_cache.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
|
|
|
|
AutoLoadInfo &info = E->get();
|
|
|
|
|
|
|
|
info.node = _create_autoload(info.path);
|
2018-05-22 16:26:13 +00:00
|
|
|
|
|
|
|
if (info.node) {
|
|
|
|
Ref<Script> scr = info.node->get_script();
|
|
|
|
info.in_editor = scr.is_valid() && scr->is_tool();
|
|
|
|
info.node->set_name(info.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.is_singleton) {
|
|
|
|
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
|
|
|
|
ScriptServer::get_language(i)->add_named_global_constant(info.name, info.node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
if (!info.is_singleton && !info.in_editor && info.node != nullptr) {
|
2018-05-22 16:26:13 +00:00
|
|
|
memdelete(info.node);
|
2021-05-04 14:00:45 +00:00
|
|
|
info.node = nullptr;
|
2018-05-22 16:26:13 +00:00
|
|
|
}
|
2018-06-18 18:52:46 +00:00
|
|
|
}
|
2018-05-01 14:06:23 +00:00
|
|
|
|
2016-07-04 23:22:28 +00:00
|
|
|
autoload_changed = "autoload_changed";
|
|
|
|
|
|
|
|
updating_autoload = false;
|
|
|
|
selected_autoload = "";
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
HBoxContainer *hbc = memnew(HBoxContainer);
|
2016-07-04 23:22:28 +00:00
|
|
|
add_child(hbc);
|
|
|
|
|
2021-09-13 20:54:20 +00:00
|
|
|
error_message = memnew(Label);
|
|
|
|
error_message->hide();
|
|
|
|
error_message->set_align(Label::Align::ALIGN_RIGHT);
|
|
|
|
error_message->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
|
|
|
|
add_child(error_message);
|
|
|
|
|
2017-12-17 17:22:26 +00:00
|
|
|
Label *l = memnew(Label);
|
|
|
|
l->set_text(TTR("Path:"));
|
|
|
|
hbc->add_child(l);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
autoload_add_path = memnew(EditorLineEditFileChooser);
|
2016-07-04 23:22:28 +00:00
|
|
|
autoload_add_path->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
autoload_add_path->get_file_dialog()->set_mode(EditorFileDialog::MODE_OPEN_FILE);
|
|
|
|
autoload_add_path->get_file_dialog()->connect("file_selected", this, "_autoload_file_callback");
|
2019-11-27 23:07:01 +00:00
|
|
|
autoload_add_path->get_line_edit()->connect("text_changed", this, "_autoload_path_text_changed");
|
|
|
|
|
2017-12-17 17:22:26 +00:00
|
|
|
hbc->add_child(autoload_add_path);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-12-17 17:22:26 +00:00
|
|
|
l = memnew(Label);
|
|
|
|
l->set_text(TTR("Node Name:"));
|
|
|
|
hbc->add_child(l);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
autoload_add_name = memnew(LineEdit);
|
2016-07-04 23:22:28 +00:00
|
|
|
autoload_add_name->set_h_size_flags(SIZE_EXPAND_FILL);
|
2018-12-15 04:22:04 +00:00
|
|
|
autoload_add_name->connect("text_entered", this, "_autoload_text_entered");
|
2019-11-27 23:07:01 +00:00
|
|
|
autoload_add_name->connect("text_changed", this, "_autoload_text_changed");
|
2017-12-17 17:22:26 +00:00
|
|
|
hbc->add_child(autoload_add_name);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2019-11-27 23:07:01 +00:00
|
|
|
add_autoload = memnew(Button);
|
2016-07-04 23:22:28 +00:00
|
|
|
add_autoload->set_text(TTR("Add"));
|
|
|
|
add_autoload->connect("pressed", this, "_autoload_add");
|
2019-11-27 23:07:01 +00:00
|
|
|
// The button will be enabled once a valid name is entered (either automatically or manually).
|
|
|
|
add_autoload->set_disabled(true);
|
2017-12-17 17:22:26 +00:00
|
|
|
hbc->add_child(add_autoload);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tree = memnew(Tree);
|
2016-07-04 23:22:28 +00:00
|
|
|
tree->set_hide_root(true);
|
|
|
|
tree->set_select_mode(Tree::SELECT_MULTI);
|
2017-08-18 21:19:12 +00:00
|
|
|
tree->set_allow_reselect(true);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
tree->set_drag_forwarding(this);
|
|
|
|
|
|
|
|
tree->set_columns(4);
|
|
|
|
tree->set_column_titles_visible(true);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tree->set_column_title(0, TTR("Name"));
|
|
|
|
tree->set_column_expand(0, true);
|
2021-05-10 17:06:57 +00:00
|
|
|
tree->set_column_min_width(0, 100 * EDSCALE);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tree->set_column_title(1, TTR("Path"));
|
|
|
|
tree->set_column_expand(1, true);
|
2021-05-10 17:06:57 +00:00
|
|
|
tree->set_column_min_width(1, 100 * EDSCALE);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2021-05-10 17:06:57 +00:00
|
|
|
tree->set_column_title(2, TTR("Global Variable"));
|
2017-03-05 15:44:50 +00:00
|
|
|
tree->set_column_expand(2, false);
|
2021-05-10 17:06:57 +00:00
|
|
|
// Reserve enough space for translations of "Global Variable" which may be longer.
|
|
|
|
tree->set_column_min_width(2, 150 * EDSCALE);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tree->set_column_expand(3, false);
|
2018-03-11 14:28:23 +00:00
|
|
|
tree->set_column_min_width(3, 120 * EDSCALE);
|
2016-07-04 23:22:28 +00:00
|
|
|
|
|
|
|
tree->connect("cell_selected", this, "_autoload_selected");
|
|
|
|
tree->connect("item_edited", this, "_autoload_edited");
|
|
|
|
tree->connect("button_pressed", this, "_autoload_button_pressed");
|
2018-01-02 06:24:02 +00:00
|
|
|
tree->connect("item_activated", this, "_autoload_activated");
|
2017-12-17 17:22:26 +00:00
|
|
|
tree->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
|
|
|
add_child(tree, true);
|
2016-07-04 23:22:28 +00:00
|
|
|
}
|
2018-05-22 16:26:13 +00:00
|
|
|
|
|
|
|
EditorAutoloadSettings::~EditorAutoloadSettings() {
|
|
|
|
for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
|
|
|
|
AutoLoadInfo &info = E->get();
|
|
|
|
if (info.node && !info.in_editor) {
|
|
|
|
memdelete(info.node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|