From cda069215956b0c26f150000f301b555a919f2ee Mon Sep 17 00:00:00 2001 From: kobewi Date: Tue, 5 Jul 2022 19:37:59 +0200 Subject: [PATCH] Rework scene creation dialog --- editor/create_dialog.cpp | 7 + editor/create_dialog.h | 1 + editor/filesystem_dock.cpp | 64 ++----- editor/filesystem_dock.h | 4 +- editor/scene_create_dialog.cpp | 319 +++++++++++++++++++++++++++++++++ editor/scene_create_dialog.h | 105 +++++++++++ 6 files changed, 448 insertions(+), 52 deletions(-) create mode 100644 editor/scene_create_dialog.cpp create mode 100644 editor/scene_create_dialog.h diff --git a/editor/create_dialog.cpp b/editor/create_dialog.cpp index b7c2ea39145..f852de88a3d 100644 --- a/editor/create_dialog.cpp +++ b/editor/create_dialog.cpp @@ -492,6 +492,13 @@ String CreateDialog::get_base_type() const { return base_type; } +void CreateDialog::select_base() { + if (search_options_types.empty()) { + _update_search(); + } + select_type(base_type); +} + void CreateDialog::set_preferred_search_result_type(const String &p_preferred_type) { preferred_search_result_type = p_preferred_type; } diff --git a/editor/create_dialog.h b/editor/create_dialog.h index 2327f771c8d..cfa810522cb 100644 --- a/editor/create_dialog.h +++ b/editor/create_dialog.h @@ -99,6 +99,7 @@ public: void set_base_type(const String &p_base); String get_base_type() const; + void select_base(); void set_preferred_search_result_type(const String &p_preferred_type); String get_preferred_search_result_type(); diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index b0afd3fca3b..0278c8dd4fc 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -37,6 +37,7 @@ #include "core/os/keyboard.h" #include "core/os/os.h" #include "core/project_settings.h" +#include "editor/scene_create_dialog.h" #include "editor_feature_profile.h" #include "editor_node.h" #include "editor_resource_preview.h" @@ -1376,46 +1377,14 @@ void FileSystemDock::_make_dir_confirm() { } void FileSystemDock::_make_scene_confirm() { - String scene_name = make_scene_dialog_text->get_text().strip_edges(); - - if (scene_name.length() == 0) { - EditorNode::get_singleton()->show_warning(TTR("No name provided.")); - return; - } - - String directory = path; - if (!directory.ends_with("/")) { - directory = directory.get_base_dir(); - } - - String extension = scene_name.get_extension(); - List extensions; - Ref sd = memnew(PackedScene); - ResourceSaver::get_recognized_extensions(sd, &extensions); - - bool extension_correct = false; - for (List::Element *E = extensions.front(); E; E = E->next()) { - if (E->get() == extension) { - extension_correct = true; - break; - } - } - if (!extension_correct) { - scene_name = scene_name.get_basename() + ".tscn"; - } - - scene_name = directory.plus_file(scene_name); - - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); - if (da->file_exists(scene_name)) { - EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists.")); - memdelete(da); - return; - } - memdelete(da); + const String scene_path = make_scene_dialog->get_scene_path(); int idx = editor->new_scene(); - editor->get_editor_data().set_scene_path(idx, scene_name); + EditorNode::get_singleton()->get_editor_data().set_scene_path(idx, scene_path); + EditorNode::get_singleton()->set_edited_scene(make_scene_dialog->create_scene_root()); + Vector scenes; + scenes.push_back(scene_path); + EditorNode::get_singleton()->save_scene_list(scenes); } void FileSystemDock::_file_removed(String p_file) { @@ -1916,10 +1885,12 @@ void FileSystemDock::_file_option(int p_option, const Vector &p_selected } break; case FILE_NEW_SCENE: { - make_scene_dialog_text->set_text("new scene"); - make_scene_dialog_text->select_all(); - make_scene_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE); - make_scene_dialog_text->grab_focus(); + String directory = path; + if (!directory.ends_with("/")) { + directory = directory.get_base_dir(); + } + make_scene_dialog->config(directory); + make_scene_dialog->popup_centered(); } break; case FILE_NEW_SCRIPT: { @@ -3000,15 +2971,8 @@ FileSystemDock::FileSystemDock(EditorNode *p_editor) { make_dir_dialog->register_text_enter(make_dir_dialog_text); make_dir_dialog->connect("confirmed", this, "_make_dir_confirm"); - make_scene_dialog = memnew(ConfirmationDialog); - make_scene_dialog->set_title(TTR("Create Scene")); - VBoxContainer *make_scene_dialog_vb = memnew(VBoxContainer); - make_scene_dialog->add_child(make_scene_dialog_vb); - - make_scene_dialog_text = memnew(LineEdit); - make_scene_dialog_vb->add_margin_child(TTR("Name:"), make_scene_dialog_text); + make_scene_dialog = memnew(SceneCreateDialog); add_child(make_scene_dialog); - make_scene_dialog->register_text_enter(make_scene_dialog_text); make_scene_dialog->connect("confirmed", this, "_make_scene_confirm"); make_script_dialog = memnew(ScriptCreateDialog); diff --git a/editor/filesystem_dock.h b/editor/filesystem_dock.h index ace5482692a..c9e7ad5259a 100644 --- a/editor/filesystem_dock.h +++ b/editor/filesystem_dock.h @@ -55,6 +55,7 @@ #include "script_create_dialog.h" class EditorNode; +class SceneCreateDialog; class FileSystemDock : public VBoxContainer { GDCLASS(FileSystemDock, VBoxContainer); @@ -154,9 +155,8 @@ private: LineEdit *duplicate_dialog_text; ConfirmationDialog *make_dir_dialog; LineEdit *make_dir_dialog_text; - ConfirmationDialog *make_scene_dialog; - LineEdit *make_scene_dialog_text; ConfirmationDialog *overwrite_dialog; + SceneCreateDialog *make_scene_dialog = nullptr; ScriptCreateDialog *make_script_dialog; CreateDialog *new_resource_dialog; diff --git a/editor/scene_create_dialog.cpp b/editor/scene_create_dialog.cpp new file mode 100644 index 00000000000..41de4534169 --- /dev/null +++ b/editor/scene_create_dialog.cpp @@ -0,0 +1,319 @@ +/*************************************************************************/ +/* scene_create_dialog.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 "scene_create_dialog.h" + +#include "core/os/dir_access.h" +#include "editor/create_dialog.h" +#include "editor/editor_node.h" +#include "editor/editor_scale.h" +#include "scene/2d/node_2d.h" +#include "scene/3d/spatial.h" +#include "scene/gui/box_container.h" +#include "scene/gui/check_box.h" +#include "scene/gui/grid_container.h" +#include "scene/gui/line_edit.h" +#include "scene/gui/option_button.h" +#include "scene/gui/panel_container.h" +#include "scene/resources/packed_scene.h" + +void SceneCreateDialog::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: + case NOTIFICATION_THEME_CHANGED: { + select_node_button->set_icon(get_icon("ClassList", "EditorIcons")); + node_type_2d->set_icon(get_icon("Node2D", "EditorIcons")); + node_type_3d->set_icon(get_icon("Spatial", "EditorIcons")); + node_type_gui->set_icon(get_icon("Control", "EditorIcons")); + node_type_other->add_icon_override("icon", get_icon("Node", "EditorIcons")); + status_panel->add_style_override("panel", get_stylebox("bg", "Tree")); + } break; + } +} + +void SceneCreateDialog::config(const String &p_dir) { + directory = p_dir; + root_name_edit->set_text(""); + scene_name_edit->set_text(""); + scene_name_edit->call_deferred("grab_focus"); + update_dialog(); +} + +void SceneCreateDialog::accept_create(Variant p_discard) { + if (!get_ok()->is_disabled()) { + hide(); + emit_signal("confirmed"); + } +} + +void SceneCreateDialog::browse_types() { + select_node_dialog->popup_create(true); + select_node_dialog->set_title(TTR("Pick Root Node Type")); + select_node_dialog->get_ok()->set_text(TTR("Pick")); +} + +void SceneCreateDialog::on_type_picked() { + other_type_display->set_text(select_node_dialog->get_selected_type().get_slice(" ", 0)); + if (node_type_other->is_pressed()) { + update_dialog(); + } else { + node_type_other->set_pressed(true); // Calls update_dialog() via group. + } +} + +void SceneCreateDialog::update_dialog(Variant p_discard) { + scene_name = scene_name_edit->get_text().strip_edges(); + update_error(file_error_label, MSG_OK, TTR("Scene name is valid.")); + + bool is_valid = true; + if (scene_name.empty()) { + update_error(file_error_label, MSG_ERROR, TTR("Scene name is empty.")); + is_valid = false; + } + + if (is_valid) { + if (!scene_name.ends_with(".")) { + scene_name += "."; + } + scene_name += scene_extension_picker->get_selected_metadata().operator String(); + } + + if (is_valid && !scene_name.is_valid_filename()) { + update_error(file_error_label, MSG_ERROR, TTR("File name invalid.")); + is_valid = false; + } + + if (is_valid) { + scene_name = directory.plus_file(scene_name); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + if (da->file_exists(scene_name)) { + update_error(file_error_label, MSG_ERROR, TTR("File already exists.")); + is_valid = false; + } + } + + const StringName root_type_name = StringName(other_type_display->get_text()); + if (has_icon(root_type_name, "EditorIcons")) { + node_type_other->set_icon(get_icon(root_type_name, "EditorIcons")); + } else { + node_type_other->set_icon(nullptr); + } + + update_error(node_error_label, MSG_OK, "Root node valid."); + + root_name = root_name_edit->get_text().strip_edges(); + if (root_name.empty()) { + root_name = scene_name.get_file().get_basename(); + } + + if (!root_name.is_valid_identifier()) { + update_error(node_error_label, MSG_ERROR, TTR("Invalid root node name.")); + is_valid = false; + } + + get_ok()->set_disabled(!is_valid); +} + +void SceneCreateDialog::update_error(Label *p_label, MsgType p_type, const String &p_msg) { + p_label->set_text(String::utf8("• ") + p_msg); + switch (p_type) { + case MSG_OK: + p_label->add_color_override("font_color", get_color("success_color", "Editor")); + break; + case MSG_ERROR: + p_label->add_color_override("font_color", get_color("error_color", "Editor")); + break; + } +} + +String SceneCreateDialog::get_scene_path() const { + return scene_name; +} + +Node *SceneCreateDialog::create_scene_root() { + ERR_FAIL_NULL_V(node_type_group->get_pressed_button(), nullptr); + RootType type = (RootType)node_type_group->get_pressed_button()->get_meta(type_meta).operator int(); + + Node *root = nullptr; + switch (type) { + case ROOT_2D_SCENE: + root = memnew(Node2D); + break; + case ROOT_3D_SCENE: + root = memnew(Spatial); + break; + case ROOT_USER_INTERFACE: { + Control *gui = memnew(Control); + gui->set_anchors_and_margins_preset(Control::PRESET_WIDE); + root = gui; + } break; + case ROOT_OTHER: + root = Object::cast_to(select_node_dialog->instance_selected()); + break; + } + + ERR_FAIL_NULL_V(root, nullptr); + root->set_name(root_name); + return root; +} + +void SceneCreateDialog::_bind_methods() { + ClassDB::bind_method(D_METHOD("on_type_picked"), &SceneCreateDialog::on_type_picked); + ClassDB::bind_method(D_METHOD("browse_types"), &SceneCreateDialog::browse_types); + ClassDB::bind_method(D_METHOD("update_dialog"), &SceneCreateDialog::update_dialog); + ClassDB::bind_method(D_METHOD("accept_create"), &SceneCreateDialog::accept_create); +} + +SceneCreateDialog::SceneCreateDialog() { + select_node_dialog = memnew(CreateDialog); + add_child(select_node_dialog); + select_node_dialog->set_base_type("Node"); + select_node_dialog->select_base(); + select_node_dialog->connect("create", this, "on_type_picked"); + + VBoxContainer *main_vb = memnew(VBoxContainer); + add_child(main_vb); + + GridContainer *gc = memnew(GridContainer); + main_vb->add_child(gc); + gc->set_columns(2); + + { + Label *label = memnew(Label(TTR("Root Type:"))); + gc->add_child(label); + label->set_v_size_flags(0); + + VBoxContainer *vb = memnew(VBoxContainer); + gc->add_child(vb); + + node_type_group.instance(); + + node_type_2d = memnew(CheckBox); + vb->add_child(node_type_2d); + node_type_2d->set_text(TTR("2D Scene")); + node_type_2d->set_button_group(node_type_group); + node_type_2d->set_meta(type_meta, ROOT_2D_SCENE); + node_type_2d->set_pressed(true); + + node_type_3d = memnew(CheckBox); + vb->add_child(node_type_3d); + node_type_3d->set_text(TTR("3D Scene")); + node_type_3d->set_button_group(node_type_group); + node_type_3d->set_meta(type_meta, ROOT_3D_SCENE); + + node_type_gui = memnew(CheckBox); + vb->add_child(node_type_gui); + node_type_gui->set_text(TTR("User Interface")); + node_type_gui->set_button_group(node_type_group); + node_type_gui->set_meta(type_meta, ROOT_USER_INTERFACE); + + HBoxContainer *hb = memnew(HBoxContainer); + vb->add_child(hb); + + node_type_other = memnew(CheckBox); + hb->add_child(node_type_other); + node_type_other->set_button_group(node_type_group); + node_type_other->set_meta(type_meta, ROOT_OTHER); + + Control *spacing = memnew(Control); + hb->add_child(spacing); + spacing->set_custom_minimum_size(Size2(4 * EDSCALE, 0)); + + other_type_display = memnew(LineEdit); + hb->add_child(other_type_display); + other_type_display->set_h_size_flags(Control::SIZE_EXPAND_FILL); + other_type_display->set_editable(false); + other_type_display->set_text("Node"); + + select_node_button = memnew(Button); + hb->add_child(select_node_button); + select_node_button->connect("pressed", this, "browse_types"); + + node_type_group->connect("pressed", this, "update_dialog"); + } + + { + Label *label = memnew(Label(TTR("Scene Name:"))); + gc->add_child(label); + + HBoxContainer *hb = memnew(HBoxContainer); + gc->add_child(hb); + + scene_name_edit = memnew(LineEdit); + hb->add_child(scene_name_edit); + scene_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); + scene_name_edit->connect("text_changed", this, "update_dialog"); + scene_name_edit->connect("text_submitted", this, "accept_create"); + + List extensions; + Ref sd = memnew(PackedScene); + ResourceSaver::get_recognized_extensions(sd, &extensions); + + scene_extension_picker = memnew(OptionButton); + hb->add_child(scene_extension_picker); + for (List::Element *E = extensions.front(); E; E = E->next()) { + scene_extension_picker->add_item("." + E->get()); + scene_extension_picker->set_item_metadata(scene_extension_picker->get_item_count() - 1, E->get()); + } + } + + { + Label *label = memnew(Label(TTR("Root Name:"))); + gc->add_child(label); + + root_name_edit = memnew(LineEdit); + gc->add_child(root_name_edit); + root_name_edit->set_placeholder(TTR("Leave empty to use scene name")); + root_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); + root_name_edit->connect("text_changed", this, "update_dialog"); + root_name_edit->connect("text_submitted", this, "accept_create"); + } + + Control *spacing = memnew(Control); + main_vb->add_child(spacing); + spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE)); + + status_panel = memnew(PanelContainer); + main_vb->add_child(status_panel); + status_panel->set_h_size_flags(Control::SIZE_FILL); + status_panel->set_v_size_flags(Control::SIZE_EXPAND_FILL); + + VBoxContainer *status_vb = memnew(VBoxContainer); + status_panel->add_child(status_vb); + + file_error_label = memnew(Label); + status_vb->add_child(file_error_label); + + node_error_label = memnew(Label); + status_vb->add_child(node_error_label); + + set_title(TTR("Create New Scene")); + set_custom_minimum_size(Size2i(400 * EDSCALE, 0)); +} diff --git a/editor/scene_create_dialog.h b/editor/scene_create_dialog.h new file mode 100644 index 00000000000..1e1bead7f12 --- /dev/null +++ b/editor/scene_create_dialog.h @@ -0,0 +1,105 @@ +/*************************************************************************/ +/* scene_create_dialog.h */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ + +#ifndef SCENE_CREATE_DIALOG_H +#define SCENE_CREATE_DIALOG_H + +#include "scene/gui/dialogs.h" + +class ButtonGroup; +class CheckBox; +class CreateDialog; +class EditorFileDialog; +class Label; +class LineEdit; +class OptionButton; +class PanelContainer; + +class SceneCreateDialog : public ConfirmationDialog { + GDCLASS(SceneCreateDialog, ConfirmationDialog); + + enum MsgType { + MSG_OK, + MSG_ERROR, + }; + + const StringName type_meta = StringName("type"); + +public: + enum RootType { + ROOT_2D_SCENE, + ROOT_3D_SCENE, + ROOT_USER_INTERFACE, + ROOT_OTHER, + }; + +private: + String directory; + String scene_name; + String root_name; + + Ref node_type_group; + CheckBox *node_type_2d = nullptr; + CheckBox *node_type_3d = nullptr; + CheckBox *node_type_gui = nullptr; + CheckBox *node_type_other = nullptr; + + LineEdit *other_type_display = nullptr; + Button *select_node_button = nullptr; + CreateDialog *select_node_dialog = nullptr; + + LineEdit *scene_name_edit = nullptr; + OptionButton *scene_extension_picker = nullptr; + LineEdit *root_name_edit = nullptr; + + PanelContainer *status_panel = nullptr; + Label *file_error_label = nullptr; + Label *node_error_label = nullptr; + + void accept_create(Variant p_discard = Variant()); // Extra unused argument, because unbind() doesn't exist in 3.x. + void browse_types(); + void on_type_picked(); + void update_dialog(Variant p_discard = Variant()); + void update_error(Label *p_label, MsgType p_type, const String &p_msg); + +protected: + void _notification(int p_what); + static void _bind_methods(); + +public: + void config(const String &p_dir); + + String get_scene_path() const; + Node *create_scene_root(); + + SceneCreateDialog(); +}; + +#endif // SCENE_CREATE_DIALOG_H