Add game speed controls to the embedded game window

This commit is contained in:
DexterFstone 2025-06-07 21:47:43 -07:00
parent abbe792575
commit 7ddce8ab26
10 changed files with 173 additions and 11 deletions

View file

@ -134,6 +134,28 @@ void GameViewDebugger::next_frame() {
}
}
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;
@ -497,6 +519,8 @@ void GameView::_update_debugger_buttons() {
suspend_button->set_disabled(empty);
camera_override_button->set_disabled(empty);
speed_state_button->set_disabled(empty);
reset_speed_button->set_disabled(empty);
PopupMenu *menu = camera_override_menu->get_popup();
@ -509,6 +533,8 @@ void GameView::_update_debugger_buttons() {
camera_override_button->set_pressed(false);
}
next_frame_button->set_disabled(!suspend_button->is_pressed());
_reset_time_scales();
}
void GameView::_handle_shortcut_requested(int p_embed_action) {
@ -589,6 +615,51 @@ void GameView::_size_mode_button_pressed(int size_mode) {
_update_embed_window_size();
}
void GameView::_reset_time_scales() {
if (!is_visible_in_tree()) {
return;
}
time_scale_index = DEFAULT_TIME_SCALE_INDEX;
debugger->reset_time_scale();
_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_buttons();
}
void GameView::_update_speed_buttons() {
bool disabled = time_scale_index == DEFAULT_TIME_SCALE_INDEX;
reset_speed_button->set_disabled(disabled);
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);
}
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(DisplayServer::FEATURE_WINDOW_EMBEDDING)) {
return EMBED_NOT_AVAILABLE_FEATURE_NOT_SUPPORTED;
@ -775,9 +846,14 @@ void GameView::_notification(int p_what) {
_update_ui();
} break;
case NOTIFICATION_POST_ENTER_TREE: {
_update_speed_state_size();
} break;
case NOTIFICATION_THEME_CHANGED: {
suspend_button->set_button_icon(get_editor_theme_icon(SNAME("Suspend")));
next_frame_button->set_button_icon(get_editor_theme_icon(SNAME("NextFrame")));
reset_speed_button->set_button_icon(get_editor_theme_icon(SNAME("Reload")));
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_button_icon(get_editor_theme_icon(SNAME("InputEventJoypadMotion")));
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_button_icon(get_editor_theme_icon(SNAME("2DNodes")));
@ -796,6 +872,9 @@ void GameView::_notification(int p_what) {
camera_override_button->set_button_icon(get_editor_theme_icon(SNAME("Camera")));
camera_override_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
_update_speed_state_size();
_update_speed_state_color();
} break;
case NOTIFICATION_READY: {
@ -1073,6 +1152,26 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
next_frame_button->set_accessibility_name(TTRC("Next Frame"));
next_frame_button->set_shortcut(ED_GET_SHORTCUT("editor/next_frame_embedded_project"));
speed_state_button = memnew(MenuButton);
main_menu_hbox->add_child(speed_state_button);
speed_state_button->set_text(U"1.0×");
speed_state_button->set_theme_type_variation(SceneStringName(FlatButton));
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));
}
reset_speed_button = memnew(Button);
main_menu_hbox->add_child(reset_speed_button);
reset_speed_button->set_theme_type_variation(SceneStringName(FlatButton));
reset_speed_button->set_tooltip_text(TTRC("Reset the game speed."));
reset_speed_button->set_accessibility_name(TTRC("Reset Speed"));
reset_speed_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_reset_time_scales));
main_menu_hbox->add_child(memnew(VSeparator));
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE] = memnew(Button);
@ -1154,7 +1253,7 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
camera_override_menu->set_h_size_flags(SIZE_SHRINK_END);
camera_override_menu->set_tooltip_text(TTRC("Camera Override Options"));
PopupMenu *menu = camera_override_menu->get_popup();
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);