Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/editor_interface.cpp
9822 views
1
/**************************************************************************/
2
/* editor_interface.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 "editor_interface.h"
32
#include "editor_interface.compat.inc"
33
34
#include "core/config/project_settings.h"
35
#include "editor/docks/filesystem_dock.h"
36
#include "editor/docks/inspector_dock.h"
37
#include "editor/editor_main_screen.h"
38
#include "editor/editor_node.h"
39
#include "editor/editor_undo_redo_manager.h"
40
#include "editor/file_system/editor_paths.h"
41
#include "editor/gui/create_dialog.h"
42
#include "editor/gui/editor_quick_open_dialog.h"
43
#include "editor/gui/editor_toaster.h"
44
#include "editor/inspector/editor_preview_plugins.h"
45
#include "editor/inspector/editor_resource_preview.h"
46
#include "editor/inspector/property_selector.h"
47
#include "editor/run/editor_run_bar.h"
48
#include "editor/scene/3d/node_3d_editor_plugin.h"
49
#include "editor/scene/editor_scene_tabs.h"
50
#include "editor/scene/scene_tree_editor.h"
51
#include "editor/settings/editor_command_palette.h"
52
#include "editor/settings/editor_feature_profile.h"
53
#include "editor/settings/editor_settings.h"
54
#include "editor/themes/editor_scale.h"
55
#include "main/main.h"
56
#include "scene/3d/light_3d.h"
57
#include "scene/3d/mesh_instance_3d.h"
58
#include "scene/gui/box_container.h"
59
#include "scene/gui/control.h"
60
#include "scene/main/window.h"
61
#include "scene/resources/theme.h"
62
63
EditorInterface *EditorInterface::singleton = nullptr;
64
65
void EditorInterface::restart_editor(bool p_save) {
66
if (p_save) {
67
EditorNode::get_singleton()->save_all_scenes();
68
}
69
EditorNode::get_singleton()->restart_editor();
70
}
71
72
// Editor tools.
73
74
EditorCommandPalette *EditorInterface::get_command_palette() const {
75
return EditorCommandPalette::get_singleton();
76
}
77
78
EditorFileSystem *EditorInterface::get_resource_file_system() const {
79
return EditorFileSystem::get_singleton();
80
}
81
82
EditorPaths *EditorInterface::get_editor_paths() const {
83
return EditorPaths::get_singleton();
84
}
85
86
EditorResourcePreview *EditorInterface::get_resource_previewer() const {
87
return EditorResourcePreview::get_singleton();
88
}
89
90
EditorSelection *EditorInterface::get_selection() const {
91
return EditorNode::get_singleton()->get_editor_selection();
92
}
93
94
Ref<EditorSettings> EditorInterface::get_editor_settings() const {
95
return EditorSettings::get_singleton();
96
}
97
98
EditorToaster *EditorInterface::get_editor_toaster() const {
99
return EditorToaster::get_singleton();
100
}
101
102
EditorUndoRedoManager *EditorInterface::get_editor_undo_redo() const {
103
return EditorUndoRedoManager::get_singleton();
104
}
105
106
AABB EditorInterface::_calculate_aabb_for_scene(Node *p_node, AABB &p_scene_aabb) {
107
MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(p_node);
108
if (mesh_node && mesh_node->get_mesh().is_valid()) {
109
Transform3D accum_xform;
110
Node3D *base = mesh_node;
111
while (base) {
112
accum_xform = base->get_transform() * accum_xform;
113
base = Object::cast_to<Node3D>(base->get_parent());
114
}
115
116
AABB aabb = accum_xform.xform(mesh_node->get_mesh()->get_aabb());
117
p_scene_aabb.merge_with(aabb);
118
}
119
120
for (int i = 0; i < p_node->get_child_count(); i++) {
121
p_scene_aabb = _calculate_aabb_for_scene(p_node->get_child(i), p_scene_aabb);
122
}
123
124
return p_scene_aabb;
125
}
126
127
TypedArray<Texture2D> EditorInterface::_make_mesh_previews(const TypedArray<Mesh> &p_meshes, int p_preview_size) {
128
Vector<Ref<Mesh>> meshes;
129
130
for (int i = 0; i < p_meshes.size(); i++) {
131
meshes.push_back(p_meshes[i]);
132
}
133
134
Vector<Ref<Texture2D>> textures = make_mesh_previews(meshes, nullptr, p_preview_size);
135
TypedArray<Texture2D> ret;
136
for (int i = 0; i < textures.size(); i++) {
137
ret.push_back(textures[i]);
138
}
139
140
return ret;
141
}
142
143
Vector<Ref<Texture2D>> EditorInterface::make_mesh_previews(const Vector<Ref<Mesh>> &p_meshes, Vector<Transform3D> *p_transforms, int p_preview_size) {
144
int size = p_preview_size;
145
146
RID scenario = RS::get_singleton()->scenario_create();
147
148
RID viewport = RS::get_singleton()->viewport_create();
149
RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_ALWAYS);
150
RS::get_singleton()->viewport_set_scenario(viewport, scenario);
151
RS::get_singleton()->viewport_set_size(viewport, size, size);
152
RS::get_singleton()->viewport_set_transparent_background(viewport, true);
153
RS::get_singleton()->viewport_set_active(viewport, true);
154
RID viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);
155
156
RID camera = RS::get_singleton()->camera_create();
157
RS::get_singleton()->viewport_attach_camera(viewport, camera);
158
159
RID light = RS::get_singleton()->directional_light_create();
160
RID light_instance = RS::get_singleton()->instance_create2(light, scenario);
161
162
RID light2 = RS::get_singleton()->directional_light_create();
163
RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
164
RID light_instance2 = RS::get_singleton()->instance_create2(light2, scenario);
165
166
EditorProgress ep("mlib", TTR("Creating Mesh Previews"), p_meshes.size());
167
168
Vector<Ref<Texture2D>> textures;
169
170
for (int i = 0; i < p_meshes.size(); i++) {
171
const Ref<Mesh> &mesh = p_meshes[i];
172
if (mesh.is_null()) {
173
textures.push_back(Ref<Texture2D>());
174
continue;
175
}
176
177
Transform3D mesh_xform;
178
if (p_transforms != nullptr) {
179
mesh_xform = (*p_transforms)[i];
180
}
181
182
RID inst = RS::get_singleton()->instance_create2(mesh->get_rid(), scenario);
183
RS::get_singleton()->instance_set_transform(inst, mesh_xform);
184
185
AABB aabb = mesh->get_aabb();
186
Vector3 ofs = aabb.get_center();
187
aabb.position -= ofs;
188
Transform3D xform;
189
xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math::PI / 6);
190
xform.basis = Basis().rotated(Vector3(1, 0, 0), Math::PI / 6) * xform.basis;
191
AABB rot_aabb = xform.xform(aabb);
192
float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
193
if (m == 0) {
194
textures.push_back(Ref<Texture2D>());
195
continue;
196
}
197
xform.origin = -xform.basis.xform(ofs); //-ofs*m;
198
xform.origin.z -= rot_aabb.size.z * 2;
199
xform.invert();
200
xform = mesh_xform * xform;
201
202
RS::get_singleton()->camera_set_transform(camera, xform * Transform3D(Basis(), Vector3(0, 0, 3)));
203
RS::get_singleton()->camera_set_orthogonal(camera, m * 2, 0.01, 1000.0);
204
205
RS::get_singleton()->instance_set_transform(light_instance, xform * Transform3D().looking_at(Vector3(-2, -1, -1), Vector3(0, 1, 0)));
206
RS::get_singleton()->instance_set_transform(light_instance2, xform * Transform3D().looking_at(Vector3(+1, -1, -2), Vector3(0, 1, 0)));
207
208
ep.step(TTR("Thumbnail..."), i);
209
DisplayServer::get_singleton()->process_events();
210
Main::iteration();
211
Main::iteration();
212
Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);
213
ERR_CONTINUE(img.is_null() || img->is_empty());
214
Ref<ImageTexture> it = ImageTexture::create_from_image(img);
215
216
RS::get_singleton()->free(inst);
217
218
textures.push_back(it);
219
}
220
221
RS::get_singleton()->free(viewport);
222
RS::get_singleton()->free(light);
223
RS::get_singleton()->free(light_instance);
224
RS::get_singleton()->free(light2);
225
RS::get_singleton()->free(light_instance2);
226
RS::get_singleton()->free(camera);
227
RS::get_singleton()->free(scenario);
228
229
return textures;
230
}
231
232
void EditorInterface::make_scene_preview(const String &p_path, Node *p_scene, int p_preview_size) {
233
if (!Engine::get_singleton()->is_editor_hint() || !DisplayServer::get_singleton()->window_can_draw()) {
234
return;
235
}
236
ERR_FAIL_COND_MSG(p_path.is_empty(), "Path is empty, cannot generate preview.");
237
ERR_FAIL_NULL_MSG(p_scene, "The provided scene is null, cannot generate preview.");
238
ERR_FAIL_COND_MSG(p_scene->is_inside_tree(), "The scene must not be inside the tree.");
239
ERR_FAIL_NULL_MSG(EditorNode::get_singleton(), "EditorNode doesn't exist.");
240
241
SubViewport *sub_viewport_node = memnew(SubViewport);
242
AABB scene_aabb;
243
scene_aabb = _calculate_aabb_for_scene(p_scene, scene_aabb);
244
245
sub_viewport_node->set_update_mode(SubViewport::UPDATE_ALWAYS);
246
sub_viewport_node->set_size(Vector2i(p_preview_size, p_preview_size));
247
sub_viewport_node->set_transparent_background(false);
248
Ref<World3D> world;
249
world.instantiate();
250
sub_viewport_node->set_world_3d(world);
251
252
EditorNode::get_singleton()->add_child(sub_viewport_node);
253
Ref<Environment> env;
254
env.instantiate();
255
env->set_background(Environment::BG_CLEAR_COLOR);
256
257
Ref<CameraAttributesPractical> camera_attributes;
258
camera_attributes.instantiate();
259
260
Node3D *root = memnew(Node3D);
261
root->set_name("Root");
262
sub_viewport_node->add_child(root);
263
264
Camera3D *camera = memnew(Camera3D);
265
camera->set_environment(env);
266
camera->set_attributes(camera_attributes);
267
camera->set_name("Camera3D");
268
root->add_child(camera);
269
camera->set_current(true);
270
271
camera->set_position(Vector3(0.0, 0.0, 3.0));
272
273
DirectionalLight3D *light = memnew(DirectionalLight3D);
274
light->set_name("Light");
275
DirectionalLight3D *light2 = memnew(DirectionalLight3D);
276
light2->set_name("Light2");
277
light2->set_color(Color(0.7, 0.7, 0.7, 1.0));
278
279
root->add_child(light);
280
root->add_child(light2);
281
282
sub_viewport_node->add_child(p_scene);
283
284
// Calculate the camera and lighting position based on the size of the scene.
285
Vector3 center = scene_aabb.get_center();
286
float camera_size = scene_aabb.get_longest_axis_size();
287
288
const float cam_rot_x = -Math::PI / 4;
289
const float cam_rot_y = -Math::PI / 4;
290
291
camera->set_orthogonal(camera_size * 2.0, 0.0001, camera_size * 2.0);
292
293
Transform3D xf;
294
xf.basis = Basis(Vector3(0, 1, 0), cam_rot_y) * Basis(Vector3(1, 0, 0), cam_rot_x);
295
xf.origin = center;
296
xf.translate_local(0, 0, camera_size);
297
298
camera->set_transform(xf);
299
300
Transform3D xform;
301
xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math::PI / 6);
302
xform.basis = Basis().rotated(Vector3(1, 0, 0), Math::PI / 6) * xform.basis;
303
304
light->set_transform(xform * Transform3D().looking_at(Vector3(-2, -1, -1), Vector3(0, 1, 0)));
305
light2->set_transform(xform * Transform3D().looking_at(Vector3(+1, -1, -2), Vector3(0, 1, 0)));
306
307
// Update the renderer to get the screenshot.
308
DisplayServer::get_singleton()->process_events();
309
Main::iteration();
310
Main::iteration();
311
312
// Get the texture.
313
Ref<Texture2D> texture = sub_viewport_node->get_texture();
314
ERR_FAIL_COND_MSG(texture.is_null(), "Failed to get texture from sub_viewport_node.");
315
316
// Remove the initial scene node.
317
sub_viewport_node->remove_child(p_scene);
318
319
// Cleanup the viewport.
320
if (sub_viewport_node) {
321
if (sub_viewport_node->get_parent()) {
322
sub_viewport_node->get_parent()->remove_child(sub_viewport_node);
323
}
324
sub_viewport_node->queue_free();
325
sub_viewport_node = nullptr;
326
}
327
328
// Now generate the cache image.
329
Ref<Image> img = texture->get_image();
330
if (img.is_valid() && img->get_width() > 0 && img->get_height() > 0) {
331
img = img->duplicate();
332
333
int preview_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
334
preview_size *= EDSCALE;
335
336
int vp_size = MIN(img->get_width(), img->get_height());
337
int x = (img->get_width() - vp_size) / 2;
338
int y = (img->get_height() - vp_size) / 2;
339
340
if (vp_size < preview_size) {
341
img->crop_from_point(x, y, vp_size, vp_size);
342
} else {
343
int ratio = vp_size / preview_size;
344
int size = preview_size * MAX(1, ratio / 2);
345
346
x = (img->get_width() - size) / 2;
347
y = (img->get_height() - size) / 2;
348
349
img->crop_from_point(x, y, size, size);
350
img->resize(preview_size, preview_size, Image::INTERPOLATE_LANCZOS);
351
}
352
img->convert(Image::FORMAT_RGB8);
353
354
String temp_path = EditorPaths::get_singleton()->get_cache_dir();
355
String cache_base = ProjectSettings::get_singleton()->globalize_path(p_path).md5_text();
356
cache_base = temp_path.path_join("resthumb-" + cache_base);
357
358
post_process_preview(img);
359
img->save_png(cache_base + ".png");
360
}
361
362
EditorResourcePreview::get_singleton()->check_for_invalidation(p_path);
363
EditorFileSystem::get_singleton()->emit_signal(SNAME("filesystem_changed"));
364
}
365
366
void EditorInterface::set_plugin_enabled(const String &p_plugin, bool p_enabled) {
367
EditorNode::get_singleton()->set_addon_plugin_enabled(p_plugin, p_enabled, true);
368
}
369
370
bool EditorInterface::is_plugin_enabled(const String &p_plugin) const {
371
return EditorNode::get_singleton()->is_addon_plugin_enabled(p_plugin);
372
}
373
374
// Editor GUI.
375
376
Ref<Theme> EditorInterface::get_editor_theme() const {
377
return EditorNode::get_singleton()->get_editor_theme();
378
}
379
380
Control *EditorInterface::get_base_control() const {
381
return EditorNode::get_singleton()->get_gui_base();
382
}
383
384
VBoxContainer *EditorInterface::get_editor_main_screen() const {
385
return EditorNode::get_singleton()->get_editor_main_screen()->get_control();
386
}
387
388
ScriptEditor *EditorInterface::get_script_editor() const {
389
return ScriptEditor::get_singleton();
390
}
391
392
SubViewport *EditorInterface::get_editor_viewport_2d() const {
393
return EditorNode::get_singleton()->get_scene_root();
394
}
395
396
SubViewport *EditorInterface::get_editor_viewport_3d(int p_idx) const {
397
ERR_FAIL_INDEX_V(p_idx, static_cast<int>(Node3DEditor::VIEWPORTS_COUNT), nullptr);
398
return Node3DEditor::get_singleton()->get_editor_viewport(p_idx)->get_viewport_node();
399
}
400
401
void EditorInterface::set_main_screen_editor(const String &p_name) {
402
EditorNode::get_singleton()->get_editor_main_screen()->select_by_name(p_name);
403
}
404
405
void EditorInterface::set_distraction_free_mode(bool p_enter) {
406
EditorNode::get_singleton()->set_distraction_free_mode(p_enter);
407
}
408
409
bool EditorInterface::is_distraction_free_mode_enabled() const {
410
return EditorNode::get_singleton()->is_distraction_free_mode_enabled();
411
}
412
413
bool EditorInterface::is_multi_window_enabled() const {
414
return EditorNode::get_singleton()->is_multi_window_enabled();
415
}
416
417
float EditorInterface::get_editor_scale() const {
418
return EDSCALE;
419
}
420
421
void EditorInterface::popup_dialog(Window *p_dialog, const Rect2i &p_screen_rect) {
422
p_dialog->popup_exclusive(EditorNode::get_singleton(), p_screen_rect);
423
}
424
425
void EditorInterface::popup_dialog_centered(Window *p_dialog, const Size2i &p_minsize) {
426
p_dialog->popup_exclusive_centered(EditorNode::get_singleton(), p_minsize);
427
}
428
429
void EditorInterface::popup_dialog_centered_ratio(Window *p_dialog, float p_ratio) {
430
p_dialog->popup_exclusive_centered_ratio(EditorNode::get_singleton(), p_ratio);
431
}
432
433
void EditorInterface::popup_dialog_centered_clamped(Window *p_dialog, const Size2i &p_size, float p_fallback_ratio) {
434
p_dialog->popup_exclusive_centered_clamped(EditorNode::get_singleton(), p_size, p_fallback_ratio);
435
}
436
437
String EditorInterface::get_current_feature_profile() const {
438
return EditorFeatureProfileManager::get_singleton()->get_current_profile_name();
439
}
440
441
void EditorInterface::set_current_feature_profile(const String &p_profile_name) {
442
EditorFeatureProfileManager::get_singleton()->set_current_profile(p_profile_name, true);
443
}
444
445
// Editor dialogs.
446
447
void EditorInterface::popup_node_selector(const Callable &p_callback, const TypedArray<StringName> &p_valid_types, Node *p_current_value) {
448
if (!node_selector) {
449
node_selector = memnew(SceneTreeDialog);
450
get_base_control()->add_child(node_selector);
451
}
452
453
Vector<StringName> valid_types;
454
int length = p_valid_types.size();
455
valid_types.resize(length);
456
for (int i = 0; i < length; i++) {
457
valid_types.write[i] = p_valid_types[i];
458
}
459
node_selector->set_valid_types(valid_types);
460
node_selector->popup_scenetree_dialog(p_current_value);
461
462
const Callable callback = callable_mp(this, &EditorInterface::_node_selected);
463
node_selector->connect(SNAME("selected"), callback.bind(p_callback), CONNECT_DEFERRED);
464
node_selector->connect(SNAME("canceled"), callback.bind(NodePath(), p_callback), CONNECT_DEFERRED);
465
}
466
467
void EditorInterface::popup_property_selector(Object *p_object, const Callable &p_callback, const PackedInt32Array &p_type_filter, const String &p_current_value) {
468
if (!property_selector) {
469
property_selector = memnew(PropertySelector);
470
get_base_control()->add_child(property_selector);
471
}
472
473
Vector<Variant::Type> type_filter;
474
int length = p_type_filter.size();
475
type_filter.resize(length);
476
for (int i = 0; i < length; i++) {
477
type_filter.write[i] = (Variant::Type)p_type_filter[i];
478
}
479
property_selector->set_type_filter(type_filter);
480
property_selector->select_property_from_instance(p_object, p_current_value);
481
482
const Callable callback = callable_mp(this, &EditorInterface::_property_selected);
483
property_selector->connect(SNAME("selected"), callback.bind(p_callback), CONNECT_DEFERRED);
484
property_selector->connect(SNAME("canceled"), callback.bind(String(), p_callback), CONNECT_DEFERRED);
485
}
486
487
void EditorInterface::popup_method_selector(Object *p_object, const Callable &p_callback, const String &p_current_value) {
488
if (!method_selector) {
489
method_selector = memnew(PropertySelector);
490
get_base_control()->add_child(method_selector);
491
}
492
493
method_selector->select_method_from_instance(p_object, p_current_value);
494
495
const Callable callback = callable_mp(this, &EditorInterface::_method_selected);
496
method_selector->connect(SNAME("selected"), callback.bind(p_callback), CONNECT_DEFERRED);
497
method_selector->connect(SNAME("canceled"), callback.bind(String(), p_callback), CONNECT_DEFERRED);
498
}
499
500
void EditorInterface::popup_quick_open(const Callable &p_callback, const TypedArray<StringName> &p_base_types) {
501
StringName required_type = SNAME("Resource");
502
Vector<StringName> base_types;
503
if (p_base_types.is_empty()) {
504
base_types.append(required_type);
505
} else {
506
for (int i = 0; i < p_base_types.size(); i++) {
507
StringName type = p_base_types[i];
508
ERR_FAIL_COND_MSG(!(ClassDB::is_parent_class(type, required_type) || EditorNode::get_editor_data().script_class_is_parent(type, required_type)), "Only types deriving from Resource are supported in the quick open dialog.");
509
base_types.append(type);
510
}
511
}
512
513
EditorQuickOpenDialog *quick_open = EditorNode::get_singleton()->get_quick_open_dialog();
514
quick_open->connect(SNAME("canceled"), callable_mp(this, &EditorInterface::_quick_open).bind(String(), p_callback));
515
quick_open->popup_dialog(base_types, callable_mp(this, &EditorInterface::_quick_open).bind(p_callback));
516
}
517
518
void EditorInterface::popup_create_dialog(const Callable &p_callback, const StringName &p_base_type, const String &p_current_type, const String &p_dialog_title, const TypedArray<StringName> &p_custom_type_blocklist) {
519
if (!create_dialog) {
520
create_dialog = memnew(CreateDialog);
521
get_base_control()->add_child(create_dialog);
522
}
523
524
HashSet<StringName> blocklist;
525
for (const Variant &E : p_custom_type_blocklist) {
526
blocklist.insert(E);
527
}
528
create_dialog->set_type_blocklist(blocklist);
529
530
String safe_base_type = p_base_type;
531
if (p_base_type.is_empty() || (!ClassDB::class_exists(p_base_type) && !ScriptServer::is_global_class(p_base_type))) {
532
ERR_PRINT(vformat("Invalid base type '%s'. The base type has fallen back to 'Object'.", p_base_type));
533
safe_base_type = "Object";
534
}
535
536
create_dialog->set_base_type(safe_base_type);
537
create_dialog->popup_create(false, true, p_current_type, "");
538
create_dialog->set_title(p_dialog_title.is_empty() ? vformat(TTR("Create New %s"), p_base_type) : p_dialog_title);
539
540
const Callable callback = callable_mp(this, &EditorInterface::_create_dialog_item_selected);
541
create_dialog->connect(SNAME("create"), callback.bind(false, p_callback), CONNECT_DEFERRED);
542
create_dialog->connect(SNAME("canceled"), callback.bind(true, p_callback), CONNECT_DEFERRED);
543
}
544
545
void EditorInterface::_node_selected(const NodePath &p_node_path, const Callable &p_callback) {
546
const Callable callback = callable_mp(this, &EditorInterface::_node_selected);
547
node_selector->disconnect(SNAME("selected"), callback);
548
node_selector->disconnect(SNAME("canceled"), callback);
549
550
if (p_node_path.is_empty()) {
551
_call_dialog_callback(p_callback, NodePath(), "node selection canceled");
552
} else {
553
const NodePath path = get_edited_scene_root()->get_path().rel_path_to(p_node_path);
554
_call_dialog_callback(p_callback, path, "node selected");
555
}
556
}
557
558
void EditorInterface::_property_selected(const String &p_property_name, const Callable &p_callback) {
559
const Callable callback = callable_mp(this, &EditorInterface::_property_selected);
560
property_selector->disconnect(SNAME("selected"), callback);
561
property_selector->disconnect(SNAME("canceled"), callback);
562
563
if (p_property_name.is_empty()) {
564
_call_dialog_callback(p_callback, NodePath(p_property_name).get_as_property_path(), "property selection canceled");
565
} else {
566
_call_dialog_callback(p_callback, NodePath(p_property_name).get_as_property_path(), "property selected");
567
}
568
}
569
570
void EditorInterface::_method_selected(const String &p_method_name, const Callable &p_callback) {
571
const Callable callback = callable_mp(this, &EditorInterface::_method_selected);
572
method_selector->disconnect(SNAME("selected"), callback);
573
method_selector->disconnect(SNAME("canceled"), callback);
574
575
if (p_method_name.is_empty()) {
576
_call_dialog_callback(p_callback, p_method_name, "method selection canceled");
577
} else {
578
_call_dialog_callback(p_callback, p_method_name, "method selected");
579
}
580
}
581
582
void EditorInterface::_quick_open(const String &p_file_path, const Callable &p_callback) {
583
EditorQuickOpenDialog *quick_open = EditorNode::get_singleton()->get_quick_open_dialog();
584
quick_open->disconnect(SNAME("canceled"), callable_mp(this, &EditorInterface::_quick_open));
585
_call_dialog_callback(p_callback, p_file_path, "quick open");
586
}
587
588
void EditorInterface::_create_dialog_item_selected(bool p_is_canceled, const Callable &p_callback) {
589
const Callable callback = callable_mp(this, &EditorInterface::_create_dialog_item_selected);
590
create_dialog->disconnect(SNAME("create"), callback);
591
create_dialog->disconnect(SNAME("canceled"), callback);
592
_call_dialog_callback(p_callback, p_is_canceled ? "" : create_dialog->get_selected_type(), "create dialog");
593
}
594
595
void EditorInterface::_call_dialog_callback(const Callable &p_callback, const Variant &p_selected, const String &p_context) {
596
Callable::CallError ce;
597
Variant ret;
598
const Variant *args[1] = { &p_selected };
599
p_callback.callp(args, 1, ret, ce);
600
if (ce.error != Callable::CallError::CALL_OK) {
601
ERR_PRINT(vformat("Error calling %s callback: %s", p_context, Variant::get_callable_error_text(p_callback, args, 1, ce)));
602
}
603
}
604
605
// Editor docks.
606
607
FileSystemDock *EditorInterface::get_file_system_dock() const {
608
return FileSystemDock::get_singleton();
609
}
610
611
void EditorInterface::select_file(const String &p_file) {
612
FileSystemDock::get_singleton()->select_file(p_file);
613
}
614
615
Vector<String> EditorInterface::get_selected_paths() const {
616
return FileSystemDock::get_singleton()->get_selected_paths();
617
}
618
619
String EditorInterface::get_current_path() const {
620
return FileSystemDock::get_singleton()->get_current_path();
621
}
622
623
String EditorInterface::get_current_directory() const {
624
return FileSystemDock::get_singleton()->get_current_directory();
625
}
626
627
EditorInspector *EditorInterface::get_inspector() const {
628
return InspectorDock::get_inspector_singleton();
629
}
630
631
// Object/Resource/Node editing.
632
633
void EditorInterface::inspect_object(Object *p_obj, const String &p_for_property, bool p_inspector_only) {
634
EditorNode::get_singleton()->push_item(p_obj, p_for_property, p_inspector_only);
635
}
636
637
void EditorInterface::edit_resource(const Ref<Resource> &p_resource) {
638
EditorNode::get_singleton()->edit_resource(p_resource);
639
}
640
641
void EditorInterface::edit_node(Node *p_node) {
642
EditorNode::get_singleton()->edit_node(p_node);
643
}
644
645
void EditorInterface::edit_script(const Ref<Script> &p_script, int p_line, int p_col, bool p_grab_focus) {
646
ScriptEditor::get_singleton()->edit(p_script, p_line - 1, p_col - 1, p_grab_focus);
647
}
648
649
void EditorInterface::open_scene_from_path(const String &scene_path, bool p_set_inherited) {
650
if (EditorNode::get_singleton()->is_changing_scene()) {
651
return;
652
}
653
EditorNode::get_singleton()->load_scene(scene_path, false, p_set_inherited);
654
}
655
656
void EditorInterface::reload_scene_from_path(const String &scene_path) {
657
if (EditorNode::get_singleton()->is_changing_scene()) {
658
return;
659
}
660
661
EditorNode::get_singleton()->reload_scene(scene_path);
662
}
663
664
Node *EditorInterface::get_edited_scene_root() const {
665
return EditorNode::get_singleton()->get_edited_scene();
666
}
667
668
PackedStringArray EditorInterface::get_open_scenes() const {
669
PackedStringArray ret;
670
Vector<EditorData::EditedScene> scenes = EditorNode::get_editor_data().get_edited_scenes();
671
672
for (EditorData::EditedScene &edited_scene : scenes) {
673
if (edited_scene.root == nullptr) {
674
continue;
675
}
676
ret.push_back(edited_scene.root->get_scene_file_path());
677
}
678
return ret;
679
}
680
681
TypedArray<Node> EditorInterface::get_open_scene_roots() const {
682
TypedArray<Node> ret;
683
Vector<EditorData::EditedScene> scenes = EditorNode::get_editor_data().get_edited_scenes();
684
685
for (EditorData::EditedScene &edited_scene : scenes) {
686
if (edited_scene.root == nullptr) {
687
continue;
688
}
689
ret.push_back(edited_scene.root);
690
}
691
return ret;
692
}
693
694
Error EditorInterface::save_scene() {
695
if (!get_edited_scene_root()) {
696
return ERR_CANT_CREATE;
697
}
698
if (get_edited_scene_root()->get_scene_file_path().is_empty()) {
699
return ERR_CANT_CREATE;
700
}
701
702
save_scene_as(get_edited_scene_root()->get_scene_file_path());
703
return OK;
704
}
705
706
void EditorInterface::save_scene_as(const String &p_scene, bool p_with_preview) {
707
EditorNode::get_singleton()->save_scene_to_path(p_scene, p_with_preview);
708
}
709
710
void EditorInterface::mark_scene_as_unsaved() {
711
EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(EditorNode::get_editor_data().get_current_edited_scene_history_id());
712
EditorSceneTabs::get_singleton()->update_scene_tabs();
713
}
714
715
void EditorInterface::save_all_scenes() {
716
EditorNode::get_singleton()->save_all_scenes();
717
}
718
719
Error EditorInterface::close_scene() {
720
return EditorNode::get_singleton()->close_scene() ? OK : ERR_DOES_NOT_EXIST;
721
}
722
723
// Scene playback.
724
725
void EditorInterface::play_main_scene() {
726
EditorRunBar::get_singleton()->play_main_scene();
727
}
728
729
void EditorInterface::play_current_scene() {
730
EditorRunBar::get_singleton()->play_current_scene();
731
}
732
733
void EditorInterface::play_custom_scene(const String &scene_path) {
734
EditorRunBar::get_singleton()->play_custom_scene(scene_path);
735
}
736
737
void EditorInterface::stop_playing_scene() {
738
EditorRunBar::get_singleton()->stop_playing();
739
}
740
741
bool EditorInterface::is_playing_scene() const {
742
return EditorRunBar::get_singleton()->is_playing();
743
}
744
745
String EditorInterface::get_playing_scene() const {
746
return EditorRunBar::get_singleton()->get_playing_scene();
747
}
748
749
void EditorInterface::set_movie_maker_enabled(bool p_enabled) {
750
EditorRunBar::get_singleton()->set_movie_maker_enabled(p_enabled);
751
}
752
753
bool EditorInterface::is_movie_maker_enabled() const {
754
return EditorRunBar::get_singleton()->is_movie_maker_enabled();
755
}
756
757
void EditorInterface::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
758
const String pf = p_function;
759
if (p_idx == 0) {
760
if (pf == "set_main_screen_editor") {
761
for (String E : { "\"2D\"", "\"3D\"", "\"Script\"", "\"Game\"", "\"AssetLib\"" }) {
762
r_options->push_back(E);
763
}
764
} else if (pf == "get_editor_viewport_3d") {
765
for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
766
r_options->push_back(String::num_int64(i));
767
}
768
}
769
}
770
Object::get_argument_options(p_function, p_idx, r_options);
771
}
772
773
// Base.
774
775
void EditorInterface::_bind_methods() {
776
ClassDB::bind_method(D_METHOD("restart_editor", "save"), &EditorInterface::restart_editor, DEFVAL(true));
777
778
// Editor tools.
779
780
ClassDB::bind_method(D_METHOD("get_command_palette"), &EditorInterface::get_command_palette);
781
ClassDB::bind_method(D_METHOD("get_resource_filesystem"), &EditorInterface::get_resource_file_system);
782
ClassDB::bind_method(D_METHOD("get_editor_paths"), &EditorInterface::get_editor_paths);
783
ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorInterface::get_resource_previewer);
784
ClassDB::bind_method(D_METHOD("get_selection"), &EditorInterface::get_selection);
785
ClassDB::bind_method(D_METHOD("get_editor_settings"), &EditorInterface::get_editor_settings);
786
ClassDB::bind_method(D_METHOD("get_editor_toaster"), &EditorInterface::get_editor_toaster);
787
ClassDB::bind_method(D_METHOD("get_editor_undo_redo"), &EditorInterface::get_editor_undo_redo);
788
789
ClassDB::bind_method(D_METHOD("make_mesh_previews", "meshes", "preview_size"), &EditorInterface::_make_mesh_previews);
790
791
ClassDB::bind_method(D_METHOD("set_plugin_enabled", "plugin", "enabled"), &EditorInterface::set_plugin_enabled);
792
ClassDB::bind_method(D_METHOD("is_plugin_enabled", "plugin"), &EditorInterface::is_plugin_enabled);
793
794
// Editor GUI.
795
796
ClassDB::bind_method(D_METHOD("get_editor_theme"), &EditorInterface::get_editor_theme);
797
ClassDB::bind_method(D_METHOD("get_base_control"), &EditorInterface::get_base_control);
798
ClassDB::bind_method(D_METHOD("get_editor_main_screen"), &EditorInterface::get_editor_main_screen);
799
ClassDB::bind_method(D_METHOD("get_script_editor"), &EditorInterface::get_script_editor);
800
ClassDB::bind_method(D_METHOD("get_editor_viewport_2d"), &EditorInterface::get_editor_viewport_2d);
801
ClassDB::bind_method(D_METHOD("get_editor_viewport_3d", "idx"), &EditorInterface::get_editor_viewport_3d, DEFVAL(0));
802
803
ClassDB::bind_method(D_METHOD("set_main_screen_editor", "name"), &EditorInterface::set_main_screen_editor);
804
ClassDB::bind_method(D_METHOD("set_distraction_free_mode", "enter"), &EditorInterface::set_distraction_free_mode);
805
ClassDB::bind_method(D_METHOD("is_distraction_free_mode_enabled"), &EditorInterface::is_distraction_free_mode_enabled);
806
ClassDB::bind_method(D_METHOD("is_multi_window_enabled"), &EditorInterface::is_multi_window_enabled);
807
808
ClassDB::bind_method(D_METHOD("get_editor_scale"), &EditorInterface::get_editor_scale);
809
810
ClassDB::bind_method(D_METHOD("popup_dialog", "dialog", "rect"), &EditorInterface::popup_dialog, DEFVAL(Rect2i()));
811
ClassDB::bind_method(D_METHOD("popup_dialog_centered", "dialog", "minsize"), &EditorInterface::popup_dialog_centered, DEFVAL(Size2i()));
812
ClassDB::bind_method(D_METHOD("popup_dialog_centered_ratio", "dialog", "ratio"), &EditorInterface::popup_dialog_centered_ratio, DEFVAL(0.8));
813
ClassDB::bind_method(D_METHOD("popup_dialog_centered_clamped", "dialog", "minsize", "fallback_ratio"), &EditorInterface::popup_dialog_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
814
815
ClassDB::bind_method(D_METHOD("get_current_feature_profile"), &EditorInterface::get_current_feature_profile);
816
ClassDB::bind_method(D_METHOD("set_current_feature_profile", "profile_name"), &EditorInterface::set_current_feature_profile);
817
818
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "distraction_free_mode"), "set_distraction_free_mode", "is_distraction_free_mode_enabled");
819
820
// Editor dialogs.
821
822
ClassDB::bind_method(D_METHOD("popup_node_selector", "callback", "valid_types", "current_value"), &EditorInterface::popup_node_selector, DEFVAL(TypedArray<StringName>()), DEFVAL(Variant()));
823
ClassDB::bind_method(D_METHOD("popup_property_selector", "object", "callback", "type_filter", "current_value"), &EditorInterface::popup_property_selector, DEFVAL(PackedInt32Array()), DEFVAL(String()));
824
ClassDB::bind_method(D_METHOD("popup_method_selector", "object", "callback", "current_value"), &EditorInterface::popup_method_selector, DEFVAL(String()));
825
ClassDB::bind_method(D_METHOD("popup_quick_open", "callback", "base_types"), &EditorInterface::popup_quick_open, DEFVAL(TypedArray<StringName>()));
826
ClassDB::bind_method(D_METHOD("popup_create_dialog", "callback", "base_type", "current_type", "dialog_title", "type_blocklist"), &EditorInterface::popup_create_dialog, DEFVAL(""), DEFVAL(""), DEFVAL(""), DEFVAL(TypedArray<StringName>()));
827
828
// Editor docks.
829
830
ClassDB::bind_method(D_METHOD("get_file_system_dock"), &EditorInterface::get_file_system_dock);
831
ClassDB::bind_method(D_METHOD("select_file", "file"), &EditorInterface::select_file);
832
ClassDB::bind_method(D_METHOD("get_selected_paths"), &EditorInterface::get_selected_paths);
833
ClassDB::bind_method(D_METHOD("get_current_path"), &EditorInterface::get_current_path);
834
ClassDB::bind_method(D_METHOD("get_current_directory"), &EditorInterface::get_current_directory);
835
836
ClassDB::bind_method(D_METHOD("get_inspector"), &EditorInterface::get_inspector);
837
838
// Object/Resource/Node editing.
839
840
ClassDB::bind_method(D_METHOD("inspect_object", "object", "for_property", "inspector_only"), &EditorInterface::inspect_object, DEFVAL(String()), DEFVAL(false));
841
842
ClassDB::bind_method(D_METHOD("edit_resource", "resource"), &EditorInterface::edit_resource);
843
ClassDB::bind_method(D_METHOD("edit_node", "node"), &EditorInterface::edit_node);
844
ClassDB::bind_method(D_METHOD("edit_script", "script", "line", "column", "grab_focus"), &EditorInterface::edit_script, DEFVAL(-1), DEFVAL(0), DEFVAL(true));
845
ClassDB::bind_method(D_METHOD("open_scene_from_path", "scene_filepath", "set_inherited"), &EditorInterface::open_scene_from_path, DEFVAL(false));
846
ClassDB::bind_method(D_METHOD("reload_scene_from_path", "scene_filepath"), &EditorInterface::reload_scene_from_path);
847
848
ClassDB::bind_method(D_METHOD("get_open_scenes"), &EditorInterface::get_open_scenes);
849
ClassDB::bind_method(D_METHOD("get_open_scene_roots"), &EditorInterface::get_open_scene_roots);
850
ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorInterface::get_edited_scene_root);
851
852
ClassDB::bind_method(D_METHOD("save_scene"), &EditorInterface::save_scene);
853
ClassDB::bind_method(D_METHOD("save_scene_as", "path", "with_preview"), &EditorInterface::save_scene_as, DEFVAL(true));
854
ClassDB::bind_method(D_METHOD("save_all_scenes"), &EditorInterface::save_all_scenes);
855
ClassDB::bind_method(D_METHOD("close_scene"), &EditorInterface::close_scene);
856
857
ClassDB::bind_method(D_METHOD("mark_scene_as_unsaved"), &EditorInterface::mark_scene_as_unsaved);
858
859
// Scene playback.
860
861
ClassDB::bind_method(D_METHOD("play_main_scene"), &EditorInterface::play_main_scene);
862
ClassDB::bind_method(D_METHOD("play_current_scene"), &EditorInterface::play_current_scene);
863
ClassDB::bind_method(D_METHOD("play_custom_scene", "scene_filepath"), &EditorInterface::play_custom_scene);
864
ClassDB::bind_method(D_METHOD("stop_playing_scene"), &EditorInterface::stop_playing_scene);
865
ClassDB::bind_method(D_METHOD("is_playing_scene"), &EditorInterface::is_playing_scene);
866
ClassDB::bind_method(D_METHOD("get_playing_scene"), &EditorInterface::get_playing_scene);
867
868
ClassDB::bind_method(D_METHOD("set_movie_maker_enabled", "enabled"), &EditorInterface::set_movie_maker_enabled);
869
ClassDB::bind_method(D_METHOD("is_movie_maker_enabled"), &EditorInterface::is_movie_maker_enabled);
870
871
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_maker_enabled"), "set_movie_maker_enabled", "is_movie_maker_enabled");
872
}
873
874
void EditorInterface::create() {
875
memnew(EditorInterface);
876
}
877
878
void EditorInterface::free() {
879
ERR_FAIL_NULL(singleton);
880
memdelete(singleton);
881
}
882
883
EditorInterface::EditorInterface() {
884
ERR_FAIL_COND(singleton != nullptr);
885
singleton = this;
886
}
887
888