editor_run_bar.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**************************************************************************/
  2. /* editor_run_bar.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_run_bar.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/debugger/editor_debugger_node.h"
  33. #include "editor/editor_command_palette.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_run_native.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/gui/editor_quick_open_dialog.h"
  39. #include "scene/gui/box_container.h"
  40. #include "scene/gui/button.h"
  41. #include "scene/gui/panel_container.h"
  42. EditorRunBar *EditorRunBar::singleton = nullptr;
  43. void EditorRunBar::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_POSTINITIALIZE: {
  46. _reset_play_buttons();
  47. } break;
  48. case NOTIFICATION_THEME_CHANGED: {
  49. _update_play_buttons();
  50. pause_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
  51. stop_button->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
  52. if (is_movie_maker_enabled()) {
  53. main_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("LaunchPadMovieMode"), EditorStringName(EditorStyles)));
  54. write_movie_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("MovieWriterButtonPressed"), EditorStringName(EditorStyles)));
  55. } else {
  56. main_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("LaunchPadNormal"), EditorStringName(EditorStyles)));
  57. write_movie_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("MovieWriterButtonNormal"), EditorStringName(EditorStyles)));
  58. }
  59. write_movie_button->set_button_icon(get_editor_theme_icon(SNAME("MainMovieWrite")));
  60. // This button behaves differently, so color it as such.
  61. write_movie_button->begin_bulk_theme_override();
  62. write_movie_button->add_theme_color_override("icon_normal_color", get_theme_color(SNAME("movie_writer_icon_normal"), EditorStringName(EditorStyles)));
  63. write_movie_button->add_theme_color_override("icon_pressed_color", get_theme_color(SNAME("movie_writer_icon_pressed"), EditorStringName(EditorStyles)));
  64. write_movie_button->add_theme_color_override("icon_hover_color", get_theme_color(SNAME("movie_writer_icon_hover"), EditorStringName(EditorStyles)));
  65. write_movie_button->add_theme_color_override("icon_hover_pressed_color", get_theme_color(SNAME("movie_writer_icon_hover_pressed"), EditorStringName(EditorStyles)));
  66. write_movie_button->end_bulk_theme_override();
  67. } break;
  68. }
  69. }
  70. void EditorRunBar::_reset_play_buttons() {
  71. play_button->set_pressed(false);
  72. play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  73. play_button->set_tooltip_text(TTR("Play the project."));
  74. play_scene_button->set_pressed(false);
  75. play_scene_button->set_button_icon(get_editor_theme_icon(SNAME("PlayScene")));
  76. play_scene_button->set_tooltip_text(TTR("Play the edited scene."));
  77. play_custom_scene_button->set_pressed(false);
  78. play_custom_scene_button->set_button_icon(get_editor_theme_icon(SNAME("PlayCustom")));
  79. play_custom_scene_button->set_tooltip_text(TTR("Play a custom scene."));
  80. }
  81. void EditorRunBar::_update_play_buttons() {
  82. _reset_play_buttons();
  83. if (!is_playing()) {
  84. return;
  85. }
  86. Button *active_button = nullptr;
  87. if (current_mode == RUN_CURRENT) {
  88. active_button = play_scene_button;
  89. } else if (current_mode == RUN_CUSTOM) {
  90. active_button = play_custom_scene_button;
  91. } else {
  92. active_button = play_button;
  93. }
  94. if (active_button) {
  95. active_button->set_pressed(true);
  96. active_button->set_button_icon(get_editor_theme_icon(SNAME("Reload")));
  97. active_button->set_tooltip_text(TTR("Reload the played scene."));
  98. }
  99. }
  100. void EditorRunBar::_write_movie_toggled(bool p_enabled) {
  101. if (p_enabled) {
  102. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("LaunchPadMovieMode"), EditorStringName(EditorStyles)));
  103. write_movie_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("MovieWriterButtonPressed"), EditorStringName(EditorStyles)));
  104. } else {
  105. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("LaunchPadNormal"), EditorStringName(EditorStyles)));
  106. write_movie_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("MovieWriterButtonNormal"), EditorStringName(EditorStyles)));
  107. }
  108. }
  109. void EditorRunBar::_quick_run_selected(const String &p_file_path) {
  110. play_custom_scene(p_file_path);
  111. }
  112. void EditorRunBar::_play_custom_pressed() {
  113. if (editor_run.get_status() == EditorRun::STATUS_STOP || current_mode != RunMode::RUN_CUSTOM) {
  114. stop_playing();
  115. EditorNode::get_singleton()->get_quick_open_dialog()->popup_dialog({ "PackedScene" }, callable_mp(this, &EditorRunBar::_quick_run_selected));
  116. play_custom_scene_button->set_pressed(false);
  117. } else {
  118. // Reload if already running a custom scene.
  119. String last_custom_scene = run_custom_filename; // This is necessary to have a copy of the string.
  120. play_custom_scene(last_custom_scene);
  121. }
  122. }
  123. void EditorRunBar::_play_current_pressed() {
  124. if (editor_run.get_status() == EditorRun::STATUS_STOP || current_mode != RunMode::RUN_CURRENT) {
  125. play_current_scene();
  126. } else {
  127. // Reload if already running the current scene.
  128. play_current_scene(true);
  129. }
  130. }
  131. void EditorRunBar::_run_scene(const String &p_scene_path) {
  132. ERR_FAIL_COND_MSG(current_mode == RUN_CUSTOM && p_scene_path.is_empty(), "Attempting to run a custom scene with an empty path.");
  133. if (editor_run.get_status() == EditorRun::STATUS_PLAY) {
  134. return;
  135. }
  136. _reset_play_buttons();
  137. String write_movie_file;
  138. if (is_movie_maker_enabled()) {
  139. if (current_mode == RUN_CURRENT) {
  140. Node *scene_root = nullptr;
  141. if (p_scene_path.is_empty()) {
  142. scene_root = get_tree()->get_edited_scene_root();
  143. } else {
  144. int scene_index = EditorNode::get_editor_data().get_edited_scene_from_path(p_scene_path);
  145. if (scene_index >= 0) {
  146. scene_root = EditorNode::get_editor_data().get_edited_scene_root(scene_index);
  147. }
  148. }
  149. if (scene_root && scene_root->has_meta("movie_file")) {
  150. // If the scene file has a movie_file metadata set, use this as file.
  151. // Quick workaround if you want to have multiple scenes that write to
  152. // multiple movies.
  153. write_movie_file = scene_root->get_meta("movie_file");
  154. }
  155. }
  156. if (write_movie_file.is_empty()) {
  157. write_movie_file = GLOBAL_GET("editor/movie_writer/movie_file");
  158. }
  159. if (write_movie_file.is_empty()) {
  160. // TODO: Provide options to directly resolve the issue with a custom dialog.
  161. EditorNode::get_singleton()->show_accept(TTR("Movie Maker mode is enabled, but no movie file path has been specified.\nA default movie file path can be specified in the project settings under the Editor > Movie Writer category.\nAlternatively, for running single scenes, a `movie_file` string metadata can be added to the root node,\nspecifying the path to a movie file that will be used when recording that scene."), TTR("OK"));
  162. return;
  163. }
  164. }
  165. String run_filename;
  166. switch (current_mode) {
  167. case RUN_CUSTOM: {
  168. run_filename = p_scene_path;
  169. run_custom_filename = run_filename;
  170. } break;
  171. case RUN_CURRENT: {
  172. if (!p_scene_path.is_empty()) {
  173. run_filename = p_scene_path;
  174. run_current_filename = run_filename;
  175. break;
  176. }
  177. Node *scene_root = get_tree()->get_edited_scene_root();
  178. if (!scene_root) {
  179. EditorNode::get_singleton()->show_accept(TTR("There is no defined scene to run."), TTR("OK"));
  180. return;
  181. }
  182. if (scene_root->get_scene_file_path().is_empty()) {
  183. EditorNode::get_singleton()->save_before_run();
  184. return;
  185. }
  186. run_filename = scene_root->get_scene_file_path();
  187. run_current_filename = run_filename;
  188. } break;
  189. default: {
  190. if (!EditorNode::get_singleton()->ensure_main_scene(false)) {
  191. return;
  192. }
  193. run_filename = GLOBAL_GET("application/run/main_scene");
  194. } break;
  195. }
  196. EditorNode::get_singleton()->try_autosave();
  197. if (!EditorNode::get_singleton()->call_build()) {
  198. return;
  199. }
  200. EditorDebuggerNode::get_singleton()->start();
  201. Error error = editor_run.run(run_filename, write_movie_file);
  202. if (error != OK) {
  203. EditorDebuggerNode::get_singleton()->stop();
  204. EditorNode::get_singleton()->show_accept(TTR("Could not start subprocess(es)!"), TTR("OK"));
  205. return;
  206. }
  207. _update_play_buttons();
  208. stop_button->set_disabled(false);
  209. emit_signal(SNAME("play_pressed"));
  210. }
  211. void EditorRunBar::_run_native(const Ref<EditorExportPreset> &p_preset) {
  212. EditorNode::get_singleton()->try_autosave();
  213. if (run_native->is_deploy_debug_remote_enabled()) {
  214. stop_playing();
  215. if (!EditorNode::get_singleton()->call_build()) {
  216. return; // Build failed.
  217. }
  218. EditorDebuggerNode::get_singleton()->start(p_preset->get_platform()->get_debug_protocol());
  219. emit_signal(SNAME("play_pressed"));
  220. editor_run.run_native_notify();
  221. }
  222. }
  223. void EditorRunBar::play_main_scene(bool p_from_native) {
  224. if (p_from_native) {
  225. run_native->resume_run_native();
  226. } else {
  227. stop_playing();
  228. current_mode = RunMode::RUN_MAIN;
  229. _run_scene();
  230. }
  231. }
  232. void EditorRunBar::play_current_scene(bool p_reload) {
  233. String last_current_scene = run_current_filename; // This is necessary to have a copy of the string.
  234. EditorNode::get_singleton()->save_default_environment();
  235. stop_playing();
  236. current_mode = RunMode::RUN_CURRENT;
  237. if (p_reload) {
  238. _run_scene(last_current_scene);
  239. } else {
  240. _run_scene();
  241. }
  242. }
  243. void EditorRunBar::play_custom_scene(const String &p_custom) {
  244. stop_playing();
  245. current_mode = RunMode::RUN_CUSTOM;
  246. _run_scene(p_custom);
  247. }
  248. void EditorRunBar::stop_playing() {
  249. if (editor_run.get_status() == EditorRun::STATUS_STOP) {
  250. return;
  251. }
  252. current_mode = RunMode::STOPPED;
  253. editor_run.stop();
  254. EditorDebuggerNode::get_singleton()->stop();
  255. run_custom_filename.clear();
  256. run_current_filename.clear();
  257. stop_button->set_pressed(false);
  258. stop_button->set_disabled(true);
  259. _reset_play_buttons();
  260. emit_signal(SNAME("stop_pressed"));
  261. }
  262. bool EditorRunBar::is_playing() const {
  263. EditorRun::Status status = editor_run.get_status();
  264. return (status == EditorRun::STATUS_PLAY || status == EditorRun::STATUS_PAUSED);
  265. }
  266. String EditorRunBar::get_playing_scene() const {
  267. String run_filename = editor_run.get_running_scene();
  268. if (run_filename.is_empty() && is_playing()) {
  269. run_filename = GLOBAL_GET("application/run/main_scene"); // Must be the main scene then.
  270. }
  271. return run_filename;
  272. }
  273. Error EditorRunBar::start_native_device(int p_device_id) {
  274. return run_native->start_run_native(p_device_id);
  275. }
  276. OS::ProcessID EditorRunBar::has_child_process(OS::ProcessID p_pid) const {
  277. return editor_run.has_child_process(p_pid);
  278. }
  279. void EditorRunBar::stop_child_process(OS::ProcessID p_pid) {
  280. if (!has_child_process(p_pid)) {
  281. return;
  282. }
  283. editor_run.stop_child_process(p_pid);
  284. if (!editor_run.get_child_process_count()) { // All children stopped. Closing.
  285. stop_playing();
  286. }
  287. }
  288. void EditorRunBar::set_movie_maker_enabled(bool p_enabled) {
  289. write_movie_button->set_pressed(p_enabled);
  290. }
  291. bool EditorRunBar::is_movie_maker_enabled() const {
  292. return write_movie_button->is_pressed();
  293. }
  294. HBoxContainer *EditorRunBar::get_buttons_container() {
  295. return main_hbox;
  296. }
  297. void EditorRunBar::_bind_methods() {
  298. ADD_SIGNAL(MethodInfo("play_pressed"));
  299. ADD_SIGNAL(MethodInfo("stop_pressed"));
  300. }
  301. EditorRunBar::EditorRunBar() {
  302. singleton = this;
  303. main_panel = memnew(PanelContainer);
  304. add_child(main_panel);
  305. main_hbox = memnew(HBoxContainer);
  306. main_panel->add_child(main_hbox);
  307. play_button = memnew(Button);
  308. main_hbox->add_child(play_button);
  309. play_button->set_theme_type_variation("RunBarButton");
  310. play_button->set_toggle_mode(true);
  311. play_button->set_focus_mode(Control::FOCUS_NONE);
  312. play_button->set_tooltip_text(TTR("Run the project's default scene."));
  313. play_button->connect(SceneStringName(pressed), callable_mp(this, &EditorRunBar::play_main_scene).bind(false));
  314. ED_SHORTCUT_AND_COMMAND("editor/run_project", TTR("Run Project"), Key::F5);
  315. ED_SHORTCUT_OVERRIDE("editor/run_project", "macos", KeyModifierMask::META | Key::B);
  316. play_button->set_shortcut(ED_GET_SHORTCUT("editor/run_project"));
  317. pause_button = memnew(Button);
  318. main_hbox->add_child(pause_button);
  319. pause_button->set_theme_type_variation("RunBarButton");
  320. pause_button->set_toggle_mode(true);
  321. pause_button->set_focus_mode(Control::FOCUS_NONE);
  322. pause_button->set_tooltip_text(TTR("Pause the running project's execution for debugging."));
  323. pause_button->set_disabled(true);
  324. ED_SHORTCUT("editor/pause_running_project", TTR("Pause Running Project"), Key::F7);
  325. ED_SHORTCUT_OVERRIDE("editor/pause_running_project", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::Y);
  326. pause_button->set_shortcut(ED_GET_SHORTCUT("editor/pause_running_project"));
  327. stop_button = memnew(Button);
  328. main_hbox->add_child(stop_button);
  329. stop_button->set_theme_type_variation("RunBarButton");
  330. stop_button->set_focus_mode(Control::FOCUS_NONE);
  331. stop_button->set_tooltip_text(TTR("Stop the currently running project."));
  332. stop_button->set_disabled(true);
  333. stop_button->connect(SceneStringName(pressed), callable_mp(this, &EditorRunBar::stop_playing));
  334. ED_SHORTCUT("editor/stop_running_project", TTR("Stop Running Project"), Key::F8);
  335. ED_SHORTCUT_OVERRIDE("editor/stop_running_project", "macos", KeyModifierMask::META | Key::PERIOD);
  336. stop_button->set_shortcut(ED_GET_SHORTCUT("editor/stop_running_project"));
  337. run_native = memnew(EditorRunNative);
  338. main_hbox->add_child(run_native);
  339. run_native->connect("native_run", callable_mp(this, &EditorRunBar::_run_native));
  340. play_scene_button = memnew(Button);
  341. main_hbox->add_child(play_scene_button);
  342. play_scene_button->set_theme_type_variation("RunBarButton");
  343. play_scene_button->set_toggle_mode(true);
  344. play_scene_button->set_focus_mode(Control::FOCUS_NONE);
  345. play_scene_button->set_tooltip_text(TTR("Run the currently edited scene."));
  346. play_scene_button->connect(SceneStringName(pressed), callable_mp(this, &EditorRunBar::_play_current_pressed));
  347. ED_SHORTCUT_AND_COMMAND("editor/run_current_scene", TTR("Run Current Scene"), Key::F6);
  348. ED_SHORTCUT_OVERRIDE("editor/run_current_scene", "macos", KeyModifierMask::META | Key::R);
  349. play_scene_button->set_shortcut(ED_GET_SHORTCUT("editor/run_current_scene"));
  350. play_custom_scene_button = memnew(Button);
  351. main_hbox->add_child(play_custom_scene_button);
  352. play_custom_scene_button->set_theme_type_variation("RunBarButton");
  353. play_custom_scene_button->set_toggle_mode(true);
  354. play_custom_scene_button->set_focus_mode(Control::FOCUS_NONE);
  355. play_custom_scene_button->set_tooltip_text(TTR("Run a specific scene."));
  356. play_custom_scene_button->connect(SceneStringName(pressed), callable_mp(this, &EditorRunBar::_play_custom_pressed));
  357. ED_SHORTCUT_AND_COMMAND("editor/run_specific_scene", TTR("Run Specific Scene"), KeyModifierMask::CTRL | KeyModifierMask::SHIFT | Key::F5);
  358. ED_SHORTCUT_OVERRIDE("editor/run_specific_scene", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::R);
  359. play_custom_scene_button->set_shortcut(ED_GET_SHORTCUT("editor/run_specific_scene"));
  360. write_movie_panel = memnew(PanelContainer);
  361. main_hbox->add_child(write_movie_panel);
  362. write_movie_button = memnew(Button);
  363. write_movie_panel->add_child(write_movie_button);
  364. write_movie_button->set_theme_type_variation("RunBarButton");
  365. write_movie_button->set_toggle_mode(true);
  366. write_movie_button->set_pressed(false);
  367. write_movie_button->set_focus_mode(Control::FOCUS_NONE);
  368. write_movie_button->set_tooltip_text(TTR("Enable Movie Maker mode.\nThe project will run at stable FPS and the visual and audio output will be recorded to a video file."));
  369. write_movie_button->connect(SceneStringName(toggled), callable_mp(this, &EditorRunBar::_write_movie_toggled));
  370. }