Path: blob/master/editor/settings/editor_autoload_settings.cpp
9903 views
/**************************************************************************/1/* editor_autoload_settings.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_autoload_settings.h"3132#include "core/config/project_settings.h"33#include "core/core_constants.h"34#include "editor/docks/filesystem_dock.h"35#include "editor/editor_node.h"36#include "editor/editor_string_names.h"37#include "editor/editor_undo_redo_manager.h"38#include "editor/gui/editor_file_dialog.h"39#include "editor/settings/project_settings_editor.h"40#include "scene/main/window.h"41#include "scene/resources/packed_scene.h"4243#define PREVIEW_LIST_MAX_SIZE 104445void EditorAutoloadSettings::_notification(int p_what) {46switch (p_what) {47case NOTIFICATION_ENTER_TREE: {48List<String> afn;49ResourceLoader::get_recognized_extensions_for_type("Script", &afn);50ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn);5152for (const String &E : afn) {53file_dialog->add_filter("*." + E);54}5556browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder")));57} break;5859case NOTIFICATION_TRANSLATION_CHANGED: {60if (!error_message->get_text().is_empty()) {61_autoload_text_changed(autoload_add_name->get_text());62}63} break;6465case NOTIFICATION_THEME_CHANGED: {66browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder")));67add_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add")));68} break;6970case NOTIFICATION_VISIBILITY_CHANGED: {71FileSystemDock *dock = FileSystemDock::get_singleton();7273if (dock != nullptr) {74ScriptCreateDialog *dialog = dock->get_script_create_dialog();7576if (dialog != nullptr) {77Callable script_created = callable_mp(this, &EditorAutoloadSettings::_script_created);7879if (is_visible_in_tree()) {80if (!dialog->is_connected(SNAME("script_created"), script_created)) {81dialog->connect("script_created", script_created);82}83} else {84if (dialog->is_connected(SNAME("script_created"), script_created)) {85dialog->disconnect("script_created", script_created);86}87}88}89}90} break;91}92}9394bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, String *r_error) {95if (!p_name.is_valid_unicode_identifier()) {96if (r_error) {97*r_error = TTR("Invalid name.") + " " + TTR("Must be a valid Unicode identifier.");98}99100return false;101}102103if (ClassDB::class_exists(p_name)) {104if (r_error) {105*r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing engine class name.");106}107108return false;109}110111if (ScriptServer::is_global_class(p_name)) {112if (r_error) {113*r_error = TTR("Invalid name.") + "\n" + TTR("Must not collide with an existing global script class name.");114}115116return false;117}118119if (Variant::get_type_by_name(p_name) < Variant::VARIANT_MAX) {120if (r_error) {121*r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing built-in type name.");122}123124return false;125}126127for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {128if (CoreConstants::get_global_constant_name(i) == p_name) {129if (r_error) {130*r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing global constant name.");131}132133return false;134}135}136137for (int i = 0; i < ScriptServer::get_language_count(); i++) {138for (const String &keyword : ScriptServer::get_language(i)->get_reserved_words()) {139if (keyword == p_name) {140if (r_error) {141*r_error = TTR("Invalid name.") + " " + TTR("Keyword cannot be used as an Autoload name.");142}143144return false;145}146}147}148149return true;150}151152void EditorAutoloadSettings::_autoload_add() {153if (autoload_add_path->get_text().is_empty()) {154ScriptCreateDialog *dialog = FileSystemDock::get_singleton()->get_script_create_dialog();155String fpath = path;156if (!fpath.ends_with("/")) {157fpath = fpath.get_base_dir();158}159dialog->config("Node", fpath.path_join(vformat("%s.gd", autoload_add_name->get_text())), false, false);160dialog->popup_centered();161} else {162if (autoload_add(autoload_add_name->get_text(), autoload_add_path->get_text())) {163autoload_add_path->set_text("");164}165166autoload_add_name->set_text("");167add_autoload->set_disabled(true);168}169}170171void EditorAutoloadSettings::_autoload_selected() {172TreeItem *ti = tree->get_selected();173174if (!ti) {175return;176}177178selected_autoload = "autoload/" + ti->get_text(0);179}180181void EditorAutoloadSettings::_autoload_edited() {182if (updating_autoload) {183return;184}185186TreeItem *ti = tree->get_edited();187int column = tree->get_edited_column();188189EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();190191if (column == 0) {192String name = ti->get_text(0);193String old_name = selected_autoload.get_slicec('/', 1);194195if (name == old_name) {196return;197}198199String error;200if (!_autoload_name_is_valid(name, &error)) {201ti->set_text(0, old_name);202EditorNode::get_singleton()->show_warning(error);203return;204}205206if (ProjectSettings::get_singleton()->has_setting("autoload/" + name)) {207ti->set_text(0, old_name);208EditorNode::get_singleton()->show_warning(vformat(TTR("Autoload '%s' already exists!"), name));209return;210}211212updating_autoload = true;213214name = "autoload/" + name;215216int order = ProjectSettings::get_singleton()->get_order(selected_autoload);217String scr_path = GLOBAL_GET(selected_autoload);218219undo_redo->create_action(TTR("Rename Autoload"));220221undo_redo->add_do_property(ProjectSettings::get_singleton(), name, scr_path);222undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, order);223undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", selected_autoload);224225undo_redo->add_undo_property(ProjectSettings::get_singleton(), selected_autoload, scr_path);226undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", selected_autoload, order);227undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", name);228229undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload");230undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_autoload");231232undo_redo->add_do_method(this, "emit_signal", autoload_changed);233undo_redo->add_undo_method(this, "emit_signal", autoload_changed);234235undo_redo->commit_action();236237selected_autoload = name;238} else if (column == 2) {239updating_autoload = true;240241bool checked = ti->is_checked(2);242String base = "autoload/" + ti->get_text(0);243244int order = ProjectSettings::get_singleton()->get_order(base);245String scr_path = GLOBAL_GET(base);246247if (scr_path.begins_with("*")) {248scr_path = scr_path.substr(1);249}250251// Singleton autoloads are represented with a leading "*" in their path.252if (checked) {253scr_path = "*" + scr_path;254}255256undo_redo->create_action(TTR("Toggle Autoload Globals"));257258undo_redo->add_do_property(ProjectSettings::get_singleton(), base, scr_path);259undo_redo->add_undo_property(ProjectSettings::get_singleton(), base, GLOBAL_GET(base));260261undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", base, order);262undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", base, order);263264undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload");265undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_autoload");266267undo_redo->add_do_method(this, "emit_signal", autoload_changed);268undo_redo->add_undo_method(this, "emit_signal", autoload_changed);269270undo_redo->commit_action();271}272273updating_autoload = false;274}275276void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {277if (p_mouse_button != MouseButton::LEFT) {278return;279}280TreeItem *ti = Object::cast_to<TreeItem>(p_item);281282String name = "autoload/" + ti->get_text(0);283284EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();285286switch (p_button) {287case BUTTON_OPEN: {288_autoload_open(ti->get_text(1));289} break;290case BUTTON_MOVE_UP:291case BUTTON_MOVE_DOWN: {292TreeItem *swap = nullptr;293294if (p_button == BUTTON_MOVE_UP) {295swap = ti->get_prev();296} else {297swap = ti->get_next();298}299300if (!swap) {301return;302}303304String swap_name = "autoload/" + swap->get_text(0);305306int order = ProjectSettings::get_singleton()->get_order(name);307int swap_order = ProjectSettings::get_singleton()->get_order(swap_name);308309undo_redo->create_action(TTR("Move Autoload"));310311undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, swap_order);312undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);313314undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", swap_name, order);315undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", swap_name, swap_order);316317undo_redo->add_do_method(this, "update_autoload");318undo_redo->add_undo_method(this, "update_autoload");319320undo_redo->add_do_method(this, "emit_signal", autoload_changed);321undo_redo->add_undo_method(this, "emit_signal", autoload_changed);322323undo_redo->commit_action();324} break;325case BUTTON_DELETE: {326int order = ProjectSettings::get_singleton()->get_order(name);327328undo_redo->create_action(TTR("Remove Autoload"));329330undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());331332undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));333undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);334335undo_redo->add_do_method(this, "update_autoload");336undo_redo->add_undo_method(this, "update_autoload");337338undo_redo->add_do_method(this, "emit_signal", autoload_changed);339undo_redo->add_undo_method(this, "emit_signal", autoload_changed);340341undo_redo->commit_action();342} break;343}344}345346void EditorAutoloadSettings::_autoload_activated() {347TreeItem *ti = tree->get_selected();348if (!ti) {349return;350}351_autoload_open(ti->get_text(1));352}353354void EditorAutoloadSettings::_autoload_open(const String &fpath) {355EditorNode::get_singleton()->load_scene_or_resource(fpath);356ProjectSettingsEditor::get_singleton()->hide();357}358359void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) {360// Convert the file name to PascalCase, which is the convention for classes in GDScript.361const String class_name = p_path.get_file().get_basename().to_pascal_case();362363// If the name collides with a built-in class, prefix the name to make it possible to add without having to edit the name.364// The prefix is subjective, but it provides better UX than leaving the Add button disabled :)365const String prefix = ClassDB::class_exists(class_name) ? "Global" : "";366367autoload_add_name->set_text(prefix + class_name);368add_autoload->set_disabled(false);369}370371void EditorAutoloadSettings::_autoload_text_submitted(const String &p_name) {372if (!autoload_add_path->get_text().is_empty() && _autoload_name_is_valid(p_name, nullptr)) {373_autoload_add();374}375}376377void EditorAutoloadSettings::_autoload_path_text_changed(const String &p_path) {378add_autoload->set_disabled(!_autoload_name_is_valid(autoload_add_name->get_text(), nullptr));379}380381void EditorAutoloadSettings::_autoload_text_changed(const String &p_name) {382String error_string;383bool is_name_valid = _autoload_name_is_valid(p_name, &error_string);384add_autoload->set_disabled(!is_name_valid);385error_message->set_text(error_string);386error_message->set_visible(!autoload_add_name->get_text().is_empty() && !is_name_valid);387}388389Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {390Node *n = nullptr;391if (ResourceLoader::get_resource_type(p_path) == "PackedScene") {392// Cache the scene reference before loading it (for cyclic references)393Ref<PackedScene> scn;394scn.instantiate();395scn->set_path(p_path);396scn->reload_from_file();397ERR_FAIL_COND_V_MSG(scn.is_null(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path));398399if (scn.is_valid()) {400n = scn->instantiate();401}402} else {403Ref<Resource> res = ResourceLoader::load(p_path);404ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path));405406Ref<Script> scr = res;407if (scr.is_valid()) {408ERR_FAIL_COND_V_MSG(!scr->is_valid(), nullptr, vformat("Failed to create an autoload, script '%s' is not compiling.", p_path));409410StringName ibt = scr->get_instance_base_type();411bool valid_type = ClassDB::is_parent_class(ibt, "Node");412ERR_FAIL_COND_V_MSG(!valid_type, nullptr, vformat("Failed to create an autoload, script '%s' does not inherit from 'Node'.", p_path));413414Object *obj = ClassDB::instantiate(ibt);415ERR_FAIL_NULL_V_MSG(obj, nullptr, vformat("Failed to create an autoload, cannot instantiate '%s'.", ibt));416417n = Object::cast_to<Node>(obj);418n->set_script(scr);419}420}421422ERR_FAIL_NULL_V_MSG(n, nullptr, vformat("Failed to create an autoload, path is not pointing to a scene or a script: %s.", p_path));423424return n;425}426427void EditorAutoloadSettings::init_autoloads() {428for (AutoloadInfo &info : autoload_cache) {429info.node = _create_autoload(info.path);430431if (info.node) {432Ref<Script> scr = info.node->get_script();433info.in_editor = scr.is_valid() && scr->is_tool();434info.node->set_name(info.name);435}436437if (info.is_singleton) {438for (int i = 0; i < ScriptServer::get_language_count(); i++) {439ScriptServer::get_language(i)->add_named_global_constant(info.name, info.node);440}441}442443if (!info.is_singleton && !info.in_editor && info.node != nullptr) {444memdelete(info.node);445info.node = nullptr;446}447}448449for (const AutoloadInfo &info : autoload_cache) {450if (info.node && info.in_editor) {451// It's important to add the node without deferring because code in plugins or tool scripts452// could use the autoload node when they are enabled.453get_tree()->get_root()->add_child(info.node);454}455}456}457458void EditorAutoloadSettings::update_autoload() {459if (updating_autoload) {460return;461}462463updating_autoload = true;464465HashMap<String, AutoloadInfo> to_remove;466List<AutoloadInfo *> to_add;467468for (const AutoloadInfo &info : autoload_cache) {469to_remove.insert(info.name, info);470}471472autoload_cache.clear();473474tree->clear();475TreeItem *root = tree->create_item();476477List<PropertyInfo> props;478ProjectSettings::get_singleton()->get_property_list(&props);479480for (const PropertyInfo &pi : props) {481if (!pi.name.begins_with("autoload/")) {482continue;483}484485String name = pi.name.get_slicec('/', 1);486String scr_path = GLOBAL_GET(pi.name);487488if (name.is_empty()) {489continue;490}491492AutoloadInfo info;493info.is_singleton = scr_path.begins_with("*");494495if (info.is_singleton) {496scr_path = scr_path.substr(1);497}498499info.name = name;500info.path = scr_path;501info.order = ProjectSettings::get_singleton()->get_order(pi.name);502503bool need_to_add = true;504if (to_remove.has(name)) {505AutoloadInfo &old_info = to_remove[name];506if (old_info.path == info.path) {507// Still the same resource, check status508info.node = old_info.node;509if (info.node) {510Ref<Script> scr = info.node->get_script();511info.in_editor = scr.is_valid() && scr->is_tool();512if (info.is_singleton == old_info.is_singleton && info.in_editor == old_info.in_editor) {513to_remove.erase(name);514need_to_add = false;515} else {516info.node = nullptr;517}518}519}520}521522autoload_cache.push_back(info);523524if (need_to_add) {525to_add.push_back(&(autoload_cache.back()->get()));526}527528TreeItem *item = tree->create_item(root);529item->set_text(0, name);530item->set_editable(0, true);531532item->set_text(1, scr_path);533item->set_selectable(1, true);534535item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);536item->set_editable(2, true);537item->set_text(2, TTRC("Enable"));538item->set_checked(2, info.is_singleton);539item->add_button(3, get_editor_theme_icon(SNAME("Load")), BUTTON_OPEN);540item->add_button(3, get_editor_theme_icon(SNAME("MoveUp")), BUTTON_MOVE_UP);541item->add_button(3, get_editor_theme_icon(SNAME("MoveDown")), BUTTON_MOVE_DOWN);542item->add_button(3, get_editor_theme_icon(SNAME("Remove")), BUTTON_DELETE);543item->set_selectable(3, false);544}545546// Remove deleted/changed autoloads547for (KeyValue<String, AutoloadInfo> &E : to_remove) {548AutoloadInfo &info = E.value;549if (info.is_singleton) {550for (int i = 0; i < ScriptServer::get_language_count(); i++) {551ScriptServer::get_language(i)->remove_named_global_constant(info.name);552}553}554if (info.in_editor) {555ERR_CONTINUE(!info.node);556callable_mp((Node *)get_tree()->get_root(), &Node::remove_child).call_deferred(info.node);557}558559if (info.node) {560info.node->queue_free();561info.node = nullptr;562}563}564565// Load new/changed autoloads566List<Node *> nodes_to_add;567for (AutoloadInfo *info : to_add) {568info->node = _create_autoload(info->path);569570ERR_CONTINUE(!info->node);571info->node->set_name(info->name);572573Ref<Script> scr = info->node->get_script();574info->in_editor = scr.is_valid() && scr->is_tool();575576if (info->in_editor) {577//defer so references are all valid on _ready()578nodes_to_add.push_back(info->node);579}580581if (info->is_singleton) {582for (int i = 0; i < ScriptServer::get_language_count(); i++) {583ScriptServer::get_language(i)->add_named_global_constant(info->name, info->node);584}585}586587if (!info->in_editor && !info->is_singleton) {588// No reason to keep this node589memdelete(info->node);590info->node = nullptr;591}592}593594for (Node *E : nodes_to_add) {595get_tree()->get_root()->add_child(E);596}597598updating_autoload = false;599}600601void EditorAutoloadSettings::_script_created(Ref<Script> p_script) {602FileSystemDock::get_singleton()->get_script_create_dialog()->hide();603path = p_script->get_path().get_base_dir();604autoload_add_path->set_text(p_script->get_path());605_autoload_add();606}607608LineEdit *EditorAutoloadSettings::get_path_box() const {609return autoload_add_path;610}611612Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control *p_control) {613if (autoload_cache.size() <= 1) {614return false;615}616617PackedStringArray autoloads;618619TreeItem *next = tree->get_next_selected(nullptr);620621while (next) {622autoloads.push_back(next->get_text(0));623next = tree->get_next_selected(next);624}625626if (autoloads.is_empty() || autoloads.size() == autoload_cache.size()) {627return Variant();628}629630VBoxContainer *preview = memnew(VBoxContainer);631632int max_size = MIN(PREVIEW_LIST_MAX_SIZE, autoloads.size());633634for (int i = 0; i < max_size; i++) {635Label *label = memnew(Label(autoloads[i]));636label->set_self_modulate(Color(1, 1, 1, Math::lerp(1, 0, float(i) / PREVIEW_LIST_MAX_SIZE)));637label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);638639preview->add_child(label);640}641642tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);643tree->set_drag_preview(preview);644645Dictionary drop_data;646drop_data["type"] = "autoload";647drop_data["autoloads"] = autoloads;648649return drop_data;650}651652bool EditorAutoloadSettings::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const {653if (updating_autoload) {654return false;655}656657Dictionary drop_data = p_data;658659if (!drop_data.has("type")) {660return false;661}662663if (drop_data.has("type")) {664TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_selected() : tree->get_item_at_position(p_point);665666if (!ti) {667return false;668}669670int section = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_drop_section_at_position(tree->get_item_rect(ti).position) : tree->get_drop_section_at_position(p_point);671672return section >= -1;673}674675return false;676}677678void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) {679TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_selected() : tree->get_item_at_position(p_point);680681if (!ti) {682return;683}684685int section = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_drop_section_at_position(tree->get_item_rect(ti).position) : tree->get_drop_section_at_position(p_point);686687if (section < -1) {688return;689}690691String name;692bool move_to_back = false;693694if (section < 0) {695name = ti->get_text(0);696} else if (ti->get_next()) {697name = ti->get_next()->get_text(0);698} else {699name = ti->get_text(0);700move_to_back = true;701}702703int order = ProjectSettings::get_singleton()->get_order("autoload/" + name);704705AutoloadInfo aux;706List<AutoloadInfo>::Element *E = nullptr;707708if (!move_to_back) {709aux.order = order;710E = autoload_cache.find(aux);711}712713Dictionary drop_data = p_data;714PackedStringArray autoloads = drop_data["autoloads"];715716// Store the initial order of the autoloads for comparison.717Vector<int> initial_orders;718initial_orders.resize(autoload_cache.size());719int idx = 0;720for (const AutoloadInfo &F : autoload_cache) {721initial_orders.write[idx++] = F.order;722}723724// Perform the drag-and-drop operation.725Vector<int> orders;726orders.resize(autoload_cache.size());727728for (int i = 0; i < autoloads.size(); i++) {729aux.order = ProjectSettings::get_singleton()->get_order("autoload/" + autoloads[i]);730731List<AutoloadInfo>::Element *I = autoload_cache.find(aux);732733if (move_to_back) {734autoload_cache.move_to_back(I);735} else if (E != I) {736autoload_cache.move_before(I, E);737} else if (E->next()) {738E = E->next();739} else {740break;741}742}743744idx = 0;745for (const AutoloadInfo &F : autoload_cache) {746orders.write[idx++] = F.order;747}748749// If the order didn't change, we shouldn't create undo/redo actions.750if (orders == initial_orders) {751return;752}753754orders.sort();755756EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();757758undo_redo->create_action(TTR("Rearrange Autoloads"));759760idx = 0;761for (const AutoloadInfo &F : autoload_cache) {762undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", "autoload/" + F.name, orders[idx++]);763undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", "autoload/" + F.name, F.order);764}765766orders.clear();767768undo_redo->add_do_method(this, "update_autoload");769undo_redo->add_undo_method(this, "update_autoload");770771undo_redo->add_do_method(this, "emit_signal", autoload_changed);772undo_redo->add_undo_method(this, "emit_signal", autoload_changed);773774undo_redo->commit_action();775}776777bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_path) {778String name = p_name;779780String error;781if (!_autoload_name_is_valid(name, &error)) {782EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + error);783return false;784}785786if (!FileAccess::exists(p_path)) {787EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + vformat(TTR("%s is an invalid path. File does not exist."), p_path));788return false;789}790791if (!p_path.begins_with("res://")) {792EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + vformat(TTR("%s is an invalid path. Not in resource path (res://)."), p_path));793return false;794}795796name = "autoload/" + name;797798EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();799800undo_redo->create_action(TTR("Add Autoload"));801// Singleton autoloads are represented with a leading "*" in their path.802undo_redo->add_do_property(ProjectSettings::get_singleton(), name, "*" + p_path);803804if (ProjectSettings::get_singleton()->has_setting(name)) {805undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));806} else {807undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, Variant());808}809810undo_redo->add_do_method(this, "update_autoload");811undo_redo->add_undo_method(this, "update_autoload");812813undo_redo->add_do_method(this, "emit_signal", autoload_changed);814undo_redo->add_undo_method(this, "emit_signal", autoload_changed);815816undo_redo->commit_action();817818return true;819}820821void EditorAutoloadSettings::autoload_remove(const String &p_name) {822String name = "autoload/" + p_name;823824EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();825826int order = ProjectSettings::get_singleton()->get_order(name);827828undo_redo->create_action(TTR("Remove Autoload"));829830undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());831832undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));833undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);834835undo_redo->add_do_method(this, "update_autoload");836undo_redo->add_undo_method(this, "update_autoload");837838undo_redo->add_do_method(this, "emit_signal", autoload_changed);839undo_redo->add_undo_method(this, "emit_signal", autoload_changed);840841undo_redo->commit_action();842}843844void EditorAutoloadSettings::_bind_methods() {845ClassDB::bind_method("update_autoload", &EditorAutoloadSettings::update_autoload);846ClassDB::bind_method("autoload_add", &EditorAutoloadSettings::autoload_add);847ClassDB::bind_method("autoload_remove", &EditorAutoloadSettings::autoload_remove);848849ADD_SIGNAL(MethodInfo("autoload_changed"));850}851852EditorAutoloadSettings::EditorAutoloadSettings() {853ProjectSettings::get_singleton()->add_hidden_prefix("autoload/");854855// Make first cache856List<PropertyInfo> props;857ProjectSettings::get_singleton()->get_property_list(&props);858for (const PropertyInfo &pi : props) {859if (!pi.name.begins_with("autoload/")) {860continue;861}862863String name = pi.name.get_slicec('/', 1);864String scr_path = GLOBAL_GET(pi.name);865866if (name.is_empty()) {867continue;868}869870AutoloadInfo info;871info.is_singleton = scr_path.begins_with("*");872873if (info.is_singleton) {874scr_path = scr_path.substr(1);875}876877info.name = name;878info.path = scr_path;879info.order = ProjectSettings::get_singleton()->get_order(pi.name);880881if (info.is_singleton) {882// Make sure name references work before parsing scripts883for (int i = 0; i < ScriptServer::get_language_count(); i++) {884ScriptServer::get_language(i)->add_named_global_constant(info.name, Variant());885}886}887888autoload_cache.push_back(info);889}890891HBoxContainer *hbc = memnew(HBoxContainer);892add_child(hbc);893894error_message = memnew(Label);895error_message->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);896error_message->set_focus_mode(FOCUS_ACCESSIBILITY);897error_message->hide();898error_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);899error_message->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));900add_child(error_message);901902Label *l = memnew(Label);903l->set_text(TTRC("Path:"));904hbc->add_child(l);905906autoload_add_path = memnew(LineEdit);907hbc->add_child(autoload_add_path);908autoload_add_path->set_accessibility_name(TTRC("Autoload Path"));909autoload_add_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);910autoload_add_path->set_clear_button_enabled(true);911autoload_add_path->set_placeholder(TTRC("Set path or press \"Add\" to create a script."));912autoload_add_path->connect(SceneStringName(text_changed), callable_mp(this, &EditorAutoloadSettings::_autoload_path_text_changed));913914browse_button = memnew(Button);915hbc->add_child(browse_button);916browse_button->set_accessibility_name(TTRC("Select Autoload Path"));917browse_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_browse_autoload_add_path));918919file_dialog = memnew(EditorFileDialog);920hbc->add_child(file_dialog);921file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));922file_dialog->connect("dir_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));923file_dialog->connect("files_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));924925hbc->set_h_size_flags(SIZE_EXPAND_FILL);926file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);927file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_file_callback));928929l = memnew(Label);930l->set_text(TTRC("Node Name:"));931hbc->add_child(l);932933autoload_add_name = memnew(LineEdit);934autoload_add_name->set_accessibility_name(TTRC("Node Name:"));935autoload_add_name->set_h_size_flags(SIZE_EXPAND_FILL);936autoload_add_name->connect(SceneStringName(text_submitted), callable_mp(this, &EditorAutoloadSettings::_autoload_text_submitted));937autoload_add_name->connect(SceneStringName(text_changed), callable_mp(this, &EditorAutoloadSettings::_autoload_text_changed));938hbc->add_child(autoload_add_name);939940add_autoload = memnew(Button);941add_autoload->set_text(TTRC("Add"));942add_autoload->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_autoload_add));943// The button will be enabled once a valid name is entered (either automatically or manually).944add_autoload->set_disabled(true);945hbc->add_child(add_autoload);946947tree = memnew(Tree);948tree->set_accessibility_name(TTRC("Autoloads"));949tree->set_hide_root(true);950tree->set_select_mode(Tree::SELECT_MULTI);951tree->set_allow_reselect(true);952953SET_DRAG_FORWARDING_GCD(tree, EditorAutoloadSettings);954955tree->set_columns(4);956tree->set_column_titles_visible(true);957958tree->set_column_title(0, TTRC("Name"));959tree->set_column_title_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);960tree->set_column_expand(0, true);961tree->set_column_expand_ratio(0, 1);962963tree->set_column_title(1, TTRC("Path"));964tree->set_column_title_alignment(1, HORIZONTAL_ALIGNMENT_LEFT);965tree->set_column_expand(1, true);966tree->set_column_clip_content(1, true);967tree->set_column_expand_ratio(1, 2);968969tree->set_column_title(2, TTRC("Global Variable"));970tree->set_column_expand(2, false);971972tree->set_column_expand(3, false);973974tree->connect("cell_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_selected));975tree->connect("item_edited", callable_mp(this, &EditorAutoloadSettings::_autoload_edited));976tree->connect("button_clicked", callable_mp(this, &EditorAutoloadSettings::_autoload_button_pressed));977tree->connect("item_activated", callable_mp(this, &EditorAutoloadSettings::_autoload_activated));978tree->set_v_size_flags(SIZE_EXPAND_FILL);979980add_child(tree, true);981}982983EditorAutoloadSettings::~EditorAutoloadSettings() {984for (const AutoloadInfo &info : autoload_cache) {985if (info.node && !info.in_editor) {986memdelete(info.node);987}988}989}990991void EditorAutoloadSettings::_set_autoload_add_path(const String &p_text) {992autoload_add_path->set_text(p_text);993autoload_add_path->emit_signal(SceneStringName(text_submitted), p_text);994}995996void EditorAutoloadSettings::_browse_autoload_add_path() {997file_dialog->popup_file_dialog();998}99910001001