feat: updated engine

This commit is contained in:
Sara Gerretsen 2026-07-10 17:04:34 +02:00
parent cbe99774ff
commit f4cf6b3999
6607 changed files with 910135 additions and 430025 deletions

View file

@ -32,13 +32,23 @@
#include "core/config/project_settings.h"
#include "core/core_constants.h"
#include "core/io/resource_loader.h"
#include "core/io/resource_saver.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/gui/editor_validation_panel.h"
#include "editor/scene/scene_create_dialog.h"
#include "editor/settings/project_settings_editor.h"
#include "scene/main/window.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/button.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/tree.h"
#include "scene/main/scene_tree.h"
#include "scene/resources/packed_scene.h"
#define PREVIEW_LIST_MAX_SIZE 10
@ -47,47 +57,23 @@ void EditorAutoloadSettings::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
List<String> 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);
scene_file_dialog->add_filter("*." + E);
}
browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder")));
} break;
ResourceLoader::get_recognized_extensions_for_type("Script", &afn);
case NOTIFICATION_TRANSLATION_CHANGED: {
if (!error_message->get_text().is_empty()) {
_autoload_text_changed(autoload_add_name->get_text());
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("Folder")));
add_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add")));
} 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);
}
}
}
}
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;
}
}
@ -150,22 +136,11 @@ bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, Strin
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.path_join(vformat("%s.gd", autoload_add_name->get_text())), 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::_validate_autoload_name() {
String error;
bool is_valid = _autoload_name_is_valid(name_edit->get_text(), &error);
if (!is_valid) {
name_validator->set_message(0, error, EditorValidationPanel::MSG_ERROR);
}
}
@ -221,11 +196,11 @@ void EditorAutoloadSettings::_autoload_edited() {
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_do_property(ProjectSettings::get_singleton(), selected_autoload, Variant());
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_undo_property(ProjectSettings::get_singleton(), name, Variant());
undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload");
undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_autoload");
@ -285,9 +260,6 @@ void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_colu
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
switch (p_button) {
case BUTTON_OPEN: {
_autoload_open(ti->get_text(1));
} break;
case BUTTON_MOVE_UP:
case BUTTON_MOVE_DOWN: {
TreeItem *swap = nullptr;
@ -357,36 +329,6 @@ void EditorAutoloadSettings::_autoload_open(const String &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().to_pascal_case();
// 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) {
Node *n = nullptr;
if (ResourceLoader::get_resource_type(p_path) == "PackedScene") {
@ -406,7 +348,7 @@ Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
Ref<Script> scr = res;
if (scr.is_valid()) {
ERR_FAIL_COND_V_MSG(!scr->is_valid(), nullptr, vformat("Failed to create an autoload, script '%s' is not compiling.", p_path));
ERR_FAIL_COND_V_MSG(!scr->is_script_valid(), nullptr, vformat("Failed to create an autoload, script '%s' is not compiling.", p_path));
StringName ibt = scr->get_instance_base_type();
bool valid_type = ClassDB::is_parent_class(ibt, "Node");
@ -424,6 +366,20 @@ Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
return n;
}
void EditorAutoloadSettings::_create_script_autoload() {
if (!script_create_dialog) {
script_create_dialog = memnew(ScriptCreateDialog);
add_child(script_create_dialog);
script_create_dialog->connect("script_created", callable_mp(this, &EditorAutoloadSettings::_script_created));
}
script_create_dialog->config("Node", "res://new_autoload_script.gd", false, false);
script_create_dialog->popup_centered();
}
void EditorAutoloadSettings::_create_scene_autoload() {
scene_file_dialog->set_current_file("new_autoload_scene.tscn");
scene_file_dialog->popup_file_dialog();
}
void EditorAutoloadSettings::init_autoloads() {
for (AutoloadInfo &info : autoload_cache) {
@ -456,6 +412,65 @@ void EditorAutoloadSettings::init_autoloads() {
}
}
void EditorAutoloadSettings::_autoload_file_selected(const String &p_path) {
// Convert the file name to PascalCase, which is the convention for classes in GDScript.
_try_add_autoload(p_path.get_file().get_basename().to_pascal_case(), p_path);
}
void EditorAutoloadSettings::_scene_file_selected(const String &p_path) {
if (!scene_create_dialog) {
scene_create_dialog = memnew(SceneCreateDialog);
add_child(scene_create_dialog);
scene_create_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorAutoloadSettings::_scene_created));
}
scene_create_dialog->config(p_path.get_base_dir(), p_path.get_file().get_basename());
scene_create_dialog->popup_centered();
}
void EditorAutoloadSettings::_script_created(Ref<Script> p_script) {
FileSystemDock::get_singleton()->get_script_create_dialog()->hide();
path = p_script->get_path().get_base_dir();
_try_add_autoload(p_script->get_path().get_file().get_basename().to_pascal_case(), p_script->get_path());
}
void EditorAutoloadSettings::_scene_created() {
Node *root = scene_create_dialog->create_scene_root();
Ref<PackedScene> ps;
ps.instantiate();
ps->pack(root);
Error err = ResourceSaver::save(ps, scene_create_dialog->get_scene_path());
if (err != OK) {
EditorNode::get_singleton()->show_warning(vformat(TTR("Failed to create scene. Error: %d."), err));
return;
}
_try_add_autoload(scene_create_dialog->get_root_name().to_pascal_case(), scene_create_dialog->get_scene_path());
}
void EditorAutoloadSettings::_add_autoload(const String &p_name, const String &p_path) {
autoload_add(p_name, p_path, true);
}
void EditorAutoloadSettings::_try_add_autoload(const String &p_name, const String &p_path) {
if (_autoload_name_is_valid(p_name)) {
_add_autoload(p_name, p_path);
return;
}
pending_autoload_path = p_path;
name_edit->set_text(p_name);
name_validator->update();
name_dialog->popup_centered(Vector2i(600 * EDSCALE, 0));
name_edit->grab_focus();
name_edit->select_all();
}
void EditorAutoloadSettings::_confirm_autoload_name() {
_add_autoload(name_edit->get_text(), pending_autoload_path);
}
void EditorAutoloadSettings::update_autoload() {
if (updating_autoload) {
return;
@ -507,7 +522,7 @@ void EditorAutoloadSettings::update_autoload() {
if (old_info.path == info.path) {
// Still the same resource, check status
info.node = old_info.node;
if (info.node) {
if (info.node && info.node->is_instance()) {
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) {
@ -528,6 +543,7 @@ void EditorAutoloadSettings::update_autoload() {
TreeItem *item = tree->create_item(root);
item->set_text(0, name);
item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED); // For tooltip.
item->set_editable(0, true);
item->set_text(1, ResourceUID::ensure_path(scr_path));
@ -537,7 +553,6 @@ void EditorAutoloadSettings::update_autoload() {
item->set_editable(2, true);
item->set_text(2, TTRC("Enable"));
item->set_checked(2, info.is_singleton);
item->add_button(3, get_editor_theme_icon(SNAME("Load")), BUTTON_OPEN);
item->add_button(3, get_editor_theme_icon(SNAME("MoveUp")), BUTTON_MOVE_UP);
item->add_button(3, get_editor_theme_icon(SNAME("MoveDown")), BUTTON_MOVE_DOWN);
item->add_button(3, get_editor_theme_icon(SNAME("Remove")), BUTTON_DELETE);
@ -599,17 +614,6 @@ void EditorAutoloadSettings::update_autoload() {
updating_autoload = false;
}
void EditorAutoloadSettings::_script_created(Ref<Script> p_script) {
FileSystemDock::get_singleton()->get_script_create_dialog()->hide();
path = p_script->get_path().get_base_dir();
autoload_add_path->set_text(p_script->get_path());
_autoload_add();
}
LineEdit *EditorAutoloadSettings::get_path_box() const {
return autoload_add_path;
}
Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control *p_control) {
if (autoload_cache.size() <= 1) {
return false;
@ -775,7 +779,7 @@ void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant &
undo_redo->commit_action();
}
bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_path) {
bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_path, bool p_use_undo) {
String name = p_name;
String error;
@ -796,6 +800,13 @@ bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_
name = "autoload/" + name;
if (!p_use_undo) {
ProjectSettings::get_singleton()->set(name, "*" + p_path);
update_autoload();
emit_signal(autoload_changed);
return true;
}
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Add Autoload"));
@ -819,33 +830,14 @@ bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_
return true;
}
void EditorAutoloadSettings::autoload_remove(const String &p_name) {
String name = "autoload/" + p_name;
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
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();
void EditorAutoloadSettings::autoload_remove(const StringName &p_name) {
ProjectSettings::get_singleton()->remove_autoload(p_name);
update_autoload();
emit_signal(autoload_changed);
}
void EditorAutoloadSettings::_bind_methods() {
ClassDB::bind_method("update_autoload", &EditorAutoloadSettings::update_autoload);
ClassDB::bind_method("autoload_add", &EditorAutoloadSettings::autoload_add);
ClassDB::bind_method("autoload_remove", &EditorAutoloadSettings::autoload_remove);
ADD_SIGNAL(MethodInfo("autoload_changed"));
}
@ -892,58 +884,30 @@ EditorAutoloadSettings::EditorAutoloadSettings() {
HBoxContainer *hbc = memnew(HBoxContainer);
add_child(hbc);
error_message = memnew(Label);
error_message->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
error_message->set_focus_mode(FOCUS_ACCESSIBILITY);
error_message->hide();
error_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
error_message->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));
add_child(error_message);
autoload_file_dialog = memnew(EditorFileDialog);
autoload_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
hbc->add_child(autoload_file_dialog);
autoload_file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_file_selected));
Label *l = memnew(Label);
l->set_text(TTRC("Path:"));
hbc->add_child(l);
autoload_add_path = memnew(LineEdit);
hbc->add_child(autoload_add_path);
autoload_add_path->set_accessibility_name(TTRC("Autoload Path"));
autoload_add_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
autoload_add_path->set_clear_button_enabled(true);
autoload_add_path->set_placeholder(TTRC("Set path or press \"Add\" to create a script."));
autoload_add_path->connect(SceneStringName(text_changed), callable_mp(this, &EditorAutoloadSettings::_autoload_path_text_changed));
scene_file_dialog = memnew(EditorFileDialog);
scene_file_dialog->set_file_mode(FileDialog::FILE_MODE_SAVE_FILE);
hbc->add_child(scene_file_dialog);
scene_file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_scene_file_selected));
browse_button = memnew(Button);
browse_button->set_text(TTRC("Select Script/Scene"));
hbc->add_child(browse_button);
browse_button->set_accessibility_name(TTRC("Select Autoload Path"));
browse_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_browse_autoload_add_path));
browse_button->connect(SceneStringName(pressed), callable_mp(autoload_file_dialog, &FileDialog::popup_file_dialog));
file_dialog = memnew(EditorFileDialog);
hbc->add_child(file_dialog);
file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
file_dialog->connect("dir_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
file_dialog->connect("files_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
create_script_autoload = memnew(Button);
create_script_autoload->set_text(TTRC("Create Script"));
hbc->add_child(create_script_autoload);
create_script_autoload->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_create_script_autoload));
hbc->set_h_size_flags(SIZE_EXPAND_FILL);
file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_file_callback));
l = memnew(Label);
l->set_text(TTRC("Node Name:"));
hbc->add_child(l);
autoload_add_name = memnew(LineEdit);
autoload_add_name->set_accessibility_name(TTRC("Node Name:"));
autoload_add_name->set_h_size_flags(SIZE_EXPAND_FILL);
autoload_add_name->connect(SceneStringName(text_submitted), callable_mp(this, &EditorAutoloadSettings::_autoload_text_submitted));
autoload_add_name->connect(SceneStringName(text_changed), callable_mp(this, &EditorAutoloadSettings::_autoload_text_changed));
hbc->add_child(autoload_add_name);
add_autoload = memnew(Button);
add_autoload->set_text(TTRC("Add"));
add_autoload->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_autoload_add));
// The button will be enabled once a valid name is entered (either automatically or manually).
add_autoload->set_disabled(true);
hbc->add_child(add_autoload);
create_scene_autoload = memnew(Button);
create_scene_autoload->set_text(TTRC("Create Scene"));
hbc->add_child(create_scene_autoload);
create_scene_autoload->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_create_scene_autoload));
MarginContainer *mc = memnew(MarginContainer);
mc->set_v_size_flags(SIZE_EXPAND_FILL);
@ -964,17 +928,20 @@ EditorAutoloadSettings::EditorAutoloadSettings() {
tree->set_column_titles_visible(true);
tree->set_column_title(0, TTRC("Name"));
tree->set_column_title_tooltip_text(0, TTRC("Name of the autoload. Double-click to rename."));
tree->set_column_title_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
tree->set_column_expand(0, true);
tree->set_column_expand_ratio(0, 1);
tree->set_column_title(1, TTRC("Path"));
tree->set_column_title_tooltip_text(1, TTRC("Path to the autoload file. Double-click to open it."));
tree->set_column_title_alignment(1, HORIZONTAL_ALIGNMENT_LEFT);
tree->set_column_expand(1, true);
tree->set_column_clip_content(1, true);
tree->set_column_expand_ratio(1, 2);
tree->set_column_title(2, TTRC("Global Variable"));
tree->set_column_title_tooltip_text(2, TTRC("If enabled, this autoload can be accessed in scripts using its name."));
tree->set_column_expand(2, false);
tree->set_column_expand(3, false);
@ -985,6 +952,25 @@ EditorAutoloadSettings::EditorAutoloadSettings() {
tree->connect("item_activated", callable_mp(this, &EditorAutoloadSettings::_autoload_activated));
mc->add_child(tree, true);
name_dialog = memnew(ConfirmationDialog);
name_dialog->set_title(TTRC("Enter Autoload Name"));
add_child(name_dialog);
name_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorAutoloadSettings::_confirm_autoload_name));
VBoxContainer *name_vbox = memnew(VBoxContainer);
name_dialog->add_child(name_vbox);
name_edit = memnew(LineEdit);
name_vbox->add_child(name_edit);
name_dialog->register_text_enter(name_edit);
name_validator = memnew(EditorValidationPanel);
name_validator->set_accept_button(name_dialog->get_ok_button());
name_validator->set_update_callback(callable_mp(this, &EditorAutoloadSettings::_validate_autoload_name));
name_validator->add_line(0, TTRC("Autoload name is valid."));
name_vbox->add_child(name_validator);
name_edit->connect(SceneStringName(text_changed), callable_mp(name_validator, &EditorValidationPanel::update).unbind(1));
}
EditorAutoloadSettings::~EditorAutoloadSettings() {
@ -994,12 +980,3 @@ EditorAutoloadSettings::~EditorAutoloadSettings() {
}
}
}
void EditorAutoloadSettings::_set_autoload_add_path(const String &p_text) {
autoload_add_path->set_text(p_text);
autoload_add_path->emit_signal(SceneStringName(text_submitted), p_text);
}
void EditorAutoloadSettings::_browse_autoload_add_path() {
file_dialog->popup_file_dialog();
}