diff --git a/editor/script/script_editor_plugin.cpp b/editor/script/script_editor_plugin.cpp index 7ea67402a40..9740f44418f 100644 --- a/editor/script/script_editor_plugin.cpp +++ b/editor/script/script_editor_plugin.cpp @@ -752,7 +752,8 @@ void ScriptEditor::_go_to_tab(int p_idx) { EditorHelp *eh = Object::cast_to(c); if (eh) { - script_name_label->set_text(eh->get_class()); + script_name_button->set_text(eh->get_class()); + _calculate_script_name_button_size(); if (is_visible_in_tree()) { eh->set_focused(); @@ -935,7 +936,8 @@ void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) { } else { _update_selected_editor_menu(); _update_online_doc(); - script_name_label->set_text(String()); + script_name_button->set_text(String()); + _calculate_script_name_button_size(); } _update_history_arrows(); @@ -1819,6 +1821,8 @@ void ScriptEditor::_notification(int p_what) { case NOTIFICATION_THEME_CHANGED: { tab_container->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("ScriptEditor"), EditorStringName(EditorStyles))); + _calculate_script_name_button_size(); + help_search->set_button_icon(get_editor_theme_icon(SNAME("HelpSearch"))); site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink"))); @@ -2059,7 +2063,8 @@ void ScriptEditor::_script_selected(int p_idx) { grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT); //amazing hack, simply amazing _go_to_tab(script_list->get_item_metadata(p_idx)); - script_name_label->set_text(script_list->get_item_text(p_idx)); + script_name_button->set_text(script_list->get_item_text(p_idx)); + _calculate_script_name_button_size(); grab_focus_block = false; } @@ -2484,8 +2489,6 @@ void ScriptEditor::_update_script_names() { if (tab_container->get_current_tab() == sedata_filtered[i].index) { script_list->select(index); - script_name_label->set_text(sedata_filtered[i].name); - ScriptEditorBase *se = _get_current_editor(); if (se) { se->enable_editor(this); @@ -2494,6 +2497,14 @@ void ScriptEditor::_update_script_names() { } } + for (const _ScriptEditorItemData &sedata_i : sedata) { + if (tab_container->get_current_tab() == sedata_i.index) { + script_name_button->set_text(sedata_i.name); + _calculate_script_name_button_size(); + break; + } + } + if (!waiting_update_names) { _update_members_overview(); _update_help_overview(); @@ -3973,6 +3984,36 @@ void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) { auto_reload_running_scripts = p_enabled; } +void ScriptEditor::_calculate_script_name_button_size() { + Ref font = script_name_button->get_theme_font(SceneStringName(font), SNAME("Button")); + HorizontalAlignment alignment = script_name_button->get_text_alignment(); + int font_size = script_name_button->get_theme_font_size(SceneStringName(font_size), SNAME("Button")); + String text = script_name_button->get_text(); + int jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE; + TextServer::Direction direction = TextServer::Direction(script_name_button->get_text_direction()); + Vector2 text_size = font->get_string_size(text, alignment, -1, font_size, jst_flags, direction, TextServer::ORIENTATION_HORIZONTAL); + + script_name_width = text_size.x + script_name_button->get_theme_stylebox(CoreStringName(normal))->get_content_margin(SIDE_LEFT) + script_name_button->get_theme_stylebox(CoreStringName(normal))->get_content_margin(SIDE_RIGHT); + _calculate_script_name_button_ratio(); +} + +void ScriptEditor::_calculate_script_name_button_ratio() { + const float total_width = script_name_button_hbox->get_size().width; + if (total_width <= 0) { + return; + } + + // Make the ratios a fraction bigger, to avoid unnecessary trimming. + const float extra_ratio = 4 / total_width; + + const float script_name_ratio = MIN(1, script_name_width / total_width + extra_ratio); + script_name_button->set_stretch_ratio(script_name_ratio); + + float ratio_left = 1 - script_name_ratio; + script_name_button_left_spacer->set_stretch_ratio(ratio_left / 2); + script_name_button_right_spacer->set_stretch_ratio(ratio_left / 2); +} + void ScriptEditor::_help_search(const String &p_text) { help_search_dialog->popup_dialog(p_text); } @@ -4411,12 +4452,27 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) { debugger->connect("breakpoint_set_in_tree", callable_mp(this, &ScriptEditor::_set_breakpoint)); debugger->connect("breakpoints_cleared_in_tree", callable_mp(this, &ScriptEditor::_clear_breakpoints)); - script_name_label = memnew(Label); - script_name_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); - script_name_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); - script_name_label->set_focus_mode(FOCUS_ACCESSIBILITY); - script_name_label->set_h_size_flags(SIZE_EXPAND_FILL); - menu_hb->add_child(script_name_label); + script_name_button_hbox = memnew(HBoxContainer); + script_name_button_hbox->set_h_size_flags(SIZE_EXPAND_FILL); + script_name_button_hbox->add_theme_constant_override("separation", 0); + script_name_button_hbox->connect(SceneStringName(item_rect_changed), callable_mp(this, &ScriptEditor::_calculate_script_name_button_ratio)); + menu_hb->add_child(script_name_button_hbox); + + script_name_button_left_spacer = memnew(Control); + script_name_button_left_spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL); + script_name_button_hbox->add_child(script_name_button_left_spacer); + + script_name_button = memnew(Button); + script_name_button->set_flat(true); + script_name_button->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); + script_name_button->set_h_size_flags(SIZE_EXPAND_FILL); + script_name_button->set_tooltip_text(TTRC("Navigate to script list.")); + script_name_button->connect(SceneStringName(pressed), callable_mp(script_list, &ItemList::ensure_current_is_visible)); + script_name_button_hbox->add_child(script_name_button); + + script_name_button_right_spacer = memnew(Control); + script_name_button_right_spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL); + script_name_button_hbox->add_child(script_name_button_right_spacer); site_search = memnew(Button); site_search->set_theme_type_variation(SceneStringName(FlatButton)); diff --git a/editor/script/script_editor_plugin.h b/editor/script/script_editor_plugin.h index 5cc46eceb0a..152c86fb704 100644 --- a/editor/script/script_editor_plugin.h +++ b/editor/script/script_editor_plugin.h @@ -359,7 +359,11 @@ class ScriptEditor : public PanelContainer { float zoom_factor = 1.0f; - Label *script_name_label = nullptr; + HBoxContainer *script_name_button_hbox = nullptr; + Control *script_name_button_left_spacer = nullptr; + Control *script_name_button_right_spacer = nullptr; + Button *script_name_button = nullptr; + int script_name_width = 0; Button *script_back = nullptr; Button *script_forward = nullptr; @@ -517,6 +521,9 @@ class ScriptEditor : public PanelContainer { void _script_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index); void _make_script_list_context_menu(); + void _calculate_script_name_button_size(); + void _calculate_script_name_button_ratio(); + void _help_search(const String &p_text); void _history_forward();