game_view_plugin.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /**************************************************************************/
  2. /* game_view_plugin.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 "game_view_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/debugger_marshalls.h"
  33. #include "core/string/translation_server.h"
  34. #include "editor/debugger/editor_debugger_node.h"
  35. #include "editor/debugger/script_editor_debugger.h"
  36. #include "editor/editor_feature_profile.h"
  37. #include "editor/editor_interface.h"
  38. #include "editor/editor_main_screen.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/editor_settings.h"
  41. #include "editor/editor_string_names.h"
  42. #include "editor/gui/editor_bottom_panel.h"
  43. #include "editor/gui/editor_run_bar.h"
  44. #include "editor/plugins/embedded_process.h"
  45. #include "editor/themes/editor_scale.h"
  46. #include "editor/window_wrapper.h"
  47. #include "scene/gui/button.h"
  48. #include "scene/gui/label.h"
  49. #include "scene/gui/menu_button.h"
  50. #include "scene/gui/panel.h"
  51. #include "scene/gui/separator.h"
  52. void GameViewDebugger::_session_started(Ref<EditorDebuggerSession> p_session) {
  53. if (!is_feature_enabled) {
  54. return;
  55. }
  56. Dictionary settings;
  57. settings["debugger/max_node_selection"] = EDITOR_GET("debugger/max_node_selection");
  58. settings["editors/panning/2d_editor_panning_scheme"] = EDITOR_GET("editors/panning/2d_editor_panning_scheme");
  59. settings["editors/panning/simple_panning"] = EDITOR_GET("editors/panning/simple_panning");
  60. settings["editors/panning/warped_mouse_panning"] = EDITOR_GET("editors/panning/warped_mouse_panning");
  61. settings["editors/panning/2d_editor_pan_speed"] = EDITOR_GET("editors/panning/2d_editor_pan_speed");
  62. settings["editors/polygon_editor/point_grab_radius"] = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  63. settings["canvas_item_editor/pan_view"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/pan_view"));
  64. settings["box_selection_fill_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("box_selection_fill_color"), EditorStringName(Editor));
  65. settings["box_selection_stroke_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor));
  66. settings["editors/3d/default_fov"] = EDITOR_GET("editors/3d/default_fov");
  67. settings["editors/3d/default_z_near"] = EDITOR_GET("editors/3d/default_z_near");
  68. settings["editors/3d/default_z_far"] = EDITOR_GET("editors/3d/default_z_far");
  69. settings["editors/3d/navigation/invert_x_axis"] = EDITOR_GET("editors/3d/navigation/invert_x_axis");
  70. settings["editors/3d/navigation/invert_y_axis"] = EDITOR_GET("editors/3d/navigation/invert_y_axis");
  71. settings["editors/3d/navigation/warped_mouse_panning"] = EDITOR_GET("editors/3d/navigation/warped_mouse_panning");
  72. settings["editors/3d/freelook/freelook_base_speed"] = EDITOR_GET("editors/3d/freelook/freelook_base_speed");
  73. settings["editors/3d/freelook/freelook_sensitivity"] = EDITOR_GET("editors/3d/freelook/freelook_sensitivity");
  74. settings["editors/3d/navigation_feel/orbit_sensitivity"] = EDITOR_GET("editors/3d/navigation_feel/orbit_sensitivity");
  75. settings["editors/3d/navigation_feel/translation_sensitivity"] = EDITOR_GET("editors/3d/navigation_feel/translation_sensitivity");
  76. settings["editors/3d/selection_box_color"] = EDITOR_GET("editors/3d/selection_box_color");
  77. settings["editors/3d/freelook/freelook_base_speed"] = EDITOR_GET("editors/3d/freelook/freelook_base_speed");
  78. Array setup_data;
  79. setup_data.append(settings);
  80. p_session->send_message("scene:runtime_node_select_setup", setup_data);
  81. Array type;
  82. type.append(node_type);
  83. p_session->send_message("scene:runtime_node_select_set_type", type);
  84. Array visible;
  85. visible.append(selection_visible);
  86. p_session->send_message("scene:runtime_node_select_set_visible", visible);
  87. Array mode;
  88. mode.append(select_mode);
  89. p_session->send_message("scene:runtime_node_select_set_mode", mode);
  90. Array mute_audio_data;
  91. mute_audio_data.append(mute_audio);
  92. p_session->send_message("scene:debug_mute_audio", mute_audio_data);
  93. Dictionary shortcut_settings;
  94. shortcut_settings["editor/suspend_resume_embedded_project"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("editor/suspend_resume_embedded_project"));
  95. shortcut_settings["editor/next_frame_embedded_project"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("editor/next_frame_embedded_project"));
  96. p_session->send_message("scene:setup_embedded_shortcuts", { shortcut_settings });
  97. emit_signal(SNAME("session_started"));
  98. }
  99. void GameViewDebugger::_session_stopped() {
  100. if (!is_feature_enabled) {
  101. return;
  102. }
  103. emit_signal(SNAME("session_stopped"));
  104. }
  105. void GameViewDebugger::set_suspend(bool p_enabled) {
  106. Array message;
  107. message.append(p_enabled);
  108. for (Ref<EditorDebuggerSession> &I : sessions) {
  109. if (I->is_active()) {
  110. I->send_message("scene:suspend_changed", message);
  111. }
  112. }
  113. }
  114. void GameViewDebugger::next_frame() {
  115. for (Ref<EditorDebuggerSession> &I : sessions) {
  116. if (I->is_active()) {
  117. I->send_message("scene:next_frame", Array());
  118. }
  119. }
  120. }
  121. void GameViewDebugger::set_node_type(int p_type) {
  122. node_type = p_type;
  123. Array message;
  124. message.append(p_type);
  125. for (Ref<EditorDebuggerSession> &I : sessions) {
  126. if (I->is_active()) {
  127. I->send_message("scene:runtime_node_select_set_type", message);
  128. }
  129. }
  130. }
  131. void GameViewDebugger::set_selection_visible(bool p_visible) {
  132. selection_visible = p_visible;
  133. Array message;
  134. message.append(p_visible);
  135. for (Ref<EditorDebuggerSession> &I : sessions) {
  136. if (I->is_active()) {
  137. I->send_message("scene:runtime_node_select_set_visible", message);
  138. }
  139. }
  140. }
  141. void GameViewDebugger::set_select_mode(int p_mode) {
  142. select_mode = p_mode;
  143. Array message;
  144. message.append(p_mode);
  145. for (Ref<EditorDebuggerSession> &I : sessions) {
  146. if (I->is_active()) {
  147. I->send_message("scene:runtime_node_select_set_mode", message);
  148. }
  149. }
  150. }
  151. void GameViewDebugger::set_debug_mute_audio(bool p_enabled) {
  152. mute_audio = p_enabled;
  153. EditorDebuggerNode::get_singleton()->set_debug_mute_audio(p_enabled);
  154. }
  155. void GameViewDebugger::set_camera_override(bool p_enabled) {
  156. EditorDebuggerNode::get_singleton()->set_camera_override(p_enabled ? camera_override_mode : EditorDebuggerNode::OVERRIDE_NONE);
  157. }
  158. void GameViewDebugger::set_camera_manipulate_mode(EditorDebuggerNode::CameraOverride p_mode) {
  159. camera_override_mode = p_mode;
  160. if (EditorDebuggerNode::get_singleton()->get_camera_override() != EditorDebuggerNode::OVERRIDE_NONE) {
  161. set_camera_override(true);
  162. }
  163. }
  164. void GameViewDebugger::reset_camera_2d_position() {
  165. for (Ref<EditorDebuggerSession> &I : sessions) {
  166. if (I->is_active()) {
  167. I->send_message("scene:runtime_node_select_reset_camera_2d", Array());
  168. }
  169. }
  170. }
  171. void GameViewDebugger::reset_camera_3d_position() {
  172. for (Ref<EditorDebuggerSession> &I : sessions) {
  173. if (I->is_active()) {
  174. I->send_message("scene:runtime_node_select_reset_camera_3d", Array());
  175. }
  176. }
  177. }
  178. void GameViewDebugger::setup_session(int p_session_id) {
  179. Ref<EditorDebuggerSession> session = get_session(p_session_id);
  180. ERR_FAIL_COND(session.is_null());
  181. sessions.append(session);
  182. session->connect("started", callable_mp(this, &GameViewDebugger::_session_started).bind(session));
  183. session->connect("stopped", callable_mp(this, &GameViewDebugger::_session_stopped));
  184. }
  185. void GameViewDebugger::_feature_profile_changed() {
  186. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  187. is_feature_enabled = profile.is_null() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_GAME);
  188. }
  189. void GameViewDebugger::_bind_methods() {
  190. ADD_SIGNAL(MethodInfo("session_started"));
  191. ADD_SIGNAL(MethodInfo("session_stopped"));
  192. }
  193. GameViewDebugger::GameViewDebugger() {
  194. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &GameViewDebugger::_feature_profile_changed));
  195. }
  196. ///////
  197. void GameView::_sessions_changed() {
  198. // The debugger session's `session_started/stopped` signal can be unreliable, so count it manually.
  199. active_sessions = 0;
  200. Array sessions = debugger->get_sessions();
  201. for (int i = 0; i < sessions.size(); i++) {
  202. if (Object::cast_to<EditorDebuggerSession>(sessions[i])->is_active()) {
  203. active_sessions++;
  204. }
  205. }
  206. _update_debugger_buttons();
  207. #ifdef MACOS_ENABLED
  208. if (!embedded_script_debugger || !embedded_script_debugger->is_session_active() || embedded_script_debugger->get_remote_pid() != embedded_process->get_embedded_pid()) {
  209. _attach_script_debugger();
  210. }
  211. #else
  212. if (embedded_process->is_embedding_completed()) {
  213. if (!embedded_script_debugger || !embedded_script_debugger->is_session_active() || embedded_script_debugger->get_remote_pid() != embedded_process->get_embedded_pid()) {
  214. _attach_script_debugger();
  215. }
  216. }
  217. #endif
  218. }
  219. void GameView::_instance_starting_static(int p_idx, List<String> &r_arguments) {
  220. ERR_FAIL_NULL(singleton);
  221. singleton->_instance_starting(p_idx, r_arguments);
  222. }
  223. void GameView::_instance_starting(int p_idx, List<String> &r_arguments) {
  224. if (!is_feature_enabled) {
  225. return;
  226. }
  227. if (p_idx == 0 && embed_on_play && make_floating_on_play && window_wrapper->is_window_available() && !window_wrapper->get_window_enabled() && _get_embed_available() == EMBED_AVAILABLE) {
  228. // Set the Floating Window default title. Always considered in DEBUG mode, same as in Window::set_title.
  229. String appname = GLOBAL_GET("application/config/name");
  230. appname = vformat("%s (DEBUG)", TranslationServer::get_singleton()->translate(appname));
  231. window_wrapper->set_window_title(appname);
  232. _show_update_window_wrapper();
  233. embedded_process->grab_focus();
  234. }
  235. _update_arguments_for_instance(p_idx, r_arguments);
  236. }
  237. void GameView::_show_update_window_wrapper() {
  238. EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
  239. Point2 position = floating_window_rect.position;
  240. Size2i size = floating_window_rect.size;
  241. int screen = floating_window_screen;
  242. // Obtain the size around the embedded process control. Usually, the difference between the game view's get_size
  243. // and the embedded control should work. However, when the control is hidden and has never been displayed,
  244. // the size of the embedded control is not calculated.
  245. Size2 old_min_size = embedded_process->get_custom_minimum_size();
  246. embedded_process->set_custom_minimum_size(Size2i());
  247. Size2 embedded_process_min_size = get_minimum_size();
  248. Size2 wrapped_margins_size = window_wrapper->get_margins_size();
  249. Size2 wrapped_min_size = window_wrapper->get_minimum_size();
  250. Point2 offset_embedded_process = embedded_process->get_global_position() - get_global_position();
  251. // On the first startup, the global position of the embedded process control is invalid because it was
  252. // never displayed. We will calculate it manually using the minimum size of the window.
  253. if (offset_embedded_process == Point2()) {
  254. offset_embedded_process.y = wrapped_min_size.y;
  255. }
  256. offset_embedded_process.x += embedded_process->get_margin_size(SIDE_LEFT);
  257. offset_embedded_process.y += embedded_process->get_margin_size(SIDE_TOP);
  258. offset_embedded_process += window_wrapper->get_margins_top_left();
  259. embedded_process->set_custom_minimum_size(old_min_size);
  260. Point2 size_diff_embedded_process = Point2(0, embedded_process_min_size.y) + embedded_process->get_margins_size();
  261. if (placement.position != Point2i(INT_MAX, INT_MAX)) {
  262. position = placement.position - offset_embedded_process;
  263. screen = placement.screen;
  264. }
  265. if (placement.size != Size2i()) {
  266. size = placement.size + size_diff_embedded_process + wrapped_margins_size;
  267. }
  268. window_wrapper->restore_window_from_saved_position(Rect2(position, size), screen, Rect2i());
  269. }
  270. void GameView::_play_pressed() {
  271. if (!is_feature_enabled) {
  272. return;
  273. }
  274. OS::ProcessID current_process_id = EditorRunBar::get_singleton()->get_current_process();
  275. if (current_process_id == 0) {
  276. return;
  277. }
  278. if (!window_wrapper->get_window_enabled()) {
  279. screen_index_before_start = EditorNode::get_singleton()->get_editor_main_screen()->get_selected_index();
  280. }
  281. if (embed_on_play && _get_embed_available() == EMBED_AVAILABLE) {
  282. // It's important to disable the low power mode when unfocused because otherwise
  283. // the button in the editor are not responsive and if the user moves the mouse quickly,
  284. // the mouse clicks are not registered.
  285. EditorNode::get_singleton()->set_unfocused_low_processor_usage_mode_enabled(false);
  286. _update_embed_window_size();
  287. if (!window_wrapper->get_window_enabled()) {
  288. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_GAME);
  289. // Reset the normal size of the bottom panel when fully expanded.
  290. EditorNode::get_singleton()->get_bottom_panel()->set_expanded(false);
  291. embedded_process->grab_focus();
  292. }
  293. embedded_process->embed_process(current_process_id);
  294. _update_ui();
  295. }
  296. }
  297. void GameView::_stop_pressed() {
  298. if (!is_feature_enabled) {
  299. return;
  300. }
  301. _detach_script_debugger();
  302. paused = false;
  303. EditorNode::get_singleton()->set_unfocused_low_processor_usage_mode_enabled(true);
  304. embedded_process->reset();
  305. _update_ui();
  306. if (window_wrapper->get_window_enabled()) {
  307. window_wrapper->set_window_enabled(false);
  308. }
  309. if (screen_index_before_start >= 0 && EditorNode::get_singleton()->get_editor_main_screen()->get_selected_index() == EditorMainScreen::EDITOR_GAME) {
  310. // We go back to the screen where the user was before starting the game.
  311. EditorNode::get_singleton()->get_editor_main_screen()->select(screen_index_before_start);
  312. }
  313. screen_index_before_start = -1;
  314. }
  315. void GameView::_embedding_completed() {
  316. #ifndef MACOS_ENABLED
  317. _attach_script_debugger();
  318. #endif
  319. _update_ui();
  320. if (make_floating_on_play) {
  321. get_window()->set_flag(Window::FLAG_ALWAYS_ON_TOP, bool(GLOBAL_GET("display/window/size/always_on_top")));
  322. }
  323. }
  324. void GameView::_embedding_failed() {
  325. state_label->set_text(TTR("Connection impossible to the game process."));
  326. }
  327. void GameView::_embedded_process_updated() {
  328. const Rect2i game_rect = embedded_process->get_screen_embedded_window_rect();
  329. game_size_label->set_text(vformat("%dx%d", game_rect.size.x, game_rect.size.y));
  330. }
  331. void GameView::_embedded_process_focused() {
  332. if (embed_on_play && !window_wrapper->get_window_enabled()) {
  333. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_GAME);
  334. }
  335. }
  336. void GameView::_editor_or_project_settings_changed() {
  337. // Update the window size and aspect ratio.
  338. _update_embed_window_size();
  339. if (window_wrapper->get_window_enabled()) {
  340. _show_update_window_wrapper();
  341. if (embedded_process->is_embedding_completed()) {
  342. embedded_process->queue_update_embedded_process();
  343. }
  344. }
  345. _update_ui();
  346. }
  347. void GameView::_update_debugger_buttons() {
  348. bool empty = active_sessions == 0;
  349. suspend_button->set_disabled(empty);
  350. camera_override_button->set_disabled(empty);
  351. PopupMenu *menu = camera_override_menu->get_popup();
  352. bool disable_camera_reset = empty || !camera_override_button->is_pressed() || !menu->is_item_checked(menu->get_item_index(CAMERA_MODE_INGAME));
  353. menu->set_item_disabled(CAMERA_RESET_2D, disable_camera_reset);
  354. menu->set_item_disabled(CAMERA_RESET_3D, disable_camera_reset);
  355. if (empty) {
  356. suspend_button->set_pressed(false);
  357. camera_override_button->set_pressed(false);
  358. }
  359. next_frame_button->set_disabled(!suspend_button->is_pressed());
  360. }
  361. void GameView::_handle_shortcut_requested(int p_embed_action) {
  362. switch (p_embed_action) {
  363. case ScriptEditorDebugger::EMBED_SUSPEND_TOGGLE: {
  364. _toggle_suspend_button();
  365. } break;
  366. case ScriptEditorDebugger::EMBED_NEXT_FRAME: {
  367. debugger->next_frame();
  368. } break;
  369. }
  370. }
  371. void GameView::_toggle_suspend_button() {
  372. const bool new_pressed = !suspend_button->is_pressed();
  373. suspend_button->set_pressed(new_pressed);
  374. _suspend_button_toggled(new_pressed);
  375. }
  376. void GameView::_suspend_button_toggled(bool p_pressed) {
  377. _update_debugger_buttons();
  378. debugger->set_suspend(p_pressed);
  379. }
  380. void GameView::_node_type_pressed(int p_option) {
  381. RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)p_option;
  382. for (int i = 0; i < RuntimeNodeSelect::NODE_TYPE_MAX; i++) {
  383. node_type_button[i]->set_pressed_no_signal(i == type);
  384. }
  385. _update_debugger_buttons();
  386. debugger->set_node_type(type);
  387. }
  388. void GameView::_select_mode_pressed(int p_option) {
  389. RuntimeNodeSelect::SelectMode mode = (RuntimeNodeSelect::SelectMode)p_option;
  390. if (!select_mode_button[mode]->is_visible()) {
  391. return;
  392. }
  393. for (int i = 0; i < RuntimeNodeSelect::SELECT_MODE_MAX; i++) {
  394. select_mode_button[i]->set_pressed_no_signal(i == mode);
  395. }
  396. debugger->set_select_mode(mode);
  397. EditorSettings::get_singleton()->set_project_metadata("game_view", "select_mode", mode);
  398. }
  399. void GameView::_embed_options_menu_menu_id_pressed(int p_id) {
  400. switch (p_id) {
  401. case EMBED_RUN_GAME_EMBEDDED: {
  402. embed_on_play = !embed_on_play;
  403. int game_mode = EDITOR_GET("run/window_placement/game_embed_mode");
  404. if (game_mode == 0) { // Save only if not overridden by editor.
  405. EditorSettings::get_singleton()->set_project_metadata("game_view", "embed_on_play", embed_on_play);
  406. }
  407. } break;
  408. case EMBED_MAKE_FLOATING_ON_PLAY: {
  409. make_floating_on_play = !make_floating_on_play;
  410. int game_mode = EDITOR_GET("run/window_placement/game_embed_mode");
  411. if (game_mode == 0) { // Save only if not overridden by editor.
  412. EditorSettings::get_singleton()->set_project_metadata("game_view", "make_floating_on_play", make_floating_on_play);
  413. }
  414. } break;
  415. }
  416. _update_embed_menu_options();
  417. _update_ui();
  418. }
  419. void GameView::_size_mode_button_pressed(int size_mode) {
  420. embed_size_mode = (EmbedSizeMode)size_mode;
  421. EditorSettings::get_singleton()->set_project_metadata("game_view", "embed_size_mode", size_mode);
  422. _update_embed_menu_options();
  423. _update_embed_window_size();
  424. }
  425. GameView::EmbedAvailability GameView::_get_embed_available() {
  426. if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_EMBEDDING)) {
  427. return EMBED_NOT_AVAILABLE_FEATURE_NOT_SUPPORTED;
  428. }
  429. if (get_tree()->get_root()->is_embedding_subwindows()) {
  430. return EMBED_NOT_AVAILABLE_SINGLE_WINDOW_MODE;
  431. }
  432. String display_driver = GLOBAL_GET("display/display_server/driver");
  433. if (display_driver == "headless" || display_driver == "wayland") {
  434. return EMBED_NOT_AVAILABLE_PROJECT_DISPLAY_DRIVER;
  435. }
  436. EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
  437. if (placement.force_fullscreen) {
  438. return EMBED_NOT_AVAILABLE_FULLSCREEN;
  439. }
  440. if (placement.force_maximized) {
  441. return EMBED_NOT_AVAILABLE_MAXIMIZED;
  442. }
  443. DisplayServer::WindowMode window_mode = (DisplayServer::WindowMode)(GLOBAL_GET("display/window/size/mode").operator int());
  444. if (window_mode == DisplayServer::WindowMode::WINDOW_MODE_MINIMIZED) {
  445. return EMBED_NOT_AVAILABLE_MINIMIZED;
  446. }
  447. if (window_mode == DisplayServer::WindowMode::WINDOW_MODE_MAXIMIZED) {
  448. return EMBED_NOT_AVAILABLE_MAXIMIZED;
  449. }
  450. if (window_mode == DisplayServer::WindowMode::WINDOW_MODE_FULLSCREEN || window_mode == DisplayServer::WindowMode::WINDOW_MODE_EXCLUSIVE_FULLSCREEN) {
  451. return EMBED_NOT_AVAILABLE_FULLSCREEN;
  452. }
  453. return EMBED_AVAILABLE;
  454. }
  455. void GameView::_update_ui() {
  456. bool show_game_size = false;
  457. EmbedAvailability available = _get_embed_available();
  458. switch (available) {
  459. case EMBED_AVAILABLE:
  460. if (embedded_process->is_embedding_completed()) {
  461. state_label->set_text("");
  462. show_game_size = true;
  463. } else if (embedded_process->is_embedding_in_progress()) {
  464. state_label->set_text(TTR("Game starting..."));
  465. } else if (EditorRunBar::get_singleton()->is_playing()) {
  466. state_label->set_text(TTR("Game running not embedded."));
  467. } else if (embed_on_play) {
  468. state_label->set_text(TTR("Press play to start the game."));
  469. } else {
  470. state_label->set_text(TTR("Embedding is disabled."));
  471. }
  472. break;
  473. case EMBED_NOT_AVAILABLE_FEATURE_NOT_SUPPORTED:
  474. if (DisplayServer::get_singleton()->get_name() == "Wayland") {
  475. state_label->set_text(TTR("Game embedding not available on Wayland.\nWayland can be disabled in the Editor Settings (Run > Platforms > Linux/*BSD > Prefer Wayland)."));
  476. } else {
  477. state_label->set_text(TTR("Game embedding not available on your OS."));
  478. }
  479. break;
  480. case EMBED_NOT_AVAILABLE_PROJECT_DISPLAY_DRIVER:
  481. state_label->set_text(vformat(TTR("Game embedding not available for the Display Server: '%s'.\nDisplay Server can be modified in the Project Settings (Display > Display Server > Driver)."), GLOBAL_GET("display/display_server/driver")));
  482. break;
  483. case EMBED_NOT_AVAILABLE_MINIMIZED:
  484. state_label->set_text(TTR("Game embedding not available when the game starts minimized.") + "\n" + TTR("Consider overriding the window mode project setting with the editor feature tag to Windowed to use game embedding while leaving the exported project intact."));
  485. break;
  486. case EMBED_NOT_AVAILABLE_MAXIMIZED:
  487. state_label->set_text(TTR("Game embedding not available when the game starts maximized.") + "\n" + TTR("Consider overriding the window mode project setting with the editor feature tag to Windowed to use game embedding while leaving the exported project intact."));
  488. break;
  489. case EMBED_NOT_AVAILABLE_FULLSCREEN:
  490. state_label->set_text(TTR("Game embedding not available when the game starts in fullscreen.") + "\n" + TTR("Consider overriding the window mode project setting with the editor feature tag to Windowed to use game embedding while leaving the exported project intact."));
  491. break;
  492. case EMBED_NOT_AVAILABLE_SINGLE_WINDOW_MODE:
  493. state_label->set_text(TTR("Game embedding not available in single window mode."));
  494. break;
  495. }
  496. if (available == EMBED_AVAILABLE) {
  497. if (state_label->has_theme_color_override(SceneStringName(font_color))) {
  498. state_label->remove_theme_color_override(SceneStringName(font_color));
  499. }
  500. } else {
  501. state_label->add_theme_color_override(SceneStringName(font_color), state_label->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  502. }
  503. game_size_label->set_visible(show_game_size);
  504. }
  505. void GameView::_update_embed_menu_options() {
  506. bool is_multi_window = window_wrapper->is_window_available();
  507. PopupMenu *menu = embed_options_menu->get_popup();
  508. menu->set_item_checked(menu->get_item_index(EMBED_RUN_GAME_EMBEDDED), embed_on_play);
  509. menu->set_item_checked(menu->get_item_index(EMBED_MAKE_FLOATING_ON_PLAY), make_floating_on_play && is_multi_window);
  510. menu->set_item_disabled(menu->get_item_index(EMBED_MAKE_FLOATING_ON_PLAY), !embed_on_play || !is_multi_window);
  511. fixed_size_button->set_pressed(embed_size_mode == SIZE_MODE_FIXED);
  512. keep_aspect_button->set_pressed(embed_size_mode == SIZE_MODE_KEEP_ASPECT);
  513. stretch_button->set_pressed(embed_size_mode == SIZE_MODE_STRETCH);
  514. }
  515. void GameView::_update_embed_window_size() {
  516. if (paused) {
  517. // When paused, Godot does not re-render. As a result, resizing the game window to a larger size
  518. // causes artifacts and flickering. However, resizing to a smaller size seems fine.
  519. // To prevent artifacts and flickering, we will force the game window to maintain its size.
  520. // Using the same technique as SIZE_MODE_FIXED, the embedded process control will
  521. // prevent resizing the game to a larger size while maintaining the aspect ratio.
  522. embedded_process->set_window_size(size_paused);
  523. embedded_process->set_keep_aspect(false);
  524. } else {
  525. if (embed_size_mode == SIZE_MODE_FIXED || embed_size_mode == SIZE_MODE_KEEP_ASPECT) {
  526. // The embedded process control will need the desired window size.
  527. EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
  528. embedded_process->set_window_size(placement.size);
  529. } else {
  530. // Stretch... No need for the window size.
  531. embedded_process->set_window_size(Size2i());
  532. }
  533. embedded_process->set_keep_aspect(embed_size_mode == SIZE_MODE_KEEP_ASPECT);
  534. }
  535. }
  536. void GameView::_hide_selection_toggled(bool p_pressed) {
  537. hide_selection->set_button_icon(get_editor_theme_icon(p_pressed ? SNAME("GuiVisibilityHidden") : SNAME("GuiVisibilityVisible")));
  538. debugger->set_selection_visible(!p_pressed);
  539. EditorSettings::get_singleton()->set_project_metadata("game_view", "hide_selection", p_pressed);
  540. }
  541. void GameView::_debug_mute_audio_button_pressed() {
  542. debug_mute_audio = !debug_mute_audio;
  543. debug_mute_audio_button->set_button_icon(get_editor_theme_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer")));
  544. debug_mute_audio_button->set_tooltip_text(debug_mute_audio ? TTRC("Unmute game audio.") : TTRC("Mute game audio."));
  545. debugger->set_debug_mute_audio(debug_mute_audio);
  546. }
  547. void GameView::_camera_override_button_toggled(bool p_pressed) {
  548. _update_debugger_buttons();
  549. debugger->set_camera_override(p_pressed);
  550. }
  551. void GameView::_camera_override_menu_id_pressed(int p_id) {
  552. PopupMenu *menu = camera_override_menu->get_popup();
  553. if (p_id != CAMERA_RESET_2D && p_id != CAMERA_RESET_3D) {
  554. for (int i = 0; i < menu->get_item_count(); i++) {
  555. menu->set_item_checked(i, false);
  556. }
  557. }
  558. switch (p_id) {
  559. case CAMERA_RESET_2D: {
  560. debugger->reset_camera_2d_position();
  561. } break;
  562. case CAMERA_RESET_3D: {
  563. debugger->reset_camera_3d_position();
  564. } break;
  565. case CAMERA_MODE_INGAME: {
  566. debugger->set_camera_manipulate_mode(EditorDebuggerNode::OVERRIDE_INGAME);
  567. menu->set_item_checked(menu->get_item_index(p_id), true);
  568. _update_debugger_buttons();
  569. EditorSettings::get_singleton()->set_project_metadata("game_view", "camera_override_mode", p_id);
  570. } break;
  571. case CAMERA_MODE_EDITORS: {
  572. debugger->set_camera_manipulate_mode(EditorDebuggerNode::OVERRIDE_EDITORS);
  573. menu->set_item_checked(menu->get_item_index(p_id), true);
  574. _update_debugger_buttons();
  575. EditorSettings::get_singleton()->set_project_metadata("game_view", "camera_override_mode", p_id);
  576. } break;
  577. }
  578. }
  579. void GameView::_notification(int p_what) {
  580. switch (p_what) {
  581. case NOTIFICATION_THEME_CHANGED: {
  582. suspend_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
  583. next_frame_button->set_button_icon(get_editor_theme_icon(SNAME("NextFrame")));
  584. node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_button_icon(get_editor_theme_icon(SNAME("InputEventJoypadMotion")));
  585. node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_button_icon(get_editor_theme_icon(SNAME("2DNodes")));
  586. node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_button_icon(get_editor_theme_icon(SNAME("Node3D")));
  587. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));
  588. select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_button_icon(get_editor_theme_icon(SNAME("ListSelect")));
  589. hide_selection->set_button_icon(get_editor_theme_icon(hide_selection->is_pressed() ? SNAME("GuiVisibilityHidden") : SNAME("GuiVisibilityVisible")));
  590. fixed_size_button->set_button_icon(get_editor_theme_icon(SNAME("FixedSize")));
  591. keep_aspect_button->set_button_icon(get_editor_theme_icon(SNAME("KeepAspect")));
  592. stretch_button->set_button_icon(get_editor_theme_icon(SNAME("Stretch")));
  593. embed_options_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
  594. debug_mute_audio_button->set_button_icon(get_editor_theme_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer")));
  595. camera_override_button->set_button_icon(get_editor_theme_icon(SNAME("Camera")));
  596. camera_override_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
  597. } break;
  598. case NOTIFICATION_READY: {
  599. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_EMBEDDING)) {
  600. // Embedding available.
  601. int game_mode = EDITOR_GET("run/window_placement/game_embed_mode");
  602. switch (game_mode) {
  603. case -1: { // Disabled.
  604. embed_on_play = false;
  605. make_floating_on_play = false;
  606. } break;
  607. case 1: { // Embed.
  608. embed_on_play = true;
  609. make_floating_on_play = false;
  610. } break;
  611. case 2: { // Floating.
  612. embed_on_play = true;
  613. make_floating_on_play = true;
  614. } break;
  615. default: {
  616. embed_on_play = EditorSettings::get_singleton()->get_project_metadata("game_view", "embed_on_play", true);
  617. make_floating_on_play = EditorSettings::get_singleton()->get_project_metadata("game_view", "make_floating_on_play", true);
  618. } break;
  619. }
  620. embed_size_mode = (EmbedSizeMode)(int)EditorSettings::get_singleton()->get_project_metadata("game_view", "embed_size_mode", SIZE_MODE_FIXED);
  621. keep_aspect_button->set_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "keep_aspect", true));
  622. _update_embed_menu_options();
  623. EditorRunBar::get_singleton()->connect("play_pressed", callable_mp(this, &GameView::_play_pressed));
  624. EditorRunBar::get_singleton()->connect("stop_pressed", callable_mp(this, &GameView::_stop_pressed));
  625. EditorRun::instance_starting_callback = _instance_starting_static;
  626. // Listen for project settings changes to update the window size and aspect ratio.
  627. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &GameView::_editor_or_project_settings_changed));
  628. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &GameView::_editor_or_project_settings_changed));
  629. } else {
  630. // Embedding not available.
  631. embedding_separator->hide();
  632. embed_options_menu->hide();
  633. fixed_size_button->hide();
  634. keep_aspect_button->hide();
  635. stretch_button->hide();
  636. }
  637. _update_ui();
  638. } break;
  639. case NOTIFICATION_WM_POSITION_CHANGED: {
  640. if (window_wrapper->get_window_enabled()) {
  641. _update_floating_window_settings();
  642. }
  643. } break;
  644. }
  645. }
  646. void GameView::set_window_layout(Ref<ConfigFile> p_layout) {
  647. floating_window_rect = p_layout->get_value("GameView", "floating_window_rect", Rect2i());
  648. floating_window_screen = p_layout->get_value("GameView", "floating_window_screen", -1);
  649. }
  650. void GameView::get_window_layout(Ref<ConfigFile> p_layout) {
  651. if (window_wrapper->get_window_enabled()) {
  652. _update_floating_window_settings();
  653. }
  654. p_layout->set_value("GameView", "floating_window_rect", floating_window_rect);
  655. p_layout->set_value("GameView", "floating_window_screen", floating_window_screen);
  656. }
  657. void GameView::_update_floating_window_settings() {
  658. if (window_wrapper->get_window_enabled()) {
  659. floating_window_rect = window_wrapper->get_window_rect();
  660. floating_window_screen = window_wrapper->get_window_screen();
  661. }
  662. }
  663. void GameView::_attach_script_debugger() {
  664. if (embedded_script_debugger) {
  665. _detach_script_debugger();
  666. }
  667. embedded_script_debugger = nullptr;
  668. int i = 0;
  669. while (ScriptEditorDebugger *script_debugger = EditorDebuggerNode::get_singleton()->get_debugger(i)) {
  670. if (script_debugger->is_session_active() && script_debugger->get_remote_pid() == embedded_process->get_embedded_pid()) {
  671. embedded_script_debugger = script_debugger;
  672. break;
  673. }
  674. i++;
  675. }
  676. #ifdef MACOS_ENABLED
  677. embedded_process->set_script_debugger(embedded_script_debugger);
  678. #endif
  679. if (embedded_script_debugger) {
  680. embedded_script_debugger->connect("remote_window_title_changed", callable_mp(this, &GameView::_remote_window_title_changed));
  681. embedded_script_debugger->connect("embed_shortcut_requested", callable_mp(this, &GameView::_handle_shortcut_requested));
  682. }
  683. }
  684. void GameView::_detach_script_debugger() {
  685. if (embedded_script_debugger) {
  686. embedded_script_debugger->disconnect("remote_window_title_changed", callable_mp(this, &GameView::_remote_window_title_changed));
  687. embedded_script_debugger->disconnect("embed_shortcut_requested", callable_mp(this, &GameView::_handle_shortcut_requested));
  688. embedded_script_debugger = nullptr;
  689. }
  690. }
  691. void GameView::_remote_window_title_changed(String title) {
  692. window_wrapper->set_window_title(title);
  693. }
  694. void GameView::_update_arguments_for_instance(int p_idx, List<String> &r_arguments) {
  695. if (p_idx != 0 || !embed_on_play || _get_embed_available() != EMBED_AVAILABLE) {
  696. return;
  697. }
  698. // Remove duplicates/unwanted parameters.
  699. List<String>::Element *E = r_arguments.front();
  700. List<String>::Element *user_args_element = nullptr;
  701. HashSet<String> remove_args({ "--position", "--resolution", "--screen" });
  702. #ifdef MACOS_ENABLED
  703. // macOS requires the embedded display driver.
  704. remove_args.insert("--display-driver");
  705. #endif
  706. while (E) {
  707. List<String>::Element *N = E->next();
  708. // For these parameters, we need to also remove the value.
  709. if (remove_args.has(E->get())) {
  710. r_arguments.erase(E);
  711. if (N) {
  712. List<String>::Element *V = N->next();
  713. r_arguments.erase(N);
  714. N = V;
  715. }
  716. } else if (E->get() == "-f" || E->get() == "--fullscreen" || E->get() == "-m" || E->get() == "--maximized" || E->get() == "-t" || E->get() == "-always-on-top") {
  717. r_arguments.erase(E);
  718. } else if (E->get() == "--" || E->get() == "++") {
  719. user_args_element = E;
  720. break;
  721. }
  722. E = N;
  723. }
  724. // Add the editor window's native ID so the started game can directly set it as its parent.
  725. List<String>::Element *N = r_arguments.insert_before(user_args_element, "--wid");
  726. N = r_arguments.insert_after(N, itos(DisplayServer::get_singleton()->window_get_native_handle(DisplayServer::WINDOW_HANDLE, get_window()->get_window_id())));
  727. #if MACOS_ENABLED
  728. N = r_arguments.insert_after(N, "--embedded");
  729. #endif
  730. // Be sure to have the correct window size in the embedded_process control.
  731. _update_embed_window_size();
  732. Rect2i rect = embedded_process->get_screen_embedded_window_rect();
  733. // Usually, the global rect of the embedded process control is invalid because it was hidden. We will calculate it manually.
  734. if (!window_wrapper->get_window_enabled()) {
  735. Size2 old_min_size = embedded_process->get_custom_minimum_size();
  736. embedded_process->set_custom_minimum_size(Size2i());
  737. Control *container = EditorNode::get_singleton()->get_editor_main_screen()->get_control();
  738. rect = container->get_global_rect();
  739. Size2 wrapped_min_size = window_wrapper->get_minimum_size();
  740. rect.position.y += wrapped_min_size.y;
  741. rect.size.y -= wrapped_min_size.y;
  742. rect = embedded_process->get_adjusted_embedded_window_rect(rect);
  743. embedded_process->set_custom_minimum_size(old_min_size);
  744. }
  745. // When using the floating window, we need to force the position and size from the
  746. // editor/project settings, because the get_screen_embedded_window_rect of the
  747. // embedded_process will be updated only on the next frame.
  748. if (window_wrapper->get_window_enabled()) {
  749. EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
  750. if (placement.position != Point2i(INT_MAX, INT_MAX)) {
  751. rect.position = placement.position;
  752. }
  753. if (placement.size != Size2i()) {
  754. rect.size = placement.size;
  755. }
  756. }
  757. N = r_arguments.insert_after(N, "--position");
  758. N = r_arguments.insert_after(N, itos(rect.position.x) + "," + itos(rect.position.y));
  759. N = r_arguments.insert_after(N, "--resolution");
  760. r_arguments.insert_after(N, itos(rect.size.x) + "x" + itos(rect.size.y));
  761. }
  762. void GameView::_window_close_request() {
  763. // Before the parent window closed, we close the embedded game. That prevents
  764. // the embedded game to be seen without a parent window for a fraction of second.
  765. if (EditorRunBar::get_singleton()->is_playing() && (embedded_process->is_embedding_completed() || embedded_process->is_embedding_in_progress())) {
  766. // When the embedding is not complete, we need to kill the process.
  767. // If the game is paused, the close request will not be processed by the game, so it's better to kill the process.
  768. if (paused || embedded_process->is_embedding_in_progress()) {
  769. embedded_process->reset();
  770. // Call deferred to prevent the _stop_pressed callback to be executed before the wrapper window
  771. // actually closes.
  772. callable_mp(EditorRunBar::get_singleton(), &EditorRunBar::stop_playing).call_deferred();
  773. } else {
  774. // Try to gracefully close the window. That way, the NOTIFICATION_WM_CLOSE_REQUEST
  775. // notification should be propagated in the game process.
  776. embedded_process->request_close();
  777. }
  778. }
  779. }
  780. void GameView::_debugger_breaked(bool p_breaked, bool p_can_debug) {
  781. if (p_breaked == paused) {
  782. return;
  783. }
  784. paused = p_breaked;
  785. if (paused) {
  786. size_paused = embedded_process->get_screen_embedded_window_rect().size;
  787. }
  788. _update_embed_window_size();
  789. }
  790. void GameView::_feature_profile_changed() {
  791. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  792. bool is_profile_null = profile.is_null();
  793. is_feature_enabled = is_profile_null || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_GAME);
  794. bool is_3d_enabled = is_profile_null || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D);
  795. if (!is_3d_enabled && node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->is_pressed()) {
  796. _node_type_pressed(RuntimeNodeSelect::NODE_TYPE_NONE);
  797. }
  798. node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_visible(is_3d_enabled);
  799. }
  800. GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process, WindowWrapper *p_wrapper) {
  801. singleton = this;
  802. debugger = p_debugger;
  803. window_wrapper = p_wrapper;
  804. embedded_process = p_embedded_process;
  805. // Add some margin to the sides for better aesthetics.
  806. // This prevents the first button's hover/pressed effect from "touching" the panel's border,
  807. // which looks ugly.
  808. MarginContainer *toolbar_margin = memnew(MarginContainer);
  809. toolbar_margin->add_theme_constant_override("margin_left", 4 * EDSCALE);
  810. toolbar_margin->add_theme_constant_override("margin_right", 4 * EDSCALE);
  811. add_child(toolbar_margin);
  812. HBoxContainer *main_menu_hbox = memnew(HBoxContainer);
  813. toolbar_margin->add_child(main_menu_hbox);
  814. suspend_button = memnew(Button);
  815. main_menu_hbox->add_child(suspend_button);
  816. suspend_button->set_toggle_mode(true);
  817. suspend_button->set_theme_type_variation(SceneStringName(FlatButton));
  818. suspend_button->connect(SceneStringName(toggled), callable_mp(this, &GameView::_suspend_button_toggled));
  819. suspend_button->set_tooltip_text(TTR("Suspend"));
  820. suspend_button->set_accessibility_name(TTRC("Suspend"));
  821. ED_SHORTCUT("editor/suspend_resume_embedded_project", TTRC("Suspend/Resume Embedded Project"), Key::F9);
  822. ED_SHORTCUT_OVERRIDE("editor/suspend_resume_embedded_project", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B);
  823. suspend_button->set_shortcut(ED_GET_SHORTCUT("editor/suspend_resume_embedded_project"));
  824. next_frame_button = memnew(Button);
  825. main_menu_hbox->add_child(next_frame_button);
  826. next_frame_button->set_theme_type_variation(SceneStringName(FlatButton));
  827. next_frame_button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::next_frame));
  828. next_frame_button->set_tooltip_text(TTR("Next Frame"));
  829. next_frame_button->set_accessibility_name(TTRC("Next Frame"));
  830. next_frame_button->set_shortcut(ED_SHORTCUT("editor/next_frame_embedded_project", TTRC("Next Frame"), Key::F10));
  831. main_menu_hbox->add_child(memnew(VSeparator));
  832. node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE] = memnew(Button);
  833. main_menu_hbox->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]);
  834. node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_text(TTR("Input"));
  835. node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_toggle_mode(true);
  836. node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_pressed(true);
  837. node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_theme_type_variation(SceneStringName(FlatButton));
  838. node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_NONE));
  839. node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_tooltip_text(TTR("Allow game input."));
  840. node_type_button[RuntimeNodeSelect::NODE_TYPE_2D] = memnew(Button);
  841. main_menu_hbox->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]);
  842. node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_text(TTR("2D"));
  843. node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_toggle_mode(true);
  844. node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_theme_type_variation(SceneStringName(FlatButton));
  845. node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_2D));
  846. node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_tooltip_text(TTR("Disable game input and allow to select Node2Ds, Controls, and manipulate the 2D camera."));
  847. node_type_button[RuntimeNodeSelect::NODE_TYPE_3D] = memnew(Button);
  848. main_menu_hbox->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]);
  849. node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_text(TTR("3D"));
  850. node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_toggle_mode(true);
  851. node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_theme_type_variation(SceneStringName(FlatButton));
  852. node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_3D));
  853. node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_tooltip_text(TTR("Disable game input and allow to select Node3Ds and manipulate the 3D camera."));
  854. main_menu_hbox->add_child(memnew(VSeparator));
  855. hide_selection = memnew(Button);
  856. main_menu_hbox->add_child(hide_selection);
  857. hide_selection->set_toggle_mode(true);
  858. hide_selection->set_theme_type_variation(SceneStringName(FlatButton));
  859. hide_selection->connect(SceneStringName(toggled), callable_mp(this, &GameView::_hide_selection_toggled));
  860. hide_selection->set_tooltip_text(TTR("Toggle Selection Visibility"));
  861. hide_selection->set_accessibility_name(TTRC("Selection Visibility"));
  862. hide_selection->set_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "hide_selection", false));
  863. main_menu_hbox->add_child(memnew(VSeparator));
  864. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE] = memnew(Button);
  865. main_menu_hbox->add_child(select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]);
  866. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_toggle_mode(true);
  867. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_pressed(true);
  868. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_theme_type_variation(SceneStringName(FlatButton));
  869. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_select_mode_pressed).bind(RuntimeNodeSelect::SELECT_MODE_SINGLE));
  870. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_select", TTRC("Select Mode"), Key::Q));
  871. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_shortcut_context(this);
  872. select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_tooltip_text(keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Alt+RMB: Show list of all nodes at position clicked."));
  873. select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST] = memnew(Button);
  874. main_menu_hbox->add_child(select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]);
  875. select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_toggle_mode(true);
  876. select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_theme_type_variation(SceneStringName(FlatButton));
  877. select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_select_mode_pressed).bind(RuntimeNodeSelect::SELECT_MODE_LIST));
  878. select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_tooltip_text(TTR("Show list of selectable nodes at position clicked."));
  879. _select_mode_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "select_mode", 0));
  880. main_menu_hbox->add_child(memnew(VSeparator));
  881. debug_mute_audio_button = memnew(Button);
  882. main_menu_hbox->add_child(debug_mute_audio_button);
  883. debug_mute_audio_button->set_theme_type_variation("FlatButton");
  884. debug_mute_audio_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_debug_mute_audio_button_pressed));
  885. debug_mute_audio_button->set_tooltip_text(debug_mute_audio ? TTRC("Unmute game audio.") : TTRC("Mute game audio."));
  886. main_menu_hbox->add_child(memnew(VSeparator));
  887. camera_override_button = memnew(Button);
  888. main_menu_hbox->add_child(camera_override_button);
  889. camera_override_button->set_toggle_mode(true);
  890. camera_override_button->set_theme_type_variation(SceneStringName(FlatButton));
  891. camera_override_button->set_tooltip_text(TTR("Override the in-game camera."));
  892. camera_override_button->set_accessibility_name(TTRC("Override In-game Camera"));
  893. camera_override_button->connect(SceneStringName(toggled), callable_mp(this, &GameView::_camera_override_button_toggled));
  894. camera_override_menu = memnew(MenuButton);
  895. main_menu_hbox->add_child(camera_override_menu);
  896. camera_override_menu->set_flat(false);
  897. camera_override_menu->set_theme_type_variation("FlatMenuButton");
  898. camera_override_menu->set_h_size_flags(SIZE_SHRINK_END);
  899. camera_override_menu->set_tooltip_text(TTR("Camera Override Options"));
  900. camera_override_menu->set_accessibility_name(TTRC("Camera Override Options"));
  901. PopupMenu *menu = camera_override_menu->get_popup();
  902. menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_camera_override_menu_id_pressed));
  903. menu->add_item(TTR("Reset 2D Camera"), CAMERA_RESET_2D);
  904. menu->add_item(TTR("Reset 3D Camera"), CAMERA_RESET_3D);
  905. menu->add_separator();
  906. menu->add_radio_check_item(TTR("Manipulate In-Game"), CAMERA_MODE_INGAME);
  907. menu->set_item_checked(menu->get_item_index(CAMERA_MODE_INGAME), true);
  908. menu->add_radio_check_item(TTR("Manipulate From Editors"), CAMERA_MODE_EDITORS);
  909. _camera_override_menu_id_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "camera_override_mode", 0));
  910. embedding_separator = memnew(VSeparator);
  911. main_menu_hbox->add_child(embedding_separator);
  912. fixed_size_button = memnew(Button);
  913. main_menu_hbox->add_child(fixed_size_button);
  914. fixed_size_button->set_toggle_mode(true);
  915. fixed_size_button->set_theme_type_variation("FlatButton");
  916. fixed_size_button->set_tooltip_text(TTR("Embedded game size is based on project settings.\nThe 'Keep Aspect' mode is used when the Game Workspace is smaller than the desired size."));
  917. fixed_size_button->set_accessibility_name(TTRC("Fixed Size"));
  918. fixed_size_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_size_mode_button_pressed).bind(SIZE_MODE_FIXED));
  919. keep_aspect_button = memnew(Button);
  920. main_menu_hbox->add_child(keep_aspect_button);
  921. keep_aspect_button->set_toggle_mode(true);
  922. keep_aspect_button->set_theme_type_variation("FlatButton");
  923. keep_aspect_button->set_tooltip_text(TTR("Keep the aspect ratio of the embedded game."));
  924. keep_aspect_button->set_accessibility_name(TTRC("Keep Aspect Ratio"));
  925. keep_aspect_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_size_mode_button_pressed).bind(SIZE_MODE_KEEP_ASPECT));
  926. stretch_button = memnew(Button);
  927. main_menu_hbox->add_child(stretch_button);
  928. stretch_button->set_toggle_mode(true);
  929. stretch_button->set_theme_type_variation("FlatButton");
  930. stretch_button->set_tooltip_text(TTR("Embedded game size stretches to fit the Game Workspace."));
  931. stretch_button->set_accessibility_name(TTRC("Stretch"));
  932. stretch_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_size_mode_button_pressed).bind(SIZE_MODE_STRETCH));
  933. embed_options_menu = memnew(MenuButton);
  934. main_menu_hbox->add_child(embed_options_menu);
  935. embed_options_menu->set_flat(false);
  936. embed_options_menu->set_theme_type_variation("FlatMenuButton");
  937. embed_options_menu->set_h_size_flags(SIZE_SHRINK_END);
  938. embed_options_menu->set_tooltip_text(TTR("Embedding Options"));
  939. embed_options_menu->set_accessibility_name(TTRC("Embedding Options"));
  940. menu = embed_options_menu->get_popup();
  941. menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_embed_options_menu_menu_id_pressed));
  942. menu->add_check_item(TTR("Embed Game on Next Play"), EMBED_RUN_GAME_EMBEDDED);
  943. menu->add_check_item(TTR("Make Game Workspace Floating on Next Play"), EMBED_MAKE_FLOATING_ON_PLAY);
  944. main_menu_hbox->add_spacer();
  945. game_size_label = memnew(Label());
  946. main_menu_hbox->add_child(game_size_label);
  947. game_size_label->hide();
  948. // Setting the minimum size prevents the game workspace from resizing indefinitely
  949. // due to the label size oscillating by a few pixels when the game is in stretch mode
  950. // and the game workspace is at its minimum size.
  951. game_size_label->set_custom_minimum_size(Size2(80 * EDSCALE, 0));
  952. game_size_label->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_RIGHT);
  953. panel = memnew(Panel);
  954. add_child(panel);
  955. panel->set_theme_type_variation("GamePanel");
  956. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  957. panel->add_child(embedded_process);
  958. embedded_process->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  959. embedded_process->connect("embedding_failed", callable_mp(this, &GameView::_embedding_failed));
  960. embedded_process->connect("embedding_completed", callable_mp(this, &GameView::_embedding_completed));
  961. embedded_process->connect("embedded_process_updated", callable_mp(this, &GameView::_embedded_process_updated));
  962. embedded_process->connect("embedded_process_focused", callable_mp(this, &GameView::_embedded_process_focused));
  963. embedded_process->set_custom_minimum_size(Size2i(100, 100));
  964. MarginContainer *state_container = memnew(MarginContainer);
  965. state_container->add_theme_constant_override("margin_left", 8 * EDSCALE);
  966. state_container->add_theme_constant_override("margin_right", 8 * EDSCALE);
  967. state_container->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  968. #ifdef MACOS_ENABLED
  969. state_container->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  970. #endif
  971. panel->add_child(state_container);
  972. state_label = memnew(Label());
  973. state_container->add_child(state_label);
  974. state_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  975. state_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  976. state_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  977. state_label->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  978. _update_debugger_buttons();
  979. p_debugger->connect("session_started", callable_mp(this, &GameView::_sessions_changed));
  980. p_debugger->connect("session_stopped", callable_mp(this, &GameView::_sessions_changed));
  981. p_wrapper->set_override_close_request(true);
  982. p_wrapper->connect("window_close_requested", callable_mp(this, &GameView::_window_close_request));
  983. p_wrapper->connect("window_size_changed", callable_mp(this, &GameView::_update_floating_window_settings));
  984. EditorDebuggerNode::get_singleton()->connect("breaked", callable_mp(this, &GameView::_debugger_breaked));
  985. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &GameView::_feature_profile_changed));
  986. }
  987. ///////
  988. void GameViewPluginBase::selected_notify() {
  989. if (_is_window_wrapper_enabled()) {
  990. #ifdef ANDROID_ENABLED
  991. notify_main_screen_changed(get_plugin_name());
  992. #else
  993. window_wrapper->grab_window_focus();
  994. #endif // ANDROID_ENABLED
  995. _focus_another_editor();
  996. }
  997. }
  998. #ifndef ANDROID_ENABLED
  999. void GameViewPluginBase::make_visible(bool p_visible) {
  1000. if (p_visible) {
  1001. window_wrapper->show();
  1002. } else {
  1003. window_wrapper->hide();
  1004. }
  1005. }
  1006. void GameViewPluginBase::set_window_layout(Ref<ConfigFile> p_layout) {
  1007. game_view->set_window_layout(p_layout);
  1008. }
  1009. void GameViewPluginBase::get_window_layout(Ref<ConfigFile> p_layout) {
  1010. game_view->get_window_layout(p_layout);
  1011. }
  1012. void GameViewPluginBase::setup(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process) {
  1013. debugger = p_debugger;
  1014. window_wrapper = memnew(WindowWrapper);
  1015. window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Game Workspace")));
  1016. window_wrapper->set_margins_enabled(true);
  1017. game_view = memnew(GameView(debugger, p_embedded_process, window_wrapper));
  1018. game_view->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1019. window_wrapper->set_wrapped_control(game_view, nullptr);
  1020. EditorNode::get_singleton()->get_editor_main_screen()->get_control()->add_child(window_wrapper);
  1021. window_wrapper->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1022. window_wrapper->hide();
  1023. window_wrapper->connect("window_visibility_changed", callable_mp(this, &GameViewPlugin::_focus_another_editor).unbind(1));
  1024. }
  1025. #endif // ANDROID_ENABLED
  1026. void GameViewPluginBase::_notification(int p_what) {
  1027. switch (p_what) {
  1028. case NOTIFICATION_ENTER_TREE: {
  1029. add_debugger_plugin(debugger);
  1030. connect("main_screen_changed", callable_mp(this, &GameViewPluginBase::_save_last_editor));
  1031. } break;
  1032. case NOTIFICATION_EXIT_TREE: {
  1033. remove_debugger_plugin(debugger);
  1034. disconnect("main_screen_changed", callable_mp(this, &GameViewPluginBase::_save_last_editor));
  1035. } break;
  1036. }
  1037. }
  1038. void GameViewPluginBase::_save_last_editor(const String &p_editor) {
  1039. if (p_editor != get_plugin_name()) {
  1040. last_editor = p_editor;
  1041. }
  1042. }
  1043. void GameViewPluginBase::_focus_another_editor() {
  1044. if (_is_window_wrapper_enabled()) {
  1045. if (last_editor.is_empty()) {
  1046. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_2D);
  1047. } else {
  1048. EditorInterface::get_singleton()->set_main_screen_editor(last_editor);
  1049. }
  1050. }
  1051. }
  1052. bool GameViewPluginBase::_is_window_wrapper_enabled() const {
  1053. #ifdef ANDROID_ENABLED
  1054. return true;
  1055. #else
  1056. return window_wrapper->get_window_enabled();
  1057. #endif // ANDROID_ENABLED
  1058. }
  1059. GameViewPluginBase::GameViewPluginBase() {
  1060. }
  1061. GameViewPlugin::GameViewPlugin() :
  1062. GameViewPluginBase() {
  1063. #ifndef ANDROID_ENABLED
  1064. Ref<GameViewDebugger> game_view_debugger;
  1065. game_view_debugger.instantiate();
  1066. EmbeddedProcess *embedded_process = memnew(EmbeddedProcess);
  1067. setup(game_view_debugger, embedded_process);
  1068. #endif
  1069. }