editor_interface.cpp 36 KB

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