diff options
Diffstat (limited to 'editor/editor_autoload_settings.cpp')
-rw-r--r-- | editor/editor_autoload_settings.cpp | 141 |
1 files changed, 82 insertions, 59 deletions
diff --git a/editor/editor_autoload_settings.cpp b/editor/editor_autoload_settings.cpp index ed628ff620..200ae7045f 100644 --- a/editor/editor_autoload_settings.cpp +++ b/editor/editor_autoload_settings.cpp @@ -41,17 +41,12 @@ #define PREVIEW_LIST_MAX_SIZE 10 void EditorAutoloadSettings::_notification(int p_what) { - if (p_what == NOTIFICATION_ENTER_TREE) { - List<String> afn; ResourceLoader::get_recognized_extensions_for_type("Script", &afn); ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn); - EditorFileDialog *file_dialog = autoload_add_path->get_file_dialog(); - for (List<String>::Element *E = afn.front(); E; E = E->next()) { - file_dialog->add_filter("*." + E->get()); } @@ -61,29 +56,34 @@ void EditorAutoloadSettings::_notification(int p_what) { 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) + 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) + 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) + if (r_error) { *r_error = TTR("Invalid name.") + "\n" + TTR("Must not collide with an existing built-in type name."); + } return false; } @@ -91,8 +91,9 @@ bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, Strin for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) { if (GlobalConstants::get_global_constant_name(i) == p_name) { - if (r_error) + if (r_error) { *r_error = TTR("Invalid name.") + "\n" + TTR("Must not collide with an existing global constant name."); + } return false; } @@ -103,8 +104,9 @@ bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, Strin ScriptServer::get_language(i)->get_reserved_words(&keywords); for (List<String>::Element *E = keywords.front(); E; E = E->next()) { if (E->get() == p_name) { - if (r_error) + if (r_error) { *r_error = TTR("Invalid name.") + "\n" + TTR("Keyword cannot be used as an autoload name."); + } return false; } @@ -115,28 +117,28 @@ bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, Strin } void EditorAutoloadSettings::_autoload_add() { - - if (autoload_add(autoload_add_name->get_text(), autoload_add_path->get_line_edit()->get_text())) - autoload_add_path->get_line_edit()->set_text(""); + 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) + if (!ti) { return; + } selected_autoload = "autoload/" + ti->get_text(0); } void EditorAutoloadSettings::_autoload_edited() { - - if (updating_autoload) + if (updating_autoload) { return; + } TreeItem *ti = tree->get_edited(); int column = tree->get_edited_column(); @@ -147,8 +149,9 @@ void EditorAutoloadSettings::_autoload_edited() { String name = ti->get_text(0); String old_name = selected_autoload.get_slice("/", 1); - if (name == old_name) + if (name == old_name) { return; + } String error; if (!_autoload_name_is_valid(name, &error)) { @@ -198,12 +201,14 @@ void EditorAutoloadSettings::_autoload_edited() { int order = ProjectSettings::get_singleton()->get_order(base); String path = ProjectSettings::get_singleton()->get(base); - if (path.begins_with("*")) + if (path.begins_with("*")) { path = path.substr(1, path.length()); + } // Singleton autoloads are represented with a leading "*" in their path. - if (checked) + if (checked) { path = "*" + path; + } undo_redo->create_action(TTR("Toggle AutoLoad Globals")); @@ -226,7 +231,6 @@ void EditorAutoloadSettings::_autoload_edited() { } void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button) { - TreeItem *ti = Object::cast_to<TreeItem>(p_item); String name = "autoload/" + ti->get_text(0); @@ -239,7 +243,6 @@ void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_colu } break; case BUTTON_MOVE_UP: case BUTTON_MOVE_DOWN: { - TreeItem *swap = nullptr; if (p_button == BUTTON_MOVE_UP) { @@ -248,8 +251,9 @@ void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_colu swap = ti->get_next(); } - if (!swap) + if (!swap) { return; + } String swap_name = "autoload/" + swap->get_text(0); @@ -273,7 +277,6 @@ void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_colu undo_redo->commit_action(); } break; case BUTTON_DELETE: { - int order = ProjectSettings::get_singleton()->get_order(name); undo_redo->create_action(TTR("Remove Autoload")); @@ -297,8 +300,9 @@ void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_colu void EditorAutoloadSettings::_autoload_activated() { TreeItem *ti = tree->get_selected(); - if (!ti) + if (!ti) { return; + } _autoload_open(ti->get_text(1)); } @@ -312,7 +316,6 @@ void EditorAutoloadSettings::_autoload_open(const String &fpath) { } 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(" ", ""); @@ -325,22 +328,19 @@ void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) { } void EditorAutoloadSettings::_autoload_text_entered(const String p_name) { - - if (autoload_add_path->get_line_edit()->get_text() != "" && _autoload_name_is_valid(p_name, nullptr)) { + 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_line_edit()->get_text() == "" || !_autoload_name_is_valid(p_name, nullptr)); + autoload_add_path->get_text() == "" || !_autoload_name_is_valid(p_name, nullptr)); } Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { @@ -370,9 +370,9 @@ Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { } void EditorAutoloadSettings::update_autoload() { - - if (updating_autoload) + if (updating_autoload) { return; + } updating_autoload = true; @@ -393,17 +393,18 @@ void EditorAutoloadSettings::update_autoload() { ProjectSettings::get_singleton()->get_property_list(&props); for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) { - const PropertyInfo &pi = E->get(); - if (!pi.name.begins_with("autoload/")) + if (!pi.name.begins_with("autoload/")) { continue; + } String name = pi.name.get_slice("/", 1); String path = ProjectSettings::get_singleton()->get(pi.name); - if (name.empty()) + if (name.empty()) { continue; + } AutoLoadInfo info; info.is_singleton = path.begins_with("*"); @@ -517,9 +518,9 @@ void EditorAutoloadSettings::update_autoload() { } Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control *p_control) { - - if (autoload_cache.size() <= 1) + if (autoload_cache.size() <= 1) { return false; + } PackedStringArray autoloads; @@ -530,8 +531,9 @@ Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control next = tree->get_next_selected(next); } - if (autoloads.size() == 0 || autoloads.size() == autoload_cache.size()) + if (autoloads.size() == 0 || autoloads.size() == autoload_cache.size()) { return Variant(); + } VBoxContainer *preview = memnew(VBoxContainer); @@ -555,19 +557,22 @@ Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control } bool EditorAutoloadSettings::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const { - if (updating_autoload) + if (updating_autoload) { return false; + } Dictionary drop_data = p_data; - if (!drop_data.has("type")) + if (!drop_data.has("type")) { return false; + } if (drop_data.has("type")) { TreeItem *ti = tree->get_item_at_position(p_point); - if (!ti) + if (!ti) { return false; + } int section = tree->get_drop_section_at_position(p_point); @@ -578,16 +583,17 @@ bool EditorAutoloadSettings::can_drop_data_fw(const Point2 &p_point, const Varia } void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) { - TreeItem *ti = tree->get_item_at_position(p_point); - if (!ti) + if (!ti) { return; + } int section = tree->get_drop_section_at_position(p_point); - if (section < -1) + if (section < -1) { return; + } String name; bool move_to_back = false; @@ -664,7 +670,6 @@ void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant & } bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_path) { - String name = p_name; String error; @@ -710,7 +715,6 @@ bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_ } void EditorAutoloadSettings::autoload_remove(const String &p_name) { - String name = "autoload/" + p_name; UndoRedo *undo_redo = EditorNode::get_undo_redo(); @@ -735,7 +739,6 @@ void EditorAutoloadSettings::autoload_remove(const String &p_name) { } void EditorAutoloadSettings::_bind_methods() { - ClassDB::bind_method("_autoload_open", &EditorAutoloadSettings::_autoload_open); ClassDB::bind_method("get_drag_data_fw", &EditorAutoloadSettings::get_drag_data_fw); @@ -750,22 +753,22 @@ void EditorAutoloadSettings::_bind_methods() { } EditorAutoloadSettings::EditorAutoloadSettings() { - // Make first cache List<PropertyInfo> props; ProjectSettings::get_singleton()->get_property_list(&props); for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) { - const PropertyInfo &pi = E->get(); - if (!pi.name.begins_with("autoload/")) + if (!pi.name.begins_with("autoload/")) { continue; + } String name = pi.name.get_slice("/", 1); String path = ProjectSettings::get_singleton()->get(pi.name); - if (name.empty()) + if (name.empty()) { continue; + } AutoLoadInfo info; info.is_singleton = path.begins_with("*"); @@ -823,13 +826,24 @@ EditorAutoloadSettings::EditorAutoloadSettings() { l->set_text(TTR("Path:")); hbc->add_child(l); - autoload_add_path = memnew(EditorLineEditFileChooser); - autoload_add_path->set_h_size_flags(SIZE_EXPAND_FILL); - autoload_add_path->get_file_dialog()->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); - autoload_add_path->get_file_dialog()->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_file_callback)); - autoload_add_path->get_line_edit()->connect("text_changed", callable_mp(this, &EditorAutoloadSettings::_autoload_path_text_changed)); - + autoload_add_path = memnew(LineEdit); hbc->add_child(autoload_add_path); + autoload_add_path->set_h_size_flags(Control::SIZE_EXPAND_FILL); + autoload_add_path->connect("text_changed", callable_mp(this, &EditorAutoloadSettings::_autoload_path_text_changed)); + + browse_button = memnew(Button); + hbc->add_child(browse_button); + browse_button->connect("pressed", callable_mp(this, &EditorAutoloadSettings::_browse_autoload_add_path)); + + 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)); + + 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(TTR("Node Name:")); @@ -890,3 +904,12 @@ EditorAutoloadSettings::~EditorAutoloadSettings() { } } } + +void EditorAutoloadSettings::_set_autoload_add_path(const String &p_text) { + autoload_add_path->set_text(p_text); + autoload_add_path->emit_signal("text_entered", p_text); +} + +void EditorAutoloadSettings::_browse_autoload_add_path() { + file_dialog->popup_centered_ratio(); +}
\ No newline at end of file |