editor_interface.cpp 28 KB

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