Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions editor/settings/editor_autoload_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@
#include "editor/editor_node.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 "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/main/window.h"
#include "scene/resources/packed_scene.h"

#define PREVIEW_LIST_MAX_SIZE 10
Expand Down Expand Up @@ -133,6 +136,14 @@ bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, Strin
return 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);
}
}

void EditorAutoloadSettings::_autoload_selected() {
TreeItem *ti = tree->get_selected();

Expand Down Expand Up @@ -402,7 +413,7 @@ void EditorAutoloadSettings::init_autoloads() {
}

void EditorAutoloadSettings::_autoload_file_selected(const String &p_path) {
_add_autoload(p_path.get_file().get_basename(), p_path);
_try_add_autoload(p_path.get_file().get_basename(), p_path);
}

void EditorAutoloadSettings::_scene_file_selected(const String &p_path) {
Expand All @@ -418,7 +429,7 @@ void EditorAutoloadSettings::_scene_file_selected(const String &p_path) {
void EditorAutoloadSettings::_script_created(Ref<Script> p_script) {
FileSystemDock::get_singleton()->get_script_create_dialog()->hide();
path = p_script->get_path().get_base_dir();
_add_autoload(p_script->get_path().get_file().get_basename(), p_script->get_path());
_try_add_autoload(p_script->get_path().get_file().get_basename(), p_script->get_path());
}

void EditorAutoloadSettings::_scene_created() {
Expand All @@ -434,7 +445,7 @@ void EditorAutoloadSettings::_scene_created() {
return;
}

_add_autoload(scene_create_dialog->get_root_name(), scene_create_dialog->get_scene_path());
_try_add_autoload(scene_create_dialog->get_root_name(), scene_create_dialog->get_scene_path());
}

void EditorAutoloadSettings::_add_autoload(const String &p_name, const String &p_path) {
Expand All @@ -451,6 +462,22 @@ void EditorAutoloadSettings::_add_autoload(const String &p_name, const String &p
autoload_add(autoload_name, p_path);
}

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);
Comment on lines +466 to +467
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks against a wrong name, as _add_autoload afterwards does:

String autoload_name = p_name.to_pascal_case();

The name should be modified before the validation instead of in _add_autoload (so for p_name = game_manager it will detect collision with class_name GameManager; currently it still doesn't work for MRP from #119055).

return;
}
pending_autoload_path = p_path;

name_edit->set_text(p_name);
name_validator->update();
name_dialog->popup_centered(Vector2i(600 * EDSCALE, 0));
}

void EditorAutoloadSettings::_confirm_autoload_name() {
_add_autoload(name_edit->get_text(), pending_autoload_path);
}

void EditorAutoloadSettings::update_autoload() {
if (updating_autoload) {
return;
Expand Down Expand Up @@ -944,6 +971,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() {
Expand Down
11 changes: 11 additions & 0 deletions editor/settings/editor_autoload_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
#include "scene/gui/box_container.h"

class Button;
class ConfirmationDialog;
class EditorValidationPanel;
class FileDialog;
class LineEdit;
class SceneCreateDialog;
class ScriptCreateDialog;
class Tree;
Expand All @@ -49,6 +52,7 @@ class EditorAutoloadSettings : public VBoxContainer {

String path = "res://";
String autoload_changed = "autoload_changed";
String pending_autoload_path;

struct AutoloadInfo {
String name;
Expand Down Expand Up @@ -77,7 +81,12 @@ class EditorAutoloadSettings : public VBoxContainer {
ScriptCreateDialog *script_create_dialog = nullptr;
SceneCreateDialog *scene_create_dialog = nullptr;

ConfirmationDialog *name_dialog = nullptr;
LineEdit *name_edit = nullptr;
EditorValidationPanel *name_validator = nullptr;

bool _autoload_name_is_valid(const String &p_name, String *r_error = nullptr);
void _validate_autoload_name();

void _autoload_selected();
void _autoload_edited();
Expand All @@ -94,6 +103,8 @@ class EditorAutoloadSettings : public VBoxContainer {
void _script_created(Ref<Script> p_script);
void _scene_created();
void _add_autoload(const String &p_name, const String &p_path);
void _try_add_autoload(const String &p_name, const String &p_path);
void _confirm_autoload_name();

Variant get_drag_data_fw(const Point2 &p_point, Control *p_control);
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const;
Expand Down
Loading