/*************************************************************************/ /* editor_autoload_settings.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2021 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_node.h" #include "editor_scale.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) { if (p_what == NOTIFICATION_ENTER_TREE) { List afn; ResourceLoader::get_recognized_extensions_for_type("Script", &afn); ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn); for (List::Element *E = afn.front(); E; E = E->next()) { file_dialog->add_filter("*." + E->get()); } for (List::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); } } browse_button->set_icon(get_theme_icon("Folder", "EditorIcons")); } else if (p_what == NOTIFICATION_THEME_CHANGED) { browse_button->set_icon(get_theme_icon("Folder", "EditorIcons")); } } 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.") + "\n" + 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.") + "\n" + TTR("Must not collide with an existing engine 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.") + "\n" + 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.") + "\n" + 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 (List::Element *E = keywords.front(); E; E = E->next()) { if (E->get() == p_name) { if (r_error) { *r_error = TTR("Invalid name.") + "\n" + TTR("Keyword cannot be used as an autoload name."); } return false; } } } return true; } void EditorAutoloadSettings::_autoload_add() { 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(); UndoRedo *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) { TreeItem *ti = Object::cast_to(p_item); String name = "autoload/" + ti->get_text(0); UndoRedo *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() != "" && _autoload_name_is_valid(p_name, nullptr)) { _autoload_add(); } } void EditorAutoloadSettings::_autoload_path_text_changed(const String p_path) { add_autoload->set_disabled( p_path == "" || !_autoload_name_is_valid(autoload_add_name->get_text(), nullptr)); } void EditorAutoloadSettings::_autoload_text_changed(const String p_name) { add_autoload->set_disabled( autoload_add_path->get_text() == "" || !_autoload_name_is_valid(p_name, nullptr)); } Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { RES res = ResourceLoader::load(p_path); ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, "Can't autoload: " + p_path + "."); Node *n = nullptr; if (res->is_class("PackedScene")) { Ref ps = res; n = ps->instance(); } else if (res->is_class("Script")) { Ref