Godot Engine – Multi-platform 2D and 3D game engine
/**************************************************************************/
/* game_view_plugin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "game_view_plugin.h"
#include "core/config/project_settings.h"
#include "core/debugger/debugger_marshalls.h"
#include "core/object/callable_mp.h"
#include "core/os/process_id.h"
#include "core/string/translation_server.h"
#include "editor/debugger/editor_debugger_node.h"
#include "editor/debugger/script_editor_debugger.h"
#include "editor/docks/editor_dock.h"
#include "editor/docks/editor_dock_manager.h"
#include "editor/docks/inspector_dock.h"
#include "editor/editor_interface.h"
#include "editor/editor_main_screen.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/gui/editor_bottom_panel.h"
#include "editor/gui/editor_icon_manager.h"
#include "editor/gui/editor_toaster.h"
#include "editor/gui/editor_toolbar_group.h"
#include "editor/gui/window_wrapper.h"
#include "editor/run/editor_run_bar.h"
#include "editor/run/embedded_process.h"
#include "editor/run/run_instances_dialog.h"
#include "editor/scene/canvas_item_editor_plugin.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/settings/editor_feature_profile.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "scene/debugger/canvas_item_manipulator.h"
#include "scene/gui/box_container.h"
#include "scene/gui/flow_container.h"
#include "scene/gui/label.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/panel.h"
#include "scene/gui/separator.h"
#include "scene/main/scene_tree.h"
#include "servers/display/display_server.h"
void GameViewDebugger::_session_started(Ref<EditorDebuggerSession> p_session) {
bool is_native = EditorRunBar::get_singleton()->is_running_natively();
Dictionary settings;
settings["debugger/max_node_selection"] = EDITOR_GET("debugger/max_node_selection");
settings["editors/2d/selection_rectangle_color"] = EDITOR_GET("editors/2d/selection_rectangle_color");
settings["editors/2d/locked_selection_rectangle_color"] = EDITOR_GET("editors/2d/locked_selection_rectangle_color");
settings["editors/panning/2d_editor_panning_scheme"] = EDITOR_GET("editors/panning/2d_editor_panning_scheme");
settings["editors/panning/simple_panning"] = EDITOR_GET("editors/panning/simple_panning");
settings["editors/panning/warped_mouse_panning"] = EDITOR_GET("editors/panning/warped_mouse_panning");
settings["editors/panning/2d_editor_pan_speed"] = EDITOR_GET("editors/panning/2d_editor_pan_speed");
settings["editors/polygon_editor/point_grab_radius"] = EDITOR_GET("editors/polygon_editor/point_grab_radius");
settings["canvas_item_editor/pan_view"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/pan_view"));
settings["canvas_item_editor/cancel_transform"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/cancel_transform"));
settings["box_selection_fill_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("box_selection_fill_color"), EditorStringName(Editor));
settings["box_selection_stroke_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor));
settings["editor_scale"] = EDSCALE;
if (!is_native) {
settings["accent_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("accent_color"), EditorStringName(Editor));
settings["axis_x_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("axis_x_color"), EditorStringName(Editor));
settings["axis_y_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("axis_y_color"), EditorStringName(Editor));
}
#ifndef _3D_DISABLED
settings["editors/3d/default_fov"] = EDITOR_GET("editors/3d/default_fov");
settings["editors/3d/default_z_near"] = EDITOR_GET("editors/3d/default_z_near");
settings["editors/3d/default_z_far"] = EDITOR_GET("editors/3d/default_z_far");
settings["editors/3d/navigation/invert_x_axis"] = EDITOR_GET("editors/3d/navigation/invert_x_axis");
settings["editors/3d/navigation/invert_y_axis"] = EDITOR_GET("editors/3d/navigation/invert_y_axis");
settings["editors/3d/navigation/warped_mouse_panning"] = EDITOR_GET("editors/3d/navigation/warped_mouse_panning");
settings["editors/3d/navigation/pan_mouse_button"] = EDITOR_GET("editors/3d/navigation/pan_mouse_button");
settings["editors/3d/freelook/freelook_activation_modifier"] = EDITOR_GET("editors/3d/freelook/freelook_activation_modifier");
settings["editors/3d/freelook/freelook_navigation_scheme"] = EDITOR_GET("editors/3d/freelook/freelook_navigation_scheme");
settings["editors/3d/freelook/freelook_base_speed"] = EDITOR_GET("editors/3d/freelook/freelook_base_speed");
settings["editors/3d/freelook/freelook_sensitivity"] = EDITOR_GET("editors/3d/freelook/freelook_sensitivity");
settings["editors/3d/freelook/freelook_inertia"] = EDITOR_GET("editors/3d/freelook/freelook_inertia");
settings["editors/3d/freelook/freelook_speed_zoom_link"] = EDITOR_GET("editors/3d/freelook/freelook_speed_zoom_link");
settings["editors/3d/freelook/freelook_invert_y_axis"] = EDITOR_GET("editors/3d/freelook/freelook_invert_y_axis");
settings["editors/3d/navigation/orbit_mouse_button"] = EDITOR_GET("editors/3d/navigation/orbit_mouse_button");
settings["editors/3d/navigation_feel/orbit_sensitivity"] = EDITOR_GET("editors/3d/navigation_feel/orbit_sensitivity");
settings["editors/3d/navigation_feel/orbit_inertia"] = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
settings["editors/3d/navigation/zoom_style"] = EDITOR_GET("editors/3d/navigation/zoom_style");
settings["editors/3d/navigation_feel/zoom_inertia"] = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia");
settings["editors/3d/navigation/zoom_mouse_button"] = EDITOR_GET("editors/3d/navigation/zoom_mouse_button");
settings["editors/3d/navigation_feel/translation_sensitivity"] = EDITOR_GET("editors/3d/navigation_feel/translation_sensitivity");
settings["editors/3d/navigation_feel/translation_inertia"] = EDITOR_GET("editors/3d/navigation_feel/translation_inertia");
settings["editors/3d/navigation_feel/angle_snap_threshold"] = EDITOR_GET("editors/3d/navigation_feel/angle_snap_threshold");
settings["editors/3d/navigation/emulate_3_button_mouse"] = EDITOR_GET("editors/3d/navigation/emulate_3_button_mouse");
settings["editors/3d/navigation/emulate_numpad"] = EDITOR_GET("editors/3d/navigation/emulate_numpad");
settings["editors/3d/selection_box_color"] = EDITOR_GET("editors/3d/selection_box_color");
settings["spatial_editor/decrease_fov"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/decrease_fov"));
settings["spatial_editor/increase_fov"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/increase_fov"));
settings["spatial_editor/reset_fov"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/reset_fov"));
settings["spatial_editor/viewport_pan_modifier_1"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/viewport_pan_modifier_1"));
settings["spatial_editor/viewport_pan_modifier_2"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/viewport_pan_modifier_2"));
settings["spatial_editor/viewport_orbit_modifier_1"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/viewport_orbit_modifier_1"));
settings["spatial_editor/viewport_orbit_modifier_2"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/viewport_orbit_modifier_2"));
settings["spatial_editor/viewport_orbit_snap_modifier_1"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/viewport_orbit_snap_modifier_1"));
settings["spatial_editor/viewport_orbit_snap_modifier_2"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/viewport_orbit_snap_modifier_2"));
settings["spatial_editor/viewport_zoom_modifier_1"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/viewport_zoom_modifier_1"));
settings["spatial_editor/viewport_zoom_modifier_2"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/viewport_zoom_modifier_2"));
settings["spatial_editor/freelook_forward"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_forward"));
settings["spatial_editor/freelook_backwards"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_backwards"));
settings["spatial_editor/freelook_left"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_left"));
settings["spatial_editor/freelook_right"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_right"));
settings["spatial_editor/freelook_up"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_up"));
settings["spatial_editor/freelook_down"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_down"));
settings["spatial_editor/freelook_toggle"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_toggle"));
settings["spatial_editor/freelook_speed_modifier"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_speed_modifier"));
settings["spatial_editor/freelook_slow_modifier"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_slow_modifier"));
#endif // _3D_DISABLED
p_session->send_message("scene:runtime_node_select_setup", { settings });
p_session->send_message("scene:runtime_node_select_set_node_type", { node_type });
p_session->send_message("scene:runtime_node_select_set_selection_visible", { selection_visible });
p_session->send_message("scene:runtime_node_select_set_ci_tool", { ci_tool, false });
if (!is_native) {
p_session->send_message("scene:runtime_node_select_set_ci_local_space", { ci_local_space });
}
p_session->send_message("scene:runtime_node_select_set_n3d_tool", { n3d_tool });
p_session->send_message("scene:runtime_node_select_set_avoid_locked", { selection_avoid_locked });
p_session->send_message("scene:runtime_node_select_set_prefer_group", { selection_prefer_group });
p_session->send_message("scene:debug_mute_audio", { mute_audio });
Dictionary shortcut_settings;
shortcut_settings["editor/suspend_resume_embedded_project"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("editor/suspend_resume_embedded_project"));
shortcut_settings["editor/next_frame_embedded_project"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("editor/next_frame_embedded_project"));
shortcut_settings["canvas_item_editor/select_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/select_mode"));
shortcut_settings["canvas_item_editor/pan_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/pan_mode"));
if (!is_native) {
shortcut_settings["canvas_item_editor/move_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/move_mode"));
shortcut_settings["canvas_item_editor/rotate_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/rotate_mode"));
shortcut_settings["canvas_item_editor/scale_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/scale_mode"));
shortcut_settings["ui_undo"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("ui_undo"));
shortcut_settings["ui_redo"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("ui_redo"));
}
p_session->send_message("scene:setup_embedded_shortcuts", { shortcut_settings });
p_session->send_message("scene:setup_game_view", Array());
emit_signal(SNAME("session_started"));
}
void GameViewDebugger::_session_stopped() {
if (!is_feature_enabled && !was_feature_disabled_mid_play) {
return;
}
was_feature_disabled_mid_play = false;
emit_signal(SNAME("session_stopped"));
}
void GameViewDebugger::window_request_size() {
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:window_request_size", Array());
}
}
}
void GameViewDebugger::hdr_output_request_state() {
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:hdr_output_request_state", Array());
}
}
}
void GameViewDebugger::set_suspend(bool p_enabled) {
Array message;
message.append(p_enabled);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:suspend_changed", message);
}
}
}
void GameViewDebugger::next_frame() {
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:next_frame", Array());
}
}
}
void GameViewDebugger::set_time_scale(double p_scale) {
Array message;
message.append(p_scale);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:speed_changed", message);
}
}
}
void GameViewDebugger::reset_time_scale() {
Array message;
message.append(1.0);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:speed_changed", message);
}
}
}
void GameViewDebugger::set_node_type(int p_type) {
node_type = p_type;
Array message;
message.append(p_type);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_set_node_type", message);
}
}
}
void GameViewDebugger::set_selection_visible(bool p_visible) {
selection_visible = p_visible;
Array message;
message.append(p_visible);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_set_selection_visible", message);
}
}
}
void GameViewDebugger::set_selection_avoid_locked(bool p_enabled) {
selection_avoid_locked = p_enabled;
Array message;
message.append(p_enabled);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_set_avoid_locked", message);
}
}
}
void GameViewDebugger::set_selection_prefer_group(bool p_enabled) {
selection_prefer_group = p_enabled;
Array message;
message.append(p_enabled);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_set_prefer_group", message);
}
}
}
void GameViewDebugger::set_ci_tool(int p_tool) {
ci_tool = p_tool;
Array message;
message.append(p_tool);
message.append(Input::get_singleton()->is_key_pressed(Key::SHIFT));
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_set_ci_tool", message);
}
}
}
void GameViewDebugger::set_n3d_tool(int p_tool) {
n3d_tool = p_tool;
Array message;
message.append(p_tool);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_set_n3d_tool", message);
}
}
}
void GameViewDebugger::set_debug_mute_audio(bool p_enabled) {
mute_audio = p_enabled;
EditorDebuggerNode::get_singleton()->set_debug_mute_audio(p_enabled);
}
void GameViewDebugger::toggle_hdr_output_requested() {
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:hdr_output_toggle_requested");
I->send_message("scene:hdr_output_request_state", Array());
}
}
}
void GameViewDebugger::set_camera_override(bool p_enabled) {
EditorDebuggerNode::get_singleton()->set_camera_override(p_enabled ? camera_override_mode : EditorDebuggerNode::OVERRIDE_NONE);
}
void GameViewDebugger::set_camera_manipulate_mode(EditorDebuggerNode::CameraOverride p_mode) {
camera_override_mode = p_mode;
if (EditorDebuggerNode::get_singleton()->get_camera_override() != EditorDebuggerNode::OVERRIDE_NONE) {
set_camera_override(true);
}
}
void GameViewDebugger::set_ci_local_space(bool p_enabled) {
ci_local_space = p_enabled;
Array message;
message.append(p_enabled);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_set_ci_local_space", message);
}
}
}
void GameViewDebugger::report_window_focused(bool p_focused) {
Array message;
message.append(p_focused);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:report_window_focused", message);
}
}
}
void GameViewDebugger::reset_camera_2d_position() {
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_reset_camera_2d", Array());
}
}
}
void GameViewDebugger::reset_camera_3d_position() {
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
I->send_message("scene:runtime_node_select_reset_camera_3d", Array());
}
}
}
void GameViewDebugger::setup_session(int p_session_id) {
Ref<EditorDebuggerSession> session = get_session(p_session_id);
ERR_FAIL_COND(session.is_null());
sessions.append(session);
session->connect("started", callable_mp(this, &GameViewDebugger::_session_started).bind(session));
session->connect("stopped", callable_mp(this, &GameViewDebugger::_session_stopped));
}
void GameViewDebugger::_feature_profile_changed() {
Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
is_feature_enabled = profile.is_null() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_GAME);
was_feature_disabled_mid_play = !is_feature_enabled && EditorRunBar::get_singleton()->is_playing();
}
void GameViewDebugger::_bind_methods() {
ADD_SIGNAL(MethodInfo("session_started"));
ADD_SIGNAL(MethodInfo("session_stopped"));
ADD_SIGNAL(MethodInfo("setup_complete"));
ADD_SIGNAL(MethodInfo("game_window_size_received", PropertyInfo(Variant::VECTOR2I, "size")));
ADD_SIGNAL(MethodInfo("hdr_state_received", PropertyInfo(Variant::ARRAY, "hdr_state")));
}
bool GameViewDebugger::add_screenshot_callback(const Callable &p_callaback, const Rect2i &p_rect) {
bool found = false;
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
ScreenshotCB sd;
sd.cb = p_callaback;
sd.rect = p_rect;
screenshot_callbacks[scr_rq_id] = sd;
Array arr;
arr.append(scr_rq_id);
I->send_message("scene:rq_screenshot", arr);
scr_rq_id++;
found = true;
}
}
return found;
}
bool GameViewDebugger::_msg_get_screenshot(const Array &p_args) {
ERR_FAIL_COND_V_MSG(p_args.size() != 4, false, "get_screenshot: invalid number of arguments");
int64_t id = p_args[0];
int64_t w = p_args[1];
int64_t h = p_args[2];
const String &path = p_args[3];
if (screenshot_callbacks.has(id)) {
if (screenshot_callbacks[id].cb.is_valid()) {
screenshot_callbacks[id].cb.call(w, h, path, screenshot_callbacks[id].rect);
}
screenshot_callbacks.erase(id);
}
return true;
}
bool GameViewDebugger::_msg_show_toaster(const Array &p_args) {
ERR_FAIL_COND_V_MSG(p_args.size() != 2, false, "show_toaster: invalid number of arguments");
EditorToaster::get_singleton()->popup_str(p_args[0], p_args[1]);
return true;
}
bool GameViewDebugger::_msg_open_scene(const Array &p_args) {
ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "open_scene: invalid number of arguments");
EditorNode::get_singleton()->open_scene(p_args[0]);
return true;
}
bool GameViewDebugger::capture(const String &p_message, const Array &p_data, int p_session) {
Ref<EditorDebuggerSession> session = get_session(p_session);
ERR_FAIL_COND_V(session.is_null(), true);
if (p_message == "game_view:get_screenshot") {
return _msg_get_screenshot(p_data);
} else if (p_message == "game_view:show_toaster") {
return _msg_show_toaster(p_data);
} else if (p_message == "game_view:open_scene") {
return _msg_open_scene(p_data);
} else if (p_message == "game_view:setup_complete") {
emit_signal(SNAME("setup_complete"));
return true;
} else if (p_message == "game_view:window_size") {
emit_signal(SNAME("game_window_size_received"), p_data);
return true;
} else if (p_message == "game_view:hdr_state") {
emit_signal(SNAME("hdr_state_received"), p_data);
return true;
} else {
// Any other messages with this prefix should be ignored.
WARN_PRINT("GameViewDebugger unknown message: " + p_message);
return false;
}
return true;
}
bool GameViewDebugger::has_capture(const String &p_capture) const {
return p_capture == "game_view";
}
GameViewDebugger::GameViewDebugger() {
EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &GameViewDebugger::_feature_profile_changed));
ED_SHORTCUT("editor/suspend_resume_embedded_project", TTRC("Suspend/Resume Embedded Game"), Key::F9);
ED_SHORTCUT_OVERRIDE("editor/suspend_resume_embedded_project", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B);
ED_SHORTCUT("editor/next_frame_embedded_project", TTRC("Next Frame"), Key::F10);
}
///////
void GameView::_sessions_changed() {
// The debugger session's `session_started/stopped` signal can be unreliable, so count it manually.
active_sessions = 0;
Array sessions = debugger->get_sessions();
for (int i = 0; i < sessions.size(); i++) {
if (Object::cast_to<EditorDebuggerSession>(sessions[i])->is_active()) {
active_sessions++;
}
}
_update_ui();
_update_debugger_buttons();
#ifdef MACOS_ENABLED
if (!embedded_script_debugger || !embedded_script_debugger->is_session_active() || embedded_script_debugger->get_remote_pid() != embedded_process->get_embedded_pid()) {
_attach_script_debugger();
}
#else
if (embedded_process->is_embedding_completed()) {
if (!embedded_script_debugger || !embedded_script_debugger->is_session_active() || embedded_script_debugger->get_remote_pid() != embedded_process->get_embedded_pid()) {
_attach_script_debugger();
}
}
#endif
}
void GameView::_instance_starting_static(int p_idx, List<String> &r_arguments) {
ERR_FAIL_NULL(singleton);
singleton->_instance_starting(p_idx, r_arguments);
}
void GameView::_instance_starting(int p_idx, List<String> &r_arguments) {
if (!is_feature_enabled) {
return;
}
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) {
// Set the Floating Window default title. Always considered in DEBUG mode, same as in Window::set_title.
String appname = GLOBAL_GET("application/config/name");
appname = vformat("%s (DEBUG)", TranslationServer::get_singleton()->translate(appname));
window_wrapper->set_window_title(appname);
_show_update_window_wrapper();
if (embedded_process->get_focus_mode_with_override() != FOCUS_NONE) {
embedded_process->grab_focus();
}
}
_update_arguments_for_instance(p_idx, r_arguments);
}
bool GameView::_instance_rq_screenshot_static(const Callable &p_callback) {
ERR_FAIL_NULL_V(singleton, false);
return singleton->_instance_rq_screenshot(p_callback);
}
bool GameView::_instance_rq_screenshot(const Callable &p_callback) {
if (debugger.is_null() || window_wrapper->get_window_enabled() || !embedded_process || !embedded_process->is_embedding_completed()) {
return false;
}
Rect2 r = embedded_process->get_adjusted_embedded_window_rect(embedded_process->get_rect());
r.position += embedded_process->get_global_position();
#ifndef MACOS_ENABLED
r.position -= embedded_process->get_window()->get_position();
#endif
return debugger->add_screenshot_callback(p_callback, r);
}
void GameView::_show_update_window_wrapper() {
EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
Point2 position = floating_window_rect.position;
Size2i size = floating_window_rect.size;
int screen = floating_window_screen;
// Obtain the size around the embedded process control. Usually, the difference between the game view's get_size
// and the embedded control should work. However, when the control is hidden and has never been displayed,
// the size of the embedded control is not calculated.
Size2 old_min_size = embedded_process->get_custom_minimum_size();
embedded_process->set_custom_minimum_size(Size2i());
Size2 embedded_process_min_size = get_minimum_size();
Size2 wrapped_margins_size = window_wrapper->get_margins_size();
Size2 wrapped_min_size = window_wrapper->get_minimum_size();
Point2 offset_embedded_process = embedded_process->get_global_position() - get_global_position();
// On the first startup, the global position of the embedded process control is invalid because it was
// never displayed. We will calculate it manually using the minimum size of the window.
if (offset_embedded_process == Point2()) {
offset_embedded_process.y = wrapped_min_size.y;
}
offset_embedded_process.x += embedded_process->get_margin_size(SIDE_LEFT);
offset_embedded_process.y += embedded_process->get_margin_size(SIDE_TOP);
offset_embedded_process += window_wrapper->get_margins_top_left();
embedded_process->set_custom_minimum_size(old_min_size);
Point2 size_diff_embedded_process = Point2(0, embedded_process_min_size.y) + embedded_process->get_margins_size();
if (placement.position != Point2i(INT_MAX, INT_MAX)) {
position = placement.position - offset_embedded_process;
screen = placement.screen;
}
if (placement.size != Size2i()) {
size = placement.size + size_diff_embedded_process + wrapped_margins_size;
}
window_wrapper->restore_window_from_saved_position(Rect2(position, size), screen, Rect2i());
}
void GameView::_play_pressed() {
if (!is_feature_enabled) {
return;
}
ProcessID current_process_id = EditorRunBar::get_singleton()->get_current_process();
if (current_process_id == 0) {
return;
}
if (!window_wrapper->get_window_enabled()) {
screen_index_before_start = EditorNode::get_singleton()->get_editor_main_screen()->get_current_tab();
}
if (embed_on_play && _get_embed_available() == EMBED_AVAILABLE) {
// It's important to disable the low power mode when unfocused because otherwise
// the button in the editor are not responsive and if the user moves the mouse quickly,
// the mouse clicks are not registered.
EditorNode::get_singleton()->set_unfocused_low_processor_usage_mode_enabled(false);
_update_embed_window_size();
if (!window_wrapper->get_window_enabled()) {
EditorDockManager::get_singleton()->force_focus_dock(game_dock);
// Reset the normal size of the bottom panel when fully expanded.
EditorNode::get_singleton()->get_bottom_panel()->set_expanded(false);
if (embedded_process->get_focus_mode_with_override() != FOCUS_NONE) {
embedded_process->grab_focus();
}
}
embedded_process->embed_process(current_process_id);
_update_ui();
}
InspectorDock::get_inspector_singleton()->connect(SNAME("edited_object_changed"), callable_mp(this, &GameView::_inspector_object_changed));
}
void GameView::_stop_pressed() {
if (!is_feature_enabled && !was_feature_disabled_mid_play) {
return;
}
_detach_script_debugger();
paused = false;
EditorNode::get_singleton()->set_unfocused_low_processor_usage_mode_enabled(true);
embedded_process->reset();
_update_ui();
if (window_wrapper->get_window_enabled()) {
window_wrapper->set_window_enabled(false);
}
if (screen_index_before_start >= 0 && EditorNode::get_singleton()->get_editor_main_screen()->get_current_tab_control() == game_dock) {
// We go back to the screen where the user was before starting the game.
EditorNode::get_singleton()->get_editor_main_screen()->set_current_tab(screen_index_before_start);
}
screen_index_before_start = -1;
was_feature_disabled_mid_play = false;
if (InspectorDock::get_inspector_singleton()->is_connected(SNAME("edited_object_changed"), callable_mp(this, &GameView::_inspector_object_changed))) {
InspectorDock::get_inspector_singleton()->disconnect(SNAME("edited_object_changed"), callable_mp(this, &GameView::_inspector_object_changed));
}
}
void GameView::_embedding_completed() {
#ifndef MACOS_ENABLED
_attach_script_debugger();
#endif
_update_ui();
if (make_floating_on_play) {
get_window()->set_flag(Window::FLAG_ALWAYS_ON_TOP, bool(GLOBAL_GET("display/window/size/always_on_top")));
}
}
void GameView::_embedding_failed() {
state_label->set_text(TTRC("Connection impossible to the game process."));
}
void GameView::_embedded_process_focused() {
if (embed_on_play && !window_wrapper->get_window_enabled()) {
EditorDockManager::get_singleton()->force_focus_dock(game_dock);
}
}
void GameView::_editor_or_project_settings_changed() {
if (!is_inside_tree()) {
return;
}
// Update the window size and aspect ratio.
_update_embed_window_size();
if (window_wrapper->get_window_enabled()) {
_show_update_window_wrapper();
if (embedded_process->is_embedding_completed()) {
embedded_process->queue_update_embedded_process();
}
}
_update_ui();
}
void GameView::_inspector_object_changed() {
EditorDebuggerRemoteObjects *robjs = nullptr;
Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
robjs = Object::cast_to<EditorDebuggerRemoteObjects>(obj);
bool is_native = (active_sessions && EditorRunBar::get_singleton()->is_running_natively());
ci_button[CanvasItemManipulator::TOOL_EDIT_PIVOT]->set_disabled(!robjs || !robjs->has_canvas_items || is_native);
}
void GameView::_update_debugger_buttons() {
bool empty = active_sessions == 0;
suspend_button->set_disabled(empty || !is_feature_enabled);
camera_override_button->set_disabled(empty);
speed_state_button->set_disabled(empty);
game_size_label->set_visible(!empty);
PopupMenu *menu = camera_override_menu->get_popup();
bool disable_camera_reset = empty || !camera_override_button->is_pressed() || !menu->is_item_checked(menu->get_item_index(CAMERA_MODE_INGAME));
menu->set_item_disabled(CAMERA_RESET_2D, disable_camera_reset);
menu->set_item_disabled(CAMERA_RESET_3D, disable_camera_reset);
if (empty) {
suspend_button->set_pressed(false);
camera_override_button->set_pressed(false);
_reset_time_scales();
game_size_label->set_text("");
game_size_label->set_tooltip_text("");
game_window_size = Size2i(-1, -1);
hdr_output_enabled = false;
output_max_linear_value = 1.0f;
}
next_frame_button->set_disabled(!suspend_button->is_pressed());
// Node manipulation is disabled for native debug builds.
bool is_native = !empty && EditorRunBar::get_singleton()->is_running_natively();
for (int i = 0; i < CanvasItemManipulator::TOOL_MAX; i++) {
if (ci_button[i] &&
i != CanvasItemManipulator::TOOL_SELECT &&
i != CanvasItemManipulator::TOOL_LIST_SELECT &&
i != CanvasItemManipulator::TOOL_EDIT_PIVOT &&
i != CanvasItemManipulator::TOOL_PAN) {
ci_button[i]->set_disabled(is_native);
}
}
ci_local_space_button->set_disabled(is_native);
menu = game_window_options_menu->get_popup();
if (empty) {
int menu_item_index = menu->get_item_index(WINDOW_SEPARATOR_DYNAMIC_RANGE);
if (menu_item_index >= 0) {
menu->remove_item(menu_item_index);
}
menu_item_index = menu->get_item_index(WINDOW_REQUEST_HDR_OUTPUT);
if (menu_item_index >= 0) {
menu->remove_item(menu_item_index);
}
menu_item_index = menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR);
if (menu_item_index >= 0) {
menu->remove_item(menu_item_index);
}
} else {
int menu_item_index = menu->get_item_index(WINDOW_SEPARATOR_DYNAMIC_RANGE);
if (menu_item_index < 0) {
menu->add_separator(TTRC("Window Dynamic Range"), WINDOW_SEPARATOR_DYNAMIC_RANGE);
}
if (menu->get_item_index(WINDOW_REQUEST_HDR_OUTPUT) < 0) {
if (menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR) < 0) {
menu->add_item(TTRC("Loading..."), WINDOW_HDR_OUTPUT_ERROR);
menu->set_item_disabled(menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR), true);
menu->set_item_tooltip(menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR), "");
}
}
}
}
void GameView::_suspend_button_toggled(bool p_pressed) {
_update_debugger_buttons();
debugger->set_suspend(p_pressed);
}
void GameView::_node_type_pressed(int p_option) {
RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)p_option;
ci_bar->set_visible(type == RuntimeNodeSelect::NODE_TYPE_2D);
n3d_bar->set_visible(type == RuntimeNodeSelect::NODE_TYPE_3D);
_update_debugger_buttons();
debugger->set_node_type(type);
}
void GameView::_selection_options_menu_id_pressed(int p_id) {
switch (p_id) {
case SELECTION_AVOID_LOCKED: {
selection_avoid_locked = !selection_avoid_locked;
debugger->set_selection_avoid_locked(selection_avoid_locked);
EditorSettings::get_singleton()->set_project_metadata("game_view", "selection_avoid_locked", selection_avoid_locked);
} break;
case SELECTION_PREFER_GROUP: {
selection_prefer_group = !selection_prefer_group;
debugger->set_selection_prefer_group(selection_prefer_group);
EditorSettings::get_singleton()->set_project_metadata("game_view", "selection_prefer_group", selection_prefer_group);
} break;
}
PopupMenu *menu = n3d_options_menu->get_popup();
menu->set_item_checked(menu->get_item_index(SELECTION_AVOID_LOCKED), selection_avoid_locked);
menu->set_item_checked(menu->get_item_index(SELECTION_PREFER_GROUP), selection_prefer_group);
}
void GameView::_game_embed_mode_pressed(int p_option) {
EmbedMode mode = (EmbedMode)p_option;
switch (mode) {
case EMBED_TYPE_DISABLED: {
embed_on_play = false;
make_floating_on_play = false;
} break;
case EMBED_TYPE_FLOATING: {
embed_on_play = true;
make_floating_on_play = true;
} break;
case EMBED_TYPE_EDITOR: {
embed_on_play = true;
make_floating_on_play = false;
} break;
case EMBED_TYPE_MAX:
break;
}
EditorSettings::get_singleton()->set_project_metadata("game_view", "embed_on_play", embed_on_play);
EditorSettings::get_singleton()->set_project_metadata("game_view", "make_floating_on_play", make_floating_on_play);
_update_ui();
}
void GameView::_game_window_options_menu_menu_id_pressed(int p_id) {
switch (p_id) {
case WINDOW_SIZE_MODE_FIXED:
case WINDOW_SIZE_MODE_KEEP_ASPECT:
case WINDOW_SIZE_MODE_STRETCH: {
switch (p_id) {
case WINDOW_SIZE_MODE_FIXED: {
embed_size_mode = EmbedSizeMode::SIZE_MODE_FIXED;
} break;
case WINDOW_SIZE_MODE_KEEP_ASPECT: {
embed_size_mode = EmbedSizeMode::SIZE_MODE_KEEP_ASPECT;
} break;
case WINDOW_SIZE_MODE_STRETCH: {
embed_size_mode = EmbedSizeMode::SIZE_MODE_STRETCH;
} break;
}
EditorSettings::get_singleton()->set_project_metadata("game_view", "embed_size_mode", embed_size_mode);
_update_embed_window_size();
} break;
case WINDOW_REQUEST_HDR_OUTPUT: {
debugger->toggle_hdr_output_requested();
} break;
}
_update_embed_menu_options();
}
void GameView::_reset_time_scales() {
time_scale_index = DEFAULT_TIME_SCALE_INDEX;
debugger->reset_time_scale();
if (is_inside_tree()) {
_update_speed_state_icon(DEFAULT_TIME_SCALE_INDEX);
_update_speed_buttons();
}
}
void GameView::_speed_state_menu_pressed(int p_id) {
time_scale_index = p_id;
debugger->set_time_scale(time_scale_range[time_scale_index]);
_update_speed_state_icon(p_id);
_update_speed_buttons();
}
void GameView::_update_speed_state_icon(int p_id) {
PopupMenu *menu = speed_state_button->get_popup();
for (int i = 0; i < speed_state_button->get_item_count(); i++) {
if (i == DEFAULT_TIME_SCALE_INDEX) {
continue;
}
menu->set_item_icon(i, nullptr);
}
menu->set_item_icon(p_id, get_editor_theme_icon(SNAME("KeyValue")));
if (p_id == DEFAULT_TIME_SCALE_INDEX) {
menu->set_item_icon_modulate(p_id, get_theme_color(SNAME("mono_color"), EditorStringName(Editor)));
} else {
menu->set_item_icon(DEFAULT_TIME_SCALE_INDEX, get_editor_theme_icon(SNAME("KeyBezierHandle")));
if (p_id > DEFAULT_TIME_SCALE_INDEX) {
menu->set_item_icon_modulate(p_id, get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
} else {
menu->set_item_icon_modulate(p_id, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
}
}
}
void GameView::_update_speed_buttons() {
speed_state_button->set_text(vformat(U"%s×", time_scale_label[time_scale_index]));
_update_speed_state_color();
}
void GameView::_update_speed_state_color() {
Color text_color;
if (time_scale_index == DEFAULT_TIME_SCALE_INDEX) {
text_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
} else if (time_scale_index > DEFAULT_TIME_SCALE_INDEX) {
text_color = get_theme_color(SNAME("success_color"), EditorStringName(Editor));
} else if (time_scale_index < DEFAULT_TIME_SCALE_INDEX) {
text_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
}
speed_state_button->add_theme_color_override(SceneStringName(font_color), text_color);
speed_state_button->add_theme_color_override(SNAME("font_hover_color"), text_color);
speed_state_button->add_theme_color_override(SNAME("font_hover_pressed_color"), text_color);
speed_state_button->add_theme_color_override(SNAME("font_pressed_color"), text_color);
}
void GameView::_update_speed_state_size() {
if (!speed_state_button) {
return;
}
float min_size = 0;
for (const String lbl : time_scale_label) {
min_size = MAX(speed_state_button->get_minimum_size_for_text_and_icon(vformat(U"%s×", lbl), Ref<Texture2D>()).x, min_size);
}
speed_state_button->set_custom_minimum_size(Vector2(min_size, 0));
}
GameView::EmbedAvailability GameView::_get_embed_available() {
if (!DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_WINDOW_EMBEDDING)) {
return EMBED_NOT_AVAILABLE_FEATURE_NOT_SUPPORTED;
}
if (get_tree()->get_root()->is_embedding_subwindows()) {
return EMBED_NOT_AVAILABLE_SINGLE_WINDOW_MODE;
}
String display_driver = GLOBAL_GET("display/display_server/driver");
if (display_driver == "headless") {
return EMBED_NOT_AVAILABLE_PROJECT_DISPLAY_DRIVER;
}
if (RunInstancesDialog::get_singleton()) {
List<String> instance_args;
RunInstancesDialog::get_singleton()->get_argument_list_for_instance(0, instance_args);
if (instance_args.find("--headless")) {
return EMBED_NOT_AVAILABLE_HEADLESS;
}
}
EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
if (placement.force_fullscreen) {
return EMBED_NOT_AVAILABLE_FULLSCREEN;
}
if (placement.force_maximized) {
return EMBED_NOT_AVAILABLE_MAXIMIZED;
}
DisplayServerEnums::WindowMode window_mode = (DisplayServerEnums::WindowMode)(GLOBAL_GET("display/window/size/mode").operator int());
if (window_mode == DisplayServerEnums::WindowMode::WINDOW_MODE_MINIMIZED) {
return EMBED_NOT_AVAILABLE_MINIMIZED;
}
if (window_mode == DisplayServerEnums::WindowMode::WINDOW_MODE_MAXIMIZED) {
return EMBED_NOT_AVAILABLE_MAXIMIZED;
}
if (window_mode == DisplayServerEnums::WindowMode::WINDOW_MODE_FULLSCREEN || window_mode == DisplayServerEnums::WindowMode::WINDOW_MODE_EXCLUSIVE_FULLSCREEN) {
return EMBED_NOT_AVAILABLE_FULLSCREEN;
}
return EMBED_AVAILABLE;
}
void GameView::_update_ui() {
EmbedAvailability available = _get_embed_available();
switch (available) {
case EMBED_AVAILABLE:
if (embedded_process->is_embedding_completed()) {
state_label->set_text("");
} else if (embedded_process->is_embedding_in_progress()) {
state_label->set_text(TTRC("Game starting..."));
} else if (EditorRunBar::get_singleton()->is_playing()) {
state_label->set_text(TTRC("Game running not embedded."));
} else if (embed_on_play) {
if (make_floating_on_play) {
state_label->set_text(TTRC("The game will run in a floating window with an attached toolbar."));
} else {
state_label->set_text(TTRC("The game will run here within the Game workspace."));
}
} else {
state_label->set_text(TTRC("The game will run in a floating window without the toolbar."));
}
break;
case EMBED_NOT_AVAILABLE_FEATURE_NOT_SUPPORTED:
state_label->set_text(TTRC("Game embedding not available on your OS."));
break;
case EMBED_NOT_AVAILABLE_PROJECT_DISPLAY_DRIVER:
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")));
break;
case EMBED_NOT_AVAILABLE_MINIMIZED:
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."));
break;
case EMBED_NOT_AVAILABLE_MAXIMIZED:
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."));
break;
case EMBED_NOT_AVAILABLE_FULLSCREEN:
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."));
break;
case EMBED_NOT_AVAILABLE_SINGLE_WINDOW_MODE:
state_label->set_text(TTRC("Game embedding not available in single-window mode."));
break;
case EMBED_NOT_AVAILABLE_HEADLESS:
state_label->set_text(TTRC("Game embedding not available when the game starts in headless mode."));
break;
}
if (available == EMBED_AVAILABLE) {
if (state_label->has_theme_color_override(SceneStringName(font_color))) {
state_label->remove_theme_color_override(SceneStringName(font_color));
}
} else {
state_label->add_theme_color_override(SceneStringName(font_color), state_label->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
}
}
void GameView::_update_embed_menu_options() {
PopupMenu *menu = game_window_options_menu->get_popup();
menu->set_item_checked(menu->get_item_index(WINDOW_SIZE_MODE_FIXED), embed_size_mode == SIZE_MODE_FIXED);
menu->set_item_checked(menu->get_item_index(WINDOW_SIZE_MODE_KEEP_ASPECT), embed_size_mode == SIZE_MODE_KEEP_ASPECT);
menu->set_item_checked(menu->get_item_index(WINDOW_SIZE_MODE_STRETCH), embed_size_mode == SIZE_MODE_STRETCH);
}
void GameView::_update_embed_buttons() {
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_pressed(embed_on_play && !make_floating_on_play);
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_pressed(make_floating_on_play && window_wrapper->is_window_available());
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_pressed(!embed_on_play && !make_floating_on_play);
}
void GameView::_update_game_window_size_label() {
String window_size_string = game_window_size.x < 0 ? "" : vformat("%dx%d", game_window_size.x, game_window_size.y);
game_size_label->set_text(hdr_output_enabled ? vformat("%s %s (%.2f)", window_size_string, TTRC("HDR"), output_max_linear_value)
: window_size_string);
game_size_label->set_tooltip_text(
vformat(TTR("Window size: %s\nMode: %s\nMaximum linear value: %.2f%s"),
window_size_string,
hdr_output_enabled ? TTRC("HDR") : TTRC("SDR"),
output_max_linear_value,
hdr_output_enabled ? vformat(TTR("\nReference luminance: %.0f nits\nMaximum luminance: %.0f nits"),
current_reference_luminance,
current_max_luminance)
: ""));
}
void GameView::_update_embed_window_size() {
if (paused) {
// When paused, Godot does not re-render. As a result, resizing the game window to a larger size
// causes artifacts and flickering. However, resizing to a smaller size seems fine.
// To prevent artifacts and flickering, we will force the game window to maintain its size.
// Using the same technique as SIZE_MODE_FIXED, the embedded process control will
// prevent resizing the game to a larger size while maintaining the aspect ratio.
embedded_process->set_window_size(size_paused);
embedded_process->set_keep_aspect(false);
} else {
if (embed_size_mode == SIZE_MODE_FIXED || embed_size_mode == SIZE_MODE_KEEP_ASPECT) {
// The embedded process control will need the desired window size.
EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
embedded_process->set_window_size(placement.size);
} else {
// Stretch... No need for the window size.
embedded_process->set_window_size(Size2i());
}
embedded_process->set_keep_aspect(embed_size_mode == SIZE_MODE_KEEP_ASPECT);
}
}
void GameView::_debug_hide_selection_button_pressed() {
debug_hide_selection = !debug_hide_selection;
debug_hide_selection_button->set_button_icon(EditorIconManager::get_icon(debug_hide_selection ? SNAME("GuiVisibilityHidden") : SNAME("GuiVisibilityVisible")));
debug_hide_selection_button->set_tooltip_text(debug_hide_selection ? TTRC("Show selection indicators.") : TTRC("Hide selection indicators."));
debugger->set_selection_visible(!debug_hide_selection);
EditorSettings::get_singleton()->set_project_metadata("game_view", "show_selection", !debug_hide_selection);
}
void GameView::_debug_mute_audio_button_pressed() {
debug_mute_audio = !debug_mute_audio;
debug_mute_audio_button->set_button_icon(EditorIconManager::get_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer")));
debug_mute_audio_button->set_tooltip_text(debug_mute_audio ? TTRC("Unmute game audio.") : TTRC("Mute game audio."));
debugger->set_debug_mute_audio(debug_mute_audio);
}
void GameView::_setup_complete() {
debugger->window_request_size();
debugger->hdr_output_request_state();
}
void GameView::_game_window_size_received(const Array &p_state) {
ERR_FAIL_COND_MSG(p_state.size() != 1, "_game_window_size_received: invalid number of arguments");
game_window_size = p_state[0];
_update_game_window_size_label();
}
void GameView::_hdr_state_received(const Array &p_state) {
ERR_FAIL_COND_MSG(p_state.size() != 7, "_hdr_state_received: invalid number of arguments");
PopupMenu *menu = game_window_options_menu->get_popup();
// Matches SceneDebugger::_msg_hdr_output_request_state
bool requested = p_state[0];
hdr_output_enabled = p_state[1];
current_reference_luminance = p_state[2];
current_max_luminance = p_state[3];
output_max_linear_value = p_state[4];
display_server_supports_hdr_output = p_state[5];
renderer_supports_hdr_output = p_state[6];
int request_hdr_output_index = menu->get_item_index(WINDOW_REQUEST_HDR_OUTPUT);
if (display_server_supports_hdr_output && renderer_supports_hdr_output) {
if (menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR) >= 0) {
menu->remove_item(menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR));
}
if (request_hdr_output_index < 0) {
menu->add_check_item(TTRC("Request HDR Output"), WINDOW_REQUEST_HDR_OUTPUT);
}
menu->set_item_checked(menu->get_item_index(WINDOW_REQUEST_HDR_OUTPUT), requested);
if (requested && !hdr_output_enabled) {
String error_text = TTRC("Window does not support HDR output.");
String error_tooltip_text = TTRC("Move the game window to a screen\nthat is operating in HDR mode.");
menu->add_item(error_text, WINDOW_HDR_OUTPUT_ERROR);
int error_item_index = menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR);
menu->set_item_disabled(error_item_index, true);
menu->set_item_tooltip(error_item_index, error_tooltip_text);
}
} else {
if (request_hdr_output_index >= 0) {
menu->remove_item(request_hdr_output_index);
}
int error_item_index = menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR);
String error_text = !display_server_supports_hdr_output ? TTRC("Display server does not support HDR output.")
: TTRC("Renderer does not support HDR output.");
String error_tooltip_text = !display_server_supports_hdr_output ? TTRC("Change the display/display_server/driver\nadvanced project setting to a driver\nthat supports HDR output.")
: TTRC("Change the rendering/renderer/rendering_method\nproject setting to a renderer that supports HDR output and\nthe rendering/rendering_device/driver advanced project\nsetting to a driver that supports HDR output.");
if (error_item_index < 0) {
menu->add_item(error_text, WINDOW_HDR_OUTPUT_ERROR);
error_item_index = menu->get_item_index(WINDOW_HDR_OUTPUT_ERROR);
menu->set_item_disabled(error_item_index, true);
} else {
menu->set_item_text(error_item_index, error_text);
}
menu->set_item_tooltip(error_item_index, error_tooltip_text);
}
_update_game_window_size_label();
}
void GameView::_camera_override_button_toggled(bool p_pressed) {
_update_debugger_buttons();
debugger->set_camera_override(p_pressed);
}
void GameView::_camera_override_menu_id_pressed(int p_id) {
PopupMenu *menu = camera_override_menu->get_popup();
if (p_id != CAMERA_RESET_2D && p_id != CAMERA_RESET_3D) {
for (int i = 0; i < menu->get_item_count(); i++) {
menu->set_item_checked(i, false);
}
}
switch (p_id) {
case CAMERA_RESET_2D: {
debugger->reset_camera_2d_position();
} break;
case CAMERA_RESET_3D: {
debugger->reset_camera_3d_position();
} break;
case CAMERA_MODE_INGAME: {
debugger->set_camera_manipulate_mode(EditorDebuggerNode::OVERRIDE_INGAME);
menu->set_item_checked(menu->get_item_index(p_id), true);
_update_debugger_buttons();
EditorSettings::get_singleton()->set_project_metadata("game_view", "camera_override_mode", p_id);
} break;
case CAMERA_MODE_EDITORS: {
debugger->set_camera_manipulate_mode(EditorDebuggerNode::OVERRIDE_EDITORS);
menu->set_item_checked(menu->get_item_index(p_id), true);
_update_debugger_buttons();
EditorSettings::get_singleton()->set_project_metadata("game_view", "camera_override_mode", p_id);
} break;
}
}
void GameView::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_TRANSLATION_CHANGED: {
const String show_list_tooltip = vformat(TTR("%s+RMB: Show list of all nodes at position clicked, including locked."), keycode_get_string((Key)KeyModifierMask::ALT));
ci_button[CanvasItemManipulator::TOOL_SELECT]->set_tooltip_text(vformat(TTR("%s+Drag: Rotate selected node around pivot."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL)) + "\n" + vformat(TTR("%s+Drag: Move selected node."), keycode_get_string((Key)KeyModifierMask::ALT)) + "\n" + vformat(TTR("%s+%s+Drag: Scale selected node."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL), keycode_get_string((Key)KeyModifierMask::ALT)) + "\n" + TTR("V: Set selected node's pivot position.") + "\n" + show_list_tooltip);
ci_button[CanvasItemManipulator::TOOL_MOVE]->set_tooltip_text(show_list_tooltip);
ci_button[CanvasItemManipulator::TOOL_ROTATE]->set_tooltip_text(show_list_tooltip);
ci_button[CanvasItemManipulator::TOOL_SCALE]->set_tooltip_text(TTR("Shift: Scale proportionally.") + "\n" + show_list_tooltip);
ci_button[CanvasItemManipulator::TOOL_LIST_SELECT]->set_tooltip_text(TTR("Show list of selectable nodes at position clicked.") + "\n" + show_list_tooltip);
ci_button[CanvasItemManipulator::TOOL_EDIT_PIVOT]->set_tooltip_text(TTR("Click to change object's pivot.") + "\n" + TTR("Shift: Set temporary pivot.") + "\n" + TTR("Click this button while holding Shift to put the temporary pivot in the center of the selected nodes.") + "\n" + show_list_tooltip);
ci_button[CanvasItemManipulator::TOOL_PAN]->set_tooltip_text(TTR("You can also use Pan View shortcut (Space by default) to pan in any mode.") + "\n" + show_list_tooltip);
n3d_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_tooltip_text(vformat(TTR("%s+%s+RMB: Show list of all nodes at position clicked."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL), keycode_get_string((Key)KeyModifierMask::ALT)));
_update_ui();
} break;
case NOTIFICATION_POST_ENTER_TREE: {
_update_speed_state_size();
} break;
case NOTIFICATION_THEME_CHANGED: {
_update_speed_state_icon(time_scale_index);
_update_speed_state_size();
_update_speed_state_color();
} break;
case NOTIFICATION_READY: {
if (DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_WINDOW_EMBEDDING)) {
// Embedding available.
int game_mode = EDITOR_GET("run/window_placement/game_embed_mode");
switch (game_mode) {
case -1: { // Disabled.
embed_on_play = false;
make_floating_on_play = false;
} break;
case 1: { // Embed.
embed_on_play = true;
make_floating_on_play = false;
} break;
case 2: { // Floating.
embed_on_play = true;
make_floating_on_play = true;
} break;
default: {
embed_on_play = EditorSettings::get_singleton()->get_project_metadata("game_view", "embed_on_play", true);
make_floating_on_play = EditorSettings::get_singleton()->get_project_metadata("game_view", "make_floating_on_play", false);
} break;
}
embed_size_mode = (EmbedSizeMode)(int)EditorSettings::get_singleton()->get_project_metadata("game_view", "embed_size_mode", SIZE_MODE_FIXED);
_update_embed_buttons();
_update_embed_menu_options();
EditorRunBar::get_singleton()->connect("play_pressed", callable_mp(this, &GameView::_play_pressed));
EditorRunBar::get_singleton()->connect("stop_pressed", callable_mp(this, &GameView::_stop_pressed));
EditorRun::instance_starting_callback = _instance_starting_static;
EditorRun::instance_rq_screenshot_callback = _instance_rq_screenshot_static;
// Listen for project settings changes to update the window size and aspect ratio.
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &GameView::_editor_or_project_settings_changed));
EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &GameView::_editor_or_project_settings_changed));
} else {
// Embedding not available.
embedding_hb->hide();
}
_update_ui();
} break;
case NOTIFICATION_WM_WINDOW_FOCUS_IN:
case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
if (embed_on_play) {
debugger->report_window_focused(p_what == NOTIFICATION_WM_WINDOW_FOCUS_IN);
}
} break;
case NOTIFICATION_WM_POSITION_CHANGED: {
if (window_wrapper->get_window_enabled()) {
_update_floating_window_settings();
}
} break;
}
}
void GameView::set_window_layout(Ref<ConfigFile> p_layout) {
floating_window_rect = p_layout->get_value("GameView", "floating_window_rect", Rect2i());
floating_window_screen = p_layout->get_value("GameView", "floating_window_screen", -1);
}
void GameView::get_window_layout(Ref<ConfigFile> p_layout) {
if (window_wrapper->get_window_enabled()) {
_update_floating_window_settings();
}
p_layout->set_value("GameView", "floating_window_rect", floating_window_rect);
p_layout->set_value("GameView", "floating_window_screen", floating_window_screen);
}
void GameView::_update_floating_window_settings() {
if (window_wrapper->get_window_enabled()) {
floating_window_rect = window_wrapper->get_window_rect();
floating_window_screen = window_wrapper->get_window_screen();
}
}
void GameView::_attach_script_debugger() {
_detach_script_debugger();
int i = 0;
while (ScriptEditorDebugger *script_debugger = EditorDebuggerNode::get_singleton()->get_debugger(i)) {
if (script_debugger->is_session_active() && script_debugger->get_remote_pid() == embedded_process->get_embedded_pid()) {
embedded_script_debugger = script_debugger;
break;
}
i++;
}
#ifdef MACOS_ENABLED
embedded_process->set_script_debugger(embedded_script_debugger);
#endif
if (embedded_script_debugger) {
embedded_script_debugger->connect("remote_window_title_changed", callable_mp(this, &GameView::_remote_window_title_changed));
}
}
void GameView::_detach_script_debugger() {
if (embedded_script_debugger) {
embedded_script_debugger->disconnect("remote_window_title_changed", callable_mp(this, &GameView::_remote_window_title_changed));
embedded_script_debugger = nullptr;
}
embedded_process->set_script_debugger(nullptr);
}
void GameView::_remote_window_title_changed(const String &p_title) {
window_wrapper->set_window_title(p_title);
}
void GameView::_update_arguments_for_instance(int p_idx, List<String> &r_arguments) {
if (p_idx != 0 || !embed_on_play || _get_embed_available() != EMBED_AVAILABLE) {
return;
}
// Remove duplicates/unwanted parameters.
List<String>::Element *E = r_arguments.front();
List<String>::Element *user_args_element = nullptr;
HashSet<String> remove_args({ "--position", "--resolution", "--screen" });
#ifdef MACOS_ENABLED
// macOS requires the embedded display driver.
remove_args.insert("--display-driver");
#endif
#ifdef WAYLAND_ENABLED
// Wayland requires its display driver.
if (DisplayServer::get_singleton()->get_name() == "Wayland") {
remove_args.insert("--display-driver");
}
#endif
#ifdef X11_ENABLED
// X11 requires its display driver.
if (DisplayServer::get_singleton()->get_name() == "X11") {
remove_args.insert("--display-driver");
}
#endif
while (E) {
List<String>::Element *N = E->next();
// For these parameters, we need to also remove the value.
if (remove_args.has(E->get())) {
r_arguments.erase(E);
if (N) {
List<String>::Element *V = N->next();
r_arguments.erase(N);
N = V;
}
} else if (E->get() == "-f" || E->get() == "--fullscreen" || E->get() == "-m" || E->get() == "--maximized" || E->get() == "-t" || E->get() == "-always-on-top") {
r_arguments.erase(E);
} else if (E->get() == "--" || E->get() == "++") {
user_args_element = E;
break;
}
E = N;
}
// Add the editor window's native ID so the started game can directly set it as its parent.
List<String>::Element *N = r_arguments.insert_before(user_args_element, "--wid");
N = r_arguments.insert_after(N, itos(DisplayServer::get_singleton()->window_get_native_handle(DisplayServerEnums::WINDOW_HANDLE, get_window()->get_window_id())));
#if MACOS_ENABLED
N = r_arguments.insert_after(N, "--embedded");
#endif
#ifdef WAYLAND_ENABLED
if (DisplayServer::get_singleton()->get_name() == "Wayland") {
N = r_arguments.insert_after(N, "--display-driver");
N = r_arguments.insert_after(N, "wayland");
}
#endif
#ifdef X11_ENABLED
if (DisplayServer::get_singleton()->get_name() == "X11") {
N = r_arguments.insert_after(N, "--display-driver");
N = r_arguments.insert_after(N, "x11");
}
#endif
// Be sure to have the correct window size in the embedded_process control.
_update_embed_window_size();
Rect2i rect = embedded_process->get_screen_embedded_window_rect();
// Usually, the global rect of the embedded process control is invalid because it was hidden. We will calculate it manually.
if (!window_wrapper->get_window_enabled()) {
Size2 old_min_size = embedded_process->get_custom_minimum_size();
embedded_process->set_custom_minimum_size(Size2i());
Control *container = game_dock;
rect = container->get_global_rect();
Size2 wrapped_min_size = window_wrapper->get_minimum_size();
rect.position.y += wrapped_min_size.y;
rect.size.y -= wrapped_min_size.y;
rect = embedded_process->get_adjusted_embedded_window_rect(rect);
embedded_process->set_custom_minimum_size(old_min_size);
}
// When using the floating window, we need to force the position and size from the
// editor/project settings, because the get_screen_embedded_window_rect of the
// embedded_process will be updated only on the next frame.
if (window_wrapper->get_window_enabled()) {
EditorRun::WindowPlacement placement = EditorRun::get_window_placement();
if (placement.position != Point2i(INT_MAX, INT_MAX)) {
rect.position = placement.position;
}
if (placement.size != Size2i()) {
rect.size = placement.size;
}
}
N = r_arguments.insert_after(N, "--position");
N = r_arguments.insert_after(N, itos(rect.position.x) + "," + itos(rect.position.y));
N = r_arguments.insert_after(N, "--resolution");
r_arguments.insert_after(N, itos(rect.size.x) + "x" + itos(rect.size.y));
}
void GameView::_window_close_request() {
// Before the parent window closed, we close the embedded game. That prevents
// the embedded game to be seen without a parent window for a fraction of second.
if (EditorRunBar::get_singleton()->is_playing() && (embedded_process->is_embedding_completed() || embedded_process->is_embedding_in_progress())) {
// When the embedding is not complete, we need to kill the process.
// If the game is paused, the close request will not be processed by the game, so it's better to kill the process.
if (paused || embedded_process->is_embedding_in_progress()) {
if (window_wrapper->get_window_enabled()) {
embedded_process->reset_timers();
window_wrapper->set_window_enabled(false);
}
// Call deferred to prevent the _stop_pressed callback to be executed before the wrapper window actually closes.
embedded_process->reset();
callable_mp(EditorRunBar::get_singleton(), &EditorRunBar::stop_playing).call_deferred();
} else {
// Try to gracefully close the window. That way, the NOTIFICATION_WM_CLOSE_REQUEST
// notification should be propagated in the game process.
// We shouldn't disable the window_wrapper here since if auto_accept_quit is false,
// the game will ignore this request and stay alive.
embedded_process->request_close();
}
}
}
void GameView::_debugger_breaked(bool p_breaked, bool p_can_debug) {
if (p_breaked == paused) {
return;
}
paused = p_breaked;
if (paused) {
size_paused = embedded_process->get_screen_embedded_window_rect().size;
}
_update_embed_window_size();
}
void GameView::_feature_profile_changed() {
Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
bool is_profile_null = profile.is_null();
is_feature_enabled = is_profile_null || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_GAME);
was_feature_disabled_mid_play = !is_feature_enabled && EditorRunBar::get_singleton()->is_playing();
if (!is_feature_enabled) {
if (!node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->is_pressed()) {
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_pressed(true);
_node_type_pressed(RuntimeNodeSelect::NODE_TYPE_NONE);
}
suspend_button->set_pressed(false);
camera_override_button->set_pressed(false);
_reset_time_scales();
if (debug_mute_audio) {
_debug_mute_audio_button_pressed();
}
// If the game is currently being played and embedded, stop the game.
// Otherwise it would just get stuck in the now inaccessible Game view.
_window_close_request();
}
suspend_button->set_disabled(active_sessions == 0 || !is_feature_enabled);
bool is_3d_enabled = is_profile_null || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D);
if (!is_3d_enabled && node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->is_pressed()) {
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_pressed(true);
_node_type_pressed(RuntimeNodeSelect::NODE_TYPE_NONE);
}
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_visible(is_3d_enabled);
}
Button *GameView::_setup_button(const StringName &p_icon, const Ref<ButtonGroup> &p_group, const Ref<Shortcut> &p_shortcut) {
Button *button = memnew(Button);
button->set_button_icon(EditorIconManager::get_icon(p_icon));
button->set_toggle_mode(true);
button->set_button_group(p_group);
button->set_theme_type_variation(SceneStringName(FlatButton));
if (p_shortcut.is_valid()) {
button->set_shortcut(p_shortcut);
button->set_shortcut_context(this);
}
return button;
}
GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process, WindowWrapper *p_wrapper) {
singleton = this;
game_dock = memnew(EditorDock);
game_dock->set_name(TTRC("Game"));
game_dock->set_icon_name("Game");
game_dock->set_available_layouts(EditorDock::DOCK_LAYOUT_MAIN_SCREEN);
game_dock->set_default_slot(EditorDock::DOCK_SLOT_MAIN_SCREEN);
game_dock->set_dock_shortcut(ED_GET_SHORTCUT("editor/editor_game"));
dock_singleton = game_dock;
debugger = p_debugger;
window_wrapper = p_wrapper;
embedded_process = p_embedded_process;
toolbar_margin = memnew(MarginContainer);
toolbar_margin->set_theme_type_variation("MainToolBarMargin");
add_child(toolbar_margin);
FlowContainer *main_menu_fc = memnew(FlowContainer);
toolbar_margin->add_child(main_menu_fc);
HBoxContainer *process_hb = memnew(HBoxContainer);
main_menu_fc->add_child(process_hb);
suspend_button = memnew(Button);
process_hb->add_child(suspend_button);
suspend_button->set_button_icon(EditorIconManager::get_icon(SNAME("Suspend")));
suspend_button->set_toggle_mode(true);
suspend_button->set_theme_type_variation(SceneStringName(FlatButton));
suspend_button->connect(SceneStringName(toggled), callable_mp(this, &GameView::_suspend_button_toggled));
suspend_button->set_accessibility_name(TTRC("Suspend"));
suspend_button->set_shortcut(ED_GET_SHORTCUT("editor/suspend_resume_embedded_project"));
suspend_button->set_tooltip_text(TTRC("Force pause at SceneTree level. Stops all processing, but you can still interact with the game."));
next_frame_button = memnew(Button);
process_hb->add_child(next_frame_button);
next_frame_button->set_button_icon(EditorIconManager::get_icon(SNAME("NextFrame")));
next_frame_button->set_theme_type_variation(SceneStringName(FlatButton));
next_frame_button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::next_frame));
next_frame_button->set_accessibility_name(TTRC("Next Frame"));
next_frame_button->set_shortcut(ED_GET_SHORTCUT("editor/next_frame_embedded_project"));
process_hb->add_child(memnew(VSeparator));
speed_state_button = memnew(MenuButton);
process_hb->add_child(speed_state_button);
speed_state_button->set_text(U"1.0×");
speed_state_button->set_flat(false);
speed_state_button->set_theme_type_variation("FlatMenuButton");
speed_state_button->set_tooltip_text(TTRC("Change the game speed."));
speed_state_button->set_accessibility_name(TTRC("Speed State"));
PopupMenu *menu = speed_state_button->get_popup();
menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_speed_state_menu_pressed));
for (String lbl : time_scale_label) {
menu->add_item(vformat(U"%s×", lbl));
}
process_hb->add_child(memnew(VSeparator));
HBoxContainer *input_hb = EditorToolbarGroup::create(main_menu_fc);
node_group.instantiate();
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE] = memnew(Button);
input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]);
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_text(TTRC("Input"));
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_button_icon(EditorIconManager::get_icon(SNAME("InputEventJoypadMotion")));
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_toggle_mode(true);
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_pressed(true);
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_button_group(node_group);
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_theme_type_variation("FlatButtonNoIconTint");
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_NONE));
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_tooltip_text(TTRC("Allow game input as usual."));
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D] = memnew(Button);
input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]);
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_text(TTRC("2D"));
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_button_icon(EditorIconManager::get_icon(SNAME("2DNodes")));
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_toggle_mode(true);
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_button_group(node_group);
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_theme_type_variation("FlatButtonNoIconTint");
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_2D));
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_tooltip_text(TTRC("Disable game input to allow selecting Node2Ds and Controls, as well as manipulate the 2D camera."));
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D] = memnew(Button);
input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]);
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_text(TTRC("3D"));
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_button_icon(EditorIconManager::get_icon(SNAME("Node3D")));
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_toggle_mode(true);
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_button_group(node_group);
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_theme_type_variation("FlatButtonNoIconTint");
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_3D));
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_tooltip_text(TTRC("Disable game input to allow selecting Node3Ds and manipulating the 3D camera."));
main_menu_fc->add_child(memnew(VSeparator));
HBoxContainer *camera_hb = EditorToolbarGroup::create(main_menu_fc);
camera_override_button = memnew(Button);
camera_hb->add_child(camera_override_button);
camera_override_button->set_button_icon(EditorIconManager::get_icon(SNAME("Camera")));
camera_override_button->set_toggle_mode(true);
camera_override_button->set_theme_type_variation(SceneStringName(FlatButton));
camera_override_button->set_tooltip_text(TTRC("Override the in-game camera."));
camera_override_button->connect(SceneStringName(toggled), callable_mp(this, &GameView::_camera_override_button_toggled));
camera_override_menu = memnew(MenuButton);
camera_hb->add_child(camera_override_menu);
camera_override_menu->set_button_icon(EditorIconManager::get_icon(SNAME("GuiDropdown")));
camera_override_menu->set_flat(false);
camera_override_menu->set_theme_type_variation("FlatMenuButtonNoIconTint");
camera_override_menu->set_h_size_flags(SIZE_SHRINK_END);
camera_override_menu->set_tooltip_text(TTRC("Camera override options."));
menu = camera_override_menu->get_popup();
menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_camera_override_menu_id_pressed));
menu->add_item(TTRC("Reset 2D Camera"), CAMERA_RESET_2D);
menu->add_item(TTRC("Reset 3D Camera"), CAMERA_RESET_3D);
menu->add_separator();
menu->add_radio_check_item(TTRC("Manipulate In-Game"), CAMERA_MODE_INGAME);
menu->set_item_checked(menu->get_item_index(CAMERA_MODE_INGAME), true);
menu->add_radio_check_item(TTRC("Manipulate From Editors"), CAMERA_MODE_EDITORS);
main_menu_fc->add_child(memnew(VSeparator));
HBoxContainer *audio_hb = memnew(HBoxContainer);
main_menu_fc->add_child(audio_hb);
debug_hide_selection = !EditorSettings::get_singleton()->get_project_metadata("game_view", "show_selection", true);
debug_hide_selection_button = memnew(Button);
audio_hb->add_child(debug_hide_selection_button);
debug_hide_selection_button->set_button_icon(EditorIconManager::get_icon(debug_hide_selection ? "GuiVisibilityHidden" : "GuiVisibilityVisible"));
debug_hide_selection_button->set_theme_type_variation(SceneStringName(FlatButton));
debug_hide_selection_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_debug_hide_selection_button_pressed));
debug_hide_selection_button->set_tooltip_text(debug_hide_selection ? TTRC("Show selection indicators.") : TTRC("Hide selection indicators."));
debugger->set_selection_visible(!debug_hide_selection);
debug_mute_audio_button = memnew(Button);
audio_hb->add_child(debug_mute_audio_button);
debug_mute_audio_button->set_theme_type_variation(SceneStringName(FlatButton));
debug_mute_audio_button->set_button_icon(EditorIconManager::get_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer")));
debug_mute_audio_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_debug_mute_audio_button_pressed));
debug_mute_audio_button->set_tooltip_text(debug_mute_audio ? TTRC("Unmute game audio.") : TTRC("Mute game audio."));
main_menu_fc->add_child(memnew(VSeparator));
// 2D Section
ci_bar = memnew(HBoxContainer);
main_menu_fc->add_child(ci_bar);
ci_bar->hide();
EditorToolbarGroup *ci_etg = memnew(EditorToolbarGroup);
ci_bar->add_child(ci_etg);
ci_group.instantiate();
{
Button *button = _setup_button(SNAME("ToolSelect"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/select_mode"));
ci_etg->get_hbox()->add_child(button);
button->set_pressed(true);
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_SELECT));
ci_button[CanvasItemManipulator::TOOL_SELECT] = button;
}
VSeparator *vsep = memnew(VSeparator);
vsep->set_theme_type_variation("VSeparatorButtonGroup");
ci_etg->get_hbox()->add_child(vsep);
{
Button *button = _setup_button(SNAME("ToolMove"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/move_mode"));
ci_etg->get_hbox()->add_child(button);
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_MOVE));
ci_button[CanvasItemManipulator::TOOL_MOVE] = button;
}
{
Button *button = _setup_button(SNAME("ToolRotate"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/rotate_mode"));
ci_etg->get_hbox()->add_child(button);
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_ROTATE));
ci_button[CanvasItemManipulator::TOOL_ROTATE] = button;
}
{
Button *button = _setup_button(SNAME("ToolScale"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/scale_mode"));
ci_etg->get_hbox()->add_child(button);
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_SCALE));
ci_button[CanvasItemManipulator::TOOL_SCALE] = button;
}
vsep = memnew(VSeparator);
vsep->set_theme_type_variation("VSeparatorButtonGroup");
ci_etg->get_hbox()->add_child(vsep);
{
Button *button = _setup_button(SNAME("ListSelect"), ci_group);
ci_etg->get_hbox()->add_child(button);
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_LIST_SELECT));
ci_button[CanvasItemManipulator::TOOL_LIST_SELECT] = button;
}
{
Button *button = _setup_button(SNAME("EditPivot"), ci_group);
button->set_disabled(true);
ci_etg->get_hbox()->add_child(button);
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_EDIT_PIVOT));
ci_button[CanvasItemManipulator::TOOL_EDIT_PIVOT] = button;
}
{
Button *button = _setup_button(SNAME("ToolPan"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/pan_mode"));
ci_etg->get_hbox()->add_child(button);
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_PAN));
ci_button[CanvasItemManipulator::TOOL_PAN] = button;
}
vsep = memnew(VSeparator);
ci_bar->add_child(vsep);
ci_local_space_button = memnew(Button);
ci_bar->add_child(ci_local_space_button);
ci_local_space_button->set_button_icon(EditorIconManager::get_icon(SNAME("Object")));
ci_local_space_button->set_toggle_mode(true);
ci_local_space_button->set_pressed(true);
ci_local_space_button->set_theme_type_variation(SceneStringName(FlatButton));
ci_local_space_button->connect(SceneStringName(toggled), callable_mp(*debugger, &GameViewDebugger::set_ci_local_space));
ci_local_space_button->set_shortcut(ED_GET_SHORTCUT("canvas_item_editor/use_local_space"));
ci_local_space_button->set_shortcut_context(this);
// 3D Section
n3d_bar = memnew(EditorToolbarGroup);
main_menu_fc->add_child(n3d_bar);
n3d_bar->hide();
n3d_group.instantiate();
{
Button *button = _setup_button(SNAME("ToolSelect"), n3d_group, ED_GET_SHORTCUT("spatial_editor/tool_select"));
n3d_bar->get_hbox()->add_child(button);
button->set_pressed(true);
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_n3d_tool).bind(RuntimeNodeSelect::SELECT_MODE_SINGLE));
n3d_button[RuntimeNodeSelect::SELECT_MODE_SINGLE] = button;
}
{
Button *button = _setup_button(SNAME("ListSelect"), n3d_group);
n3d_bar->get_hbox()->add_child(button);
button->set_tooltip_text(TTRC("Show list of selectable nodes at position clicked."));
button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_n3d_tool).bind(RuntimeNodeSelect::SELECT_MODE_LIST));
n3d_button[RuntimeNodeSelect::SELECT_MODE_LIST] = button;
}
n3d_options_menu = memnew(MenuButton);
n3d_bar->get_hbox()->add_child(n3d_options_menu);
n3d_options_menu->set_button_icon(EditorIconManager::get_icon(SNAME("GuiDropdown")));
n3d_options_menu->set_flat(false);
n3d_options_menu->set_theme_type_variation("FlatMenuButtonNoIconTint");
n3d_options_menu->set_h_size_flags(SIZE_SHRINK_END);
n3d_options_menu->set_tooltip_text(TTRC("Selection options."));
PopupMenu *selection_menu = n3d_options_menu->get_popup();
selection_menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_selection_options_menu_id_pressed));
selection_menu->add_check_item(TTRC("Don't Select Locked Nodes"), SELECTION_AVOID_LOCKED);
selection_menu->add_check_item(TTRC("Select Group Over Children"), SELECTION_PREFER_GROUP);
selection_avoid_locked = EditorSettings::get_singleton()->get_project_metadata("game_view", "selection_avoid_locked", false);
selection_prefer_group = EditorSettings::get_singleton()->get_project_metadata("game_view", "selection_prefer_group", false);
selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_AVOID_LOCKED), selection_avoid_locked);
selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_PREFER_GROUP), selection_prefer_group);
debugger->set_selection_avoid_locked(selection_avoid_locked);
debugger->set_selection_prefer_group(selection_prefer_group);
embedding_hb = memnew(HBoxContainer);
embedding_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
embedding_hb->set_alignment(ALIGNMENT_END);
main_menu_fc->add_child(embedding_hb);
game_size_label = memnew(Label());
embedding_hb->add_child(game_size_label);
game_hb = EditorToolbarGroup::create(embedding_hb);
game_size_label->hide();
// Setting the minimum size prevents the game workspace from resizing indefinitely
// due to the label size oscillating by a few pixels when the game is in stretch mode
// and the game workspace is at its minimum size.
game_size_label->set_custom_minimum_size(Size2(80 * EDSCALE, 0));
game_size_label->set_h_size_flags(SIZE_EXPAND_FILL);
game_size_label->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_RIGHT);
game_size_label->set_mouse_filter(MouseFilter::MOUSE_FILTER_PASS);
game_embed_group.instantiate();
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR] = memnew(Button);
game_hb->add_child(game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]);
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_button_icon(EditorIconManager::get_icon(SNAME("EmbedFused")));
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_toggle_mode(true);
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_button_group(game_embed_group);
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_theme_type_variation("FlatMenuButtonNoIconTint");
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_game_embed_mode_pressed).bind(EmbedMode::EMBED_TYPE_EDITOR));
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_tooltip_text(TTRC("Run the game embedded in the Game workspace on the next run."));
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING] = memnew(Button);
game_hb->add_child(game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]);
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_button_icon(EditorIconManager::get_icon(SNAME("EmbedFloating")));
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_toggle_mode(true);
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_button_group(game_embed_group);
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_theme_type_variation("FlatMenuButtonNoIconTint");
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_game_embed_mode_pressed).bind(EmbedMode::EMBED_TYPE_FLOATING));
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_tooltip_text(TTRC("Run the game in a floating window with the toolbar on the next run."));
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED] = memnew(Button);
game_hb->add_child(game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]);
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_button_icon(EditorIconManager::get_icon(SNAME("EmbedDisabled")));
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_toggle_mode(true);
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_button_group(game_embed_group);
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_theme_type_variation("FlatMenuButtonNoIconTint");
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_game_embed_mode_pressed).bind(EmbedMode::EMBED_TYPE_DISABLED));
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_tooltip_text(TTRC("Run the game in a floating window without the toolbar on the next run."));
game_window_options_menu = memnew(MenuButton);
game_hb->add_child(game_window_options_menu);
game_window_options_menu->set_button_icon(EditorIconManager::get_icon(SNAME("GuiDropdown")));
game_window_options_menu->set_flat(false);
game_window_options_menu->set_theme_type_variation("FlatMenuButtonNoIconTint");
game_window_options_menu->set_h_size_flags(SIZE_SHRINK_END);
game_window_options_menu->set_tooltip_text(TTRC("Game window options."));
menu = game_window_options_menu->get_popup();
menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_game_window_options_menu_menu_id_pressed));
menu->add_separator(TTRC("Embedded Window Sizing"));
menu->add_radio_check_item(TTRC("Fixed Size"), WINDOW_SIZE_MODE_FIXED);
menu->set_item_tooltip(menu->get_item_index(WINDOW_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."));
menu->add_radio_check_item(TTRC("Keep Aspect Ratio"), WINDOW_SIZE_MODE_KEEP_ASPECT);
menu->set_item_tooltip(menu->get_item_index(WINDOW_SIZE_MODE_KEEP_ASPECT), TTRC("Keep the aspect ratio of the embedded game."));
menu->add_radio_check_item(TTRC("Stretch to Fit"), WINDOW_SIZE_MODE_STRETCH);
menu->set_item_tooltip(menu->get_item_index(WINDOW_SIZE_MODE_STRETCH), TTRC("Embedded game size stretches to fit the Game workspace."));
panel = memnew(Panel);
add_child(panel);
panel->set_theme_type_variation("GamePanel");
panel->set_v_size_flags(SIZE_EXPAND_FILL);
panel->add_child(embedded_process);
embedded_process->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
embedded_process->connect("embedding_failed", callable_mp(this, &GameView::_embedding_failed));
embedded_process->connect("embedding_completed", callable_mp(this, &GameView::_embedding_completed));
embedded_process->connect("embedded_process_focused", callable_mp(this, &GameView::_embedded_process_focused));
embedded_process->set_custom_minimum_size(Size2i(100, 100));
MarginContainer *state_container = memnew(MarginContainer);
state_container->add_theme_constant_override("margin_left", 8 * EDSCALE);
state_container->add_theme_constant_override("margin_right", 8 * EDSCALE);
state_container->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
#ifdef MACOS_ENABLED
state_container->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
#endif
panel->add_child(state_container);
state_label = memnew(Label());
state_container->add_child(state_label);
state_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
state_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
state_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
state_label->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
_camera_override_menu_id_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "camera_override_mode", 0));
_update_debugger_buttons();
p_debugger->connect("session_started", callable_mp(this, &GameView::_sessions_changed));
p_debugger->connect("session_stopped", callable_mp(this, &GameView::_sessions_changed));
p_debugger->connect("setup_complete", callable_mp(this, &GameView::_setup_complete));
p_debugger->connect("game_window_size_received", callable_mp(this, &GameView::_game_window_size_received));
p_debugger->connect("hdr_state_received", callable_mp(this, &GameView::_hdr_state_received));
p_wrapper->set_override_close_request(true);
p_wrapper->connect("window_close_requested", callable_mp(this, &GameView::_window_close_request));
p_wrapper->connect("window_size_changed", callable_mp(this, &GameView::_update_floating_window_settings));
EditorDebuggerNode::get_singleton()->connect("breaked", callable_mp(this, &GameView::_debugger_breaked));
EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &GameView::_feature_profile_changed));
}
///////
#ifndef ANDROID_ENABLED
void GameViewPluginBase::set_window_layout(Ref<ConfigFile> p_layout) {
game_view->set_window_layout(p_layout);
}
void GameViewPluginBase::get_window_layout(Ref<ConfigFile> p_layout) {
game_view->get_window_layout(p_layout);
}
void GameViewPluginBase::setup(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process) {
debugger = p_debugger;
window_wrapper = memnew(WindowWrapper);
window_wrapper->set_margins_enabled(true);
game_view = memnew(GameView(debugger, p_embedded_process, window_wrapper));
game_view->set_v_size_flags(Control::SIZE_EXPAND_FILL);
window_wrapper->set_wrapped_control(game_view, nullptr);
EditorDock *game_dock = GameView::get_dock();
game_dock->add_child(window_wrapper);
game_dock->connect(SceneStringName(visibility_changed), callable_mp(this, &GameViewPluginBase::_dock_visibility_changed).bind(game_dock));
_create_empty_info_label(TTRC("The Game dock is embedded in a floating window and is not available here."));
empty_dock_info->hide();
EditorDockManager::get_singleton()->add_dock(GameView::get_dock());
window_wrapper->set_v_size_flags(Control::SIZE_EXPAND_FILL);
window_wrapper->connect("window_visibility_changed", callable_mp(this, &GameViewPlugin::_window_visibility_changed));
}
#endif // ANDROID_ENABLED
void GameViewPluginBase::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_TRANSLATION_CHANGED: {
#ifndef ANDROID_ENABLED
window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Game Workspace")));
#endif
} break;
case NOTIFICATION_ENTER_TREE: {
add_debugger_plugin(debugger);
} break;
case NOTIFICATION_EXIT_TREE: {
remove_debugger_plugin(debugger);
} break;
}
}
void GameViewPluginBase::_bind_methods() {
#ifdef ANDROID_ENABLED
ADD_SIGNAL(MethodInfo("show_android_game_dock"));
#endif
}
void GameViewPluginBase::_window_visibility_changed(bool p_visible) {
empty_dock_info->set_visible(p_visible);
_focus_another_editor();
}
void GameViewPluginBase::_create_empty_info_label(const String &p_message) {
empty_dock_info = memnew(Label(p_message));
empty_dock_info->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
empty_dock_info->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
empty_dock_info->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
GameView::get_dock()->add_child(empty_dock_info);
}
void GameViewPluginBase::_focus_another_editor() {
DockTabContainer *main_screen = EditorNode::get_editor_main_screen();
EditorDock *game_dock = GameView::get_dock();
if (main_screen->get_current_tab_control() != game_dock) {
// Already showing different screen, so nothing to do.
return;
}
EditorDock *prev_dock = main_screen->get_dock(main_screen->get_previous_tab());
if (prev_dock && prev_dock != game_dock && prev_dock->is_dock_open()) {
EditorDockManager::get_singleton()->force_focus_dock(prev_dock);
return;
}
// The above switch can still fail when a tab is closed. Try to open the first tab that isn't Game.
for (int i = 0; i < main_screen->get_tab_count(); i++) {
if (main_screen->get_dock(i) != game_dock) {
EditorDockManager::get_singleton()->force_focus_dock(main_screen->get_dock(i));
break;
}
}
}
void GameViewPluginBase::_dock_visibility_changed(EditorDock *p_dock) {
if (!p_dock->is_visible_in_tree()) {
return;
}
if (window_wrapper) {
if (EditorRunBar::get_singleton()->is_playing() && window_wrapper->get_window_enabled()) {
_focus_another_editor();
window_wrapper->grab_window_focus();
}
} else {
_focus_another_editor();
}
}
bool GameViewPluginBase::_is_window_wrapper_enabled() const {
#ifdef ANDROID_ENABLED
return true;
#else
return window_wrapper->get_window_enabled();
#endif // ANDROID_ENABLED
}
GameViewPluginBase::GameViewPluginBase() {
#ifdef ANDROID_ENABLED
debugger.instantiate();
#endif
}
GameViewPlugin::GameViewPlugin() {
#ifndef ANDROID_ENABLED
Ref<GameViewDebugger> game_view_debugger;
game_view_debugger.instantiate();
EmbeddedProcess *embedded_process = memnew(EmbeddedProcess);
setup(game_view_debugger, embedded_process);
#else
setup_android();
#endif
}
#ifdef ANDROID_ENABLED
void GameViewPluginBase::setup_android() {
EditorDock *game_dock = memnew(EditorDock);
game_dock->set_name(TTRC("Game"));
game_dock->set_icon_name("Game");
game_dock->set_available_layouts(EditorDock::DOCK_LAYOUT_MAIN_SCREEN);
game_dock->set_default_slot(EditorDock::DOCK_SLOT_MAIN_SCREEN);
game_dock->set_dock_shortcut(ED_GET_SHORTCUT("editor/editor_game"));
GameView::dock_singleton = game_dock;
EditorDockManager::get_singleton()->add_dock(game_dock);
game_dock->connect(SceneStringName(visibility_changed), callable_mp(this, &GameViewPluginBase::_dock_visibility_changed).bind(game_dock));
// Add a button to invoke the Android Game dock.
Button *show_game_dock = memnew(Button(TTRC("Tap to show Game dock")));
show_game_dock->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
show_game_dock->connect(SceneStringName(pressed), callable_mp(this, &GameViewPluginBase::_on_show_game_dock));
GameView::get_dock()->add_child(show_game_dock);
}
void GameViewPluginBase::_on_show_game_dock() {
emit_signal(SNAME("show_android_game_dock"));
}
#endif