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