/*************************************************************************/ /* editor_autoload_settings.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* 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 "editor/editor_file_dialog.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" #include "editor/editor_undo_redo_manager.h" #include "editor/filesystem_dock.h" #include "project_settings_editor.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("Script", &afn); ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn); for (const String &E : afn) { file_dialog->add_filter("*." + E); } for (const AutoloadInfo &info : autoload_cache) { if (info.node && info.in_editor) { get_tree()->get_root()->call_deferred(SNAME("add_child"), info.node); } } } break; case NOTIFICATION_THEME_CHANGED: { browse_button->set_icon(get_theme_icon(SNAME("Folder"), SNAME("EditorIcons"))); } break; case NOTIFICATION_VISIBILITY_CHANGED: { FileSystemDock *dock = FileSystemDock::get_singleton(); if (dock != nullptr) { ScriptCreateDialog *dialog = dock->get_script_create_dialog(); if (dialog != nullptr) { Callable script_created = callable_mp(this, &EditorAutoloadSettings::_script_created); if (is_visible_in_tree()) { if (!dialog->is_connected(SNAME("script_created"), script_created)) { dialog->connect("script_created", script_created); } } else { if (dialog->is_connected(SNAME("script_created"), script_created)) { dialog->disconnect("script_created", script_created); } } } } } break; } } bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, String *r_error) { if (!p_name.is_valid_identifier()) { if (r_error) { *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 _"; } } return false; } if (ClassDB::class_exists(p_name)) { if (r_error) { *r_error = TTR("Invalid name.") + " " + 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("Invalid name.") + "\n" + TTR("Must not collide with an existing global script class name."); } return false; } for (int i = 0; i < Variant::VARIANT_MAX; i++) { if (Variant::get_type_name(Variant::Type(i)) == p_name) { if (r_error) { *r_error = TTR("Invalid name.") + " " + 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("Invalid name.") + " " + TTR("Must not collide with an existing global constant name."); } return false; } } for (int i = 0; i < ScriptServer::get_language_count(); i++) { List keywords; ScriptServer::get_language(i)->get_reserved_words(&keywords); for (const String &E : keywords) { if (E == p_name) { if (r_error) { *r_error = TTR("Invalid name.") + " " + TTR("Keyword cannot be used as an Autoload name."); } return false; } } } return true; } void EditorAutoloadSettings::_autoload_add() { if (autoload_add_path->get_text().is_empty()) { ScriptCreateDialog *dialog = FileSystemDock::get_singleton()->get_script_create_dialog(); String fpath = path; if (!fpath.ends_with("/")) { fpath = fpath.get_base_dir(); } dialog->config("Node", fpath.plus_file(vformat("%s.gd", autoload_add_name->get_text().camelcase_to_underscore())), false, false); dialog->popup_centered(); } else { if (autoload_add(autoload_add_name->get_text(), autoload_add_path->get_text())) { autoload_add_path->set_text(""); } autoload_add_name->set_text(""); add_autoload->set_disabled(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(); Ref undo_redo = EditorNode::get_undo_redo(); if (column == 0) { String name = ti->get_text(0); String old_name = selected_autoload.get_slice("/", 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 path = ProjectSettings::get_singleton()->get(selected_autoload); undo_redo->create_action(TTR("Rename Autoload")); 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); 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); undo_redo->add_do_method(this, "call_deferred", "update_autoload"); undo_redo->add_undo_method(this, "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 path = ProjectSettings::get_singleton()->get(base); if (path.begins_with("*")) { path = path.substr(1, path.length()); } // Singleton autoloads are represented with a leading "*" in their path. if (checked) { path = "*" + path; } undo_redo->create_action(TTR("Toggle Autoload Globals")); undo_redo->add_do_property(ProjectSettings::get_singleton(), base, path); undo_redo->add_undo_property(ProjectSettings::get_singleton(), base, ProjectSettings::get_singleton()->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, "call_deferred", "update_autoload"); undo_redo->add_undo_method(this, "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); Ref undo_redo = EditorNode::get_undo_redo(); switch (p_button) { case BUTTON_OPEN: { _autoload_open(ti->get_text(1)); } break; 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, 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(); } 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) { if (ResourceLoader::get_resource_type(fpath) == "PackedScene") { EditorNode::get_singleton()->open_request(fpath); } else { EditorNode::get_singleton()->load_resource(fpath); } ProjectSettingsEditor::get_singleton()->hide(); } void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) { // 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_submitted(const String p_name) { if (!autoload_add_path->get_text().is_empty() && _autoload_name_is_valid(p_name, nullptr)) { _autoload_add(); } } void EditorAutoloadSettings::_autoload_path_text_changed(const String p_path) { add_autoload->set_disabled(!_autoload_name_is_valid(autoload_add_name->get_text(), nullptr)); } void EditorAutoloadSettings::_autoload_text_changed(const String p_name) { String error_string; bool is_name_valid = _autoload_name_is_valid(p_name, &error_string); add_autoload->set_disabled(!is_name_valid); error_message->set_text(error_string); error_message->set_visible(!autoload_add_name->get_text().is_empty() && !is_name_valid); } Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { Ref res = ResourceLoader::load(p_path); ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, "Can't autoload: " + p_path + "."); Node *n = nullptr; Ref scn = res; Ref