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
33#include "editor/editor_command_palette.h"
34#include "editor/editor_feature_profile.h"
35#include "editor/editor_node.h"
36#include "editor/editor_paths.h"
37#include "editor/editor_resource_preview.h"
38#include "editor/editor_scale.h"
39#include "editor/editor_settings.h"
40#include "editor/editor_undo_redo_manager.h"
41#include "editor/filesystem_dock.h"
42#include "editor/gui/editor_run_bar.h"
43#include "editor/inspector_dock.h"
44#include "main/main.h"
45#include "scene/gui/box_container.h"
46#include "scene/gui/control.h"
47#include "scene/main/window.h"
48#include "scene/resources/theme.h"
49
50EditorInterface *EditorInterface::singleton = nullptr;
51
52void EditorInterface::restart_editor(bool p_save) {
53 if (p_save) {
54 EditorNode::get_singleton()->save_all_scenes();
55 }
56 EditorNode::get_singleton()->restart_editor();
57}
58
59// Editor tools.
60
61EditorCommandPalette *EditorInterface::get_command_palette() const {
62 return EditorCommandPalette::get_singleton();
63}
64
65EditorFileSystem *EditorInterface::get_resource_file_system() const {
66 return EditorFileSystem::get_singleton();
67}
68
69EditorPaths *EditorInterface::get_editor_paths() const {
70 return EditorPaths::get_singleton();
71}
72
73EditorResourcePreview *EditorInterface::get_resource_previewer() const {
74 return EditorResourcePreview::get_singleton();
75}
76
77EditorSelection *EditorInterface::get_selection() const {
78 return EditorNode::get_singleton()->get_editor_selection();
79}
80
81Ref<EditorSettings> EditorInterface::get_editor_settings() const {
82 return EditorSettings::get_singleton();
83}
84
85TypedArray<Texture2D> EditorInterface::_make_mesh_previews(const TypedArray<Mesh> &p_meshes, int p_preview_size) {
86 Vector<Ref<Mesh>> meshes;
87
88 for (int i = 0; i < p_meshes.size(); i++) {
89 meshes.push_back(p_meshes[i]);
90 }
91
92 Vector<Ref<Texture2D>> textures = make_mesh_previews(meshes, nullptr, p_preview_size);
93 TypedArray<Texture2D> ret;
94 for (int i = 0; i < textures.size(); i++) {
95 ret.push_back(textures[i]);
96 }
97
98 return ret;
99}
100
101Vector<Ref<Texture2D>> EditorInterface::make_mesh_previews(const Vector<Ref<Mesh>> &p_meshes, Vector<Transform3D> *p_transforms, int p_preview_size) {
102 int size = p_preview_size;
103
104 RID scenario = RS::get_singleton()->scenario_create();
105
106 RID viewport = RS::get_singleton()->viewport_create();
107 RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_ALWAYS);
108 RS::get_singleton()->viewport_set_scenario(viewport, scenario);
109 RS::get_singleton()->viewport_set_size(viewport, size, size);
110 RS::get_singleton()->viewport_set_transparent_background(viewport, true);
111 RS::get_singleton()->viewport_set_active(viewport, true);
112 RID viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);
113
114 RID camera = RS::get_singleton()->camera_create();
115 RS::get_singleton()->viewport_attach_camera(viewport, camera);
116
117 RID light = RS::get_singleton()->directional_light_create();
118 RID light_instance = RS::get_singleton()->instance_create2(light, scenario);
119
120 RID light2 = RS::get_singleton()->directional_light_create();
121 RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
122 RID light_instance2 = RS::get_singleton()->instance_create2(light2, scenario);
123
124 EditorProgress ep("mlib", TTR("Creating Mesh Previews"), p_meshes.size());
125
126 Vector<Ref<Texture2D>> textures;
127
128 for (int i = 0; i < p_meshes.size(); i++) {
129 Ref<Mesh> mesh = p_meshes[i];
130 if (!mesh.is_valid()) {
131 textures.push_back(Ref<Texture2D>());
132 continue;
133 }
134
135 Transform3D mesh_xform;
136 if (p_transforms != nullptr) {
137 mesh_xform = (*p_transforms)[i];
138 }
139
140 RID inst = RS::get_singleton()->instance_create2(mesh->get_rid(), scenario);
141 RS::get_singleton()->instance_set_transform(inst, mesh_xform);
142
143 AABB aabb = mesh->get_aabb();
144 Vector3 ofs = aabb.get_center();
145 aabb.position -= ofs;
146 Transform3D xform;
147 xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI / 6);
148 xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI / 6) * xform.basis;
149 AABB rot_aabb = xform.xform(aabb);
150 float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
151 if (m == 0) {
152 textures.push_back(Ref<Texture2D>());
153 continue;
154 }
155 xform.origin = -xform.basis.xform(ofs); //-ofs*m;
156 xform.origin.z -= rot_aabb.size.z * 2;
157 xform.invert();
158 xform = mesh_xform * xform;
159
160 RS::get_singleton()->camera_set_transform(camera, xform * Transform3D(Basis(), Vector3(0, 0, 3)));
161 RS::get_singleton()->camera_set_orthogonal(camera, m * 2, 0.01, 1000.0);
162
163 RS::get_singleton()->instance_set_transform(light_instance, xform * Transform3D().looking_at(Vector3(-2, -1, -1), Vector3(0, 1, 0)));
164 RS::get_singleton()->instance_set_transform(light_instance2, xform * Transform3D().looking_at(Vector3(+1, -1, -2), Vector3(0, 1, 0)));
165
166 ep.step(TTR("Thumbnail..."), i);
167 DisplayServer::get_singleton()->process_events();
168 Main::iteration();
169 Main::iteration();
170 Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);
171 ERR_CONTINUE(!img.is_valid() || img->is_empty());
172 Ref<ImageTexture> it = ImageTexture::create_from_image(img);
173
174 RS::get_singleton()->free(inst);
175
176 textures.push_back(it);
177 }
178
179 RS::get_singleton()->free(viewport);
180 RS::get_singleton()->free(light);
181 RS::get_singleton()->free(light_instance);
182 RS::get_singleton()->free(light2);
183 RS::get_singleton()->free(light_instance2);
184 RS::get_singleton()->free(camera);
185 RS::get_singleton()->free(scenario);
186
187 return textures;
188}
189
190void EditorInterface::set_plugin_enabled(const String &p_plugin, bool p_enabled) {
191 EditorNode::get_singleton()->set_addon_plugin_enabled(p_plugin, p_enabled, true);
192}
193
194bool EditorInterface::is_plugin_enabled(const String &p_plugin) const {
195 return EditorNode::get_singleton()->is_addon_plugin_enabled(p_plugin);
196}
197
198// Editor GUI.
199
200Ref<Theme> EditorInterface::get_editor_theme() const {
201 return EditorNode::get_singleton()->get_editor_theme();
202}
203
204Control *EditorInterface::get_base_control() const {
205 return EditorNode::get_singleton()->get_gui_base();
206}
207
208VBoxContainer *EditorInterface::get_editor_main_screen() const {
209 return EditorNode::get_singleton()->get_main_screen_control();
210}
211
212ScriptEditor *EditorInterface::get_script_editor() const {
213 return ScriptEditor::get_singleton();
214}
215
216void EditorInterface::set_main_screen_editor(const String &p_name) {
217 EditorNode::get_singleton()->select_editor_by_name(p_name);
218}
219
220void EditorInterface::set_distraction_free_mode(bool p_enter) {
221 EditorNode::get_singleton()->set_distraction_free_mode(p_enter);
222}
223
224bool EditorInterface::is_distraction_free_mode_enabled() const {
225 return EditorNode::get_singleton()->is_distraction_free_mode_enabled();
226}
227
228float EditorInterface::get_editor_scale() const {
229 return EDSCALE;
230}
231
232void EditorInterface::popup_dialog(Window *p_dialog, const Rect2i &p_screen_rect) {
233 p_dialog->popup_exclusive(EditorNode::get_singleton(), p_screen_rect);
234}
235
236void EditorInterface::popup_dialog_centered(Window *p_dialog, const Size2i &p_minsize) {
237 p_dialog->popup_exclusive_centered(EditorNode::get_singleton(), p_minsize);
238}
239
240void EditorInterface::popup_dialog_centered_ratio(Window *p_dialog, float p_ratio) {
241 p_dialog->popup_exclusive_centered_ratio(EditorNode::get_singleton(), p_ratio);
242}
243
244void EditorInterface::popup_dialog_centered_clamped(Window *p_dialog, const Size2i &p_size, float p_fallback_ratio) {
245 p_dialog->popup_exclusive_centered_clamped(EditorNode::get_singleton(), p_size, p_fallback_ratio);
246}
247
248String EditorInterface::get_current_feature_profile() const {
249 return EditorFeatureProfileManager::get_singleton()->get_current_profile_name();
250}
251
252void EditorInterface::set_current_feature_profile(const String &p_profile_name) {
253 EditorFeatureProfileManager::get_singleton()->set_current_profile(p_profile_name, true);
254}
255
256// Editor docks.
257
258FileSystemDock *EditorInterface::get_file_system_dock() const {
259 return FileSystemDock::get_singleton();
260}
261
262void EditorInterface::select_file(const String &p_file) {
263 FileSystemDock::get_singleton()->select_file(p_file);
264}
265
266Vector<String> EditorInterface::get_selected_paths() const {
267 return FileSystemDock::get_singleton()->get_selected_paths();
268}
269
270String EditorInterface::get_current_path() const {
271 return FileSystemDock::get_singleton()->get_current_path();
272}
273
274String EditorInterface::get_current_directory() const {
275 return FileSystemDock::get_singleton()->get_current_directory();
276}
277
278EditorInspector *EditorInterface::get_inspector() const {
279 return InspectorDock::get_inspector_singleton();
280}
281
282// Object/Resource/Node editing.
283
284void EditorInterface::inspect_object(Object *p_obj, const String &p_for_property, bool p_inspector_only) {
285 EditorNode::get_singleton()->push_item(p_obj, p_for_property, p_inspector_only);
286}
287
288void EditorInterface::edit_resource(const Ref<Resource> &p_resource) {
289 EditorNode::get_singleton()->edit_resource(p_resource);
290}
291
292void EditorInterface::edit_node(Node *p_node) {
293 EditorNode::get_singleton()->edit_node(p_node);
294}
295
296void EditorInterface::edit_script(const Ref<Script> &p_script, int p_line, int p_col, bool p_grab_focus) {
297 ScriptEditor::get_singleton()->edit(p_script, p_line - 1, p_col - 1, p_grab_focus);
298}
299
300void EditorInterface::open_scene_from_path(const String &scene_path) {
301 if (EditorNode::get_singleton()->is_changing_scene()) {
302 return;
303 }
304
305 EditorNode::get_singleton()->open_request(scene_path);
306}
307
308void EditorInterface::reload_scene_from_path(const String &scene_path) {
309 if (EditorNode::get_singleton()->is_changing_scene()) {
310 return;
311 }
312
313 EditorNode::get_singleton()->reload_scene(scene_path);
314}
315
316Node *EditorInterface::get_edited_scene_root() const {
317 return EditorNode::get_singleton()->get_edited_scene();
318}
319
320PackedStringArray EditorInterface::get_open_scenes() const {
321 PackedStringArray ret;
322 Vector<EditorData::EditedScene> scenes = EditorNode::get_editor_data().get_edited_scenes();
323
324 int scns_amount = scenes.size();
325 for (int idx_scn = 0; idx_scn < scns_amount; idx_scn++) {
326 if (scenes[idx_scn].root == nullptr) {
327 continue;
328 }
329 ret.push_back(scenes[idx_scn].root->get_scene_file_path());
330 }
331 return ret;
332}
333
334Error EditorInterface::save_scene() {
335 if (!get_edited_scene_root()) {
336 return ERR_CANT_CREATE;
337 }
338 if (get_edited_scene_root()->get_scene_file_path().is_empty()) {
339 return ERR_CANT_CREATE;
340 }
341
342 save_scene_as(get_edited_scene_root()->get_scene_file_path());
343 return OK;
344}
345
346void EditorInterface::save_scene_as(const String &p_scene, bool p_with_preview) {
347 EditorNode::get_singleton()->save_scene_to_path(p_scene, p_with_preview);
348}
349
350void EditorInterface::mark_scene_as_unsaved() {
351 EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(EditorNode::get_editor_data().get_current_edited_scene_history_id());
352}
353
354void EditorInterface::save_all_scenes() {
355 EditorNode::get_singleton()->save_all_scenes();
356}
357
358// Scene playback.
359
360void EditorInterface::play_main_scene() {
361 EditorRunBar::get_singleton()->play_main_scene();
362}
363
364void EditorInterface::play_current_scene() {
365 EditorRunBar::get_singleton()->play_current_scene();
366}
367
368void EditorInterface::play_custom_scene(const String &scene_path) {
369 EditorRunBar::get_singleton()->play_custom_scene(scene_path);
370}
371
372void EditorInterface::stop_playing_scene() {
373 EditorRunBar::get_singleton()->stop_playing();
374}
375
376bool EditorInterface::is_playing_scene() const {
377 return EditorRunBar::get_singleton()->is_playing();
378}
379
380String EditorInterface::get_playing_scene() const {
381 return EditorRunBar::get_singleton()->get_playing_scene();
382}
383
384void EditorInterface::set_movie_maker_enabled(bool p_enabled) {
385 EditorRunBar::get_singleton()->set_movie_maker_enabled(p_enabled);
386}
387
388bool EditorInterface::is_movie_maker_enabled() const {
389 return EditorRunBar::get_singleton()->is_movie_maker_enabled();
390}
391
392// Base.
393
394void EditorInterface::_bind_methods() {
395 ClassDB::bind_method(D_METHOD("restart_editor", "save"), &EditorInterface::restart_editor, DEFVAL(true));
396
397 // Editor tools.
398
399 ClassDB::bind_method(D_METHOD("get_command_palette"), &EditorInterface::get_command_palette);
400 ClassDB::bind_method(D_METHOD("get_resource_filesystem"), &EditorInterface::get_resource_file_system);
401 ClassDB::bind_method(D_METHOD("get_editor_paths"), &EditorInterface::get_editor_paths);
402 ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorInterface::get_resource_previewer);
403 ClassDB::bind_method(D_METHOD("get_selection"), &EditorInterface::get_selection);
404 ClassDB::bind_method(D_METHOD("get_editor_settings"), &EditorInterface::get_editor_settings);
405
406 ClassDB::bind_method(D_METHOD("make_mesh_previews", "meshes", "preview_size"), &EditorInterface::_make_mesh_previews);
407
408 ClassDB::bind_method(D_METHOD("set_plugin_enabled", "plugin", "enabled"), &EditorInterface::set_plugin_enabled);
409 ClassDB::bind_method(D_METHOD("is_plugin_enabled", "plugin"), &EditorInterface::is_plugin_enabled);
410
411 // Editor GUI.
412
413 ClassDB::bind_method(D_METHOD("get_editor_theme"), &EditorInterface::get_editor_theme);
414 ClassDB::bind_method(D_METHOD("get_base_control"), &EditorInterface::get_base_control);
415 ClassDB::bind_method(D_METHOD("get_editor_main_screen"), &EditorInterface::get_editor_main_screen);
416 ClassDB::bind_method(D_METHOD("get_script_editor"), &EditorInterface::get_script_editor);
417
418 ClassDB::bind_method(D_METHOD("set_main_screen_editor", "name"), &EditorInterface::set_main_screen_editor);
419 ClassDB::bind_method(D_METHOD("set_distraction_free_mode", "enter"), &EditorInterface::set_distraction_free_mode);
420 ClassDB::bind_method(D_METHOD("is_distraction_free_mode_enabled"), &EditorInterface::is_distraction_free_mode_enabled);
421
422 ClassDB::bind_method(D_METHOD("get_editor_scale"), &EditorInterface::get_editor_scale);
423
424 ClassDB::bind_method(D_METHOD("popup_dialog", "dialog", "rect"), &EditorInterface::popup_dialog, DEFVAL(Rect2i()));
425 ClassDB::bind_method(D_METHOD("popup_dialog_centered", "dialog", "minsize"), &EditorInterface::popup_dialog_centered, DEFVAL(Size2i()));
426 ClassDB::bind_method(D_METHOD("popup_dialog_centered_ratio", "dialog", "ratio"), &EditorInterface::popup_dialog_centered_ratio, DEFVAL(0.8));
427 ClassDB::bind_method(D_METHOD("popup_dialog_centered_clamped", "dialog", "minsize", "fallback_ratio"), &EditorInterface::popup_dialog_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
428
429 ClassDB::bind_method(D_METHOD("get_current_feature_profile"), &EditorInterface::get_current_feature_profile);
430 ClassDB::bind_method(D_METHOD("set_current_feature_profile", "profile_name"), &EditorInterface::set_current_feature_profile);
431
432 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "distraction_free_mode"), "set_distraction_free_mode", "is_distraction_free_mode_enabled");
433
434 // Editor docks.
435
436 ClassDB::bind_method(D_METHOD("get_file_system_dock"), &EditorInterface::get_file_system_dock);
437 ClassDB::bind_method(D_METHOD("select_file", "file"), &EditorInterface::select_file);
438 ClassDB::bind_method(D_METHOD("get_selected_paths"), &EditorInterface::get_selected_paths);
439 ClassDB::bind_method(D_METHOD("get_current_path"), &EditorInterface::get_current_path);
440 ClassDB::bind_method(D_METHOD("get_current_directory"), &EditorInterface::get_current_directory);
441
442 ClassDB::bind_method(D_METHOD("get_inspector"), &EditorInterface::get_inspector);
443
444 // Object/Resource/Node editing.
445
446 ClassDB::bind_method(D_METHOD("inspect_object", "object", "for_property", "inspector_only"), &EditorInterface::inspect_object, DEFVAL(String()), DEFVAL(false));
447
448 ClassDB::bind_method(D_METHOD("edit_resource", "resource"), &EditorInterface::edit_resource);
449 ClassDB::bind_method(D_METHOD("edit_node", "node"), &EditorInterface::edit_node);
450 ClassDB::bind_method(D_METHOD("edit_script", "script", "line", "column", "grab_focus"), &EditorInterface::edit_script, DEFVAL(-1), DEFVAL(0), DEFVAL(true));
451 ClassDB::bind_method(D_METHOD("open_scene_from_path", "scene_filepath"), &EditorInterface::open_scene_from_path);
452 ClassDB::bind_method(D_METHOD("reload_scene_from_path", "scene_filepath"), &EditorInterface::reload_scene_from_path);
453
454 ClassDB::bind_method(D_METHOD("get_open_scenes"), &EditorInterface::get_open_scenes);
455 ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorInterface::get_edited_scene_root);
456
457 ClassDB::bind_method(D_METHOD("save_scene"), &EditorInterface::save_scene);
458 ClassDB::bind_method(D_METHOD("save_scene_as", "path", "with_preview"), &EditorInterface::save_scene_as, DEFVAL(true));
459 ClassDB::bind_method(D_METHOD("save_all_scenes"), &EditorInterface::save_all_scenes);
460
461 ClassDB::bind_method(D_METHOD("mark_scene_as_unsaved"), &EditorInterface::mark_scene_as_unsaved);
462
463 // Scene playback.
464
465 ClassDB::bind_method(D_METHOD("play_main_scene"), &EditorInterface::play_main_scene);
466 ClassDB::bind_method(D_METHOD("play_current_scene"), &EditorInterface::play_current_scene);
467 ClassDB::bind_method(D_METHOD("play_custom_scene", "scene_filepath"), &EditorInterface::play_custom_scene);
468 ClassDB::bind_method(D_METHOD("stop_playing_scene"), &EditorInterface::stop_playing_scene);
469 ClassDB::bind_method(D_METHOD("is_playing_scene"), &EditorInterface::is_playing_scene);
470 ClassDB::bind_method(D_METHOD("get_playing_scene"), &EditorInterface::get_playing_scene);
471
472 ClassDB::bind_method(D_METHOD("set_movie_maker_enabled", "enabled"), &EditorInterface::set_movie_maker_enabled);
473 ClassDB::bind_method(D_METHOD("is_movie_maker_enabled"), &EditorInterface::is_movie_maker_enabled);
474
475 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_maker_enabled"), "set_movie_maker_enabled", "is_movie_maker_enabled");
476}
477
478void EditorInterface::create() {
479 memnew(EditorInterface);
480}
481
482void EditorInterface::free() {
483 ERR_FAIL_NULL(singleton);
484 memdelete(singleton);
485}
486
487EditorInterface::EditorInterface() {
488 ERR_FAIL_COND(singleton != nullptr);
489 singleton = this;
490}
491