Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/scene_create_dialog.cpp
20844 views
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 "core/io/resource_saver.h"
35
#include "editor/editor_node.h"
36
#include "editor/editor_string_names.h"
37
#include "editor/gui/create_dialog.h"
38
#include "editor/gui/editor_validation_panel.h"
39
#include "editor/settings/editor_feature_profile.h"
40
#include "editor/themes/editor_scale.h"
41
#include "scene/2d/node_2d.h"
42
#include "scene/3d/node_3d.h"
43
#include "scene/gui/box_container.h"
44
#include "scene/gui/check_box.h"
45
#include "scene/gui/grid_container.h"
46
#include "scene/gui/line_edit.h"
47
#include "scene/gui/option_button.h"
48
#include "scene/resources/packed_scene.h"
49
50
void SceneCreateDialog::_notification(int p_what) {
51
switch (p_what) {
52
case NOTIFICATION_THEME_CHANGED: {
53
select_node_button->set_button_icon(get_editor_theme_icon(SNAME("ClassList")));
54
node_type_2d->set_button_icon(get_editor_theme_icon(SNAME("Node2D")));
55
node_type_3d->set_button_icon(get_editor_theme_icon(SNAME("Node3D")));
56
node_type_gui->set_button_icon(get_editor_theme_icon(SNAME("Control")));
57
node_type_other->add_theme_icon_override(SNAME("icon"), get_editor_theme_icon(SNAME("Node")));
58
} break;
59
60
case NOTIFICATION_READY: {
61
select_node_dialog->select_base();
62
} break;
63
}
64
}
65
66
void SceneCreateDialog::config(const String &p_dir) {
67
directory = p_dir;
68
root_name_edit->set_text("");
69
scene_name_edit->set_text("");
70
callable_mp((Control *)scene_name_edit, &Control::grab_focus).call_deferred(false);
71
validation_panel->update();
72
73
Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
74
node_type_3d->set_visible(profile.is_null() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D));
75
if (!node_type_3d->is_visible() && node_type_3d->is_pressed()) {
76
node_type_2d->set_pressed(true);
77
}
78
}
79
80
void SceneCreateDialog::accept_create() {
81
if (!get_ok_button()->is_disabled()) {
82
hide();
83
emit_signal(SceneStringName(confirmed));
84
}
85
}
86
87
void SceneCreateDialog::browse_types() {
88
select_node_dialog->popup_create(true);
89
select_node_dialog->set_title(TTR("Pick Root Node Type"));
90
select_node_dialog->set_ok_button_text(TTR("Pick"));
91
}
92
93
void SceneCreateDialog::on_type_picked() {
94
other_type_display->set_text(select_node_dialog->get_selected_type());
95
if (node_type_other->is_pressed()) {
96
validation_panel->update();
97
} else {
98
node_type_other->set_pressed(true); // Calls validation_panel->update() via group.
99
}
100
}
101
102
void SceneCreateDialog::update_dialog() {
103
scene_name = scene_name_edit->get_text().strip_edges();
104
105
if (scene_name.is_empty()) {
106
validation_panel->set_message(MSG_ID_PATH, TTR("Scene name is empty."), EditorValidationPanel::MSG_ERROR);
107
}
108
109
if (validation_panel->is_valid()) {
110
if (!scene_name.ends_with(".")) {
111
scene_name += ".";
112
}
113
scene_name += scene_extension_picker->get_selected_metadata().operator String();
114
}
115
116
if (validation_panel->is_valid() && !scene_name.is_valid_filename()) {
117
validation_panel->set_message(MSG_ID_PATH, TTR("File name invalid."), EditorValidationPanel::MSG_ERROR);
118
} else if (validation_panel->is_valid() && scene_name[0] == '.') {
119
validation_panel->set_message(MSG_ID_PATH, TTR("File name begins with a dot."), EditorValidationPanel::MSG_ERROR);
120
}
121
122
if (validation_panel->is_valid()) {
123
scene_name = directory.path_join(scene_name);
124
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
125
if (da->file_exists(scene_name)) {
126
validation_panel->set_message(MSG_ID_PATH, TTR("File already exists."), EditorValidationPanel::MSG_ERROR);
127
}
128
}
129
130
const StringName root_type_name = StringName(other_type_display->get_text());
131
if (has_theme_icon(root_type_name, EditorStringName(EditorIcons))) {
132
node_type_other->set_button_icon(get_editor_theme_icon(root_type_name));
133
} else {
134
node_type_other->set_button_icon(nullptr);
135
}
136
137
root_name = root_name_edit->get_text().strip_edges();
138
if (root_name.is_empty()) {
139
root_name = scene_name_edit->get_text().strip_edges();
140
141
if (root_name.is_empty()) {
142
root_name_edit->set_placeholder(TTR("Leave empty to derive from scene name"));
143
} else {
144
// Respect the desired root node casing from ProjectSettings.
145
root_name = Node::adjust_name_casing(root_name);
146
root_name_edit->set_placeholder(root_name.validate_node_name());
147
}
148
}
149
150
if (root_name.is_empty()) {
151
validation_panel->set_message(MSG_ID_ROOT, TTR("Invalid root node name."), EditorValidationPanel::MSG_ERROR);
152
} else if (root_name != root_name.validate_node_name()) {
153
validation_panel->set_message(MSG_ID_ROOT, TTR("Invalid root node name characters have been replaced."), EditorValidationPanel::MSG_WARNING);
154
}
155
}
156
157
String SceneCreateDialog::get_scene_path() const {
158
return scene_name;
159
}
160
161
Node *SceneCreateDialog::create_scene_root() {
162
ERR_FAIL_NULL_V(node_type_group->get_pressed_button(), nullptr);
163
RootType type = (RootType)node_type_group->get_pressed_button()->get_meta(type_meta).operator int();
164
165
Node *root = nullptr;
166
switch (type) {
167
case ROOT_2D_SCENE:
168
root = memnew(Node2D);
169
break;
170
case ROOT_3D_SCENE:
171
root = memnew(Node3D);
172
break;
173
case ROOT_USER_INTERFACE: {
174
Control *gui_ctl = memnew(Control);
175
// Making the root control full rect by default is more useful for resizable UIs.
176
gui_ctl->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
177
gui_ctl->set_grow_direction_preset(Control::PRESET_FULL_RECT);
178
root = gui_ctl;
179
} break;
180
case ROOT_OTHER:
181
root = Object::cast_to<Node>(select_node_dialog->instantiate_selected());
182
break;
183
}
184
185
ERR_FAIL_NULL_V(root, nullptr);
186
root->set_name(root_name);
187
return root;
188
}
189
190
SceneCreateDialog::SceneCreateDialog() {
191
select_node_dialog = memnew(CreateDialog);
192
add_child(select_node_dialog);
193
select_node_dialog->set_base_type("Node");
194
select_node_dialog->connect("create", callable_mp(this, &SceneCreateDialog::on_type_picked));
195
196
VBoxContainer *main_vb = memnew(VBoxContainer);
197
add_child(main_vb);
198
199
GridContainer *gc = memnew(GridContainer);
200
main_vb->add_child(gc);
201
gc->set_columns(2);
202
203
{
204
Label *label = memnew(Label(TTR("Root Type:")));
205
gc->add_child(label);
206
label->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
207
208
VBoxContainer *vb = memnew(VBoxContainer);
209
gc->add_child(vb);
210
211
node_type_group.instantiate();
212
213
node_type_2d = memnew(CheckBox);
214
vb->add_child(node_type_2d);
215
node_type_2d->set_text(TTR("2D Scene"));
216
node_type_2d->set_theme_type_variation("CheckBoxNoIconTint");
217
node_type_2d->set_button_group(node_type_group);
218
node_type_2d->set_meta(type_meta, ROOT_2D_SCENE);
219
node_type_2d->set_pressed(true);
220
221
node_type_3d = memnew(CheckBox);
222
vb->add_child(node_type_3d);
223
node_type_3d->set_text(TTR("3D Scene"));
224
node_type_3d->set_theme_type_variation("CheckBoxNoIconTint");
225
node_type_3d->set_button_group(node_type_group);
226
node_type_3d->set_meta(type_meta, ROOT_3D_SCENE);
227
228
node_type_gui = memnew(CheckBox);
229
vb->add_child(node_type_gui);
230
node_type_gui->set_text(TTR("User Interface"));
231
node_type_gui->set_theme_type_variation("CheckBoxNoIconTint");
232
node_type_gui->set_button_group(node_type_group);
233
node_type_gui->set_meta(type_meta, ROOT_USER_INTERFACE);
234
235
HBoxContainer *hb = memnew(HBoxContainer);
236
vb->add_child(hb);
237
238
node_type_other = memnew(CheckBox);
239
hb->add_child(node_type_other);
240
node_type_other->set_accessibility_name(TTRC("Other Type"));
241
node_type_other->set_theme_type_variation("CheckBoxNoIconTint");
242
node_type_other->set_button_group(node_type_group);
243
node_type_other->set_meta(type_meta, ROOT_OTHER);
244
245
Control *spacing = memnew(Control);
246
hb->add_child(spacing);
247
spacing->set_custom_minimum_size(Size2(4 * EDSCALE, 0));
248
249
other_type_display = memnew(LineEdit);
250
other_type_display->set_accessibility_name(TTRC("Other Type"));
251
hb->add_child(other_type_display);
252
other_type_display->set_h_size_flags(Control::SIZE_EXPAND_FILL);
253
other_type_display->set_editable(false);
254
other_type_display->set_text("Node");
255
256
select_node_button = memnew(Button);
257
select_node_button->set_accessibility_name(TTRC("Select Node"));
258
hb->add_child(select_node_button);
259
select_node_button->connect(SceneStringName(pressed), callable_mp(this, &SceneCreateDialog::browse_types));
260
}
261
262
{
263
Label *label = memnew(Label(TTR("Scene Name:")));
264
gc->add_child(label);
265
266
HBoxContainer *hb = memnew(HBoxContainer);
267
gc->add_child(hb);
268
269
scene_name_edit = memnew(LineEdit);
270
hb->add_child(scene_name_edit);
271
scene_name_edit->set_accessibility_name(TTRC("Scene Name:"));
272
scene_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
273
scene_name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &SceneCreateDialog::accept_create).unbind(1));
274
275
List<String> extensions;
276
Ref<PackedScene> sd = memnew(PackedScene);
277
ResourceSaver::get_recognized_extensions(sd, &extensions);
278
279
scene_extension_picker = memnew(OptionButton);
280
scene_extension_picker->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
281
hb->add_child(scene_extension_picker);
282
for (const String &E : extensions) {
283
scene_extension_picker->add_item("." + E);
284
scene_extension_picker->set_item_metadata(-1, E);
285
}
286
}
287
288
{
289
Label *label = memnew(Label(TTR("Root Name:")));
290
gc->add_child(label);
291
292
root_name_edit = memnew(LineEdit);
293
gc->add_child(root_name_edit);
294
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."));
295
root_name_edit->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
296
root_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
297
root_name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &SceneCreateDialog::accept_create).unbind(1));
298
}
299
300
Control *spacing = memnew(Control);
301
main_vb->add_child(spacing);
302
spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
303
304
validation_panel = memnew(EditorValidationPanel);
305
main_vb->add_child(validation_panel);
306
validation_panel->add_line(MSG_ID_PATH, TTR("Scene name is valid."));
307
validation_panel->add_line(MSG_ID_ROOT, TTR("Root node valid."));
308
validation_panel->set_update_callback(callable_mp(this, &SceneCreateDialog::update_dialog));
309
validation_panel->set_accept_button(get_ok_button());
310
311
node_type_group->connect(SceneStringName(pressed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
312
scene_name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
313
root_name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
314
315
set_title(TTR("Create New Scene"));
316
set_min_size(Size2i(400 * EDSCALE, 0));
317
}
318
319