editor_interface.cpp 38 KB

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