/**************************************************************************/ /* editor_autoload_settings.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "editor_autoload_settings.h" #include "core/config/project_settings.h" #include "core/core_constants.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "editor/docks/filesystem_dock.h" #include "editor/editor_node.h" #include "editor/editor_string_names.h" #include "editor/editor_undo_redo_manager.h" #include "editor/gui/editor_file_dialog.h" #include "editor/scene/scene_create_dialog.h" #include "editor/settings/project_settings_editor.h" #include "scene/gui/button.h" #include "scene/gui/tree.h" #include "scene/main/scene_tree.h" #include "scene/main/window.h" #include "scene/resources/packed_scene.h" #define PREVIEW_LIST_MAX_SIZE 10 void EditorAutoloadSettings::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { List afn; ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn); for (const String &E : afn) { scene_file_dialog->add_filter("*." + E); } ResourceLoader::get_recognized_extensions_for_type("Script", &afn); for (const String &E : afn) { autoload_file_dialog->add_filter("*." + E); } } break; case NOTIFICATION_THEME_CHANGED: { browse_button->set_button_icon(get_editor_theme_icon(SNAME("FileBrowse"))); create_script_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add"))); create_scene_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add"))); } break; } } bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, String *r_error) { if (!p_name.is_valid_unicode_identifier()) { if (r_error) { *r_error = TTR("Must be a valid Unicode identifier."); } return false; } if (ClassDB::class_exists(p_name)) { if (r_error) { *r_error = TTR("Must not collide with an existing engine class name."); } return false; } if (ScriptServer::is_global_class(p_name)) { if (r_error) { *r_error = TTR("Must not collide with an existing global script class name."); } return false; } if (Variant::get_type_by_name(p_name) < Variant::VARIANT_MAX) { if (r_error) { *r_error = TTR("Must not collide with an existing built-in type name."); } return false; } for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) { if (CoreConstants::get_global_constant_name(i) == p_name) { if (r_error) { *r_error = TTR("Must not collide with an existing global constant name."); } return false; } } for (int i = 0; i < ScriptServer::get_language_count(); i++) { for (const String &keyword : ScriptServer::get_language(i)->get_reserved_words()) { if (keyword == p_name) { if (r_error) { *r_error = TTR("Keyword cannot be used as an Autoload name."); } return false; } } } return true; } void EditorAutoloadSettings::_autoload_selected() { TreeItem *ti = tree->get_selected(); if (!ti) { return; } selected_autoload = "autoload/" + ti->get_text(0); } void EditorAutoloadSettings::_autoload_edited() { if (updating_autoload) { return; } TreeItem *ti = tree->get_edited(); int column = tree->get_edited_column(); EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); if (column == 0) { String name = ti->get_text(0); String old_name = selected_autoload.get_slicec('/', 1); if (name == old_name) { return; } String error; if (!_autoload_name_is_valid(name, &error)) { ti->set_text(0, old_name); EditorNode::get_singleton()->show_warning(error); return; } if (ProjectSettings::get_singleton()->has_setting("autoload/" + name)) { 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; int order = ProjectSettings::get_singleton()->get_order(selected_autoload); String scr_path = GLOBAL_GET(selected_autoload); undo_redo->create_action(TTR("Rename Autoload")); undo_redo->add_do_property(ProjectSettings::get_singleton(), name, scr_path); undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, order); undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", selected_autoload); undo_redo->add_undo_property(ProjectSettings::get_singleton(), selected_autoload, scr_path); undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", selected_autoload, order); undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", name); undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload"); undo_redo->add_undo_method(this, CoreStringName(call_deferred), "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(); selected_autoload = name; } else if (column == 2) { updating_autoload = true; bool checked = ti->is_checked(2); String base = "autoload/" + ti->get_text(0); int order = ProjectSettings::get_singleton()->get_order(base); String scr_path = GLOBAL_GET(base); if (scr_path.begins_with("*")) { scr_path = scr_path.substr(1); } // Singleton autoloads are represented with a leading "*" in their path. if (checked) { scr_path = "*" + scr_path; } undo_redo->create_action(TTR("Toggle Autoload Globals")); undo_redo->add_do_property(ProjectSettings::get_singleton(), base, scr_path); undo_redo->add_undo_property(ProjectSettings::get_singleton(), base, GLOBAL_GET(base)); undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", base, order); undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", base, order); undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload"); undo_redo->add_undo_method(this, CoreStringName(call_deferred), "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(); } updating_autoload = false; } void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) { if (p_mouse_button != MouseButton::LEFT) { return; } TreeItem *ti = Object::cast_to(p_item); String name = "autoload/" + ti->get_text(0); EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); switch (p_button) { case BUTTON_MOVE_UP: case BUTTON_MOVE_DOWN: { TreeItem *swap = nullptr; if (p_button == BUTTON_MOVE_UP) { swap = ti->get_prev(); } else { swap = ti->get_next(); } if (!swap) { return; } String swap_name = "autoload/" + swap->get_text(0); int order = ProjectSettings::get_singleton()->get_order(name); int swap_order = ProjectSettings::get_singleton()->get_order(swap_name); undo_redo->create_action(TTR("Move Autoload")); 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); 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); 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: { 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, GLOBAL_GET(name)); undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, 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(); } break; } } void EditorAutoloadSettings::_autoload_activated() { TreeItem *ti = tree->get_selected(); if (!ti) { return; } _autoload_open(ti->get_text(1)); } void EditorAutoloadSettings::_autoload_open(const String &fpath) { EditorNode::get_singleton()->load_scene_or_resource(fpath); ProjectSettingsEditor::get_singleton()->hide(); } Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { Node *n = nullptr; if (ResourceLoader::get_resource_type(p_path) == "PackedScene") { // Cache the scene reference before loading it (for cyclic references) Ref scn; scn.instantiate(); scn->set_path(ResourceUID::ensure_path(p_path)); scn->reload_from_file(); ERR_FAIL_COND_V_MSG(scn.is_null(), nullptr, vformat("Failed to create an autoload, can't load from UID or path: %s.", p_path)); if (scn.is_valid()) { n = scn->instantiate(); } } else { Ref res = ResourceLoader::load(p_path); ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, vformat("Failed to create an autoload, can't load from UID or path: %s.", p_path)); Ref