| 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 | |
| 31 | #include "game_view_plugin.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/debugger/debugger_marshalls.h" |
| 35 | #include "core/debugger/engine_debugger.h" |
| 36 | #include "core/string/translation_server.h" |
| 37 | #include "editor/debugger/editor_debugger_node.h" |
| 38 | #include "editor/debugger/script_editor_debugger.h" |
| 39 | #include "editor/editor_interface.h" |
| 40 | #include "editor/editor_main_screen.h" |
| 41 | #include "editor/editor_node.h" |
| 42 | #include "editor/editor_string_names.h" |
| 43 | #include "editor/gui/editor_bottom_panel.h" |
| 44 | #include "editor/gui/window_wrapper.h" |
| 45 | #include "editor/run/editor_run_bar.h" |
| 46 | #include "editor/run/embedded_process.h" |
| 47 | #include "editor/run/run_instances_dialog.h" |
| 48 | #include "editor/settings/editor_feature_profile.h" |
| 49 | #include "editor/settings/editor_settings.h" |
| 50 | #include "editor/themes/editor_scale.h" |
| 51 | #include "scene/gui/button.h" |
| 52 | #include "scene/gui/flow_container.h" |
| 53 | #include "scene/gui/label.h" |
| 54 | #include "scene/gui/menu_button.h" |
| 55 | #include "scene/gui/panel.h" |
| 56 | #include "scene/gui/separator.h" |
| 57 | |
| 58 | void GameViewDebugger::_session_started(Ref<EditorDebuggerSession> p_session) { |
| 59 | if (!is_feature_enabled) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | Dictionary settings; |
| 64 | settings["debugger/max_node_selection"] = EDITOR_GET("debugger/max_node_selection"); |
| 65 | settings["editors/panning/2d_editor_panning_scheme"] = EDITOR_GET("editors/panning/2d_editor_panning_scheme"); |
| 66 | settings["editors/panning/simple_panning"] = EDITOR_GET("editors/panning/simple_panning"); |
| 67 | settings["editors/panning/warped_mouse_panning"] = EDITOR_GET("editors/panning/warped_mouse_panning"); |
| 68 | settings["editors/panning/2d_editor_pan_speed"] = EDITOR_GET("editors/panning/2d_editor_pan_speed"); |
| 69 | settings["editors/polygon_editor/point_grab_radius"] = EDITOR_GET("editors/polygon_editor/point_grab_radius"); |
| 70 | settings["canvas_item_editor/pan_view"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/pan_view")); |
| 71 | settings["box_selection_fill_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("box_selection_fill_color"), EditorStringName(Editor)); |
| 72 | settings["box_selection_stroke_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor)); |
| 73 | settings["editors/3d/default_fov"] = EDITOR_GET("editors/3d/default_fov"); |
| 74 | settings["editors/3d/default_z_near"] = EDITOR_GET("editors/3d/default_z_near"); |
| 75 | settings["editors/3d/default_z_far"] = EDITOR_GET("editors/3d/default_z_far"); |
| 76 | settings["editors/3d/navigation/invert_x_axis"] = EDITOR_GET("editors/3d/navigation/invert_x_axis"); |
| 77 | settings["editors/3d/navigation/invert_y_axis"] = EDITOR_GET("editors/3d/navigation/invert_y_axis"); |
| 78 | settings["editors/3d/navigation/warped_mouse_panning"] = EDITOR_GET("editors/3d/navigation/warped_mouse_panning"); |
| 79 | settings["editors/3d/freelook/freelook_base_speed"] = EDITOR_GET("editors/3d/freelook/freelook_base_speed"); |
| 80 | settings["editors/3d/freelook/freelook_sensitivity"] = EDITOR_GET("editors/3d/freelook/freelook_sensitivity"); |
| 81 | settings["editors/3d/navigation_feel/orbit_sensitivity"] = EDITOR_GET("editors/3d/navigation_feel/orbit_sensitivity"); |
| 82 | settings["editors/3d/navigation_feel/translation_sensitivity"] = EDITOR_GET("editors/3d/navigation_feel/translation_sensitivity"); |
| 83 | settings["editors/3d/selection_box_color"] = EDITOR_GET("editors/3d/selection_box_color"); |
| 84 | settings["editors/3d/freelook/freelook_base_speed"] = EDITOR_GET("editors/3d/freelook/freelook_base_speed"); |
| 85 | |
| 86 | Array setup_data; |
| 87 | setup_data.append(settings); |
| 88 | p_session->send_message("scene:runtime_node_select_setup", setup_data); |
| 89 | |
| 90 | Array type; |
| 91 | type.append(node_type); |
| 92 | p_session->send_message("scene:runtime_node_select_set_type", type); |
| 93 | Array visible; |
| 94 | visible.append(selection_visible); |
| 95 | p_session->send_message("scene:runtime_node_select_set_visible", visible); |
| 96 | Array mode; |
| 97 | mode.append(select_mode); |
| 98 | p_session->send_message("scene:runtime_node_select_set_mode", mode); |
| 99 | Array avoid_locked; |
| 100 | avoid_locked.append(selection_avoid_locked); |
| 101 | p_session->send_message("scene:runtime_node_select_set_avoid_locked", avoid_locked); |
| 102 | Array prefer_group; |
| 103 | prefer_group.append(selection_prefer_group); |
| 104 | p_session->send_message("scene:runtime_node_select_set_prefer_group", prefer_group); |
| 105 | Array mute_audio_data; |
| 106 | mute_audio_data.append(mute_audio); |
| 107 | p_session->send_message("scene:debug_mute_audio", mute_audio_data); |
| 108 | |
| 109 | Dictionary shortcut_settings; |
| 110 | shortcut_settings["editor/suspend_resume_embedded_project"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("editor/suspend_resume_embedded_project")); |
| 111 | shortcut_settings["editor/next_frame_embedded_project"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("editor/next_frame_embedded_project")); |
| 112 | |
| 113 | p_session->send_message("scene:setup_embedded_shortcuts", { shortcut_settings }); |
| 114 | |
| 115 | emit_signal(SNAME("session_started")); |
| 116 | } |
| 117 | |
| 118 | void GameViewDebugger::_session_stopped() { |
| 119 | if (!is_feature_enabled) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | emit_signal(SNAME("session_stopped")); |
| 124 | } |
| 125 | |
| 126 | void GameViewDebugger::set_suspend(bool p_enabled) { |
| 127 | Array message; |
| 128 | message.append(p_enabled); |
| 129 | |
| 130 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 131 | if (I->is_active()) { |
| 132 | I->send_message("scene:suspend_changed", message); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void GameViewDebugger::next_frame() { |
| 138 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 139 | if (I->is_active()) { |
| 140 | I->send_message("scene:next_frame", Array()); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void GameViewDebugger::set_time_scale(double p_scale) { |
| 146 | Array message; |
| 147 | message.append(p_scale); |
| 148 | |
| 149 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 150 | if (I->is_active()) { |
| 151 | I->send_message("scene:speed_changed", message); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void GameViewDebugger::reset_time_scale() { |
| 157 | Array message; |
| 158 | message.append(1.0); |
| 159 | |
| 160 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 161 | if (I->is_active()) { |
| 162 | I->send_message("scene:speed_changed", message); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void GameViewDebugger::set_node_type(int p_type) { |
| 168 | node_type = p_type; |
| 169 | |
| 170 | Array message; |
| 171 | message.append(p_type); |
| 172 | |
| 173 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 174 | if (I->is_active()) { |
| 175 | I->send_message("scene:runtime_node_select_set_type", message); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void GameViewDebugger::set_selection_visible(bool p_visible) { |
| 181 | selection_visible = p_visible; |
| 182 | |
| 183 | Array message; |
| 184 | message.append(p_visible); |
| 185 | |
| 186 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 187 | if (I->is_active()) { |
| 188 | I->send_message("scene:runtime_node_select_set_visible", message); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void GameViewDebugger::set_selection_avoid_locked(bool p_enabled) { |
| 194 | selection_avoid_locked = p_enabled; |
| 195 | |
| 196 | Array message; |
| 197 | message.append(p_enabled); |
| 198 | |
| 199 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 200 | if (I->is_active()) { |
| 201 | I->send_message("scene:runtime_node_select_set_avoid_locked", message); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void GameViewDebugger::set_selection_prefer_group(bool p_enabled) { |
| 207 | selection_prefer_group = p_enabled; |
| 208 | |
| 209 | Array message; |
| 210 | message.append(p_enabled); |
| 211 | |
| 212 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 213 | if (I->is_active()) { |
| 214 | I->send_message("scene:runtime_node_select_set_prefer_group", message); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void GameViewDebugger::set_select_mode(int p_mode) { |
| 220 | select_mode = p_mode; |
| 221 | |
| 222 | Array message; |
| 223 | message.append(p_mode); |
| 224 | |
| 225 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 226 | if (I->is_active()) { |
| 227 | I->send_message("scene:runtime_node_select_set_mode", message); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void GameViewDebugger::set_debug_mute_audio(bool p_enabled) { |
| 233 | mute_audio = p_enabled; |
| 234 | EditorDebuggerNode::get_singleton()->set_debug_mute_audio(p_enabled); |
| 235 | } |
| 236 | |
| 237 | void GameViewDebugger::set_camera_override(bool p_enabled) { |
| 238 | EditorDebuggerNode::get_singleton()->set_camera_override(p_enabled ? camera_override_mode : EditorDebuggerNode::OVERRIDE_NONE); |
| 239 | } |
| 240 | |
| 241 | void GameViewDebugger::set_camera_manipulate_mode(EditorDebuggerNode::CameraOverride p_mode) { |
| 242 | camera_override_mode = p_mode; |
| 243 | |
| 244 | if (EditorDebuggerNode::get_singleton()->get_camera_override() != EditorDebuggerNode::OVERRIDE_NONE) { |
| 245 | set_camera_override(true); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void GameViewDebugger::reset_camera_2d_position() { |
| 250 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 251 | if (I->is_active()) { |
| 252 | I->send_message("scene:runtime_node_select_reset_camera_2d", Array()); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void GameViewDebugger::reset_camera_3d_position() { |
| 258 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 259 | if (I->is_active()) { |
| 260 | I->send_message("scene:runtime_node_select_reset_camera_3d", Array()); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void GameViewDebugger::setup_session(int p_session_id) { |
| 266 | Ref<EditorDebuggerSession> session = get_session(p_session_id); |
| 267 | ERR_FAIL_COND(session.is_null()); |
| 268 | |
| 269 | sessions.append(session); |
| 270 | |
| 271 | session->connect("started", callable_mp(this, &GameViewDebugger::_session_started).bind(session)); |
| 272 | session->connect("stopped", callable_mp(this, &GameViewDebugger::_session_stopped)); |
| 273 | } |
| 274 | |
| 275 | void GameViewDebugger::_feature_profile_changed() { |
| 276 | Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile(); |
| 277 | is_feature_enabled = profile.is_null() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_GAME); |
| 278 | } |
| 279 | |
| 280 | void GameViewDebugger::_bind_methods() { |
| 281 | ADD_SIGNAL(MethodInfo("session_started")); |
| 282 | ADD_SIGNAL(MethodInfo("session_stopped")); |
| 283 | } |
| 284 | |
| 285 | bool GameViewDebugger::add_screenshot_callback(const Callable &p_callaback, const Rect2i &p_rect) { |
| 286 | bool found = false; |
| 287 | for (Ref<EditorDebuggerSession> &I : sessions) { |
| 288 | if (I->is_active()) { |
| 289 | ScreenshotCB sd; |
| 290 | sd.cb = p_callaback; |
| 291 | sd.rect = p_rect; |
| 292 | screenshot_callbacks[scr_rq_id] = sd; |
| 293 | |
| 294 | Array arr; |
| 295 | arr.append(scr_rq_id); |
| 296 | I->send_message("scene:rq_screenshot", arr); |
| 297 | scr_rq_id++; |
| 298 | found = true; |
| 299 | } |
| 300 | } |
| 301 | return found; |
| 302 | } |
| 303 | |
| 304 | bool GameViewDebugger::_msg_get_screenshot(const Array &p_args) { |
| 305 | ERR_FAIL_COND_V_MSG(p_args.size() != 4, false, "get_screenshot: invalid number of arguments"); |
| 306 | |
| 307 | int64_t id = p_args[0]; |
| 308 | int64_t w = p_args[1]; |
| 309 | int64_t h = p_args[2]; |
| 310 | const String &path = p_args[3]; |
| 311 | |
| 312 | if (screenshot_callbacks.has(id)) { |
| 313 | if (screenshot_callbacks[id].cb.is_valid()) { |
| 314 | screenshot_callbacks[id].cb.call(w, h, path, screenshot_callbacks[id].rect); |
| 315 | } |
| 316 | screenshot_callbacks.erase(id); |
| 317 | } |
| 318 | return true; |
| 319 | } |
| 320 | |
| 321 | bool GameViewDebugger::capture(const String &p_message, const Array &p_data, int p_session) { |
| 322 | Ref<EditorDebuggerSession> session = get_session(p_session); |
| 323 | ERR_FAIL_COND_V(session.is_null(), true); |
| 324 | |
| 325 | if (p_message == "game_view:get_screenshot") { |
| 326 | return _msg_get_screenshot(p_data); |
| 327 | } else { |
| 328 | // Any other messages with this prefix should be ignored. |
| 329 | WARN_PRINT("GameViewDebugger unknown message: " + p_message); |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | bool GameViewDebugger::has_capture(const String &p_capture) const { |
| 337 | return p_capture == "game_view"; |
| 338 | } |
| 339 | |
| 340 | GameViewDebugger::GameViewDebugger() { |
| 341 | EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &GameViewDebugger::_feature_profile_changed)); |
| 342 | |
| 343 | ED_SHORTCUT("editor/suspend_resume_embedded_project", TTRC("Suspend/Resume Embedded Project"), Key::F9); |
| 344 | ED_SHORTCUT_OVERRIDE("editor/suspend_resume_embedded_project", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B); |
| 345 | |
| 346 | ED_SHORTCUT("editor/next_frame_embedded_project", TTRC("Next Frame"), Key::F10); |
| 347 | } |
| 348 | |
| 349 | /////// |
| 350 | |
| 351 | void GameView::_sessions_changed() { |
| 352 | // The debugger session's `session_started/stopped` signal can be unreliable, so count it manually. |
| 353 | active_sessions = 0; |
| 354 | Array sessions = debugger->get_sessions(); |
| 355 | for (int i = 0; i < sessions.size(); i++) { |
| 356 | if (Object::cast_to<EditorDebuggerSession>(sessions[i])->is_active()) { |
| 357 | active_sessions++; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | _update_debugger_buttons(); |
| 362 | |
| 363 | #ifdef MACOS_ENABLED |
| 364 | if (!embedded_script_debugger || !embedded_script_debugger->is_session_active() || embedded_script_debugger->get_remote_pid() != embedded_process->get_embedded_pid()) { |
| 365 | _attach_script_debugger(); |
| 366 | } |
| 367 | #else |
| 368 | if (embedded_process->is_embedding_completed()) { |
| 369 | if (!embedded_script_debugger || !embedded_script_debugger->is_session_active() || embedded_script_debugger->get_remote_pid() != embedded_process->get_embedded_pid()) { |
| 370 | _attach_script_debugger(); |
| 371 | } |
| 372 | } |
| 373 | #endif |
| 374 | } |
| 375 | |
| 376 | void GameView::_instance_starting_static(int p_idx, List<String> &r_arguments) { |
| 377 | ERR_FAIL_NULL(singleton); |
| 378 | singleton->_instance_starting(p_idx, r_arguments); |
| 379 | } |
| 380 | |
| 381 | void GameView::_instance_starting(int p_idx, List<String> &r_arguments) { |
| 382 | if (!is_feature_enabled) { |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | 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) { |
| 387 | // Set the Floating Window default title. Always considered in DEBUG mode, same as in Window::set_title. |
| 388 | String appname = GLOBAL_GET("application/config/name"); |
| 389 | appname = vformat("%s (DEBUG)", TranslationServer::get_singleton()->translate(appname)); |
| 390 | window_wrapper->set_window_title(appname); |
| 391 | |
| 392 | _show_update_window_wrapper(); |
| 393 | |
| 394 | if (embedded_process->get_focus_mode_with_override() != FOCUS_NONE) { |
| 395 | embedded_process->grab_focus(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | _update_arguments_for_instance(p_idx, r_arguments); |
| 400 | } |
| 401 | |
| 402 | bool GameView::_instance_rq_screenshot_static(const Callable &p_callback) { |
| 403 | ERR_FAIL_NULL_V(singleton, false); |
| 404 | return singleton->_instance_rq_screenshot(p_callback); |
| 405 | } |
| 406 | |
| 407 | bool GameView::_instance_rq_screenshot(const Callable &p_callback) { |
| 408 | if (debugger.is_null() || window_wrapper->get_window_enabled() || !embedded_process || !embedded_process->is_embedding_completed()) { |
| 409 | return false; |
| 410 | } |
| 411 | Rect2 r = embedded_process->get_adjusted_embedded_window_rect(embedded_process->get_rect()); |
| 412 | r.position += embedded_process->get_global_position(); |
| 413 | #ifndef MACOS_ENABLED |
| 414 | r.position -= embedded_process->get_window()->get_position(); |
| 415 | #endif |
| 416 | return debugger->add_screenshot_callback(p_callback, r); |
| 417 | } |
| 418 | |
| 419 | void GameView::_show_update_window_wrapper() { |
| 420 | EditorRun::WindowPlacement placement = EditorRun::get_window_placement(); |
| 421 | Point2 position = floating_window_rect.position; |
| 422 | Size2i size = floating_window_rect.size; |
| 423 | int screen = floating_window_screen; |
| 424 | |
| 425 | // Obtain the size around the embedded process control. Usually, the difference between the game view's get_size |
| 426 | // and the embedded control should work. However, when the control is hidden and has never been displayed, |
| 427 | // the size of the embedded control is not calculated. |
| 428 | Size2 old_min_size = embedded_process->get_custom_minimum_size(); |
| 429 | embedded_process->set_custom_minimum_size(Size2i()); |
| 430 | |
| 431 | Size2 embedded_process_min_size = get_minimum_size(); |
| 432 | Size2 wrapped_margins_size = window_wrapper->get_margins_size(); |
| 433 | Size2 wrapped_min_size = window_wrapper->get_minimum_size(); |
| 434 | Point2 offset_embedded_process = embedded_process->get_global_position() - get_global_position(); |
| 435 | |
| 436 | // On the first startup, the global position of the embedded process control is invalid because it was |
| 437 | // never displayed. We will calculate it manually using the minimum size of the window. |
| 438 | if (offset_embedded_process == Point2()) { |
| 439 | offset_embedded_process.y = wrapped_min_size.y; |
| 440 | } |
| 441 | offset_embedded_process.x += embedded_process->get_margin_size(SIDE_LEFT); |
| 442 | offset_embedded_process.y += embedded_process->get_margin_size(SIDE_TOP); |
| 443 | offset_embedded_process += window_wrapper->get_margins_top_left(); |
| 444 | |
| 445 | embedded_process->set_custom_minimum_size(old_min_size); |
| 446 | |
| 447 | Point2 size_diff_embedded_process = Point2(0, embedded_process_min_size.y) + embedded_process->get_margins_size(); |
| 448 | |
| 449 | if (placement.position != Point2i(INT_MAX, INT_MAX)) { |
| 450 | position = placement.position - offset_embedded_process; |
| 451 | screen = placement.screen; |
| 452 | } |
| 453 | if (placement.size != Size2i()) { |
| 454 | size = placement.size + size_diff_embedded_process + wrapped_margins_size; |
| 455 | } |
| 456 | window_wrapper->restore_window_from_saved_position(Rect2(position, size), screen, Rect2i()); |
| 457 | } |
| 458 | |
| 459 | void GameView::_play_pressed() { |
| 460 | if (!is_feature_enabled) { |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | OS::ProcessID current_process_id = EditorRunBar::get_singleton()->get_current_process(); |
| 465 | if (current_process_id == 0) { |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | if (!window_wrapper->get_window_enabled()) { |
| 470 | screen_index_before_start = EditorNode::get_singleton()->get_editor_main_screen()->get_selected_index(); |
| 471 | } |
| 472 | |
| 473 | if (embed_on_play && _get_embed_available() == EMBED_AVAILABLE) { |
| 474 | // It's important to disable the low power mode when unfocused because otherwise |
| 475 | // the button in the editor are not responsive and if the user moves the mouse quickly, |
| 476 | // the mouse clicks are not registered. |
| 477 | EditorNode::get_singleton()->set_unfocused_low_processor_usage_mode_enabled(false); |
| 478 | _update_embed_window_size(); |
| 479 | if (!window_wrapper->get_window_enabled()) { |
| 480 | EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_GAME); |
| 481 | // Reset the normal size of the bottom panel when fully expanded. |
| 482 | EditorNode::get_singleton()->get_bottom_panel()->set_expanded(false); |
| 483 | |
| 484 | if (embedded_process->get_focus_mode_with_override() != FOCUS_NONE) { |
| 485 | embedded_process->grab_focus(); |
| 486 | } |
| 487 | } |
| 488 | embedded_process->embed_process(current_process_id); |
| 489 | _update_ui(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void GameView::_stop_pressed() { |
| 494 | if (!is_feature_enabled) { |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | _detach_script_debugger(); |
| 499 | paused = false; |
| 500 | |
| 501 | EditorNode::get_singleton()->set_unfocused_low_processor_usage_mode_enabled(true); |
| 502 | embedded_process->reset(); |
| 503 | _update_ui(); |
| 504 | |
| 505 | if (window_wrapper->get_window_enabled()) { |
| 506 | window_wrapper->set_window_enabled(false); |
| 507 | } |
| 508 | |
| 509 | if (screen_index_before_start >= 0 && EditorNode::get_singleton()->get_editor_main_screen()->get_selected_index() == EditorMainScreen::EDITOR_GAME) { |
| 510 | // We go back to the screen where the user was before starting the game. |
| 511 | EditorNode::get_singleton()->get_editor_main_screen()->select(screen_index_before_start); |
| 512 | } |
| 513 | |
| 514 | screen_index_before_start = -1; |
| 515 | } |
| 516 | |
| 517 | void GameView::_embedding_completed() { |
| 518 | #ifndef MACOS_ENABLED |
| 519 | _attach_script_debugger(); |
| 520 | #endif |
| 521 | _update_ui(); |
| 522 | if (make_floating_on_play) { |
| 523 | get_window()->set_flag(Window::FLAG_ALWAYS_ON_TOP, bool(GLOBAL_GET("display/window/size/always_on_top"))); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void GameView::_embedding_failed() { |
| 528 | state_label->set_text(TTRC("Connection impossible to the game process.")); |
| 529 | } |
| 530 | |
| 531 | void GameView::_embedded_process_updated() { |
| 532 | const Rect2i game_rect = embedded_process->get_screen_embedded_window_rect(); |
| 533 | game_size_label->set_text(vformat("%dx%d", game_rect.size.x, game_rect.size.y)); |
| 534 | } |
| 535 | |
| 536 | void GameView::_embedded_process_focused() { |
| 537 | if (embed_on_play && !window_wrapper->get_window_enabled()) { |
| 538 | EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_GAME); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | void GameView::_editor_or_project_settings_changed() { |
| 543 | if (!is_inside_tree()) { |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | // Update the window size and aspect ratio. |
| 548 | _update_embed_window_size(); |
| 549 | |
| 550 | if (window_wrapper->get_window_enabled()) { |
| 551 | _show_update_window_wrapper(); |
| 552 | if (embedded_process->is_embedding_completed()) { |
| 553 | embedded_process->queue_update_embedded_process(); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | _update_ui(); |
| 558 | } |
| 559 | |
| 560 | void GameView::_update_debugger_buttons() { |
| 561 | bool empty = active_sessions == 0; |
| 562 | |
| 563 | suspend_button->set_disabled(empty); |
| 564 | camera_override_button->set_disabled(empty); |
| 565 | speed_state_button->set_disabled(empty); |
| 566 | bool disabled = time_scale_index == DEFAULT_TIME_SCALE_INDEX; |
| 567 | |
| 568 | reset_speed_button->set_disabled(empty || disabled); |
| 569 | |
| 570 | PopupMenu *menu = camera_override_menu->get_popup(); |
| 571 | |
| 572 | bool disable_camera_reset = empty || !camera_override_button->is_pressed() || !menu->is_item_checked(menu->get_item_index(CAMERA_MODE_INGAME)); |
| 573 | menu->set_item_disabled(CAMERA_RESET_2D, disable_camera_reset); |
| 574 | menu->set_item_disabled(CAMERA_RESET_3D, disable_camera_reset); |
| 575 | |
| 576 | if (empty) { |
| 577 | suspend_button->set_pressed(false); |
| 578 | camera_override_button->set_pressed(false); |
| 579 | _reset_time_scales(); |
| 580 | } |
| 581 | |
| 582 | next_frame_button->set_disabled(!suspend_button->is_pressed()); |
| 583 | } |
| 584 | |
| 585 | void GameView::_handle_shortcut_requested(int p_embed_action) { |
| 586 | switch (p_embed_action) { |
| 587 | case ScriptEditorDebugger::EMBED_SUSPEND_TOGGLE: { |
| 588 | _toggle_suspend_button(); |
| 589 | } break; |
| 590 | case ScriptEditorDebugger::EMBED_NEXT_FRAME: { |
| 591 | debugger->next_frame(); |
| 592 | } break; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | void GameView::_toggle_suspend_button() { |
| 597 | const bool new_pressed = !suspend_button->is_pressed(); |
| 598 | suspend_button->set_pressed(new_pressed); |
| 599 | _suspend_button_toggled(new_pressed); |
| 600 | } |
| 601 | |
| 602 | void GameView::_suspend_button_toggled(bool p_pressed) { |
| 603 | _update_debugger_buttons(); |
| 604 | |
| 605 | debugger->set_suspend(p_pressed); |
| 606 | } |
| 607 | |
| 608 | void GameView::_node_type_pressed(int p_option) { |
| 609 | RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)p_option; |
| 610 | for (int i = 0; i < RuntimeNodeSelect::NODE_TYPE_MAX; i++) { |
| 611 | node_type_button[i]->set_pressed_no_signal(i == type); |
| 612 | } |
| 613 | |
| 614 | _update_debugger_buttons(); |
| 615 | |
| 616 | debugger->set_node_type(type); |
| 617 | } |
| 618 | |
| 619 | void GameView::_select_mode_pressed(int p_option) { |
| 620 | RuntimeNodeSelect::SelectMode mode = (RuntimeNodeSelect::SelectMode)p_option; |
| 621 | if (!select_mode_button[mode]->is_visible()) { |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | for (int i = 0; i < RuntimeNodeSelect::SELECT_MODE_MAX; i++) { |
| 626 | select_mode_button[i]->set_pressed_no_signal(i == mode); |
| 627 | } |
| 628 | |
| 629 | debugger->set_select_mode(mode); |
| 630 | |
| 631 | EditorSettings::get_singleton()->set_project_metadata("game_view", "select_mode", mode); |
| 632 | } |
| 633 | |
| 634 | void GameView::_selection_options_menu_id_pressed(int p_id) { |
| 635 | switch (p_id) { |
| 636 | case SELECTION_AVOID_LOCKED: { |
| 637 | selection_avoid_locked = !selection_avoid_locked; |
| 638 | debugger->set_selection_avoid_locked(selection_avoid_locked); |
| 639 | EditorSettings::get_singleton()->set_project_metadata("game_view", "selection_avoid_locked", selection_avoid_locked); |
| 640 | } break; |
| 641 | case SELECTION_PREFER_GROUP: { |
| 642 | selection_prefer_group = !selection_prefer_group; |
| 643 | debugger->set_selection_prefer_group(selection_prefer_group); |
| 644 | EditorSettings::get_singleton()->set_project_metadata("game_view", "selection_prefer_group", selection_prefer_group); |
| 645 | } break; |
| 646 | } |
| 647 | |
| 648 | PopupMenu *menu = selection_options_menu->get_popup(); |
| 649 | menu->set_item_checked(menu->get_item_index(SELECTION_AVOID_LOCKED), selection_avoid_locked); |
| 650 | menu->set_item_checked(menu->get_item_index(SELECTION_PREFER_GROUP), selection_prefer_group); |
| 651 | } |
| 652 | |
| 653 | void GameView::_embed_options_menu_menu_id_pressed(int p_id) { |
| 654 | switch (p_id) { |
| 655 | case EMBED_RUN_GAME_EMBEDDED: { |
| 656 | embed_on_play = !embed_on_play; |
| 657 | int game_mode = EDITOR_GET("run/window_placement/game_embed_mode"); |
| 658 | if (game_mode == 0) { // Save only if not overridden by editor. |
| 659 | EditorSettings::get_singleton()->set_project_metadata("game_view", "embed_on_play", embed_on_play); |
| 660 | } |
| 661 | } break; |
| 662 | case EMBED_MAKE_FLOATING_ON_PLAY: { |
| 663 | make_floating_on_play = !make_floating_on_play; |
| 664 | int game_mode = EDITOR_GET("run/window_placement/game_embed_mode"); |
| 665 | if (game_mode == 0) { // Save only if not overridden by editor. |
| 666 | EditorSettings::get_singleton()->set_project_metadata("game_view", "make_floating_on_play", make_floating_on_play); |
| 667 | } |
| 668 | } break; |
| 669 | case SIZE_MODE_FIXED: |
| 670 | case SIZE_MODE_KEEP_ASPECT: |
| 671 | case SIZE_MODE_STRETCH: { |
| 672 | embed_size_mode = (EmbedSizeMode)p_id; |
| 673 | EditorSettings::get_singleton()->set_project_metadata("game_view", "embed_size_mode", p_id); |
| 674 | |
| 675 | _update_embed_window_size(); |
| 676 | } break; |
| 677 | } |
| 678 | _update_embed_menu_options(); |
| 679 | _update_ui(); |
| 680 | } |
| 681 | |
| 682 | void GameView::_reset_time_scales() { |
| 683 | time_scale_index = DEFAULT_TIME_SCALE_INDEX; |
| 684 | debugger->reset_time_scale(); |
| 685 | if (is_inside_tree()) { |
| 686 | _update_speed_buttons(); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | void GameView::_speed_state_menu_pressed(int p_id) { |
| 691 | time_scale_index = p_id; |
| 692 | debugger->set_time_scale(time_scale_range[time_scale_index]); |
| 693 | _update_speed_buttons(); |
| 694 | } |
| 695 | |
| 696 | void GameView::_update_speed_buttons() { |
| 697 | bool disabled = time_scale_index == DEFAULT_TIME_SCALE_INDEX; |
| 698 | reset_speed_button->set_disabled(disabled); |
| 699 | speed_state_button->set_text(vformat(U"%s×", time_scale_label[time_scale_index])); |
| 700 | _update_speed_state_color(); |
| 701 | } |
| 702 | |
| 703 | void GameView::_update_speed_state_color() { |
| 704 | Color text_color; |
| 705 | if (time_scale_index == DEFAULT_TIME_SCALE_INDEX) { |
| 706 | text_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor)); |
| 707 | } else if (time_scale_index > DEFAULT_TIME_SCALE_INDEX) { |
| 708 | text_color = get_theme_color(SNAME("success_color"), EditorStringName(Editor)); |
| 709 | } else if (time_scale_index < DEFAULT_TIME_SCALE_INDEX) { |
| 710 | text_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor)); |
| 711 | } |
| 712 | speed_state_button->add_theme_color_override(SceneStringName(font_color), text_color); |
| 713 | } |
| 714 | |
| 715 | void GameView::_update_speed_state_size() { |
| 716 | if (!speed_state_button) { |
| 717 | return; |
| 718 | } |
| 719 | float min_size = 0; |
| 720 | for (const String lbl : time_scale_label) { |
| 721 | min_size = MAX(speed_state_button->get_minimum_size_for_text_and_icon(vformat(U"%s×", lbl), Ref<Texture2D>()).x, min_size); |
| 722 | } |
| 723 | speed_state_button->set_custom_minimum_size(Vector2(min_size, 0)); |
| 724 | } |
| 725 | |
| 726 | GameView::EmbedAvailability GameView::_get_embed_available() { |
| 727 | if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_EMBEDDING)) { |
| 728 | return EMBED_NOT_AVAILABLE_FEATURE_NOT_SUPPORTED; |
| 729 | } |
| 730 | if (get_tree()->get_root()->is_embedding_subwindows()) { |
| 731 | return EMBED_NOT_AVAILABLE_SINGLE_WINDOW_MODE; |
| 732 | } |
| 733 | String display_driver = GLOBAL_GET("display/display_server/driver"); |
| 734 | if (display_driver == "headless") { |
| 735 | return EMBED_NOT_AVAILABLE_PROJECT_DISPLAY_DRIVER; |
| 736 | } |
| 737 | |
| 738 | if (RunInstancesDialog::get_singleton()) { |
| 739 | List<String> instance_args; |
| 740 | RunInstancesDialog::get_singleton()->get_argument_list_for_instance(0, instance_args); |
| 741 | if (instance_args.find("--headless")) { |
| 742 | return EMBED_NOT_AVAILABLE_HEADLESS; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | EditorRun::WindowPlacement placement = EditorRun::get_window_placement(); |
| 747 | if (placement.force_fullscreen) { |
| 748 | return EMBED_NOT_AVAILABLE_FULLSCREEN; |
| 749 | } |
| 750 | if (placement.force_maximized) { |
| 751 | return EMBED_NOT_AVAILABLE_MAXIMIZED; |
| 752 | } |
| 753 | |
| 754 | DisplayServer::WindowMode window_mode = (DisplayServer::WindowMode)(GLOBAL_GET("display/window/size/mode").operator int()); |
| 755 | if (window_mode == DisplayServer::WindowMode::WINDOW_MODE_MINIMIZED) { |
| 756 | return EMBED_NOT_AVAILABLE_MINIMIZED; |
| 757 | } |
| 758 | if (window_mode == DisplayServer::WindowMode::WINDOW_MODE_MAXIMIZED) { |
| 759 | return EMBED_NOT_AVAILABLE_MAXIMIZED; |
| 760 | } |
| 761 | if (window_mode == DisplayServer::WindowMode::WINDOW_MODE_FULLSCREEN || window_mode == DisplayServer::WindowMode::WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 762 | return EMBED_NOT_AVAILABLE_FULLSCREEN; |
| 763 | } |
| 764 | |
| 765 | return EMBED_AVAILABLE; |
| 766 | } |
| 767 | |
| 768 | void GameView::_update_ui() { |
| 769 | bool show_game_size = false; |
| 770 | EmbedAvailability available = _get_embed_available(); |
| 771 | |
| 772 | switch (available) { |
| 773 | case EMBED_AVAILABLE: |
| 774 | if (embedded_process->is_embedding_completed()) { |
| 775 | state_label->set_text(""); |
| 776 | show_game_size = true; |
| 777 | } else if (embedded_process->is_embedding_in_progress()) { |
| 778 | state_label->set_text(TTRC("Game starting...")); |
| 779 | } else if (EditorRunBar::get_singleton()->is_playing()) { |
| 780 | state_label->set_text(TTRC("Game running not embedded.")); |
| 781 | } else if (embed_on_play) { |
| 782 | state_label->set_text(TTRC("Press play to start the game.")); |
| 783 | } else { |
| 784 | state_label->set_text(TTRC("Embedding is disabled.")); |
| 785 | } |
| 786 | break; |
| 787 | case EMBED_NOT_AVAILABLE_FEATURE_NOT_SUPPORTED: |
| 788 | state_label->set_text(TTRC("Game embedding not available on your OS.")); |
| 789 | break; |
| 790 | case EMBED_NOT_AVAILABLE_PROJECT_DISPLAY_DRIVER: |
| 791 | 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"))); |
| 792 | break; |
| 793 | case EMBED_NOT_AVAILABLE_MINIMIZED: |
| 794 | 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.")); |
| 795 | break; |
| 796 | case EMBED_NOT_AVAILABLE_MAXIMIZED: |
| 797 | 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.")); |
| 798 | break; |
| 799 | case EMBED_NOT_AVAILABLE_FULLSCREEN: |
| 800 | 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.")); |
| 801 | break; |
| 802 | case EMBED_NOT_AVAILABLE_SINGLE_WINDOW_MODE: |
| 803 | state_label->set_text(TTRC("Game embedding not available in single window mode.")); |
| 804 | break; |
| 805 | case EMBED_NOT_AVAILABLE_HEADLESS: |
| 806 | state_label->set_text(TTRC("Game embedding not available when the game starts in headless mode.")); |
| 807 | break; |
| 808 | } |
| 809 | |
| 810 | if (available == EMBED_AVAILABLE) { |
| 811 | if (state_label->has_theme_color_override(SceneStringName(font_color))) { |
| 812 | state_label->remove_theme_color_override(SceneStringName(font_color)); |
| 813 | } |
| 814 | } else { |
| 815 | state_label->add_theme_color_override(SceneStringName(font_color), state_label->get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); |
| 816 | } |
| 817 | |
| 818 | game_size_label->set_visible(show_game_size); |
| 819 | } |
| 820 | |
| 821 | void GameView::_update_embed_menu_options() { |
| 822 | bool is_multi_window = window_wrapper->is_window_available(); |
| 823 | PopupMenu *menu = embed_options_menu->get_popup(); |
| 824 | menu->set_item_checked(menu->get_item_index(EMBED_RUN_GAME_EMBEDDED), embed_on_play); |
| 825 | menu->set_item_checked(menu->get_item_index(EMBED_MAKE_FLOATING_ON_PLAY), make_floating_on_play && is_multi_window); |
| 826 | |
| 827 | menu->set_item_checked(menu->get_item_index(SIZE_MODE_FIXED), embed_size_mode == SIZE_MODE_FIXED); |
| 828 | menu->set_item_checked(menu->get_item_index(SIZE_MODE_KEEP_ASPECT), embed_size_mode == SIZE_MODE_KEEP_ASPECT); |
| 829 | menu->set_item_checked(menu->get_item_index(SIZE_MODE_STRETCH), embed_size_mode == SIZE_MODE_STRETCH); |
| 830 | |
| 831 | menu->set_item_disabled(menu->get_item_index(EMBED_MAKE_FLOATING_ON_PLAY), !embed_on_play || !is_multi_window); |
| 832 | } |
| 833 | |
| 834 | void GameView::_update_embed_window_size() { |
| 835 | if (paused) { |
| 836 | // When paused, Godot does not re-render. As a result, resizing the game window to a larger size |
| 837 | // causes artifacts and flickering. However, resizing to a smaller size seems fine. |
| 838 | // To prevent artifacts and flickering, we will force the game window to maintain its size. |
| 839 | // Using the same technique as SIZE_MODE_FIXED, the embedded process control will |
| 840 | // prevent resizing the game to a larger size while maintaining the aspect ratio. |
| 841 | embedded_process->set_window_size(size_paused); |
| 842 | embedded_process->set_keep_aspect(false); |
| 843 | |
| 844 | } else { |
| 845 | if (embed_size_mode == SIZE_MODE_FIXED || embed_size_mode == SIZE_MODE_KEEP_ASPECT) { |
| 846 | // The embedded process control will need the desired window size. |
| 847 | EditorRun::WindowPlacement placement = EditorRun::get_window_placement(); |
| 848 | embedded_process->set_window_size(placement.size); |
| 849 | } else { |
| 850 | // Stretch... No need for the window size. |
| 851 | embedded_process->set_window_size(Size2i()); |
| 852 | } |
| 853 | embedded_process->set_keep_aspect(embed_size_mode == SIZE_MODE_KEEP_ASPECT); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | void GameView::_hide_selection_toggled(bool p_pressed) { |
| 858 | hide_selection->set_button_icon(get_editor_theme_icon(p_pressed ? SNAME("GuiVisibilityHidden") : SNAME("GuiVisibilityVisible"))); |
| 859 | |
| 860 | debugger->set_selection_visible(!p_pressed); |
| 861 | |
| 862 | EditorSettings::get_singleton()->set_project_metadata("game_view", "hide_selection", p_pressed); |
| 863 | } |
| 864 | |
| 865 | void GameView::_debug_mute_audio_button_pressed() { |
| 866 | debug_mute_audio = !debug_mute_audio; |
| 867 | debug_mute_audio_button->set_button_icon(get_editor_theme_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer"))); |
| 868 | debug_mute_audio_button->set_tooltip_text(debug_mute_audio ? TTRC("Unmute game audio.") : TTRC("Mute game audio.")); |
| 869 | debugger->set_debug_mute_audio(debug_mute_audio); |
| 870 | } |
| 871 | |
| 872 | void GameView::_camera_override_button_toggled(bool p_pressed) { |
| 873 | _update_debugger_buttons(); |
| 874 | |
| 875 | debugger->set_camera_override(p_pressed); |
| 876 | } |
| 877 | |
| 878 | void GameView::_camera_override_menu_id_pressed(int p_id) { |
| 879 | PopupMenu *menu = camera_override_menu->get_popup(); |
| 880 | if (p_id != CAMERA_RESET_2D && p_id != CAMERA_RESET_3D) { |
| 881 | for (int i = 0; i < menu->get_item_count(); i++) { |
| 882 | menu->set_item_checked(i, false); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | switch (p_id) { |
| 887 | case CAMERA_RESET_2D: { |
| 888 | debugger->reset_camera_2d_position(); |
| 889 | } break; |
| 890 | case CAMERA_RESET_3D: { |
| 891 | debugger->reset_camera_3d_position(); |
| 892 | } break; |
| 893 | case CAMERA_MODE_INGAME: { |
| 894 | debugger->set_camera_manipulate_mode(EditorDebuggerNode::OVERRIDE_INGAME); |
| 895 | menu->set_item_checked(menu->get_item_index(p_id), true); |
| 896 | |
| 897 | _update_debugger_buttons(); |
| 898 | |
| 899 | EditorSettings::get_singleton()->set_project_metadata("game_view", "camera_override_mode", p_id); |
| 900 | } break; |
| 901 | case CAMERA_MODE_EDITORS: { |
| 902 | debugger->set_camera_manipulate_mode(EditorDebuggerNode::OVERRIDE_EDITORS); |
| 903 | menu->set_item_checked(menu->get_item_index(p_id), true); |
| 904 | |
| 905 | _update_debugger_buttons(); |
| 906 | |
| 907 | EditorSettings::get_singleton()->set_project_metadata("game_view", "camera_override_mode", p_id); |
| 908 | } break; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | void GameView::_notification(int p_what) { |
| 913 | switch (p_what) { |
| 914 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 915 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_tooltip_text(vformat(TTR("%s+Alt+RMB: Show list of all nodes at position clicked."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL))); |
| 916 | _update_ui(); |
| 917 | } break; |
| 918 | |
| 919 | case NOTIFICATION_POST_ENTER_TREE: { |
| 920 | _update_speed_state_size(); |
| 921 | } break; |
| 922 | |
| 923 | case NOTIFICATION_THEME_CHANGED: { |
| 924 | suspend_button->set_button_icon(get_editor_theme_icon(SNAME("Suspend"))); |
| 925 | next_frame_button->set_button_icon(get_editor_theme_icon(SNAME("NextFrame"))); |
| 926 | reset_speed_button->set_button_icon(get_editor_theme_icon(SNAME("Reload"))); |
| 927 | |
| 928 | node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_button_icon(get_editor_theme_icon(SNAME("InputEventJoypadMotion"))); |
| 929 | node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_button_icon(get_editor_theme_icon(SNAME("2DNodes"))); |
| 930 | node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_button_icon(get_editor_theme_icon(SNAME("Node3D"))); |
| 931 | |
| 932 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect"))); |
| 933 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_button_icon(get_editor_theme_icon(SNAME("ListSelect"))); |
| 934 | |
| 935 | hide_selection->set_button_icon(get_editor_theme_icon(hide_selection->is_pressed() ? SNAME("GuiVisibilityHidden") : SNAME("GuiVisibilityVisible"))); |
| 936 | selection_options_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl"))); |
| 937 | embed_options_menu->set_button_icon(get_editor_theme_icon(SNAME("KeepAspect"))); |
| 938 | |
| 939 | debug_mute_audio_button->set_button_icon(get_editor_theme_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer"))); |
| 940 | |
| 941 | camera_override_button->set_button_icon(get_editor_theme_icon(SNAME("Camera"))); |
| 942 | camera_override_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl"))); |
| 943 | |
| 944 | _update_speed_state_size(); |
| 945 | _update_speed_state_color(); |
| 946 | } break; |
| 947 | |
| 948 | case NOTIFICATION_READY: { |
| 949 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_EMBEDDING)) { |
| 950 | // Embedding available. |
| 951 | int game_mode = EDITOR_GET("run/window_placement/game_embed_mode"); |
| 952 | switch (game_mode) { |
| 953 | case -1: { // Disabled. |
| 954 | embed_on_play = false; |
| 955 | make_floating_on_play = false; |
| 956 | } break; |
| 957 | case 1: { // Embed. |
| 958 | embed_on_play = true; |
| 959 | make_floating_on_play = false; |
| 960 | } break; |
| 961 | case 2: { // Floating. |
| 962 | embed_on_play = true; |
| 963 | make_floating_on_play = true; |
| 964 | } break; |
| 965 | default: { |
| 966 | embed_on_play = EditorSettings::get_singleton()->get_project_metadata("game_view", "embed_on_play", true); |
| 967 | make_floating_on_play = EditorSettings::get_singleton()->get_project_metadata("game_view", "make_floating_on_play", true); |
| 968 | } break; |
| 969 | } |
| 970 | embed_size_mode = (EmbedSizeMode)(int)EditorSettings::get_singleton()->get_project_metadata("game_view", "embed_size_mode", SIZE_MODE_FIXED); |
| 971 | _update_embed_menu_options(); |
| 972 | |
| 973 | EditorRunBar::get_singleton()->connect("play_pressed", callable_mp(this, &GameView::_play_pressed)); |
| 974 | EditorRunBar::get_singleton()->connect("stop_pressed", callable_mp(this, &GameView::_stop_pressed)); |
| 975 | EditorRun::instance_starting_callback = _instance_starting_static; |
| 976 | EditorRun::instance_rq_screenshot_callback = _instance_rq_screenshot_static; |
| 977 | |
| 978 | // Listen for project settings changes to update the window size and aspect ratio. |
| 979 | ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &GameView::_editor_or_project_settings_changed)); |
| 980 | EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &GameView::_editor_or_project_settings_changed)); |
| 981 | } else { |
| 982 | // Embedding not available. |
| 983 | embedding_hb->hide(); |
| 984 | } |
| 985 | |
| 986 | _update_ui(); |
| 987 | } break; |
| 988 | case NOTIFICATION_WM_POSITION_CHANGED: { |
| 989 | if (window_wrapper->get_window_enabled()) { |
| 990 | _update_floating_window_settings(); |
| 991 | } |
| 992 | } break; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | void GameView::set_window_layout(Ref<ConfigFile> p_layout) { |
| 997 | floating_window_rect = p_layout->get_value("GameView", "floating_window_rect", Rect2i()); |
| 998 | floating_window_screen = p_layout->get_value("GameView", "floating_window_screen", -1); |
| 999 | } |
| 1000 | |
| 1001 | void GameView::get_window_layout(Ref<ConfigFile> p_layout) { |
| 1002 | if (window_wrapper->get_window_enabled()) { |
| 1003 | _update_floating_window_settings(); |
| 1004 | } |
| 1005 | |
| 1006 | p_layout->set_value("GameView", "floating_window_rect", floating_window_rect); |
| 1007 | p_layout->set_value("GameView", "floating_window_screen", floating_window_screen); |
| 1008 | } |
| 1009 | |
| 1010 | void GameView::_update_floating_window_settings() { |
| 1011 | if (window_wrapper->get_window_enabled()) { |
| 1012 | floating_window_rect = window_wrapper->get_window_rect(); |
| 1013 | floating_window_screen = window_wrapper->get_window_screen(); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | void GameView::_attach_script_debugger() { |
| 1018 | _detach_script_debugger(); |
| 1019 | |
| 1020 | int i = 0; |
| 1021 | while (ScriptEditorDebugger *script_debugger = EditorDebuggerNode::get_singleton()->get_debugger(i)) { |
| 1022 | if (script_debugger->is_session_active() && script_debugger->get_remote_pid() == embedded_process->get_embedded_pid()) { |
| 1023 | embedded_script_debugger = script_debugger; |
| 1024 | break; |
| 1025 | } |
| 1026 | i++; |
| 1027 | } |
| 1028 | |
| 1029 | #ifdef MACOS_ENABLED |
| 1030 | embedded_process->set_script_debugger(embedded_script_debugger); |
| 1031 | #endif |
| 1032 | |
| 1033 | if (embedded_script_debugger) { |
| 1034 | embedded_script_debugger->connect("remote_window_title_changed", callable_mp(this, &GameView::_remote_window_title_changed)); |
| 1035 | embedded_script_debugger->connect("embed_shortcut_requested", callable_mp(this, &GameView::_handle_shortcut_requested)); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | void GameView::_detach_script_debugger() { |
| 1040 | if (embedded_script_debugger) { |
| 1041 | embedded_script_debugger->disconnect("remote_window_title_changed", callable_mp(this, &GameView::_remote_window_title_changed)); |
| 1042 | embedded_script_debugger->disconnect("embed_shortcut_requested", callable_mp(this, &GameView::_handle_shortcut_requested)); |
| 1043 | embedded_script_debugger = nullptr; |
| 1044 | } |
| 1045 | embedded_process->set_script_debugger(nullptr); |
| 1046 | } |
| 1047 | |
| 1048 | void GameView::_remote_window_title_changed(String title) { |
| 1049 | window_wrapper->set_window_title(title); |
| 1050 | } |
| 1051 | |
| 1052 | void GameView::_update_arguments_for_instance(int p_idx, List<String> &r_arguments) { |
| 1053 | if (p_idx != 0 || !embed_on_play || _get_embed_available() != EMBED_AVAILABLE) { |
| 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | // Remove duplicates/unwanted parameters. |
| 1058 | List<String>::Element *E = r_arguments.front(); |
| 1059 | List<String>::Element *user_args_element = nullptr; |
| 1060 | HashSet<String> remove_args({ "--position", "--resolution", "--screen" }); |
| 1061 | #ifdef MACOS_ENABLED |
| 1062 | // macOS requires the embedded display driver. |
| 1063 | remove_args.insert("--display-driver"); |
| 1064 | #endif |
| 1065 | |
| 1066 | #ifdef WAYLAND_ENABLED |
| 1067 | // Wayland requires its display driver. |
| 1068 | if (DisplayServer::get_singleton()->get_name() == "Wayland") { |
| 1069 | remove_args.insert("--display-driver"); |
| 1070 | } |
| 1071 | #endif |
| 1072 | |
| 1073 | #ifdef X11_ENABLED |
| 1074 | // X11 requires its display driver. |
| 1075 | if (DisplayServer::get_singleton()->get_name() == "X11") { |
| 1076 | remove_args.insert("--display-driver"); |
| 1077 | } |
| 1078 | #endif |
| 1079 | |
| 1080 | while (E) { |
| 1081 | List<String>::Element *N = E->next(); |
| 1082 | |
| 1083 | // For these parameters, we need to also remove the value. |
| 1084 | if (remove_args.has(E->get())) { |
| 1085 | r_arguments.erase(E); |
| 1086 | if (N) { |
| 1087 | List<String>::Element *V = N->next(); |
| 1088 | r_arguments.erase(N); |
| 1089 | N = V; |
| 1090 | } |
| 1091 | } else if (E->get() == "-f" || E->get() == "--fullscreen" || E->get() == "-m" || E->get() == "--maximized" || E->get() == "-t" || E->get() == "-always-on-top") { |
| 1092 | r_arguments.erase(E); |
| 1093 | } else if (E->get() == "--" || E->get() == "++") { |
| 1094 | user_args_element = E; |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | E = N; |
| 1099 | } |
| 1100 | |
| 1101 | // Add the editor window's native ID so the started game can directly set it as its parent. |
| 1102 | List<String>::Element *N = r_arguments.insert_before(user_args_element, "--wid"); |
| 1103 | N = r_arguments.insert_after(N, itos(DisplayServer::get_singleton()->window_get_native_handle(DisplayServer::WINDOW_HANDLE, get_window()->get_window_id()))); |
| 1104 | |
| 1105 | #if MACOS_ENABLED |
| 1106 | N = r_arguments.insert_after(N, "--embedded"); |
| 1107 | #endif |
| 1108 | |
| 1109 | #ifdef WAYLAND_ENABLED |
| 1110 | if (DisplayServer::get_singleton()->get_name() == "Wayland") { |
| 1111 | N = r_arguments.insert_after(N, "--display-driver"); |
| 1112 | N = r_arguments.insert_after(N, "wayland"); |
| 1113 | } |
| 1114 | #endif |
| 1115 | |
| 1116 | #ifdef X11_ENABLED |
| 1117 | if (DisplayServer::get_singleton()->get_name() == "X11") { |
| 1118 | N = r_arguments.insert_after(N, "--display-driver"); |
| 1119 | N = r_arguments.insert_after(N, "x11"); |
| 1120 | } |
| 1121 | #endif |
| 1122 | |
| 1123 | // Be sure to have the correct window size in the embedded_process control. |
| 1124 | _update_embed_window_size(); |
| 1125 | Rect2i rect = embedded_process->get_screen_embedded_window_rect(); |
| 1126 | |
| 1127 | // Usually, the global rect of the embedded process control is invalid because it was hidden. We will calculate it manually. |
| 1128 | if (!window_wrapper->get_window_enabled()) { |
| 1129 | Size2 old_min_size = embedded_process->get_custom_minimum_size(); |
| 1130 | embedded_process->set_custom_minimum_size(Size2i()); |
| 1131 | |
| 1132 | Control *container = EditorNode::get_singleton()->get_editor_main_screen()->get_control(); |
| 1133 | rect = container->get_global_rect(); |
| 1134 | |
| 1135 | Size2 wrapped_min_size = window_wrapper->get_minimum_size(); |
| 1136 | rect.position.y += wrapped_min_size.y; |
| 1137 | rect.size.y -= wrapped_min_size.y; |
| 1138 | |
| 1139 | rect = embedded_process->get_adjusted_embedded_window_rect(rect); |
| 1140 | |
| 1141 | embedded_process->set_custom_minimum_size(old_min_size); |
| 1142 | } |
| 1143 | |
| 1144 | // When using the floating window, we need to force the position and size from the |
| 1145 | // editor/project settings, because the get_screen_embedded_window_rect of the |
| 1146 | // embedded_process will be updated only on the next frame. |
| 1147 | if (window_wrapper->get_window_enabled()) { |
| 1148 | EditorRun::WindowPlacement placement = EditorRun::get_window_placement(); |
| 1149 | if (placement.position != Point2i(INT_MAX, INT_MAX)) { |
| 1150 | rect.position = placement.position; |
| 1151 | } |
| 1152 | if (placement.size != Size2i()) { |
| 1153 | rect.size = placement.size; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | N = r_arguments.insert_after(N, "--position"); |
| 1158 | N = r_arguments.insert_after(N, itos(rect.position.x) + "," + itos(rect.position.y)); |
| 1159 | N = r_arguments.insert_after(N, "--resolution"); |
| 1160 | r_arguments.insert_after(N, itos(rect.size.x) + "x" + itos(rect.size.y)); |
| 1161 | } |
| 1162 | |
| 1163 | void GameView::_window_close_request() { |
| 1164 | if (window_wrapper->get_window_enabled()) { |
| 1165 | // Stop the embedded process timer before closing the window wrapper, |
| 1166 | // so the signal to focus EDITOR_GAME isn't sent when the window is not enabled. |
| 1167 | embedded_process->reset_timers(); |
| 1168 | window_wrapper->set_window_enabled(false); |
| 1169 | } |
| 1170 | |
| 1171 | // Before the parent window closed, we close the embedded game. That prevents |
| 1172 | // the embedded game to be seen without a parent window for a fraction of second. |
| 1173 | if (EditorRunBar::get_singleton()->is_playing() && (embedded_process->is_embedding_completed() || embedded_process->is_embedding_in_progress())) { |
| 1174 | // When the embedding is not complete, we need to kill the process. |
| 1175 | // If the game is paused, the close request will not be processed by the game, so it's better to kill the process. |
| 1176 | if (paused || embedded_process->is_embedding_in_progress()) { |
| 1177 | // Call deferred to prevent the _stop_pressed callback to be executed before the wrapper window |
| 1178 | // actually closes. |
| 1179 | embedded_process->reset(); |
| 1180 | callable_mp(EditorRunBar::get_singleton(), &EditorRunBar::stop_playing).call_deferred(); |
| 1181 | } else { |
| 1182 | // Try to gracefully close the window. That way, the NOTIFICATION_WM_CLOSE_REQUEST |
| 1183 | // notification should be propagated in the game process. |
| 1184 | embedded_process->request_close(); |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | void GameView::_debugger_breaked(bool p_breaked, bool p_can_debug) { |
| 1190 | if (p_breaked == paused) { |
| 1191 | return; |
| 1192 | } |
| 1193 | |
| 1194 | paused = p_breaked; |
| 1195 | |
| 1196 | if (paused) { |
| 1197 | size_paused = embedded_process->get_screen_embedded_window_rect().size; |
| 1198 | } |
| 1199 | |
| 1200 | _update_embed_window_size(); |
| 1201 | } |
| 1202 | |
| 1203 | void GameView::_feature_profile_changed() { |
| 1204 | Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile(); |
| 1205 | bool is_profile_null = profile.is_null(); |
| 1206 | |
| 1207 | is_feature_enabled = is_profile_null || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_GAME); |
| 1208 | |
| 1209 | bool is_3d_enabled = is_profile_null || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D); |
| 1210 | if (!is_3d_enabled && node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->is_pressed()) { |
| 1211 | _node_type_pressed(RuntimeNodeSelect::NODE_TYPE_NONE); |
| 1212 | } |
| 1213 | node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_visible(is_3d_enabled); |
| 1214 | } |
| 1215 | |
| 1216 | GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process, WindowWrapper *p_wrapper) { |
| 1217 | singleton = this; |
| 1218 | |
| 1219 | debugger = p_debugger; |
| 1220 | window_wrapper = p_wrapper; |
| 1221 | embedded_process = p_embedded_process; |
| 1222 | |
| 1223 | MarginContainer *toolbar_margin = memnew(MarginContainer); |
| 1224 | toolbar_margin->set_theme_type_variation("MainToolBarMargin"); |
| 1225 | add_child(toolbar_margin); |
| 1226 | |
| 1227 | FlowContainer *main_menu_fc = memnew(FlowContainer); |
| 1228 | toolbar_margin->add_child(main_menu_fc); |
| 1229 | |
| 1230 | HBoxContainer *process_hb = memnew(HBoxContainer); |
| 1231 | main_menu_fc->add_child(process_hb); |
| 1232 | suspend_button = memnew(Button); |
| 1233 | process_hb->add_child(suspend_button); |
| 1234 | suspend_button->set_toggle_mode(true); |
| 1235 | suspend_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 1236 | suspend_button->connect(SceneStringName(toggled), callable_mp(this, &GameView::_suspend_button_toggled)); |
| 1237 | suspend_button->set_accessibility_name(TTRC("Suspend")); |
| 1238 | suspend_button->set_shortcut(ED_GET_SHORTCUT("editor/suspend_resume_embedded_project")); |
| 1239 | suspend_button->set_tooltip_text(TTRC("Force pause at SceneTree level. Stops all processing, but you can still interact with the project.")); |
| 1240 | |
| 1241 | next_frame_button = memnew(Button); |
| 1242 | process_hb->add_child(next_frame_button); |
| 1243 | next_frame_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 1244 | next_frame_button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::next_frame)); |
| 1245 | next_frame_button->set_accessibility_name(TTRC("Next Frame")); |
| 1246 | next_frame_button->set_shortcut(ED_GET_SHORTCUT("editor/next_frame_embedded_project")); |
| 1247 | |
| 1248 | speed_state_button = memnew(MenuButton); |
| 1249 | process_hb->add_child(speed_state_button); |
| 1250 | speed_state_button->set_text(U"1.0×"); |
| 1251 | speed_state_button->set_flat(false); |
| 1252 | speed_state_button->set_theme_type_variation("FlatMenuButton"); |
| 1253 | speed_state_button->set_tooltip_text(TTRC("Change the game speed.")); |
| 1254 | speed_state_button->set_accessibility_name(TTRC("Speed State")); |
| 1255 | |
| 1256 | PopupMenu *menu = speed_state_button->get_popup(); |
| 1257 | menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_speed_state_menu_pressed)); |
| 1258 | for (String lbl : time_scale_label) { |
| 1259 | menu->add_item(vformat(U"%s×", lbl)); |
| 1260 | } |
| 1261 | |
| 1262 | reset_speed_button = memnew(Button); |
| 1263 | process_hb->add_child(reset_speed_button); |
| 1264 | reset_speed_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 1265 | reset_speed_button->set_tooltip_text(TTRC("Reset the game speed.")); |
| 1266 | reset_speed_button->set_accessibility_name(TTRC("Reset Speed")); |
| 1267 | reset_speed_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_reset_time_scales)); |
| 1268 | |
| 1269 | process_hb->add_child(memnew(VSeparator)); |
| 1270 | |
| 1271 | HBoxContainer *input_hb = memnew(HBoxContainer); |
| 1272 | main_menu_fc->add_child(input_hb); |
| 1273 | |
| 1274 | node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE] = memnew(Button); |
| 1275 | input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]); |
| 1276 | node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_text(TTRC("Input")); |
| 1277 | node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_toggle_mode(true); |
| 1278 | node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_pressed(true); |
| 1279 | node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_theme_type_variation("FlatButtonNoIconTint"); |
| 1280 | node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_NONE)); |
| 1281 | node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_tooltip_text(TTRC("Allow game input.")); |
| 1282 | |
| 1283 | node_type_button[RuntimeNodeSelect::NODE_TYPE_2D] = memnew(Button); |
| 1284 | input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]); |
| 1285 | node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_text(TTRC("2D")); |
| 1286 | node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_toggle_mode(true); |
| 1287 | node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_theme_type_variation("FlatButtonNoIconTint"); |
| 1288 | node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_2D)); |
| 1289 | node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_tooltip_text(TTRC("Disable game input and allow to select Node2Ds, Controls, and manipulate the 2D camera.")); |
| 1290 | |
| 1291 | node_type_button[RuntimeNodeSelect::NODE_TYPE_3D] = memnew(Button); |
| 1292 | input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]); |
| 1293 | node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_text(TTRC("3D")); |
| 1294 | node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_toggle_mode(true); |
| 1295 | node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_theme_type_variation("FlatButtonNoIconTint"); |
| 1296 | node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_3D)); |
| 1297 | node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_tooltip_text(TTRC("Disable game input and allow to select Node3Ds and manipulate the 3D camera.")); |
| 1298 | |
| 1299 | input_hb->add_child(memnew(VSeparator)); |
| 1300 | |
| 1301 | HBoxContainer *selection_hb = memnew(HBoxContainer); |
| 1302 | main_menu_fc->add_child(selection_hb); |
| 1303 | |
| 1304 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE] = memnew(Button); |
| 1305 | selection_hb->add_child(select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]); |
| 1306 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_toggle_mode(true); |
| 1307 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_pressed(true); |
| 1308 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_theme_type_variation(SceneStringName(FlatButton)); |
| 1309 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_select_mode_pressed).bind(RuntimeNodeSelect::SELECT_MODE_SINGLE)); |
| 1310 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_shortcut(ED_GET_SHORTCUT("spatial_editor/tool_select")); |
| 1311 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_shortcut_context(this); |
| 1312 | |
| 1313 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST] = memnew(Button); |
| 1314 | selection_hb->add_child(select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]); |
| 1315 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_toggle_mode(true); |
| 1316 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_theme_type_variation(SceneStringName(FlatButton)); |
| 1317 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_select_mode_pressed).bind(RuntimeNodeSelect::SELECT_MODE_LIST)); |
| 1318 | select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_tooltip_text(TTRC("Show list of selectable nodes at position clicked.")); |
| 1319 | |
| 1320 | _select_mode_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "select_mode", 0)); |
| 1321 | |
| 1322 | hide_selection = memnew(Button); |
| 1323 | selection_hb->add_child(hide_selection); |
| 1324 | hide_selection->set_toggle_mode(true); |
| 1325 | hide_selection->set_theme_type_variation(SceneStringName(FlatButton)); |
| 1326 | hide_selection->set_tooltip_text(TTRC("Toggle Selection Visibility")); |
| 1327 | hide_selection->set_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "hide_selection", false)); |
| 1328 | if (hide_selection->is_pressed()) { |
| 1329 | debugger->set_selection_visible(false); |
| 1330 | } |
| 1331 | hide_selection->connect(SceneStringName(toggled), callable_mp(this, &GameView::_hide_selection_toggled)); |
| 1332 | |
| 1333 | selection_options_menu = memnew(MenuButton); |
| 1334 | selection_hb->add_child(selection_options_menu); |
| 1335 | selection_options_menu->set_flat(false); |
| 1336 | selection_options_menu->set_theme_type_variation("FlatMenuButton"); |
| 1337 | selection_options_menu->set_h_size_flags(SIZE_SHRINK_END); |
| 1338 | selection_options_menu->set_tooltip_text(TTRC("Selection Options")); |
| 1339 | |
| 1340 | PopupMenu *selection_menu = selection_options_menu->get_popup(); |
| 1341 | selection_menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_selection_options_menu_id_pressed)); |
| 1342 | selection_menu->add_check_item(TTRC("Don't Select Locked Nodes"), SELECTION_AVOID_LOCKED); |
| 1343 | selection_menu->add_check_item(TTRC("Select Group Over Children"), SELECTION_PREFER_GROUP); |
| 1344 | |
| 1345 | selection_avoid_locked = EditorSettings::get_singleton()->get_project_metadata("game_view", "selection_avoid_locked", false); |
| 1346 | selection_prefer_group = EditorSettings::get_singleton()->get_project_metadata("game_view", "selection_prefer_group", false); |
| 1347 | selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_AVOID_LOCKED), selection_avoid_locked); |
| 1348 | selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_PREFER_GROUP), selection_prefer_group); |
| 1349 | |
| 1350 | debugger->set_selection_avoid_locked(selection_avoid_locked); |
| 1351 | debugger->set_selection_prefer_group(selection_prefer_group); |
| 1352 | |
| 1353 | selection_hb->add_child(memnew(VSeparator)); |
| 1354 | |
| 1355 | HBoxContainer *audio_hb = memnew(HBoxContainer); |
| 1356 | main_menu_fc->add_child(audio_hb); |
| 1357 | |
| 1358 | debug_mute_audio_button = memnew(Button); |
| 1359 | audio_hb->add_child(debug_mute_audio_button); |
| 1360 | debug_mute_audio_button->set_theme_type_variation("FlatButton"); |
| 1361 | debug_mute_audio_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_debug_mute_audio_button_pressed)); |
| 1362 | debug_mute_audio_button->set_tooltip_text(debug_mute_audio ? TTRC("Unmute game audio.") : TTRC("Mute game audio.")); |
| 1363 | |
| 1364 | audio_hb->add_child(memnew(VSeparator)); |
| 1365 | |
| 1366 | HBoxContainer *camera_hb = memnew(HBoxContainer); |
| 1367 | main_menu_fc->add_child(camera_hb); |
| 1368 | |
| 1369 | camera_override_button = memnew(Button); |
| 1370 | camera_hb->add_child(camera_override_button); |
| 1371 | camera_override_button->set_toggle_mode(true); |
| 1372 | camera_override_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 1373 | camera_override_button->set_tooltip_text(TTRC("Override the in-game camera.")); |
| 1374 | camera_override_button->connect(SceneStringName(toggled), callable_mp(this, &GameView::_camera_override_button_toggled)); |
| 1375 | |
| 1376 | camera_override_menu = memnew(MenuButton); |
| 1377 | camera_hb->add_child(camera_override_menu); |
| 1378 | camera_override_menu->set_flat(false); |
| 1379 | camera_override_menu->set_theme_type_variation("FlatMenuButton"); |
| 1380 | camera_override_menu->set_h_size_flags(SIZE_SHRINK_END); |
| 1381 | camera_override_menu->set_tooltip_text(TTRC("Camera Override Options")); |
| 1382 | |
| 1383 | menu = camera_override_menu->get_popup(); |
| 1384 | menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_camera_override_menu_id_pressed)); |
| 1385 | menu->add_item(TTRC("Reset 2D Camera"), CAMERA_RESET_2D); |
| 1386 | menu->add_item(TTRC("Reset 3D Camera"), CAMERA_RESET_3D); |
| 1387 | menu->add_separator(); |
| 1388 | menu->add_radio_check_item(TTRC("Manipulate In-Game"), CAMERA_MODE_INGAME); |
| 1389 | menu->set_item_checked(menu->get_item_index(CAMERA_MODE_INGAME), true); |
| 1390 | menu->add_radio_check_item(TTRC("Manipulate From Editors"), CAMERA_MODE_EDITORS); |
| 1391 | _camera_override_menu_id_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "camera_override_mode", 0)); |
| 1392 | |
| 1393 | camera_hb->add_child(memnew(VSeparator)); |
| 1394 | |
| 1395 | embedding_hb = memnew(HBoxContainer); |
| 1396 | embedding_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1397 | main_menu_fc->add_child(embedding_hb); |
| 1398 | |
| 1399 | embed_options_menu = memnew(MenuButton); |
| 1400 | embedding_hb->add_child(embed_options_menu); |
| 1401 | embed_options_menu->set_flat(false); |
| 1402 | embed_options_menu->set_theme_type_variation("FlatMenuButton"); |
| 1403 | embed_options_menu->set_h_size_flags(SIZE_SHRINK_END); |
| 1404 | embed_options_menu->set_tooltip_text(TTRC("Embedding Options")); |
| 1405 | |
| 1406 | menu = embed_options_menu->get_popup(); |
| 1407 | menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_embed_options_menu_menu_id_pressed)); |
| 1408 | menu->add_check_item(TTRC("Embed Game on Next Play"), EMBED_RUN_GAME_EMBEDDED); |
| 1409 | menu->add_check_item(TTRC("Make Game Workspace Floating on Next Play"), EMBED_MAKE_FLOATING_ON_PLAY); |
| 1410 | menu->add_separator(TTRC("Embedded Window Sizing")); |
| 1411 | |
| 1412 | menu->add_radio_check_item(TTRC("Fixed Size"), SIZE_MODE_FIXED); |
| 1413 | menu->set_item_tooltip(menu->get_item_index(SIZE_MODE_FIXED), TTRC("Embedded game size is based on project settings.\nThe 'Keep Aspect' mode is used when the Game Workspace is smaller than the desired size.")); |
| 1414 | menu->add_radio_check_item(TTRC("Keep Aspect Ratio"), SIZE_MODE_KEEP_ASPECT); |
| 1415 | menu->set_item_tooltip(menu->get_item_index(SIZE_MODE_KEEP_ASPECT), TTRC("Keep the aspect ratio of the embedded game.")); |
| 1416 | menu->add_radio_check_item(TTRC("Stretch to Fit"), SIZE_MODE_STRETCH); |
| 1417 | menu->set_item_tooltip(menu->get_item_index(SIZE_MODE_STRETCH), TTRC("Embedded game size stretches to fit the Game Workspace.")); |
| 1418 | |
| 1419 | game_size_label = memnew(Label()); |
| 1420 | embedding_hb->add_child(game_size_label); |
| 1421 | game_size_label->hide(); |
| 1422 | // Setting the minimum size prevents the game workspace from resizing indefinitely |
| 1423 | // due to the label size oscillating by a few pixels when the game is in stretch mode |
| 1424 | // and the game workspace is at its minimum size. |
| 1425 | game_size_label->set_custom_minimum_size(Size2(80 * EDSCALE, 0)); |
| 1426 | game_size_label->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1427 | game_size_label->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_RIGHT); |
| 1428 | |
| 1429 | panel = memnew(Panel); |
| 1430 | add_child(panel); |
| 1431 | panel->set_theme_type_variation("GamePanel"); |
| 1432 | panel->set_v_size_flags(SIZE_EXPAND_FILL); |
| 1433 | |
| 1434 | panel->add_child(embedded_process); |
| 1435 | embedded_process->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 1436 | embedded_process->connect("embedding_failed", callable_mp(this, &GameView::_embedding_failed)); |
| 1437 | embedded_process->connect("embedding_completed", callable_mp(this, &GameView::_embedding_completed)); |
| 1438 | embedded_process->connect("embedded_process_updated", callable_mp(this, &GameView::_embedded_process_updated)); |
| 1439 | embedded_process->connect("embedded_process_focused", callable_mp(this, &GameView::_embedded_process_focused)); |
| 1440 | embedded_process->set_custom_minimum_size(Size2i(100, 100)); |
| 1441 | |
| 1442 | MarginContainer *state_container = memnew(MarginContainer); |
| 1443 | state_container->add_theme_constant_override("margin_left", 8 * EDSCALE); |
| 1444 | state_container->add_theme_constant_override("margin_right", 8 * EDSCALE); |
| 1445 | state_container->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 1446 | #ifdef MACOS_ENABLED |
| 1447 | state_container->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); |
| 1448 | #endif |
| 1449 | |
| 1450 | panel->add_child(state_container); |
| 1451 | state_label = memnew(Label()); |
| 1452 | state_container->add_child(state_label); |
| 1453 | state_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 1454 | state_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
| 1455 | state_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD); |
| 1456 | state_label->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 1457 | |
| 1458 | _update_debugger_buttons(); |
| 1459 | |
| 1460 | p_debugger->connect("session_started", callable_mp(this, &GameView::_sessions_changed)); |
| 1461 | p_debugger->connect("session_stopped", callable_mp(this, &GameView::_sessions_changed)); |
| 1462 | |
| 1463 | p_wrapper->set_override_close_request(true); |
| 1464 | p_wrapper->connect("window_close_requested", callable_mp(this, &GameView::_window_close_request)); |
| 1465 | p_wrapper->connect("window_size_changed", callable_mp(this, &GameView::_update_floating_window_settings)); |
| 1466 | |
| 1467 | EditorDebuggerNode::get_singleton()->connect("breaked", callable_mp(this, &GameView::_debugger_breaked)); |
| 1468 | |
| 1469 | EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &GameView::_feature_profile_changed)); |
| 1470 | } |
| 1471 | |
| 1472 | /////// |
| 1473 | |
| 1474 | void GameViewPluginBase::selected_notify() { |
| 1475 | if (_is_window_wrapper_enabled()) { |
| 1476 | #ifdef ANDROID_ENABLED |
| 1477 | notify_main_screen_changed(get_plugin_name()); |
| 1478 | #else |
| 1479 | window_wrapper->grab_window_focus(); |
| 1480 | #endif // ANDROID_ENABLED |
| 1481 | _focus_another_editor(); |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | #ifndef ANDROID_ENABLED |
| 1486 | void GameViewPluginBase::make_visible(bool p_visible) { |
| 1487 | if (p_visible) { |
| 1488 | window_wrapper->show(); |
| 1489 | } else { |
| 1490 | window_wrapper->hide(); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | void GameViewPluginBase::set_window_layout(Ref<ConfigFile> p_layout) { |
| 1495 | game_view->set_window_layout(p_layout); |
| 1496 | } |
| 1497 | |
| 1498 | void GameViewPluginBase::get_window_layout(Ref<ConfigFile> p_layout) { |
| 1499 | game_view->get_window_layout(p_layout); |
| 1500 | } |
| 1501 | |
| 1502 | void GameViewPluginBase::setup(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process) { |
| 1503 | debugger = p_debugger; |
| 1504 | |
| 1505 | window_wrapper = memnew(WindowWrapper); |
| 1506 | window_wrapper->set_margins_enabled(true); |
| 1507 | |
| 1508 | game_view = memnew(GameView(debugger, p_embedded_process, window_wrapper)); |
| 1509 | game_view->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 1510 | |
| 1511 | window_wrapper->set_wrapped_control(game_view, nullptr); |
| 1512 | |
| 1513 | EditorNode::get_singleton()->get_editor_main_screen()->get_control()->add_child(window_wrapper); |
| 1514 | window_wrapper->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 1515 | window_wrapper->hide(); |
| 1516 | window_wrapper->connect("window_visibility_changed", callable_mp(this, &GameViewPlugin::_focus_another_editor).unbind(1)); |
| 1517 | } |
| 1518 | |
| 1519 | #endif // ANDROID_ENABLED |
| 1520 | |
| 1521 | void GameViewPluginBase::_notification(int p_what) { |
| 1522 | switch (p_what) { |
| 1523 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 1524 | #ifndef ANDROID_ENABLED |
| 1525 | window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Game Workspace"))); |
| 1526 | #endif |
| 1527 | } break; |
| 1528 | case NOTIFICATION_ENTER_TREE: { |
| 1529 | add_debugger_plugin(debugger); |
| 1530 | connect("main_screen_changed", callable_mp(this, &GameViewPluginBase::_save_last_editor)); |
| 1531 | } break; |
| 1532 | case NOTIFICATION_EXIT_TREE: { |
| 1533 | remove_debugger_plugin(debugger); |
| 1534 | disconnect("main_screen_changed", callable_mp(this, &GameViewPluginBase::_save_last_editor)); |
| 1535 | } break; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | void GameViewPluginBase::_save_last_editor(const String &p_editor) { |
| 1540 | if (p_editor != get_plugin_name()) { |
| 1541 | last_editor = p_editor; |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | void GameViewPluginBase::_focus_another_editor() { |
| 1546 | if (_is_window_wrapper_enabled()) { |
| 1547 | if (last_editor.is_empty()) { |
| 1548 | EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_2D); |
| 1549 | } else { |
| 1550 | EditorInterface::get_singleton()->set_main_screen_editor(last_editor); |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | bool GameViewPluginBase::_is_window_wrapper_enabled() const { |
| 1556 | #ifdef ANDROID_ENABLED |
| 1557 | return true; |
| 1558 | #else |
| 1559 | return window_wrapper->get_window_enabled(); |
| 1560 | #endif // ANDROID_ENABLED |
| 1561 | } |
| 1562 | |
| 1563 | GameViewPluginBase::GameViewPluginBase() { |
| 1564 | #ifdef ANDROID_ENABLED |
| 1565 | debugger.instantiate(); |
| 1566 | #endif |
| 1567 | } |
| 1568 | |
| 1569 | GameViewPlugin::GameViewPlugin() : |
| 1570 | GameViewPluginBase() { |
| 1571 | #ifndef ANDROID_ENABLED |
| 1572 | Ref<GameViewDebugger> game_view_debugger; |
| 1573 | game_view_debugger.instantiate(); |
| 1574 | EmbeddedProcess *embedded_process = memnew(EmbeddedProcess); |
| 1575 | setup(game_view_debugger, embedded_process); |
| 1576 | #endif |
| 1577 | } |