1/**************************************************************************/
2/* scene_create_dialog.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "scene_create_dialog.h"
32
33#include "core/io/dir_access.h"
34#include "editor/create_dialog.h"
35#include "editor/editor_node.h"
36#include "editor/editor_scale.h"
37#include "editor/editor_string_names.h"
38#include "editor/gui/editor_validation_panel.h"
39#include "scene/2d/node_2d.h"
40#include "scene/3d/node_3d.h"
41#include "scene/gui/box_container.h"
42#include "scene/gui/check_box.h"
43#include "scene/gui/grid_container.h"
44#include "scene/gui/line_edit.h"
45#include "scene/gui/option_button.h"
46#include "scene/resources/packed_scene.h"
47
48void SceneCreateDialog::_notification(int p_what) {
49 switch (p_what) {
50 case NOTIFICATION_ENTER_TREE:
51 case NOTIFICATION_THEME_CHANGED: {
52 select_node_button->set_icon(get_editor_theme_icon(SNAME("ClassList")));
53 node_type_2d->set_icon(get_editor_theme_icon(SNAME("Node2D")));
54 node_type_3d->set_icon(get_editor_theme_icon(SNAME("Node3D")));
55 node_type_gui->set_icon(get_editor_theme_icon(SNAME("Control")));
56 node_type_other->add_theme_icon_override(SNAME("icon"), get_editor_theme_icon(SNAME("Node")));
57 } break;
58 }
59}
60
61void SceneCreateDialog::config(const String &p_dir) {
62 directory = p_dir;
63 root_name_edit->set_text("");
64 scene_name_edit->set_text("");
65 scene_name_edit->call_deferred(SNAME("grab_focus"));
66 validation_panel->update();
67}
68
69void SceneCreateDialog::accept_create() {
70 if (!get_ok_button()->is_disabled()) {
71 hide();
72 emit_signal(SNAME("confirmed"));
73 }
74}
75
76void SceneCreateDialog::browse_types() {
77 select_node_dialog->popup_create(true);
78 select_node_dialog->set_title(TTR("Pick Root Node Type"));
79 select_node_dialog->set_ok_button_text(TTR("Pick"));
80}
81
82void SceneCreateDialog::on_type_picked() {
83 other_type_display->set_text(select_node_dialog->get_selected_type().get_slice(" ", 0));
84 if (node_type_other->is_pressed()) {
85 validation_panel->update();
86 } else {
87 node_type_other->set_pressed(true); // Calls validation_panel->update() via group.
88 }
89}
90
91void SceneCreateDialog::update_dialog() {
92 scene_name = scene_name_edit->get_text().strip_edges();
93
94 if (scene_name.is_empty()) {
95 validation_panel->set_message(MSG_ID_PATH, TTR("Scene name is empty."), EditorValidationPanel::MSG_ERROR);
96 }
97
98 if (validation_panel->is_valid()) {
99 if (!scene_name.ends_with(".")) {
100 scene_name += ".";
101 }
102 scene_name += scene_extension_picker->get_selected_metadata().operator String();
103 }
104
105 if (validation_panel->is_valid() && !scene_name.is_valid_filename()) {
106 validation_panel->set_message(MSG_ID_PATH, TTR("File name invalid."), EditorValidationPanel::MSG_ERROR);
107 } else if (validation_panel->is_valid() && scene_name[0] == '.') {
108 validation_panel->set_message(MSG_ID_PATH, TTR("File name begins with a dot."), EditorValidationPanel::MSG_ERROR);
109 }
110
111 if (validation_panel->is_valid()) {
112 scene_name = directory.path_join(scene_name);
113 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
114 if (da->file_exists(scene_name)) {
115 validation_panel->set_message(MSG_ID_PATH, TTR("File already exists."), EditorValidationPanel::MSG_ERROR);
116 }
117 }
118
119 const StringName root_type_name = StringName(other_type_display->get_text());
120 if (has_theme_icon(root_type_name, EditorStringName(EditorIcons))) {
121 node_type_other->set_icon(get_editor_theme_icon(root_type_name));
122 } else {
123 node_type_other->set_icon(nullptr);
124 }
125
126 root_name = root_name_edit->get_text().strip_edges();
127 if (root_name.is_empty()) {
128 root_name = scene_name_edit->get_text().strip_edges();
129
130 if (root_name.is_empty()) {
131 root_name_edit->set_placeholder(TTR("Leave empty to derive from scene name"));
132 } else {
133 // Respect the desired root node casing from ProjectSettings.
134 root_name = Node::adjust_name_casing(root_name);
135 root_name_edit->set_placeholder(root_name.validate_node_name());
136 }
137 }
138
139 if (root_name.is_empty()) {
140 validation_panel->set_message(MSG_ID_ROOT, TTR("Invalid root node name."), EditorValidationPanel::MSG_ERROR);
141 } else if (root_name != root_name.validate_node_name()) {
142 validation_panel->set_message(MSG_ID_ROOT, TTR("Invalid root node name characters have been replaced."), EditorValidationPanel::MSG_WARNING);
143 }
144}
145
146String SceneCreateDialog::get_scene_path() const {
147 return scene_name;
148}
149
150Node *SceneCreateDialog::create_scene_root() {
151 ERR_FAIL_NULL_V(node_type_group->get_pressed_button(), nullptr);
152 RootType type = (RootType)node_type_group->get_pressed_button()->get_meta(type_meta).operator int();
153
154 Node *root = nullptr;
155 switch (type) {
156 case ROOT_2D_SCENE:
157 root = memnew(Node2D);
158 break;
159 case ROOT_3D_SCENE:
160 root = memnew(Node3D);
161 break;
162 case ROOT_USER_INTERFACE: {
163 Control *gui_ctl = memnew(Control);
164 // Making the root control full rect by default is more useful for resizable UIs.
165 gui_ctl->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
166 gui_ctl->set_grow_direction_preset(Control::PRESET_FULL_RECT);
167 root = gui_ctl;
168 } break;
169 case ROOT_OTHER:
170 root = Object::cast_to<Node>(select_node_dialog->instantiate_selected());
171 break;
172 }
173
174 ERR_FAIL_NULL_V(root, nullptr);
175 root->set_name(root_name);
176 return root;
177}
178
179SceneCreateDialog::SceneCreateDialog() {
180 select_node_dialog = memnew(CreateDialog);
181 add_child(select_node_dialog);
182 select_node_dialog->set_base_type("Node");
183 select_node_dialog->select_base();
184 select_node_dialog->connect("create", callable_mp(this, &SceneCreateDialog::on_type_picked));
185
186 VBoxContainer *main_vb = memnew(VBoxContainer);
187 add_child(main_vb);
188
189 GridContainer *gc = memnew(GridContainer);
190 main_vb->add_child(gc);
191 gc->set_columns(2);
192
193 {
194 Label *label = memnew(Label(TTR("Root Type:")));
195 gc->add_child(label);
196 label->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
197
198 VBoxContainer *vb = memnew(VBoxContainer);
199 gc->add_child(vb);
200
201 node_type_group.instantiate();
202
203 node_type_2d = memnew(CheckBox);
204 vb->add_child(node_type_2d);
205 node_type_2d->set_text(TTR("2D Scene"));
206 node_type_2d->set_button_group(node_type_group);
207 node_type_2d->set_meta(type_meta, ROOT_2D_SCENE);
208 node_type_2d->set_pressed(true);
209
210 node_type_3d = memnew(CheckBox);
211 vb->add_child(node_type_3d);
212 node_type_3d->set_text(TTR("3D Scene"));
213 node_type_3d->set_button_group(node_type_group);
214 node_type_3d->set_meta(type_meta, ROOT_3D_SCENE);
215
216 node_type_gui = memnew(CheckBox);
217 vb->add_child(node_type_gui);
218 node_type_gui->set_text(TTR("User Interface"));
219 node_type_gui->set_button_group(node_type_group);
220 node_type_gui->set_meta(type_meta, ROOT_USER_INTERFACE);
221
222 HBoxContainer *hb = memnew(HBoxContainer);
223 vb->add_child(hb);
224
225 node_type_other = memnew(CheckBox);
226 hb->add_child(node_type_other);
227 node_type_other->set_button_group(node_type_group);
228 node_type_other->set_meta(type_meta, ROOT_OTHER);
229
230 Control *spacing = memnew(Control);
231 hb->add_child(spacing);
232 spacing->set_custom_minimum_size(Size2(4 * EDSCALE, 0));
233
234 other_type_display = memnew(LineEdit);
235 hb->add_child(other_type_display);
236 other_type_display->set_h_size_flags(Control::SIZE_EXPAND_FILL);
237 other_type_display->set_editable(false);
238 other_type_display->set_text("Node");
239
240 select_node_button = memnew(Button);
241 hb->add_child(select_node_button);
242 select_node_button->connect("pressed", callable_mp(this, &SceneCreateDialog::browse_types));
243 }
244
245 {
246 Label *label = memnew(Label(TTR("Scene Name:")));
247 gc->add_child(label);
248
249 HBoxContainer *hb = memnew(HBoxContainer);
250 gc->add_child(hb);
251
252 scene_name_edit = memnew(LineEdit);
253 hb->add_child(scene_name_edit);
254 scene_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
255 scene_name_edit->connect("text_submitted", callable_mp(this, &SceneCreateDialog::accept_create).unbind(1));
256
257 List<String> extensions;
258 Ref<PackedScene> sd = memnew(PackedScene);
259 ResourceSaver::get_recognized_extensions(sd, &extensions);
260
261 scene_extension_picker = memnew(OptionButton);
262 hb->add_child(scene_extension_picker);
263 for (const String &E : extensions) {
264 scene_extension_picker->add_item("." + E);
265 scene_extension_picker->set_item_metadata(-1, E);
266 }
267 }
268
269 {
270 Label *label = memnew(Label(TTR("Root Name:")));
271 gc->add_child(label);
272
273 root_name_edit = memnew(LineEdit);
274 gc->add_child(root_name_edit);
275 root_name_edit->set_tooltip_text(TTR("When empty, the root node name is derived from the scene name based on the \"editor/naming/node_name_casing\" project setting."));
276 root_name_edit->set_auto_translate(false);
277 root_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
278 root_name_edit->connect("text_submitted", callable_mp(this, &SceneCreateDialog::accept_create).unbind(1));
279 }
280
281 Control *spacing = memnew(Control);
282 main_vb->add_child(spacing);
283 spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
284
285 validation_panel = memnew(EditorValidationPanel);
286 main_vb->add_child(validation_panel);
287 validation_panel->add_line(MSG_ID_PATH, TTR("Scene name is valid."));
288 validation_panel->add_line(MSG_ID_ROOT, TTR("Root node valid."));
289 validation_panel->set_update_callback(callable_mp(this, &SceneCreateDialog::update_dialog));
290 validation_panel->set_accept_button(get_ok_button());
291
292 node_type_group->connect("pressed", callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
293 scene_name_edit->connect("text_changed", callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
294 root_name_edit->connect("text_changed", callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
295
296 set_title(TTR("Create New Scene"));
297 set_min_size(Size2i(400 * EDSCALE, 0));
298}
299